From 66ecf8aff004a4d64bd7226bc41cb676c1e1dfef Mon Sep 17 00:00:00 2001 From: Bart van Strien Date: Sun, 2 Oct 2011 16:11:24 +0200 Subject: [PATCH] Apply patch for increased timer accuracy (issue #303) --- src/modules/timer/sdl/Timer.cpp | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/modules/timer/sdl/Timer.cpp b/src/modules/timer/sdl/Timer.cpp index da9dca743..e15505d34 100644 --- a/src/modules/timer/sdl/Timer.cpp +++ b/src/modules/timer/sdl/Timer.cpp @@ -103,19 +103,27 @@ namespace sdl double Timer::getMicroTime() const { #ifdef LOVE_WINDOWS - __int64 ticks, freq; - LARGE_INTEGER temp; - QueryPerformanceCounter(&temp); - ticks = temp.QuadPart; - QueryPerformanceFrequency(&temp); - freq = temp.QuadPart; - __int64 secs = ticks/freq; - __int64 usecs = static_cast<__int64>((ticks%freq)/(freq/1000000.0)); - return secs%86400 + usecs/1000000.0; + static __int64 freq = 0; + + if (!freq) + { + LARGE_INTEGER temp; + QueryPerformanceFrequency(&temp); + + freq = (__int64) temp.QuadPart; + } + + LARGE_INTEGER microTime; + QueryPerformanceCounter(µTime); + + // The 64 to 32 bit integer conversion, assuming the fraction part down + // to microseconds takes 20 bits, should not be a problem unless the + // system has an uptime of a few decades. + return (double) microTime.QuadPart / (double) freq; #else timeval t; gettimeofday(&t, NULL); - return t.tv_sec%86400 + t.tv_usec/1000000.0; + return t.tv_sec + t.tv_usec/1000000.0; #endif }