SFML community forums

Help => System => Topic started by: antinoid on December 25, 2022, 11:25:18 pm

Title: using sf::Clock to get delta time stops working when my code runs too fast
Post by: antinoid on December 25, 2022, 11:25:18 pm
so I'm using sf::Close to get some delta time in my program (its for having a server timeout connections after x seconds)

It's something like this:

    float timeout = 0;
    sf::Clock deltaClock;
    while (true){
        sf::Time dt = deltaClock.restart();
        timeout += dt.asSeconds();

        if(timeout > 1){
            std::cout << "should kick player now\n";
        }
 }




except for the std::cout never gets called...

EXCEPT when I have the tick take a lot longer (I just calculate a couple of thousand square roots) and then it works perfectly.

Maybe the sf::Clock module doesn't work well with VERY small numbers? But I tried using milliseconds and microseconds (which should be bigger) but no difference.

not sure what to do.

BTW there's no GUI I'm just using SFML for the network module and the clock module.
Title: Re: using sf::Clock to get delta time stops working when my code runs too fast
Post by: eXpl0it3r on December 26, 2022, 12:33:03 am
SFML 2 uses internally a precision of nanoseconds, but the overall precision also depends on your hardware clocks.

In general however microseconds are good enough, as you don't need more than 1000000 ticks per second.

Having an infinite loop without any waiting/breaking is not recommended, as it would just consume a whole CPU core, just doing nothing really.