From 68664405d28b740962d0137d158ea8c1e608194b Mon Sep 17 00:00:00 2001 From: rxi Date: Mon, 23 Jun 2014 19:53:17 +0100 Subject: [PATCH] Added handling for uclock() returning earlier time On some systems the call to uclock() would sometimes return a slightly earlier time than the previous call, this would result in a negative delta time. This is now checked for and uclock() is now called repeatedly until we have a valid delta time. --- src/timer.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/timer.c b/src/timer.c index 253c7cc..be95b7c 100644 --- a/src/timer.c +++ b/src/timer.c @@ -23,8 +23,14 @@ double timer_avgTimer; int l_timer_step(lua_State *L) { /* Do delta */ - long long now = uclock(); - timer_lastDt = (now - timer_lastStep) / (double)UCLOCKS_PER_SEC; + long long now; + /* Sometimes a call to uclock() will return a slightly earlier time than the + * previous call, resulting in a negative delta time. The below loop keeps + * trying for a proper value if this occurs. */ + do { + now = uclock(); + timer_lastDt = (now - timer_lastStep) / (double)UCLOCKS_PER_SEC; + } while (timer_lastDt < 0); timer_lastStep = now; /* Do average */ timer_avgAcc += timer_lastDt;