-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCpuTimer.cpp
More file actions
120 lines (97 loc) · 2.82 KB
/
Copy pathCpuTimer.cpp
File metadata and controls
120 lines (97 loc) · 2.82 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "WinMin.h"
#include "CpuTimer.h"
CpuTimer::CpuTimer()
{
__int64 countsPerSec{};
QueryPerformanceFrequency((LARGE_INTEGER*)&countsPerSec);
m_SecondsPerCount = 1.0 / (double)countsPerSec;
}
float CpuTimer::TotalTime()const
{
// 如果调用了Stop(),暂停中的这段时间我们不需要计入。此外
// m_StopTime - m_BaseTime可能会包含之前的暂停时间,为
// 此我们可以从m_StopTime减去之前累积的暂停的时间
//
// |<-- 暂停的时间 -->|
// ----*---------------*-----------------*------------*------------*------> time
// m_BaseTime m_StopTime startTime m_StopTime m_CurrTime
if( m_Stopped )
{
return (float)(((m_StopTime - m_PausedTime)-m_BaseTime)*m_SecondsPerCount);
}
// m_CurrTime - m_BaseTime包含暂停时间,但我们不想将它计入。
// 为此我们可以从m_CurrTime减去之前累积的暂停的时间
//
// (m_CurrTime - m_PausedTime) - m_BaseTime
//
// |<-- 暂停的时间 -->|
// ----*---------------*-----------------*------------*------> time
// m_BaseTime m_StopTime startTime m_CurrTime
else
{
return (float)(((m_CurrTime-m_PausedTime)-m_BaseTime)*m_SecondsPerCount);
}
}
float CpuTimer::DeltaTime()const
{
return (float)m_DeltaTime;
}
void CpuTimer::Reset()
{
__int64 currTime{};
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_BaseTime = currTime;
m_PrevTime = currTime;
m_StopTime = 0;
m_PausedTime = 0; // 涉及到多次Reset的话需要将其归0
m_Stopped = false;
}
void CpuTimer::Start()
{
__int64 startTime{};
QueryPerformanceCounter((LARGE_INTEGER*)&startTime);
// 累积暂停开始到暂停结束的这段时间
//
// |<-------d------->|
// ----*---------------*-----------------*------------> time
// m_BaseTime m_StopTime startTime
if( m_Stopped )
{
m_PausedTime += (startTime - m_StopTime);
m_PrevTime = startTime;
m_StopTime = 0;
m_Stopped = false;
}
}
void CpuTimer::Stop()
{
if( !m_Stopped )
{
__int64 currTime{};
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_StopTime = currTime;
m_Stopped = true;
}
}
void CpuTimer::Tick()
{
if( m_Stopped )
{
m_DeltaTime = 0.0;
return;
}
__int64 currTime{};
QueryPerformanceCounter((LARGE_INTEGER*)&currTime);
m_CurrTime = currTime;
// 当前Tick与上一Tick的帧间隔
m_DeltaTime = (m_CurrTime - m_PrevTime)*m_SecondsPerCount;
m_PrevTime = m_CurrTime;
if(m_DeltaTime < 0.0)
{
m_DeltaTime = 0.0;
}
}
bool CpuTimer::IsStopped() const
{
return m_Stopped;
}