00001 #pragma once 00002 00003 #include "common.h" 00004 #include <windows.h> 00005 #include <time.h> 00006 00008 00012 class Timer 00013 { 00014 public: 00015 00016 Timer() : m_dStart(0.0), m_dCurrent(0.0), m_bRunning(false) 00017 { 00018 if (s_dSecondsPerTick <= 0.0) 00019 { 00020 LARGE_INTEGER frequency; 00021 00022 if (QueryPerformanceFrequency(&frequency)) 00023 s_dSecondsPerTick = 1.0 / double(frequency.QuadPart); 00024 else 00025 s_dSecondsPerTick = 1.0 / double(CLOCKS_PER_SEC); 00026 } 00027 00028 start(); 00029 }; 00030 00031 ~Timer() 00032 { 00033 }; 00034 00035 void start() 00036 { 00037 m_bRunning = true; 00038 m_dStart = m_dCurrent = GetCurrentTime(); 00039 }; 00040 00041 void stop() 00042 { 00043 m_dCurrent = GetCurrentTime(); 00044 m_bRunning = false; 00045 }; 00046 00047 operator double() const 00048 { 00049 return GetCurrentTime() - GetStartTime(); 00050 }; 00051 00052 operator float() const 00053 { 00054 return float(GetCurrentTime() - GetStartTime()); 00055 }; 00056 00057 const bool IsRunning() const 00058 { 00059 return m_bRunning; 00060 }; 00061 00062 private: 00063 00064 const double GetCurrentClock() const 00065 { 00066 LARGE_INTEGER now; 00067 00068 if (QueryPerformanceCounter(&now)) 00069 return double(now.QuadPart); 00070 00071 return double(clock()); 00072 }; 00073 00074 const double GetStartTime() const 00075 { 00076 return m_dStart; 00077 }; 00078 00079 const double GetCurrentTime() const 00080 { 00081 if (m_bRunning) 00082 return GetCurrentClock() * s_dSecondsPerTick; 00083 00084 return m_dCurrent; 00085 }; 00086 00087 private: 00088 00089 double m_dStart; 00090 double m_dCurrent; 00091 bool m_bRunning; 00092 static double s_dSecondsPerTick; 00093 };