From 365068420afb37515d362052d1b5f31d43327f1e Mon Sep 17 00:00:00 2001 From: Quit Date: Tue, 29 May 2012 21:07:11 +0200 Subject: [PATCH] Gosu::milliseconds returns milliseconds since start on Win/Unix There's still this 149-runtime-days-overflow, but at least it won't happen "randomly" anymore. --- GosuImpl/TimingUnix.cpp | 8 +++++++- GosuImpl/TimingWin.cpp | 9 +++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/GosuImpl/TimingUnix.cpp b/GosuImpl/TimingUnix.cpp index d0beaadd8..392026801 100644 --- a/GosuImpl/TimingUnix.cpp +++ b/GosuImpl/TimingUnix.cpp @@ -9,10 +9,16 @@ void Gosu::sleep(unsigned milliseconds) unsigned long Gosu::milliseconds() { + static unsigned long start = 0; + timeval tp; gettimeofday(&tp, NULL); + + if (!start) + start = tp.tv_usec / 1000UL + tp.tv_sec * 1000UL; + // Truncate to 2^30, C++ users shouldn't mind and Ruby users will // have a happy GC on 32-bit systems. // No, don't ask why this is an unsigned long then :) - return (tp.tv_usec / 1000UL + tp.tv_sec * 1000UL) & 0x1fffffff; + return (tp.tv_usec / 1000UL + tp.tv_sec * 1000UL - start) & 0x1fffffff; } diff --git a/GosuImpl/TimingWin.cpp b/GosuImpl/TimingWin.cpp index c2a1449e8..85b452ece 100644 --- a/GosuImpl/TimingWin.cpp +++ b/GosuImpl/TimingWin.cpp @@ -17,15 +17,16 @@ namespace unsigned long Gosu::milliseconds() { - static bool init = false; - if (!init) + static unsigned long start = 0; + + if (!start) { if (::timeBeginPeriod(1) != TIMERR_NOERROR) std::atexit(resetTGT); - init = true; + start = ::timeGetTime(); } // Truncate to 2^30, C++ users shouldn't mind and Ruby users will // have a happy GC on 32-bit systems. // No, don't ask why this is an unsigned long then :) - return ::timeGetTime() & 0x1fffffff; + return (::timeGetTime() - start) & 0x1fffffff; }