-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameTimer.cpp
More file actions
executable file
·92 lines (75 loc) · 1.58 KB
/
GameTimer.cpp
File metadata and controls
executable file
·92 lines (75 loc) · 1.58 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
92
#include <Windows.h>
#include "GameTimer.h"
GameTimer::GameTimer():mSecondPerCount(0.0), mDeltaTime(-1.0), mBaseTime(0),
mPausedTime(0), mPrevTime(0), mCurrTime(0), mStopped(false)
{
__int64 countsPerSec;
QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
mSecondPerCount = 1.0f/(double)countsPerSec;
}
float GameTimer::TotalTime()const
{
if(mStopped)
{
return(float)(((mStopTime - mPausedTime)-mBaseTime)*mSecondPerCount);
}else
{
float time = (float)(((mCurrTime-mPausedTime)-mBaseTime)*mSecondPerCount);
return time;
}
}
float GameTimer::DeltaTime()const
{
return (float)mDeltaTime;
}
void GameTimer::Reset()
{
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
mBaseTime = currTime;
mPrevTime = currTime;
mStopTime = 0;
mStopped = false;
}
void GameTimer::Start()
{
__int64 startTime;
QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
if(mStopped)
{
mPausedTime += (startTime - mStopTime);
mPrevTime = startTime;
mStopTime = 0;
mStopped = false;
}
}
void GameTimer::Stop()
{
if(!mStopped)
{
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
mStopTime = currTime;
mStopped = true;
}
}
void GameTimer::Tick()
{
if(mStopped)
{
mDeltaTime = 0.0;
return;
}
__int64 currTime;
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
mCurrTime = currTime;
mDeltaTime = (mCurrTime - mPrevTime)*mSecondPerCount;
mPrevTime = mCurrTime;
if(mDeltaTime < 0.0)
{
mDeltaTime = 0.0;
}
}
GameTimer::~GameTimer()
{
}