2003-02-16 04:01:45 +00:00
|
|
|
#include "global.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
|
|
|
|
#include "RageTimer.h"
|
|
|
|
|
#include "RageLog.h"
|
|
|
|
|
|
2003-09-27 22:24:43 +00:00
|
|
|
#include "SDL.h"
|
|
|
|
|
#include "SDL_timer.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
/* We only actually get 1000 using SDL. */
|
|
|
|
|
#define TIMESTAMP_RESOLUTION 1000000
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2003-09-07 22:37:29 +00:00
|
|
|
const RageZeroTimer_t RageZeroTimer;
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
float RageTimer::GetTimeSinceStart()
|
2002-06-14 22:25:22 +00:00
|
|
|
{
|
2003-07-14 06:22:08 +00:00
|
|
|
return SDL_GetTicks() / 1000.0f;
|
2002-06-14 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
void RageTimer::Touch()
|
2002-06-14 22:25:22 +00:00
|
|
|
{
|
2003-07-12 22:51:27 +00:00
|
|
|
unsigned ms = SDL_GetTicks();
|
|
|
|
|
this->m_secs = ms / 1000;
|
|
|
|
|
ms %= 1000;
|
|
|
|
|
|
|
|
|
|
unsigned mult = TIMESTAMP_RESOLUTION / 1000;
|
|
|
|
|
this->m_us = ms*mult;
|
2002-06-14 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
float RageTimer::Ago() const
|
2002-06-14 22:25:22 +00:00
|
|
|
{
|
2003-07-12 22:51:27 +00:00
|
|
|
const RageTimer Now;
|
2003-07-13 01:26:30 +00:00
|
|
|
return Now - *this;
|
2002-06-14 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
float RageTimer::GetDeltaTime()
|
2002-06-14 22:25:22 +00:00
|
|
|
{
|
2003-07-12 22:51:27 +00:00
|
|
|
const RageTimer Now;
|
|
|
|
|
const float diff = Difference( Now, *this );
|
|
|
|
|
*this = Now;
|
|
|
|
|
return diff;
|
2002-06-14 22:25:22 +00:00
|
|
|
}
|
|
|
|
|
|
2003-07-20 19:13:03 +00:00
|
|
|
/* Get a timer representing half of the time ago as this one. This is
|
|
|
|
|
* useful for averaging time. For example,
|
|
|
|
|
*
|
|
|
|
|
* RageTimer tm;
|
|
|
|
|
* ... do stuff ...
|
|
|
|
|
* RageTimer AverageTime = tm.Half();
|
|
|
|
|
* printf("Something happened between now and tm; the average time is %f.\n", tm.Ago());
|
|
|
|
|
* tm.Touch();
|
|
|
|
|
*/
|
|
|
|
|
RageTimer RageTimer::Half() const
|
|
|
|
|
{
|
|
|
|
|
const RageTimer now;
|
|
|
|
|
const float ProbableDelay = -(now - *this) / 2;
|
|
|
|
|
return *this + ProbableDelay;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2003-07-13 00:31:55 +00:00
|
|
|
RageTimer RageTimer::operator+(float tm) const
|
|
|
|
|
{
|
|
|
|
|
return Sum(*this, tm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float RageTimer::operator-(const RageTimer &rhs) const
|
|
|
|
|
{
|
|
|
|
|
return Difference(*this, rhs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RageTimer RageTimer::Sum(const RageTimer &lhs, float tm)
|
|
|
|
|
{
|
|
|
|
|
/* tm == 5.25 -> secs = 5, us = 5.25 - ( 5) = .25
|
|
|
|
|
* tm == -1.25 -> secs = -2, us = -1.25 - (-2) = .75 */
|
|
|
|
|
int seconds = (int) floorf(tm);
|
|
|
|
|
int us = int( (tm - seconds) * TIMESTAMP_RESOLUTION );
|
|
|
|
|
|
|
|
|
|
RageTimer ret;
|
|
|
|
|
ret.m_secs = seconds + lhs.m_secs;
|
|
|
|
|
ret.m_us = us + lhs.m_us;
|
|
|
|
|
|
|
|
|
|
if( ret.m_us >= TIMESTAMP_RESOLUTION )
|
|
|
|
|
{
|
|
|
|
|
ret.m_us -= TIMESTAMP_RESOLUTION;
|
|
|
|
|
++ret.m_secs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
float RageTimer::Difference(const RageTimer &lhs, const RageTimer &rhs)
|
2002-11-26 01:17:43 +00:00
|
|
|
{
|
2003-07-12 22:51:27 +00:00
|
|
|
int secs = lhs.m_secs - rhs.m_secs;
|
|
|
|
|
int us = lhs.m_us - rhs.m_us;
|
|
|
|
|
|
|
|
|
|
if( us < 0 )
|
|
|
|
|
{
|
|
|
|
|
us += TIMESTAMP_RESOLUTION;
|
|
|
|
|
--secs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return float(secs) + float(us) / TIMESTAMP_RESOLUTION;
|
2002-11-26 01:17:43 +00:00
|
|
|
}
|
2003-07-12 22:51:27 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
|
|
|
|
|
* Chris Danford
|
|
|
|
|
* Glenn Maynard
|
|
|
|
|
*/
|