mirror of
https://github.com/DrBeef/Raze.git
synced 2024-11-15 00:41:55 +00:00
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
This commit is contained in:
parent
002a52033d
commit
021e1358c7
1 changed files with 6 additions and 3 deletions
|
@ -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<steady_clock> timerlastsample;
|
||||
static int timerticspersec;
|
||||
static void(*usertimercallback)(void) = NULL;
|
||||
|
||||
int timerGetRate(void) { return timerticspersec; }
|
||||
uint32_t timerGetTicks(void) { return duration_cast<milliseconds>(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();
|
||||
|
|
Loading…
Reference in a new issue