11

I'm writing a game, and I saw the FPS algorithm doesn't work correctly (when he have to calculate more, he sleeps longer...) So, the question is very simple: how to calculate the sleeptime for having correct FPS?

I know how long it took to update the game one frame in microseconds and of course the FPS I want to reach.

I'm searching crazy for a simple example, but I can't find one....

The code may be in Java, C++ or pseudo....

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • 12
    generally you don't want to limit your FPS. If someone pays for an extremely nice graphics card, they want more FPS. You should design your code so that it measures the time between frames (or even works on some arbitrary "tick" independent of framerate) and then causes the game objects to update based on that elapsed time. If you do this right, your game will play the same way at 15fps or 100fps, with the only difference being the smoothness of motion. – rmeador Jun 23 '10 at 16:08
  • 2
    @rmeador: There's little point in going faster than the monitor's refresh rate, which is usually 60 Hz on TFT monitors (but this should not be relied upon, of course). – Thomas Jun 23 '10 at 16:55
  • 1
    @rmeador: But that will eat all the CPU... – Martijn Courteaux Jun 24 '10 at 09:52
  • 3
    Why do you think it's a *bad* thing for the game to use up all the CPU time? That's what it means for a game to run "as fast as it can." Most modern high-performance games use up most available CPU time (on at least one core). – Quintus Jun 25 '10 at 00:55
  • Many modern games implement an optional FPS limit. This allows the game to run only what is necessary (e.g. 100 FPS or 60 FPS), if the user does not wish to 'run as fast as possible'. Best of both worlds. :) – Casey Kuball Mar 27 '12 at 19:42
  • If someone wants to write up a correct answer instead of the ones below, here is how: http://gafferongames.com/game-physics/fix-your-timestep/ – Prof. Falken Oct 24 '12 at 07:02

4 Answers4

16

The time you should spend on rendering one frame is 1/FPS seconds (if you're aiming for, say 10 FPS, you should spend 1/10 = 0.1 seconds on each frame). So if it took X seconds to render, you should "sleep" for 1/FPS - X seconds.

Translating that to for instance milliseconds, you get

ms_to_sleep = 1000 / FPS - elapsed_ms;

If it, for some reason took more than 1/FPS to render the frame, you'll get a negative sleep time in which case you obviously just skip the sleeping.

aioobe
  • 413,195
  • 112
  • 811
  • 826
14

The number of microseconds per frame is 1000000 / frames_per_second. If you know that you've spent elapsed_microseconds calculating, then the time that you need to sleep is:

(1000000 / frames_per_second) - elapsed_microseconds
JSBձոգչ
  • 40,684
  • 18
  • 101
  • 169
3

Try...

static const int NUM_FPS_SAMPLES = 64;
float fpsSamples[NUM_FPS_SAMPLES]
int currentSample = 0;

float CalcFPS(int dt)
{
    fpsSamples[currentSample % NUM_FPS_SAMPLES] = 1.0f / dt;
    float fps = 0;
    for (int i = 0; i < NUM_FPS_SAMPLES; i++)
        fps += fpsSamples[i];
    fps /= NUM_FPS_SAMPLES;
    return fps;
}

... as per http://www.gamedev.net/community/forums/topic.asp?topic_id=510019

Lance
  • 5,655
  • 4
  • 30
  • 32
1

Take a look at this article about different FPS handling methods.

UPDATE: use this link from Web Archive as original website disappeared: https://web.archive.org/web/20161202094710/http://dewitters.koonsolo.com/gameloop.html

virious
  • 571
  • 1
  • 8
  • 27