From c5925aea51f55a2d22ac78e8edf9e42e69c5d4f4 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Thu, 24 Oct 2002 03:55:40 +0000 Subject: [PATCH] SDL-based timer. Trivial. --- stepmania/src/RageTimer.cpp | 37 ++++++++++++++++++------------------- stepmania/src/RageTimer.h | 8 +++----- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/stepmania/src/RageTimer.cpp b/stepmania/src/RageTimer.cpp index ea5149c192..165d4bfb25 100644 --- a/stepmania/src/RageTimer.cpp +++ b/stepmania/src/RageTimer.cpp @@ -13,42 +13,41 @@ #include "RageTimer.h" #include "RageLog.h" -#include "DXUtil.h" +#include "SDL-1.2.5/include/SDL.h" +#include "SDL-1.2.5/include/SDL_timer.h" - RageTimer* TIMER = NULL; - const float SECS_IN_DAY = 60*60*24; +/* XXX: SDL_GetTicks() wraps every month or so. Handle it. */ RageTimer::RageTimer() { - m_fTimeSinceStart = m_fLastDeltaTime = 0; - DXUtil_Timer( TIMER_START ); // Start the accurate timer -} - -RageTimer::~RageTimer() -{ - DXUtil_Timer( TIMER_STOP ); + static bool SDL_Initialized = false; + if(!SDL_Initialized) { + SDL_InitSubSystem(SDL_INIT_TIMER); + SDL_Initialized = true; + } + GetDeltaTime(); /* so the next call to GetDeltaTime is from the construction of this object */ } float RageTimer::GetDeltaTime() { - m_fLastDeltaTime = DXUtil_Timer( TIMER_GETELAPSEDTIME ); - m_fTimeSinceStart += m_fLastDeltaTime; - if( m_fTimeSinceStart > SECS_IN_DAY ) - m_fTimeSinceStart = SECS_IN_DAY; - return m_fLastDeltaTime; + /* Store the LDT in integral milliseconds, to avoid rounding error. */ + int now = SDL_GetTicks(); + int ret = now - m_iLastDeltaTime; + m_iLastDeltaTime = now; + return ret / 1000.f; } -float RageTimer::PeekDeltaTime() +float RageTimer::PeekDeltaTime() const { - return m_fLastDeltaTime; + return (SDL_GetTicks() - m_iLastDeltaTime) / 1000.f; } -float RageTimer::GetTimeSinceStart() +float RageTimer::GetTimeSinceStart() const { - return m_fTimeSinceStart; + return SDL_GetTicks() / 1000.f; } diff --git a/stepmania/src/RageTimer.h b/stepmania/src/RageTimer.h index 4bfbf652ab..6b6767a254 100644 --- a/stepmania/src/RageTimer.h +++ b/stepmania/src/RageTimer.h @@ -16,14 +16,12 @@ class RageTimer { public: RageTimer(); - ~RageTimer(); float GetDeltaTime(); // time between last call to GetDeltaTime() - float PeekDeltaTime(); - float GetTimeSinceStart(); // seconds since the program was started + float PeekDeltaTime() const; + float GetTimeSinceStart() const; // seconds since the program was started private: - float m_fLastDeltaTime; - float m_fTimeSinceStart; // seconds since the program was started + int m_iLastDeltaTime; };