# Game Timing and FPS

## Recommended Posts

I been confused with timing in games and keeping a constant frame rate, several people said isn't worth trying to make it 100% constant. I am confused about frame rate; if i am using SDL would I just for example, define FPS as 60 and call SDL_GetTicks() before the main game loop.Then simply have a function determine how many seconds have passed to narrow down what second we are on and then have another function narrow it to which 60th of a second were at and tha number (n), is the n frame out of 60? If there is a easier way to keep time, please let me know; or you dont know what I am talking about to keep time, could you lend me a hand on how?

##### Share on other sites
Aressera    2919
there's really no reason why you should want your game to run at a constant framerate. It's a much better idea to have all of your animations/physics be a function of a measurable delta time, so that your animations will always seem to happen at the same speed.

so do something like this:
long oldTime = SDL_GetTicks();long newTime;while ( game.isRunning() ){newTime = SDL_GetTicks();long dt = newTime - oldTime;oldTime = newTime;game.update( dt );}

ideally this should be encapsulated inside of a timer class which has functions like update(), getDt(), getFPS(), etc.

##### Share on other sites
KulSeran    3267
Bit of a nitpick. But you probably do want your physics in fixed step for various stability reasons.
So the idea would be to do a time accumulator on your frames.

while ( running ){ ctime = SDL_GetTicks; etime = ltime - ctime; ltime = ctime;  atime += etime; if ( atime > .04 ) {  physics.update ( .04 );  logic.update ( .04 );  animations.update ( .04 );  event_queue.update ( .04 );  atime -= .04; } getinputs(); queueUIEvents(); queueNetworkEvents(): display_frontbuffer(); }

This will give you an effect like what Supreme Commander has. Where your game will run as fast as it can
upto the limiting rate (.04), but you always get a graphically/networkly smooth gameplay because
those get processed every game loop.

If you change the if() to a while(), than you get a framerate that drops when processing time goes up, but
you can get smoother gameplay if the graphics are taking longer than the processing.

NOW if you wanted to limit everything to a framerate, than you can do

while ( running)
{
stime = SLD_GetTicks();
logic.update();
drawstuff();
while ( SDL_GetTicks() - stime < .15 ){}
}

##### Share on other sites
AfroFire    471
"Chris Okyen's" teacher just pwned him.

I'm going to rate him up to make him feel better.

##### Share on other sites
ElPeque2    122
kulseran

and maybe you should't render the scene if ( atime > .04 ) wasn't true at least once, right? (because nothing changed, there is nothing to render). And that would give you a framerate of at most, once every .04.

maybe im just very confused :P.

##### Share on other sites
Domarius    192
I'll try and give a simple explanation of the concept;

Each entity in your game doesn't care about the frame rate - all they want to know is how much time has passed since their last update.
So you check the timer difference since the last loop was completed, and it's 0.456 seconds.
So you tell Mr animation - 0.456 seconds have passed, and he advances 0.456 seconds through his animation. And you tell Mr ball physics that 0.456 seconds have passed, and he (using physics math) calculates where the ball would be after 0.456 seconds have passed... etc.

In your main game loop you have to update each of your objects with this delta time value.

Then when your frame renders, everything is where it should be at that point in time.