PocketMatrix forums

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

Manually limiting FPS

I'm currently trying to implement manual FPS limiting. It's the first time I try to do this, and nothing I have tried has been very successful.

My game has its game logic separated from input pooling and rendering. Once I have determined that the current FPS exceed the maximum allowed, what should I do?

In practice, should the game logic be limited also? Do I perform another peekmessage to pass the time?

Thanks,

--Eric
Couldnt you use a timer and check each iteration if enough time has passed to get your desired frame rate then render the frame?
simonjacobs · 10 Feb 2004, 17:22 UTC
Either run your game from WM_TIMER messages, or sleep the thread with the Sleep() function. Dont busy-wait though, you'll just drain the batteries for no reason.
Well, you should already be measuring the length of each frame to be able to display the FPS. If you want to run at 30 FPS then each frame should be 33.3 MS long. So if your frame only took 15 MS to render, then sleep for 18 MS. For accuracy you should average it over multiple frames. For example if AI requires more processing every other frame then you could run at substantially less than 30 FPS if you only look at the render time of the current frame.

Dan East
Here's a cheap and dirty way to do it;

Code:

void GameLoop()
{
g_Timer.Record();

if (g_Timer.GetTime() <= m_updateTime) {
continue;
}

m_updateTime = (1000 / m_maxFPS) + g_Timer.GetTime();

// update game logic, etc.
}


But sleep is probably a better approach. :P
That is called a busy wait and is exactly what was just stated you SHOULD NOT DO.
Thanks, I got it working.

I was a bit scared of putting a Sleep() in there, but it does work as it should.

--Eric
A better way of solving the problem is to sync game updates with a timer counter...
This is, of course, only possible if your routines depend on a time parameter (which is mostly the case...)

Just to make sure your game (or whatever) runs at constant speed on any device...
My game is a simulation and it indeed requires good timing.

What I did was to adjust the delay fed to the Sleep() function depending on the time it took to render the last frame. Or more accurately, the time it took for a full iteration of a game loop. It's working great now, but I still don't like Sleep ;)

--Eric
Hey, Sleep is good! I would like to hunt down and have a word with all the coders that do not use Sleep when they should. I just love running this certain Genesis emulator on my 1.8 GHz notebook, and my CPU forced to run at 100% even though it could handle that game running at less than 20%. It makes my fan run wide open, and shortens battery life in a big way.

Dan East
simonjacobs · 10 Feb 2004, 22:30 UTC
Yes, busy-waiting is terrible programming. If you need to waste time sleep is exactly what you need so whats not to like :)

But it's also worth considering using WM_TIMER messages. You dont get absolute accuracy, and if you need to push the device to its limits use a loop with Sleep, but for a lot of purposes it works well.
It seems that the problems I previously had with Sleep() are caused by my desktop. Here's the deal:

I targetted 30 FPS, and it took more than 1 and less than 2 ms to render the screen, so my wait delay was moving between 31 and 32. Don't ask me why, but when it sleeps for 31 ms on my desktop everything is working fine, but when the counter increases to 32 then the Sleep() doesn't return for around 50 ms and the fps drop to 20 or so.

I even tried to enter 31 manually and then 32. 31 is fine, and 32 just gives away too much juice to other processes (a guess). How weird is that... I tried to close everything that was open, but same thing. Not 33 or 30, exactly 32! Magic number!

Needless to say, everything runs perfectly on the PocketPC... Ok then, screw my desktop, Sleep() is my friend ;)

--Eric
That may have to do with the thread granularity of the OS. The OS gives apps processing time in chunks, and that may happen to cause an extra long delay with a specific sleep amount.

Since your app is well behaved and Sleeping when it doesn't need CPU time, you could try increasing the priority of your application to see if that fixes your 32 MS problem.

You might want to verify that there isn't a bug in your code. Just call GetTickCount before and after Sleep to see how many milliseconds the call actually consumed.

Dan East
xPeterx wrote:A better way of solving the problem is to sync game updates with a timer counter...
This is, of course, only possible if your routines depend on a time parameter (which is mostly the case...)

Just to make sure your game (or whatever) runs at constant speed on any device...


Quake basically does this. It basically says, how long did the last frame take? Assume that's how long the current frame will take. So simulate that much time in the game, then render it. Then it repeats.

As long as each frame takes the same time, it works perfectly. When frame time changes, it lags by one frame. But the simulation time is always pretty close to the real time, within the last frame.

Assuming I remember how it works correctly. I'm sure Dan will correct me if I misrememberd.
I know Quake's method caused people to force their game to run at a really low or really high FPS in order to cheat in multiplayer, and do moves that would otherwise be impossible. I believe that's linked to it's timing. Can't remember the specifics, but might be worth looking into in either case.
Dan, thanks for the suggestion. I tried it and using the high perf counter the Sleep call does take exactly how long it should now.

But now I'm pretty sure it's just my desktop because I just rebooted Windows 2000 and with no code change things went back to normal. From what I can tell it will come back later in the day when I abused my computer some. I'll check it again then.

Just out of curiosity, since there is a high perf counter, is there a high perf Sleep() function? And what is the difference between Sleep() and SleepEx()? I can't figure out the difference by reading the SDK definition of the Ex version.

Thanks!

--Eric