SFML community forums

Help => System => Topic started by: inaudax on December 25, 2013, 06:26:38 am

Title: sf::Clock speed inconsistent
Post by: inaudax on December 25, 2013, 06:26:38 am
I am having an issue with the clock speed being inconsistent.  Long story short, when I call my render method the clock speed seems to slow down a bit.  When I disable the render method the clock speeds back up.  Anyone else encountered this issue?  The amount is very small and barely noticeable.  Below is a very simplified sample of the code I am using:

//Before loop
sf::Clock clock;
sf::Time updateTime = clock.getElapsedTime();
sf::Time updateTimer = sf::seconds( 1.0f / 60.0f );

int updates = 0;

//in loop
if ( clock.getElapsedTime() - updateTime > updateTimer )
{
     updateTime = clock.getElapsedTime();
     updates++;
     Update();
}

Without my render method being called, updates runs around 58 or 59 fps but when I call my render method it drops to around 53 and 54 fps.  At the same time I get around 300 fps on my render method so I know it is not the whole thing slowing down.

Suggestions?

BTW I am a noob so I appreciate any feedback.

Thanks!
Title: Re: sf::Clock speed inconsistent
Post by: zsbzsb on December 25, 2013, 06:50:53 am
Please use the [ code=cpp ][ /code ] forum tags  ;)

As for your problem, you probably want something like this...

//Before loop
sf::Clock clock;
sf::Time updateTime = clock.getElapsedTime();
sf::Time updateTimer = sf::seconds( 1.0f / 60.0f );

int updates = 0;

//in loop
updateTime += clock.restart();
while ( updateTime >= updateTimer )
{
     Update();
}
Title: Re: sf::Clock speed inconsistent
Post by: inaudax on December 25, 2013, 09:15:24 am
Please use the [ code=cpp ][ /code ] forum tags  ;)

As for your problem, you probably want something like this...

//Before loop
sf::Clock clock;
sf::Time updateTime = clock.getElapsedTime();
sf::Time updateTimer = sf::seconds( 1.0f / 60.0f );

int updates = 0;

//in loop
updateTime += clock.restart();
while ( updateTime >= updateTimer )
{
     Update();
}

Still get the same...
Title: Re: sf::Clock speed inconsistent
Post by: inaudax on December 25, 2013, 09:42:30 am
I figured it out:

//before loop starts
sf::Clock clock;
sf::Time time = clock.getElapsedTime();
sf::Time updateTimer = sf::seconds( 1.0f / 60.0f );
double delta = 0;
int updates = 0;

//in loop
sf::Time now = clock.getElapsedTime();
delta += ( now.asSeconds() - time.asSeconds() ) / updateTimer.asSeconds();
time = now;
while ( delta >= 1 )
{
     Update();
     updates++;
     delta--;
}

 

This put it at a solid and consistent 60 fps which is what I was looking for.
Title: Re: sf::Clock speed inconsistent
Post by: Nexus on December 25, 2013, 12:58:20 pm
Why do people always convert sf::Time objects immediately to float (or even worse, double)? The idea behind the sf::Time class is to have a unique and type-safe way to refer to time spans in SFML. As it overloads all the necessary operators, there is no need for conversions. Only do so if you really need the float value, for example to multiply the time with a velocity.

By the way, I would rename updateTimer to updateTime (or even clearer: interval or frameDuration or similar).
float delta = 0.f;
int updates = 0;

//in loop
sf::Time now = clock.getElapsedTime();
delta += (now - time) / updateTime;
Title: Re: sf::Clock speed inconsistent
Post by: inaudax on December 26, 2013, 06:06:27 am
Why do people always convert sf::Time objects immediately to float (or even worse, double)? The idea behind the sf::Time class is to have a unique and type-safe way to refer to time spans in SFML. As it overloads all the necessary operators, there is no need for conversions. Only do so if you really need the float value, for example to multiply the time with a velocity.

By the way, I would rename updateTimer to updateTime (or even clearer: interval or frameDuration or similar).
float delta = 0.f;
int updates = 0;

//in loop
sf::Time now = clock.getElapsedTime();
delta += (now - time) / updateTime;

I tried doing it without the conversion but it gave me an error with the division operator.   Probably just something I did wrong.  I'll double (no pun intended) check.  Duly noted on the naming.  Thanks!
Title: Re: sf::Clock speed inconsistent
Post by: Nexus on December 26, 2013, 11:23:13 am
Maybe you don't have the latest Git revision, the division operator was added not so long ago.

So, even if you have to workaround it in this specific case, keep generally in mind that you should use the most appropriate types (such as sf::Time for SFML-related time handling).