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