Browse Source

Implement Timer::PollSeconds(), if necessary.

legacy
Sam Hocevar sam 14 years ago
parent
commit
0d78ca44ca
2 changed files with 18 additions and 12 deletions
  1. +17
    -12
      src/timer.cpp
  2. +1
    -0
      src/timer.h

+ 17
- 12
src/timer.cpp View File

@@ -48,35 +48,35 @@ private:
#endif #endif
} }


float GetOrWait(float seconds)
float GetOrWait(float seconds, bool update)
{ {
float ret, delta_time; float ret, delta_time;
#if defined __linux__ #if defined __linux__
struct timeval tv; struct timeval tv;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
ret = 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec); ret = 1e-6f * (tv.tv_usec - tv0.tv_usec) + (tv.tv_sec - tv0.tv_sec);
delta_time = seconds - ret;
if (!seconds)
if (update)
tv0 = tv; tv0 = tv;
else if (delta_time > 0.0f)
delta_time = seconds - ret;
if (delta_time > 0.0f)
usleep((int)(delta_time * 1e6f)); usleep((int)(delta_time * 1e6f));
#elif defined _WIN32 #elif defined _WIN32
LARGE_INTEGER cycles; LARGE_INTEGER cycles;
QueryPerformanceCounter(&cycles); QueryPerformanceCounter(&cycles);
ret = seconds_per_cycle * (cycles.QuadPart - cycles0.QuadPart); ret = seconds_per_cycle * (cycles.QuadPart - cycles0.QuadPart);
delta_time = seconds - ret;
if (!seconds)
if (update)
cycles0 = cycles; cycles0 = cycles;
else if (delta_time > 5e-4f) // FIXME: use native Win32 stuff
delta_time = seconds - ret;
if (delta_time > 5e-4f) // FIXME: use native Win32 stuff
SDL_Delay((int)(delta_time * 1e3f + 0.5f)); SDL_Delay((int)(delta_time * 1e3f + 0.5f));
#else #else
/* The crappy SDL fallback */ /* The crappy SDL fallback */
Uint32 ticks = SDL_GetTicks(); Uint32 ticks = SDL_GetTicks();
ret = 1e-3f * (ticks - ticks0); ret = 1e-3f * (ticks - ticks0);
delta_time = seconds - ret;
if (!seconds)
if (update)
ticks0 = ticks; ticks0 = ticks;
else if (delta_time > 5e-4f)
delta_time = seconds - ret;
if (delta_time > 5e-4f)
SDL_Delay((int)(delta_time * 1e3f + 0.5f)); SDL_Delay((int)(delta_time * 1e3f + 0.5f));
#endif #endif
return ret; return ret;
@@ -108,11 +108,16 @@ Timer::~Timer()


float Timer::GetSeconds() float Timer::GetSeconds()
{ {
return data->GetOrWait(0.0f);
return data->GetOrWait(0.0f, true);
}

float Timer::PollSeconds()
{
return data->GetOrWait(0.0f, false);
} }


void Timer::WaitSeconds(float seconds) void Timer::WaitSeconds(float seconds)
{ {
(void)data->GetOrWait(seconds);
(void)data->GetOrWait(seconds, false);
} }



+ 1
- 0
src/timer.h View File

@@ -20,6 +20,7 @@ public:
~Timer(); ~Timer();


float GetSeconds(); float GetSeconds();
float PollSeconds();
void WaitSeconds(float milliseconds); void WaitSeconds(float milliseconds);


private: private:


Loading…
Cancel
Save