PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Mind if I ask a basic question about game loops/throttling?

Hello all. I'm working on a GAPI library to use in my games going forward (I prefer writing my own for numerous reasons). The question I have isn't really GAPI-related, but it's probably pretty basic...

In the game I'm working on, I use the following main game loop:
Code:

DWORD LastTickCount = GetTickCount();
DWORD CurrentTickCount = LastTickCount;
PeekMessage(&msg;, NULL, 0, 0, PM_NOREMOVE);
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg;, NULL, 0, 0, PM_REMOVE)) {
TranslateMessage(&msg;);
DispatchMessage(&msg;);
} else {
CurrentTickCount = GetTickCount();
if (CurrentTickCount > (LastTickCount + 10)) {
LastTickCount = CurrentTickCount;
MainLoopHandler();
}
}
}

A pretty standard "real-time" game loop I think. Now, my problem is this... I just today added the timer checks because this loop without it runs at ridiculous speeds (200+ in the emulator, >100 on my real E-125). Obviously I need to throttle it. So, I added what you see there.

Now, the line where I add 10 to LastTickCount I was thinking I should just divide 1000 by the desired FPS (30 FPS in this case), and that's what I plug in there. My logic being, 1000 milliseconds in a second, and since I want 30 FPS, I want a frame generated every 33 milliseconds.

Well, doing the math on 30 FPS means I should add 33, but that didn't work out right, it was too slow, so I trial-and-error'ed it down to 10, which gives me ~45 on the emulator very steadily, so even though I figure it's the overhead of the rest of the loop making the math not work out right, that's not even actually my problem!

My problem is that even though as I said I get 45 FPS steady in the emulator, my E-125 still runs far faster than it should be (60+ on some screens, 50 on some others, etc). It doesn't seem to make sense like it does in the emulator (even ignoring the math for a moment, at least it's a steady frame rate there all the time).

So, after a huge explanation, my question boils down to this: assuming I'm doing the loop right in the first place, which I think I am having read a number of things on it, how can I accurately and reliably throttle that loop down to s epecified frame rate, assuming the call to MainLoopHandler(), which is where I do all my game logic and screen updating, can generate the frame rate (which it can in this case) and be sure it works across most if not all current PocketPC devices? (I say that because I know about QueryPerformanceCounter, but that seems to be OEM-specific and therefore probably not safe to rely on).

ANY and ALL help is greatly appreciated on this!
Hi Frank,

Good to see you are still around :P

The first thing I would do is replace MainLoopHandler() with a function that does nothing accept check your fps counter. By doing that you can make sure that your game logic is not taking longer than expected in your main loop.

I assume that you FPS counter is not running from the WM_TIMER event as this has proven in the past to be very unreliable.

Other than that, the code looks ok. To test your logic set the +10 to +1000, that way you can check the fps is constant ie 1 fps

I hope this is of some help
BadBazza
Yep, still around (just silent for a while... buying a house does that!)

I will try your suggestion shortly, however, I'm already very certain the main loop call isn't taking longer than expected since I seem to be able to crank out over 100 FPS as it is now. One possibility I hadn't thought of is how I'm calculating frame rate. I am doing the following there:
Code:

DWORD currentTickCount = GetTickCount();
if (currentTickCount - GX_previousTickCount >= 1000) {
GX_previousTickCount = currentTickCount;
GX_lastFPS = GX_FPS;
GX_FPS = 0;
} else {
GX_FPS = GX_FPS + 1;
}

Which is called with each iteration of the game loop. Maybe that's the problem?