Skip to content

Commit

Permalink
Merge pull request #131 from Quit/pullrequests4lyfe
Browse files Browse the repository at this point in the history
Gosu::milliseconds returns milliseconds since first call on Win/Unix
  • Loading branch information
jlnr committed May 31, 2012
2 parents 44ebc65 + 3650684 commit 0459d14
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 0459d14

Please sign in to comment.