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"
|
|
|
|
|
|
2004-06-11 01:14:42 +00:00
|
|
|
#include "arch/ArchHooks/ArchHooks.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
#define TIMESTAMP_RESOLUTION 1000000
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2004-03-13 23:11:08 +00:00
|
|
|
const RageTimer RageZeroTimer(0,0);
|
2003-09-07 22:37:29 +00:00
|
|
|
|
2004-06-11 01:14:42 +00:00
|
|
|
static void GetTime( unsigned &secs, unsigned &us )
|
2004-02-22 23:27:42 +00:00
|
|
|
{
|
2004-06-11 01:14:42 +00:00
|
|
|
/*
|
|
|
|
|
* Ticks may be less than last for at least two reasons: the time may have wrapped (after
|
2004-02-22 23:27:42 +00:00
|
|
|
* about 49 days), or the system clock may have moved backwards. If the system clock moves
|
|
|
|
|
* backwards, we can't just clamp the time; if it moved back an hour, we'll sit around for
|
|
|
|
|
* an hour until it catches up.
|
|
|
|
|
*
|
2004-06-11 01:14:42 +00:00
|
|
|
* Keep track of an offset: the amount of time to add to the result of GetMicrosecondsSinceStart.
|
2004-02-22 23:27:42 +00:00
|
|
|
* If we move back by 100ms, the offset will be increased by 100ms. If we loop, the
|
|
|
|
|
* offset will be increased by the duration 2^32 ticks. This is stored in the same
|
|
|
|
|
* notation as RageTimer: one 32-bit int for seconds and another for microseconds, so
|
|
|
|
|
* wrapping isn't a problem.
|
|
|
|
|
*/
|
|
|
|
|
|
2004-05-11 20:50:08 +00:00
|
|
|
static uint32_t last = 0;
|
|
|
|
|
static uint32_t offset_secs = 0, offset_us = 0;
|
2004-02-22 23:27:42 +00:00
|
|
|
|
2004-06-11 01:14:42 +00:00
|
|
|
const uint32_t millisecs = (uint32_t) (ArchHooks::GetMicrosecondsSinceStart(true)/1000);
|
2004-02-22 23:27:42 +00:00
|
|
|
|
|
|
|
|
/* The time has wrapped if the last time was very high and the current time is very low. */
|
2004-05-11 20:50:08 +00:00
|
|
|
const uint32_t one_day = 24*60*60*1000;
|
2004-02-22 23:27:42 +00:00
|
|
|
if( last > (0-one_day) && millisecs < one_day )
|
|
|
|
|
{
|
2004-05-11 20:50:08 +00:00
|
|
|
const uint32_t wraparound_secs = 4294967; /* (2^32 / 1000) */
|
|
|
|
|
const uint32_t wraparound_us = 296000; /* (2^32 % 1000) * 1000 */
|
2004-02-22 23:27:42 +00:00
|
|
|
offset_secs += wraparound_secs;
|
|
|
|
|
offset_us += wraparound_us;
|
|
|
|
|
}
|
|
|
|
|
else if( millisecs < last )
|
|
|
|
|
{
|
|
|
|
|
/* The time has moved backwards. Increase the offset by the amount we moved. */
|
2004-05-11 20:50:08 +00:00
|
|
|
const uint32_t offset_ms = last - millisecs;
|
2004-02-22 23:27:42 +00:00
|
|
|
offset_secs += offset_ms/1000;
|
|
|
|
|
offset_us += (offset_ms%1000) * 1000;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-23 00:04:38 +00:00
|
|
|
if( offset_us >= TIMESTAMP_RESOLUTION )
|
|
|
|
|
{
|
|
|
|
|
offset_us -= TIMESTAMP_RESOLUTION;
|
|
|
|
|
++offset_secs;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-22 23:27:42 +00:00
|
|
|
last = millisecs;
|
|
|
|
|
|
|
|
|
|
secs = millisecs / 1000;
|
|
|
|
|
us = (millisecs % 1000)*1000;
|
|
|
|
|
|
|
|
|
|
secs += offset_secs;
|
|
|
|
|
us += offset_us;
|
|
|
|
|
|
|
|
|
|
if( us >= TIMESTAMP_RESOLUTION )
|
|
|
|
|
{
|
|
|
|
|
us -= TIMESTAMP_RESOLUTION;
|
|
|
|
|
++secs;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-12 22:51:27 +00:00
|
|
|
float RageTimer::GetTimeSinceStart()
|
2002-06-14 22:25:22 +00:00
|
|
|
{
|
2004-02-22 23:27:42 +00:00
|
|
|
unsigned secs, us;
|
2004-06-11 01:14:42 +00:00
|
|
|
GetTime( secs, us );
|
2004-02-22 23:27:42 +00:00
|
|
|
return secs + us / 1000000.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
|
|
|
{
|
2004-06-11 01:14:42 +00:00
|
|
|
GetTime( this->m_secs, this->m_us );
|
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;
|
2004-02-22 23:27:42 +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;
|
2004-02-22 23:27:42 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
/*
|
2004-05-06 02:40:33 +00:00
|
|
|
* Copyright (c) 2001-2003 Chris Danford, Glenn Maynard
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, and/or sell copies of the Software, and to permit persons to
|
|
|
|
|
* whom the Software is furnished to do so, provided that the above
|
|
|
|
|
* copyright notice(s) and this permission notice appear in all copies of
|
|
|
|
|
* the Software and that both the above copyright notice(s) and this
|
|
|
|
|
* permission notice appear in supporting documentation.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
|
|
|
|
|
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
|
|
|
|
|
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
|
|
|
|
|
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
|
|
|
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
2003-07-12 22:51:27 +00:00
|
|
|
*/
|
2004-05-06 02:40:33 +00:00
|
|
|
|