PDA

View Full Version : Main loop timing.



aNSA
07-23-2013, 07:12 PM
Hey.

Coding my firsth game and having problems getting the main loop to stay in time. My fps keeps jumping between 58-62 and my main loop takes anywhere from 16ms to 33ms (and sometimes over) to run. Even if i add sleep(10) in my main loop the time per loop and fps keeps the same. Im using window.flip(1) for the timing. After adding sleep over 14 or 15 seems to drop my fps so my main loop should not take more than 1-2ms to run?

Tryed to remove every piece of code from my main loop and left only the class object that draws the timing on screen. Didnt make any difference. Switching to flip(0) without sleep crashes my program at startup. Also tryed opengl1 and swrender. opengl1 didnt make any difference but swrender worked with flip(0) showing some 500/1000 fps. flip(1) with swrender shows some 60 and 200 fps.

Updated my graphics driver (geforce gtx 650 with windows 7) with clean install and its still the same.

Any ideas how i could get more constant update on frame rate or how to troubleshoot further?

Judas
07-23-2013, 08:55 PM
One possible reason could be that you are constructing GPU objects every frame. For example, consider the following code:


void draw_fps(GrapihcContext &gc, int fps)
{
Font font(gc, "Segoe UI", 11);
font.draw_text(format_string("FPS: %1", fps));
}

Here it creates a GPU texture object, uploads the glyphs pixels, then renders the glyphs, and then it destroys the font again. A modern CPU is pretty fast at generating the glyphs, and uploading a texture every frame can be done at a pretty high speed, but I have noticed that at least with Direct3D after a little while the system halts for a small period. It is probably doing some kind of garbage collect cycle of unused resource objects.

If it is indeed this problem you are seeing, then the solution is straightforward: rather than creating GPU objects every frame, instead create them once. In the above example you'd do this by only creating Font once, rather than every time a frame is rendered.

aNSA
07-24-2013, 02:55 PM
Thanks for the reply, im creating my fonts before my main loop and pass them to my class function as reference like this.



void MyClass::draw(CL_GraphicContext &gc, CL_Font_System &font)


Im using ClanLib 2.3.6. I think i need to build couple exe files and send them to my friends and see how that goes.

Judas
07-25-2013, 07:25 AM
You mentioned a crash. What is the stack trace of that crash?

Also, does any of the ClanLib examples behave in the same way?

aNSA
07-25-2013, 01:48 PM
Looked my debug and found it was divide by zero problem and managed to fix that crash when using flip(0), seemed things were going way WAY too fast. Now the flip(0) works just fine and getting 1000 fps with that (clanlib seems really fast even if im drawing tons of stuff at screen wow).

But if i use flip(0) my avarage loop time is something like 0-1ms with occasional 2-4ms spikes. if i use flip(1) my fps is 58-62 and i get avarage loop time of 16ms with some 32ms ones that skips a frame and movement starts to jerk (a little but visible). Using sleep(1) with flip(0) seems to lock my frames per second to 58-66, using pause(1) works like i think it would. (pause(16) gives me somewhat 60 fps).

Ill testdrive my code with couple of friends and see if the code works in a same way. Oh and yeah ill try some examples as well and see if those work or if my comp is buggy.

aNSA
07-27-2013, 04:43 PM
You mentioned a crash. What is the stack trace of that crash?

Also, does any of the ClanLib examples behave in the same way?

Tested some examples and indeed they run fine. I seem to have done the the "GPU objects every frame" mistake atleast in some degree. Staring my code for weeks in a row made me have a tunnel vision. Thanks for taking the time to help newcomers like me! Ill be back in month or two! Case solved!