Question

I want to implement delta timing in my SFML loop in order to compensate for other computers that choose to run my application, right now I just have

float delta = .06 

placed before my loop, but as wikipedia describes delta timing:

It is done by calling a timer every frame per second that holds the time between now and last call in milliseconds.[2] Thereafter the resulting number (Delta Time) is used to calculate how much faster that, for instance, a game character has to move to make up for the lag spike caused in the first place.[3]

Here is what I'm doing that is WRONG currently, I can't quite seem to translate the logic into syntax:

bool running=true;                                      //set up bool to run SFML loop
    double lastTime = clock.getElapsedTime().asSeconds();
    sf::Clock clock;                                        //clock for delta and controls
    while( running )
    {
        clock.restart();
        double time= clock.getElapsedTime().asSeconds();
        double delta = time - lastTime; //not working... values are near 0.0001
        time = lastTime;
        //rest of loop
Was it helpful?

Solution

Shouldn't it be:

sf::Clock clock;
while( running )
{
    double delta =  clock.restart().asSeconds(); // asMilliseconds()
    //rest of loop
}

(I assume you do not need time and last_time)

OTHER TIPS

Your loop is running so fast that your delta in seconds is very small. Why not measure it in milliseconds instead?

Switch .asSeconds() to .asMilliseconds(). Look here for documentation.

Your approach is almost right. However, if you're calculating the time difference on your own (subtracting the previous time), you must not reset your sf::Clock.

For variable timesteps you can essentially use Dieter's solution. However, I'd suggest one tiny modification:

sf::Clock clock
while (running) {
    // do event processing here

    const sf::Time delta = clock.restart();

    // do your updates here        

    sf::sleep(sf::microseconds(1));
}

What I did different are two things:

  • I store the delta time as a sf::Time object. This isn't really any significant change. However, it allows me to retrieve the difference in different units later on (just retrieving seconds or milliseconds is fine though).
  • I wait for a very tiny amount of time. This may make a significant difference based on the time that passes during one iteration of the loop. Otherwise - on a very, very fast computer - you might end up with a delta of 0. While this is rather unlikely as long as you're using the raw time tracking microseconds, it might be an issue if you're only evaluating milliseconds (in which case you might even want to sleep for a whole milliseconds). Depending on the system's timer granulation/power (saving) settings, this might be a tad bit slower compared to not sleeping at all, but it shouldn't be noticeable (since SFML also tries to fight this issue as well).

What you want is basically this :

while(running)
{
    sf::Time now = clock.getElapsedTime();
    deltaTime = now - lastTime;
    lastTime = now;
}

As for the sf::sleep mentionned by Mario, you should just use sf::RenderWindow::setFramerateLimit(unsigned int) to cap the fps as you want, and the SFML will take care of making your application sleep for the correct amount of time at each loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top