00001 #ifndef STOP_WATCH_INCLUDED
00002 #define STOP_WATCH_INCLUDED
00003
00004 #ifndef WIN32
00005 #include <sys/time.h>
00006 #endif
00007
00008 namespace ani
00009 {
00010
00011
00012
00013
00014
00015 class StopWatch
00016 {
00017 public:
00018
00019 StopWatch( const int& averageFpsRefreshRate = 15 );
00020
00021
00022 public:
00023
00024
00025 void start();
00026
00027
00028
00029 void stop();
00030
00031
00032
00033 void pulse();
00034
00035
00036
00037
00038 void reset();
00039
00040
00041 public:
00042
00043
00044
00045
00046
00047
00048 inline void setRefreshRate( const int& rate ) { mRefreshRate = rate; }
00049
00050
00051
00052 public:
00053
00054
00055
00056
00057 inline const double& timeAverage() const { return mTimeAverage; }
00058
00059
00060
00061
00062
00063 inline const double& timeInstant() const { return mTimeInstant; }
00064
00065
00066
00067 inline const double& timeStart() const { return mTimeStarted; }
00068
00069
00070
00071 inline const double& timeStop() const { return mTimeStopped; }
00072
00073
00074 public:
00075
00076
00077
00078 inline const unsigned long& count() const { return mCount; }
00079
00080
00081 inline const double& fpsAverage() const { return mFpsAverage; }
00082
00083
00084 inline const double& fpsInstant() const { return mFpsInstant; }
00085
00086 public:
00087
00088
00089
00090 static void getTime( double& num );
00091 static double getTime();
00092
00093
00094 protected:
00095 double mTimeStarted;
00096 double mTimeStopped;
00097 double mTimeAverage;
00098 double mTimeInstant;
00099
00100
00101 protected:
00102 int mRefreshRate;
00103
00104
00105 protected:
00106 unsigned long mCount;
00107 double mFpsAverage;
00108 double mFpsInstant;
00109
00110
00111 private:
00112 double mTimeAccumulator;
00113 };
00114
00116
00118
00119
00120 inline StopWatch::StopWatch( const int& averageFpsRefreshRate ) :
00121 mTimeStarted( 0.0 ),
00122 mTimeStopped( 0.0 ),
00123 mTimeAverage( 0.0 ),
00124 mTimeInstant( 0.0 ),
00125 mRefreshRate( averageFpsRefreshRate ),
00126 mCount( 0 ),
00127 mFpsAverage( 0.0 ),
00128 mFpsInstant( 0.0 ),
00129 mTimeAccumulator( 0.0 )
00130 {
00131 }
00132
00133
00134
00135
00136
00137
00138 #ifndef WIN32
00139 inline void StopWatch::getTime( double& num )
00140 {
00141 struct timeval tv;
00142 gettimeofday( &tv, 0 );
00143
00144
00145 num = static_cast<double>( tv.tv_sec )
00146 + ( static_cast<double>( tv.tv_usec )
00147 / 1000000.0 );
00148 }
00149 #else
00150 #include <sys/types.h>
00151 #include <sys/timeb.h>
00152 inline void StopWatch::getTime( double& num )
00153 {
00154 struct _timeb tv;
00155 _ftime( &tv );
00156
00157
00158 num = static_cast<double>( tv.time )
00159 + ( static_cast<double>( tv.millitm )
00160 / 1000.0 );
00161 }
00162 #endif
00163
00164
00165 inline double StopWatch::getTime()
00166 {
00167 double num;
00168 StopWatch::getTime( num );
00169 return num;
00170 }
00171
00172
00173
00174
00175 inline void StopWatch::start()
00176 {
00177 StopWatch::getTime( mTimeStarted );
00178 }
00179
00180
00181
00182
00183
00184
00185
00186 inline void StopWatch::stop()
00187 {
00188 StopWatch::getTime( mTimeStopped );
00189
00190
00191 mTimeInstant = mTimeStopped - mTimeStarted;
00192
00193
00194 if (mCount % mRefreshRate == 0)
00195 {
00196 mFpsAverage = mRefreshRate / mTimeAccumulator;
00197 mTimeAverage = mTimeAccumulator / mRefreshRate;
00198
00199
00200 mTimeAccumulator = 0.0;
00201 }
00202
00203
00204
00205 mTimeAccumulator += mTimeInstant;
00206
00207
00208 mFpsInstant = 1.0 / mTimeInstant;
00209
00210
00211 ++mCount;
00212 }
00213
00214
00215
00216 inline void StopWatch::pulse()
00217 {
00218 this->stop();
00219 this->start();
00220 }
00221
00222
00223
00224 inline void StopWatch::reset()
00225 {
00226 mTimeStarted = 0.0;
00227 mTimeStopped = 0.0;
00228 mTimeAccumulator = 0.0;
00229
00230 mCount = 0;
00231 mFpsAverage = 0.0;
00232 mFpsInstant = 0.0;
00233 mTimeInstant = 0.0;
00234 }
00235
00236 };
00237
00238 #endif