Fix overflow when calculating time value for Clock

This commit is contained in:
Mozzg 2021-03-29 19:24:55 +03:00 committed by Lukas Dürrenberger
parent 43b2e9dc12
commit ce992ee01f

View File

@ -54,9 +54,9 @@ namespace priv
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
Time ClockImpl::getCurrentTime() Time ClockImpl::getCurrentTime()
{ {
// Get the frequency of the performance counter // Calculate inverse of frequency multiplied by 1000000 to prevent overflow in final calculation
// (it is constant across the program lifetime) // Frequency is constant across the program lifetime
static LARGE_INTEGER frequency = getFrequency(); static double inverse = 1000000.0 / getFrequency().QuadPart;
// Detect if we are on Windows XP or older // Detect if we are on Windows XP or older
static bool oldWindows = isWindowsXpOrOlder(); static bool oldWindows = isWindowsXpOrOlder();
@ -80,7 +80,7 @@ Time ClockImpl::getCurrentTime()
} }
// Return the current time as microseconds // Return the current time as microseconds
return sf::microseconds(1000000 * time.QuadPart / frequency.QuadPart); return sf::microseconds(static_cast<sf::Int64>(time.QuadPart * inverse));
} }
} // namespace priv } // namespace priv