Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Clock speed inconsistent  (Read 5212 times)

0 Members and 1 Guest are viewing this topic.

inaudax

  • Newbie
  • *
  • Posts: 7
    • View Profile
sf::Clock speed inconsistent
« 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!

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::Clock speed inconsistent
« Reply #1 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();
}
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

inaudax

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Clock speed inconsistent
« Reply #2 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...

inaudax

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Clock speed inconsistent
« Reply #3 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Clock speed inconsistent
« Reply #4 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;
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

inaudax

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: sf::Clock speed inconsistent
« Reply #5 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!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Clock speed inconsistent
« Reply #6 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).
« Last Edit: December 26, 2013, 11:25:41 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything