Browse Source

Apply patch for increased timer accuracy (issue #303)

Bart van Strien 14 years ago
parent
commit
66ecf8aff0
1 changed files with 18 additions and 10 deletions
  1. 18 10
      src/modules/timer/sdl/Timer.cpp

+ 18 - 10
src/modules/timer/sdl/Timer.cpp

@@ -103,19 +103,27 @@ namespace sdl
 	double Timer::getMicroTime() const
 	double Timer::getMicroTime() const
 	{
 	{
 #ifdef LOVE_WINDOWS
 #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(&microTime);
+
+		// 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
 #else
 		timeval t;
 		timeval t;
 		gettimeofday(&t, NULL);
 		gettimeofday(&t, NULL);
-		return t.tv_sec%86400 + t.tv_usec/1000000.0;
+		return t.tv_sec + t.tv_usec/1000000.0;
 #endif
 #endif
 	}
 	}