Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members

RageTimer.h

Go to the documentation of this file.
00001 /* RageTimer - Timer services */ 00002 00003 #ifndef RAGE_TIMER_H 00004 #define RAGE_TIMER_H 00005 00006 /* 00007 * This can be used in two ways: as a timestamp or as a timer. 00008 * 00009 * As a timer, 00010 * RageTimer Timer; 00011 * while(1) { 00012 * printf( "Will be approximately: %f", Timer.PeekDeltaTime()) ; 00013 * float fDeltaTime = Timer.GetDeltaTime(); 00014 * } 00015 * 00016 * or as a timestamp: 00017 * void foo( RageTimer &timestamp ) { 00018 * if( timestamp.IsZero() ) 00019 * printf( "The timestamp isn't set." ); 00020 * else 00021 * printf( "The timestamp happened %f ago", timestamp.Ago() ); 00022 * timestamp.Touch(); 00023 * printf( "Near zero: %f", timestamp.Age() ); 00024 * } 00025 */ 00026 class RageTimer 00027 { 00028 public: 00029 RageTimer() { Touch(); } 00030 RageTimer( int secs, int us ): m_secs(secs), m_us(us) { } 00031 00032 /* Time ago this RageTimer represents. */ 00033 float Ago() const; 00034 void Touch(); 00035 inline bool IsZero() const { return m_secs == 0 && m_us == 0; } 00036 inline void SetZero() { m_secs = m_us = 0; } 00037 00038 /* Time between last call to GetDeltaTime() (Ago() + Touch()): */ 00039 float GetDeltaTime(); 00040 /* (alias) */ 00041 float PeekDeltaTime() const { return Ago(); } 00042 00043 /* deprecated: */ 00044 static float GetTimeSinceStart(); // seconds since the program was started 00045 00046 /* Get a timer representing half of the time ago as this one. */ 00047 RageTimer Half() const; 00048 00049 /* Add (or subtract) a duration from a timestamp. The result is another timestamp. */ 00050 RageTimer operator+( float tm ) const; 00051 void operator+=( float tm ) { *this = *this + tm; } 00052 00053 /* Find the amount of time between two timestamps. The result is a duration. */ 00054 float operator-( const RageTimer &rhs ) const; 00055 00056 /* "float" is bad for a "time since start" RageTimer. If the game is running for 00057 * several days, we'll lose a lot of resolution. I don't want to use double 00058 * everywhere, since it's slow. I'd rather not use double just for RageTimers, since 00059 * it's too easy to get a type wrong and end up with obscure resolution problems. */ 00060 unsigned m_secs, m_us; 00061 00062 private: 00063 static RageTimer Sum( const RageTimer &lhs, float tm ); 00064 static float Difference( const RageTimer &lhs, const RageTimer &rhs ); 00065 }; 00066 00067 extern const RageTimer RageZeroTimer; 00068 00069 #endif 00070 00071 /* 00072 * Copyright (c) 2001-2003 Chris Danford, Glenn Maynard 00073 * All rights reserved. 00074 * 00075 * Permission is hereby granted, free of charge, to any person obtaining a 00076 * copy of this software and associated documentation files (the 00077 * "Software"), to deal in the Software without restriction, including 00078 * without limitation the rights to use, copy, modify, merge, publish, 00079 * distribute, and/or sell copies of the Software, and to permit persons to 00080 * whom the Software is furnished to do so, provided that the above 00081 * copyright notice(s) and this permission notice appear in all copies of 00082 * the Software and that both the above copyright notice(s) and this 00083 * permission notice appear in supporting documentation. 00084 * 00085 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00086 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00087 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF 00088 * THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS 00089 * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT 00090 * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS 00091 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR 00092 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 00093 * PERFORMANCE OF THIS SOFTWARE. 00094 */ 00095

Generated on Thu Jan 27 20:57:29 2005 for StepMania by doxygen 1.3.7