Skip to content

Commit

Permalink
Gosu::milliseconds returns milliseconds since start on Win/Unix
Browse files Browse the repository at this point in the history
There's still this 149-runtime-days-overflow, but at least it won't happen "randomly" anymore.
  • Loading branch information
Quit committed May 31, 2012
1 parent 44ebc65 commit 3650684
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 7 additions & 1 deletion GosuImpl/TimingUnix.cpp
Expand Up @@ -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;
}
9 changes: 5 additions & 4 deletions GosuImpl/TimingWin.cpp
Expand Up @@ -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;
}

0 comments on commit 3650684

Please sign in to comment.