00001 #pragma once 00002 00003 #include "common.h" 00004 #include <windows.h> 00005 #include <time.h> 00006 00008 class Timer 00009 { 00010 public: 00011 00015 Timer() : m_dStart(0.0), m_dCurrent(0.0), m_bRunning(false) 00016 { 00017 if (s_dSecondsPerTick <= 0.0) 00018 { 00019 LARGE_INTEGER frequency; 00020 00021 if (QueryPerformanceFrequency(&frequency)) 00022 s_dSecondsPerTick = 1.0 / double(frequency.QuadPart); 00023 else 00024 s_dSecondsPerTick = 1.0 / double(CLOCKS_PER_SEC); 00025 } 00026 00027 start(); 00028 }; 00029 00033 ~Timer() 00034 { 00035 }; 00036 00037 00041 void start() 00042 { 00043 m_bRunning = true; 00044 m_dStart = m_dCurrent = GetCurrentTime(); 00045 }; 00046 00047 00051 void stop() 00052 { 00053 m_dCurrent = GetCurrentTime(); 00054 m_bRunning = false; 00055 }; 00056 00057 operator double() const 00058 { 00059 return GetCurrentTime() - GetStartTime(); 00060 }; 00061 00062 operator float() const 00063 { 00064 return float(GetCurrentTime() - GetStartTime()); 00065 }; 00066 00071 const bool IsRunning() const 00072 { 00073 return m_bRunning; 00074 }; 00075 00076 private: 00077 00082 const double GetCurrentClock() const 00083 { 00084 LARGE_INTEGER now; 00085 00086 if (QueryPerformanceCounter(&now)) 00087 return double(now.QuadPart); 00088 00089 return double(clock()); 00090 }; 00091 00092 00097 const double GetStartTime() const 00098 { 00099 return m_dStart; 00100 }; 00101 00102 00107 const double GetCurrentTime() const 00108 { 00109 if (m_bRunning) 00110 return GetCurrentClock() * s_dSecondsPerTick; 00111 00112 return m_dCurrent; 00113 }; 00114 00115 private: 00116 00117 double m_dStart; 00118 double m_dCurrent; 00119 bool m_bRunning; 00120 static double s_dSecondsPerTick; 00121 };