Files
itgmania212121/stepmania/src/RageTimer.cpp
T

162 lines
4.4 KiB
C++
Raw Normal View History

2005-02-13 03:05:19 +00:00
/*
* This can be used in two ways: as a timestamp or as a timer.
*
* As a timer,
* RageTimer Timer;
* while(1) {
* printf( "Will be approximately: %f", Timer.PeekDeltaTime()) ;
* float fDeltaTime = Timer.GetDeltaTime();
* }
*
* or as a timestamp:
* void foo( RageTimer &timestamp ) {
* if( timestamp.IsZero() )
* printf( "The timestamp isn't set." );
* else
* printf( "The timestamp happened %f ago", timestamp.Ago() );
* timestamp.Touch();
* printf( "Near zero: %f", timestamp.Age() );
* }
*/
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
#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);
static uint64_t g_iStartTime = ArchHooks::GetMicrosecondsSinceStart( true );
2003-09-07 22:37:29 +00:00
static uint64_t GetTime( bool bAccurate )
{
// if !bAccurate, then don't call ArchHooks to find the current time. Just return the
// last calculated time. GetMicrosecondsSinceStart is slow on some archs.
static uint64_t usecs = 0;
if( bAccurate )
usecs = ArchHooks::GetMicrosecondsSinceStart( true );
return usecs;
}
float RageTimer::GetTimeSinceStart( bool bAccurate )
2002-06-14 22:25:22 +00:00
{
uint64_t usecs = GetTime( bAccurate );
usecs -= g_iStartTime;
// HACK: VC6 can't cast u64 to a double, but it can cast a i64 to a double.
// We can afford to lose one bit of precision since these numbers will
// never overflow a 64 int.
return (int64_t)usecs / 1000000.f;
2002-06-14 22:25:22 +00:00
}
void RageTimer::Touch()
2002-06-14 22:25:22 +00:00
{
uint64_t usecs = GetTime( true );
this->m_secs = unsigned(usecs / 1000000);
this->m_us = unsigned(usecs % 1000000);
2002-06-14 22:25:22 +00:00
}
float RageTimer::Ago() const
2002-06-14 22:25:22 +00:00
{
const RageTimer Now;
return Now - *this;
2002-06-14 22:25:22 +00:00
}
float RageTimer::GetDeltaTime()
2002-06-14 22:25:22 +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 );
2005-03-17 04:31:12 +00:00
RageTimer ret(0,0); // Prevent unnecessarily checking the time
2003-07-13 00:31:55 +00:00
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;
}
float RageTimer::Difference(const RageTimer &lhs, const RageTimer &rhs)
{
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;
}
/*
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.
*/
2004-05-06 02:40:33 +00:00