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

Author Topic: using sf::Clock to get delta time stops working when my code runs too fast  (Read 217 times)

0 Members and 1 Guest are viewing this topic.

antinoid

  • Newbie
  • *
  • Posts: 7
    • View Profile
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.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10390
    • View Profile
    • development blog
    • Email
Re: using sf::Clock to get delta time stops working when my code runs too fast
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/