PDA

View Full Version : Fps is dying...



01-09-2001, 08:05 AM
Hello and Happy New Year to everybody.
The following is about a project I have been working on for a while. It's a training simulation for teaching new drivers the road signs and generally how to behave while driving. Its an application that uses 3 windows : the first window with dimensions 550x640 (not really visible) is the frame for the other two. In the second window the 3D world is rendered and in the 3rd window the 2D controls for the driver are displayed. The 3D world consists of 4 building blocks, roads, some road signs and traffic lights. Most of the textures used in this program are of 256x256 resolution. The problem is this : the frame rate starts from 35 fps the momment the app is initialized and reaches 5 or even 2 fps after 4-5 minutes. At first I thought that the textures were too heavy but if this is the case why does the frame rate decline as the time goes by?

The rendering of the windows is done when there are no messages in the windows message queue to be processed.
Here is the main code snippet:

//if there are no messages in the message //queue

//Handle Car Physics
MoveVehicle();
HandleCollisions();
//Check about Collisions with another car
collided = CarCollision();
//if car is moving in the correct direction
if(ValidMoveDirection())
{
//check for road signs
if(!CheckControlAreas())
Penalty();
}

//send a WM_PAINT message to paint the 2nd //window (render the 3D world)
InvalidateRect(hViewWnd, NULL, FALSE);

//send a WM_PAINT message to paint the 3rd //window (render the 2D controls)
InvalidateRect(hOutWnd, NULL, FALSE);

All the above functions contain some fors (not nested and iterations < 20 ) but they are not costly.
Does anybody have any ideas about what is going wrong? And is there a way to keep the fps steady or at least above 10 ? Thank you very much in advance.

Deiussum
01-09-2001, 10:51 AM
It sounds to me like there may be some sort of memory leak. This may seem trivial, but double check that you deallocate any memory you allocate when it is no longer needed, and also release any GDI objects you create.

I had a problem similar to what you were describing in a small dialog-based app simply because I was creating a font object each time the dialog was painted and, even though I thought I was deleting the font object, I wasn't because it was held by the HDC still.

Anonymous Coward
01-09-2001, 10:54 AM
It seems like you're allocating memory and not deallocating them after use. Im almost sure that you are allocating memory each frame/loop cycle/etc.
Try looking for your 'new' or 'malloc' calls in your main loop program and check if they are been deallocated...

01-09-2001, 03:05 PM
Thanks a lot Deiussum. I have used a BuildFont() function but only during the RC initialization. During that call 256 (the number of the chars to be used) display lists
are created. I do not know if there is a problem with that function but I'll look into that. At least I know where to start looking.

01-09-2001, 03:17 PM
Originally posted by Anonymous Coward:
It seems like you're allocating memory and not deallocating them after use. Im almost sure that you are allocating memory each frame/loop cycle/etc.
Try looking for your 'new' or 'malloc' calls in your main loop program and check if they are been deallocated...

Thank you Anonymous Coward, but it seems that the only place I have used 'new' is when reading in the bmps for the textures. And at that place all the 'news' are paired by 'frees'. Also this code is called only during program initialization.
However, memory leak during each cycle seems reasonable enough. I hope I'll find out what's going on soon.

Deiussum
01-10-2001, 05:39 AM
If you are using 'new' to allocate memory you should use 'delete' to deallocate it rather than 'free.' free should only be used to deallocate memory allocated by calloc, malloc, or realloc.

If your only new isn't in the display loop then it probably isn't the cause of your slowdown.

I'm not sure how your BuildFont function is working, but the way I fixed my problem was to add member variables for the handles of all the fonts I used in the dialog, then just used CreateFont once for each when the dialog box was initialized. After that I just had to use those font handles rather than creating the font again.

Kilam Malik
01-10-2001, 08:03 AM
Did you already proof, that it's really a memory leak? Watch in the task manager while your program is running, if the memory peak is growing.

Kilam.

01-11-2001, 01:07 AM
Many thanks everybody, but I have found what the problem was. All memory allocated by malloc was deallocated properly. The problem was this : I used a function called MakeBuildings() to render the buildings in my world (MakeBuildings() was called every time in my 'main' RenderScene() function). I have created all the necessary display lists for those buildings in a function called BuildLists() and I thought I have used BuildLists() only once http://www.opengl.org/discussion_boards/ubb/smile.gif. Well, not exactly. This is the code for MakeBuildings() :

MakeBuildings()
{
BuildLists();
//Other code...
}

And as a result, the same display lists were created in every cycle eating up memory until the program died. Careless...
Anyway, I now get 45 - 75 fps, thanks to the memory leak tip http://www.opengl.org/discussion_boards/ubb/smile.gif.