From 021e1358c765114dabb8b31564760e40c6548e94 Mon Sep 17 00:00:00 2001 From: pogokeen Date: Wed, 14 Aug 2019 21:42:30 +0000 Subject: [PATCH] timer.cpp: improve cross-platform & cross-compiler issues relating to precision & period. Handle timerlastsample such that we don't throw away time & run slow in certain cases. Ensure when calculating n, we do the double division first before multiplying for sake of floating point precision. git-svn-id: https://svn.eduke32.com/eduke32@8009 1a8010ca-5511-0410-912e-c29ae57300e0 --- source/build/src/timer.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/source/build/src/timer.cpp b/source/build/src/timer.cpp index 4463e91da..eade9fb3c 100644 --- a/source/build/src/timer.cpp +++ b/source/build/src/timer.cpp @@ -9,13 +9,15 @@ using namespace std; using namespace chrono; +EDUKE32_STATIC_ASSERT((steady_clock::period::den/steady_clock::period::num) >= 1000000000); + static time_point timerlastsample; static int timerticspersec; static void(*usertimercallback)(void) = NULL; int timerGetRate(void) { return timerticspersec; } uint32_t timerGetTicks(void) { return duration_cast(steady_clock::now().time_since_epoch()).count(); } -uint64_t timerGetTicksU64(void) { return steady_clock::now().time_since_epoch().count(); } +uint64_t timerGetTicksU64(void) { return steady_clock::now().time_since_epoch().count() * steady_clock::period::num; } uint64_t timerGetFreqU64(void) { return steady_clock::period::den; } // Returns the time since an unspecified starting time in milliseconds. @@ -35,12 +37,13 @@ int timerInit(int const tickspersecond) ATTRIBUTE((flatten)) void timerUpdate(void) { auto time = steady_clock::now(); - int n = (time - timerlastsample).count() * (double)timerticspersec / timerGetFreqU64(); + auto elapsedTime = time - timerlastsample; + int n = elapsedTime.count() * ((double)timerticspersec / timerGetFreqU64()) * steady_clock::period::num; if (n <= 0) return; totalclock += n; - timerlastsample = time; + timerlastsample += n*nanoseconds(1000000000/timerticspersec); if (usertimercallback) for (; n > 0; n--) usertimercallback();