-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
91 lines (78 loc) · 1.78 KB
/
Timer.cpp
File metadata and controls
91 lines (78 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "Timer.h"
#include <Windows.h>
Timer &Timer::GetInstance()
{
static Timer instance;
return instance;
}
Timer::Timer() : mDeltaTime(0.0), mCountsPerSecond(0), mCurrentTime(0), mPreviousTime(0), mBaseTime(0), mStopTime(0), mPauseDuration(0), mStopped(false)
{
QueryPerformanceFrequency((LARGE_INTEGER*)&mCountsPerSecond);
}
// initialize timer
void Timer::Reset()
{
QueryPerformanceCounter((LARGE_INTEGER*)&mCurrentTime);
mPreviousTime = mCurrentTime;
mBaseTime = mCurrentTime;
mDeltaTime = 0.0;
mStopped = false;
}
void Timer::Tick()
{
if (mStopped)
mDeltaTime = 0.0;
else
{
QueryPerformanceCounter((LARGE_INTEGER*)&mCurrentTime);
mDeltaTime = (mCurrentTime - mPreviousTime) / (double)mCountsPerSecond;
mPreviousTime = mCurrentTime;
}
//std::vector<TimerEventBase*>::const_iterator cit = mTimerEvents.cbegin();
//while (cit != mTimerEvents.cend())
//{
// if ((*cit)->Check())
// {
// (*cit)->Trigger();
// cit = mTimerEvents.erase(cit); // one-shot event
// }
// else
// cit++;
//}
}
void Timer::Start()
{
long long int startTime;
QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
if (mStopped)
{
mPauseDuration += startTime - mStopTime;
mPreviousTime = startTime;
mStopped = false;
}
}
void Timer::Stop()
{
if (!mStopped)
{
QueryPerformanceCounter((LARGE_INTEGER*)&mStopTime);
mStopped = true;
}
}
// to be called after Tick()
double Timer::GetDeltaTime() const
{
return mDeltaTime;
}
// to be called after Tick()
double Timer::GetTotalTime() const
{
if (mStopped)
return ((mStopTime - mBaseTime) - mPauseDuration) / (double)mCountsPerSecond;
else
return ((mCurrentTime - mBaseTime) - mPauseDuration) / (double)mCountsPerSecond;
}
bool Timer::TimerEventBase::Check()
{
return Timer::GetInstance().GetTotalTime() - mTimeStamp >= mDelay;
}