SFML community forums

Help => General => Topic started by: ElysianShadow on June 18, 2014, 10:14:40 pm

Title: Question About Fixed Time Step
Post by: ElysianShadow on June 18, 2014, 10:14:40 pm
I have a question about some of the code involved using a fixed time step. Here is some example code:



const float TimePerFrame = 1 / 60.0f;

sf::Time timeSinceUpdate = sf::Time::Zero;

while ( window.isOpen() )
{
        sf::Time elapsed = clock.restart();
        timeSinceUpdate += elapsed;
       
        while ( timeSinceUpdate > TimePerFrame )
        {
                timeSinceUpdate -= TimePerFrame; // This is the part I don't understand. Why do this?
               
                handleEvents();
               
                update( TimePerFrame );
        }
       
        render();
}

 

I don't understand why you do the line that I commented? Also, is this code suppose to run the game at 60 fps? Because when I run it, the frame rate is over 9000. Sorry if it's a very simple question, but maybe I'm over thinking it and don't see something obvious. Thanks.
Title: Re: Question About Fixed Time Step
Post by: Ixrec on June 18, 2014, 10:23:48 pm
Without that line, the while ( timeSinceUpdate > TimePerFrame ) loop will never stop executing.
Title: Re: Question About Fixed Time Step
Post by: Tuffywub on June 18, 2014, 10:33:13 pm
it might make more sense for timeSinceUpdate to be called unprocessedTime. It keeps track of how much time has not been updated. When you run an update, you must remove the amount of time you are updating from the unprocessedTime.

Also, frame rate is different from update rate. Update should be called at 60 times per second currently, but the way you code is written, render() is called as fast as possible. IMO, this is a good thing: if you want there to be a cap on the fps use vSync.
Title: Re: Question About Fixed Time Step
Post by: ElysianShadow on June 18, 2014, 10:33:31 pm
Yeah, I was definitely over thinking it.

Anyways, thanks for the responses guys