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