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

Author Topic: Is sf::Clock accurate enough for fixed Time step game loops?  (Read 4737 times)

0 Members and 1 Guest are viewing this topic.

sonicd007

  • Newbie
  • *
  • Posts: 5
    • View Profile
Is sf::Clock accurate enough for fixed Time step game loops?
« on: February 10, 2013, 04:00:13 pm »
While asking on gamedev about fixed time steps, I was told clocks should never be used as a game timer and to use QueryPerformanceCounter() instead.  That solution however is Windows specific and I read somewhere that on multicore systems it could return different times since each cpu may not be synced.  That being said, does sf::Clock use something like QueryPerformanceCounter or does it use something like std::Clock.  Sorry I haven't checked the source code, I'm at work and this has been bugging me.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Is sf::Clock accurate enough for fixed Time step game loops?
« Reply #1 on: February 10, 2013, 04:09:01 pm »
sf::Clock uses QueryPerformanceCounter() on Windows, so you're on the safe side :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Is sf::Clock accurate enough for fixed Time step game loops?
« Reply #2 on: February 10, 2013, 04:10:15 pm »
/src/SFML/System/Win32/ClockImpl.cpp
Time ClockImpl::getCurrentTime()
{
    // Force the following code to run on first core
    // (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx)
    HANDLE currentThread = GetCurrentThread();
    DWORD_PTR previousMask = SetThreadAffinityMask(currentThread, 1);

    // Get the frequency of the performance counter
    // (it is constant across the program lifetime)
    static LARGE_INTEGER frequency = getFrequency();

    // Get the current time
    LARGE_INTEGER time;
    QueryPerformanceCounter(&time);

    // Restore the thread affinity
    SetThreadAffinityMask(currentThread, previousMask);

    // Return the current time as microseconds
    return sf::microseconds(1000000 * time.QuadPart / frequency.QuadPart);
}

 
Yes it does, apparently. I think Evil Laurent said that clocks always go for highest avaible resolution but I might be remembering wrong.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Is sf::Clock accurate enough for fixed Time step game loops?
« Reply #3 on: February 10, 2013, 04:10:55 pm »
For Windows it uses QueryPerformanceCounter (see here), for Mac OS X it seems to use mach_timebase_info (see here) and for other Unix based system it seems to use clock_gettime (see here).

None of the functions are perfect, but for nearly all games I can think of it's precise enough and you'll have a hard time finding something better.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

sonicd007

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Is sf::Clock accurate enough for fixed Time step game loops?
« Reply #4 on: February 10, 2013, 04:18:19 pm »
Thank you very much!