Fixes two major problems: time wrapping every 42 days, and timer

resolution getting poor after a couple days of play.

(for some value of "major")
This commit is contained in:
Glenn Maynard
2003-07-12 22:51:27 +00:00
parent cbfa6c72fc
commit fbcd804243
2 changed files with 87 additions and 59 deletions
+48 -16
View File
@@ -1,29 +1,61 @@
#ifndef RAGE_TIMER_H
#define RAGE_TIMER_H
/* Timer services. */
/*
-----------------------------------------------------------------------------
File: RageTimer.h
Desc: Timer services.
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
* 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() );
* }
*/
class RageTimer
{
public:
RageTimer();
unsigned int GetTicks();
float GetDeltaTime(); // time between last call to GetDeltaTime()
float PeekDeltaTime() const;
RageTimer() { Touch(); }
/* Time ago this RageTimer represents. */
float Ago() const;
inline void Touch();
inline bool IsZero() const { return m_secs == 0 && m_us == 0; }
inline void SetZero() { m_secs = m_us = 0; }
/* Time between last call to GetDeltaTime() (Ago() + Touch()): */
float GetDeltaTime();
/* (alias) */
float PeekDeltaTime() const { return Ago(); }
/* deprecated: */
static float GetTimeSinceStart(); // seconds since the program was started
private:
static void Init();
int m_iLastDeltaTime;
static float Difference(const RageTimer &lhs, const RageTimer &rhs);
/* "float" is bad for a "time since start" RageTimer. If the game is running for
* several days, we'll lose a lot of resolution. I don't want to use double
* everywhere, since it's slow. I'd rather not use double just for RageTimers, since
* it's too easy to get a type wrong and end up with obscure resolution problems. */
unsigned m_secs, m_us;
};
#endif
/*
* Copyright (c) 2001-2003 by the person(s) listed below. All rights reserved.
* Chris Danford
* Glenn Maynard
*/