00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
#include "stdafx.h"
00019
#define WIN32_LEAN_AND_MEAN
00020
#include <windows.h>
00021
#include "Timer.h"
00022
00023
#ifndef _WIN32
00024
# error This file should only be used in a Microsoft Windows build.
00025
#endif
00026
00027
00028
#pragma warning( disable : 4201 )
00029
#include <mmsystem.h>
00030
#pragma warning( default : 4201 )
00031
#pragma comment( lib, "winmm.lib" )
00032
00033
00034 CTimer::CTimer()
00035 {
00036
if ( QueryPerformanceFrequency(( LARGE_INTEGER* )&m_dwFrequency ) ==
false )
00037 {
00038
00039 m_bUsePerformanceCounter =
false;
00040
00041 m_dwTimerStart = timeGetTime();
00042 }
00043
else
00044 {
00045
00046 m_bUsePerformanceCounter =
true;
00047
00048
00049 QueryPerformanceCounter(( LARGE_INTEGER* )&m_dwTimerStart );
00050
00051
00052 m_fResolution = (
float )( 1.0/(
double )m_dwFrequency )*1000.0f;
00053 }
00054
00055 m_iPauseCount = 0;
00056 m_fPauseTime = 0;
00057 }
00058
00059 CTimer::~CTimer()
00060 {
00061
00062 }
00063
00064 void CTimer::Reset()
00065 {
00066
if ( m_bUsePerformanceCounter ==
true )
00067 QueryPerformanceCounter(( LARGE_INTEGER* )&m_dwTimerStart );
00068
else
00069 m_dwTimerStart = timeGetTime();
00070
00071 m_fPauseTime =
GetTime();
00072 }
00073
00074 double CTimer::GetTime()
00075 {
00076
if ( m_iPauseCount > 0 )
00077
return m_fPauseTime;
00078
00079 __int64 timeElapsed;
00080
00081
if ( m_bUsePerformanceCounter ==
true )
00082 {
00083 QueryPerformanceCounter(( LARGE_INTEGER* )&timeElapsed );
00084 timeElapsed -= m_dwTimerStart;
00085
return timeElapsed*m_fResolution;
00086 }
00087
else
00088 {
00089 timeElapsed = timeGetTime()-m_dwTimerStart;
00090
return (
double )timeElapsed;
00091 }
00092 }
00093
00094 void CTimer::Pause()
00095 {
00096
if ( m_iPauseCount == 0 )
00097 m_fPauseTime =
GetTime();
00098
00099 m_iPauseCount++;
00100 }
00101
00102
00103 double CTimer::GetAbsoluteTime()
00104 {
00105 __int64 timeElapsed;
00106
if ( m_bUsePerformanceCounter ==
true )
00107 QueryPerformanceCounter(( LARGE_INTEGER* )&timeElapsed );
00108
00109
else
00110 timeElapsed = timeGetTime();
00111
00112
return timeElapsed*m_fResolution;
00113 }
00114
00115 void CTimer::Unpause()
00116 {
00117
if(m_iPauseCount > 0 )
00118 m_iPauseCount--;
00119
00120
if ( m_iPauseCount == 0 )
00121 {
00122
if ( m_bUsePerformanceCounter ==
true )
00123 {
00124 __int64 time;
00125 QueryPerformanceCounter(( LARGE_INTEGER* )&time );
00126 m_dwTimerStart = time - ( __int64 )( m_fPauseTime/m_fResolution );
00127 }
00128
else
00129 {
00130 m_dwTimerStart = (
unsigned )( timeGetTime() - m_fPauseTime );
00131 }
00132 }
00133 }
00134