|
1 |
| -#ifndef FTimerH |
2 |
| -#define FTimerH |
3 | 1 | #pragma once
|
4 | 2 |
|
5 | 3 | class CTimer_paused;
|
6 | 4 |
|
7 | 5 | class XRCORE_API pauseMngr {
|
8 | 6 | xr_vector<CTimer_paused*> m_timers;
|
9 |
| - BOOL m_paused; |
10 |
| - |
| 7 | + bool paused; |
11 | 8 | public:
|
12 | 9 | pauseMngr();
|
13 |
| - BOOL Paused() { return m_paused; }; |
14 |
| - void Pause(BOOL b); |
15 |
| - void Register(CTimer_paused* t); |
16 |
| - void UnRegister(CTimer_paused* t); |
| 10 | + bool Paused() const { return paused; } |
| 11 | + void Pause(const bool b); |
| 12 | + void Register(CTimer_paused& t); |
| 13 | + void UnRegister(CTimer_paused& t); |
17 | 14 | };
|
18 | 15 |
|
19 | 16 | extern XRCORE_API pauseMngr g_pauseMngr;
|
20 | 17 |
|
21 | 18 | class XRCORE_API CTimerBase {
|
| 19 | +public: |
| 20 | + using Clock = std::chrono::high_resolution_clock; |
| 21 | + using Time = std::chrono::time_point<Clock>; |
| 22 | + using Duration = Time::duration; |
22 | 23 | protected:
|
23 |
| - u64 qwStartTime; |
24 |
| - u64 qwPausedTime; |
25 |
| - u64 qwPauseAccum; |
26 |
| - BOOL bPause; |
| 24 | + Time startTime; |
| 25 | + Duration pauseDuration; |
| 26 | + Duration pauseAccum; |
| 27 | + bool paused; |
27 | 28 |
|
28 | 29 | public:
|
29 |
| - CTimerBase() : qwStartTime(0), qwPausedTime(0), qwPauseAccum(0), bPause(FALSE) {} |
30 |
| - ICF void Start() { |
31 |
| - if (bPause) |
| 30 | + CTimerBase() : startTime(), pauseDuration(), pauseAccum(), paused(false) {} |
| 31 | + void Start() { |
| 32 | + if (paused) |
32 | 33 | return;
|
33 |
| - qwStartTime = CPU::QPC() - qwPauseAccum; |
| 34 | + startTime = Clock::now() - pauseAccum; |
34 | 35 | }
|
35 |
| - ICF u64 GetElapsed_ticks() const { |
36 |
| - if (bPause) |
37 |
| - return qwPausedTime; |
| 36 | + |
| 37 | + Duration getElapsedTime() const { |
| 38 | + if (paused) |
| 39 | + return pauseDuration; |
38 | 40 | else
|
39 |
| - return CPU::QPC() - qwStartTime - CPU::qpc_overhead - qwPauseAccum; |
| 41 | + return Clock::now() - startTime - pauseAccum; |
40 | 42 | }
|
41 |
| - IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); } |
42 |
| - IC float GetElapsed_sec() const { |
43 |
| - FPU::m64r(); |
44 |
| - float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); |
45 |
| - FPU::m24r(); |
46 |
| - return _result; |
| 43 | + |
| 44 | + u64 GetElapsed_ms() const { |
| 45 | + using namespace std::chrono; |
| 46 | + return duration_cast<milliseconds>(getElapsedTime()).count(); |
| 47 | + } |
| 48 | + |
| 49 | + float GetElapsed_sec() const { |
| 50 | + using namespace std::chrono; |
| 51 | + const auto nanos = duration_cast<nanoseconds>(getElapsedTime()).count(); |
| 52 | + return float(nanos) / 1000000000.0; |
47 | 53 | }
|
48 |
| - IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } |
| 54 | + |
| 55 | + void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } |
49 | 56 | };
|
50 | 57 |
|
51 | 58 | class XRCORE_API CTimer : public CTimerBase {
|
52 |
| - typedef CTimerBase inherited; |
53 |
| - |
54 | 59 | float m_time_factor;
|
55 |
| - u64 m_real_ticks; |
56 |
| - u64 m_ticks; |
57 |
| - |
58 |
| - IC u64 GetElapsed_ticks(const u64& current_ticks) const { |
59 |
| - u64 delta = current_ticks - m_real_ticks; |
60 |
| - double delta_d = (double)delta; |
61 |
| - double time_factor_d = time_factor(); |
62 |
| - double time = delta_d * time_factor_d + .5; |
63 |
| - u64 result = (u64)time; |
64 |
| - return (m_ticks + result); |
| 60 | + Duration realTime; |
| 61 | + Duration time; |
| 62 | + |
| 63 | + Duration getElapsedTime(const Duration current) const { |
| 64 | + const auto delta = current - realTime; |
| 65 | + const auto deltaD = double(delta.count()); |
| 66 | + const auto time_factor_d = double(time_factor()); |
| 67 | + const double time = deltaD * time_factor_d + .5; |
| 68 | + const auto result = u64(time); |
| 69 | + return Duration(result); |
65 | 70 | }
|
66 | 71 |
|
67 | 72 | public:
|
68 |
| - IC CTimer() : m_time_factor(1.f), m_real_ticks(0), m_ticks(0) {} |
| 73 | + CTimer() : m_time_factor(1.f), realTime(), time() {} |
69 | 74 |
|
70 |
| - ICF void Start() { |
71 |
| - if (bPause) |
| 75 | + void Start() { |
| 76 | + if (paused) |
72 | 77 | return;
|
73 | 78 |
|
74 |
| - inherited::Start(); |
75 |
| - |
76 |
| - m_real_ticks = 0; |
77 |
| - m_ticks = 0; |
| 79 | + __super::Start(); |
78 | 80 | }
|
79 | 81 |
|
80 |
| - IC const float& time_factor() const { return (m_time_factor); } |
| 82 | + float time_factor() const { return m_time_factor; } |
81 | 83 |
|
82 |
| - IC void time_factor(const float& time_factor) { |
83 |
| - u64 current = inherited::GetElapsed_ticks(); |
84 |
| - m_ticks = GetElapsed_ticks(current); |
85 |
| - m_real_ticks = current; |
| 84 | + void time_factor(const float time_factor) { |
| 85 | + const auto current = __super::getElapsedTime(); |
| 86 | + time = getElapsedTime(current); |
| 87 | + realTime = current; |
86 | 88 | m_time_factor = time_factor;
|
87 | 89 | }
|
88 | 90 |
|
89 |
| - IC u64 GetElapsed_ticks() const { |
90 |
| - FPU::m64r(); |
91 |
| - u64 result = GetElapsed_ticks(inherited::GetElapsed_ticks()); |
92 |
| - FPU::m24r(); |
93 |
| - |
94 |
| - return (result); |
95 |
| - } |
96 |
| - |
97 |
| - IC u32 GetElapsed_ms() const { return (u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq)); } |
98 |
| - |
99 |
| - IC float GetElapsed_sec() const { |
100 |
| - FPU::m64r(); |
101 |
| - float result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); |
102 |
| - FPU::m24r(); |
103 |
| - return (result); |
| 91 | + Duration getElapsedTime() const { |
| 92 | + return __super::getElapsedTime(); |
104 | 93 | }
|
105 |
| - |
106 |
| - IC void Dump() const { Msg("* Elapsed time (sec): %f", GetElapsed_sec()); } |
107 | 94 | };
|
108 | 95 |
|
109 | 96 | class XRCORE_API CTimer_paused_ex : public CTimer {
|
110 |
| - u64 save_clock; |
111 |
| - |
| 97 | + Time save_clock; |
112 | 98 | public:
|
113 |
| - CTimer_paused_ex() {} |
| 99 | + CTimer_paused_ex() : save_clock() {} |
114 | 100 | virtual ~CTimer_paused_ex() {}
|
115 |
| - IC BOOL Paused() const { return bPause; } |
116 |
| - IC void Pause(BOOL b) { |
117 |
| - if (bPause == b) |
| 101 | + bool Paused() const { return paused; } |
| 102 | + |
| 103 | + void Pause(const bool b) { |
| 104 | + if (paused == b) |
118 | 105 | return;
|
119 | 106 |
|
120 |
| - u64 _current = CPU::QPC() - CPU::qpc_overhead; |
| 107 | + const auto current = Clock::now(); |
121 | 108 | if (b) {
|
122 |
| - save_clock = _current; |
123 |
| - qwPausedTime = CTimerBase::GetElapsed_ticks(); |
| 109 | + save_clock = current; |
| 110 | + pauseDuration = CTimerBase::getElapsedTime(); |
124 | 111 | } else {
|
125 |
| - qwPauseAccum += _current - save_clock; |
| 112 | + pauseAccum += current - save_clock; |
126 | 113 | }
|
127 |
| - bPause = b; |
| 114 | + paused = b; |
128 | 115 | }
|
129 | 116 | };
|
130 | 117 |
|
131 | 118 | class XRCORE_API CTimer_paused : public CTimer_paused_ex {
|
132 | 119 | public:
|
133 |
| - CTimer_paused() { g_pauseMngr.Register(this); } |
134 |
| - virtual ~CTimer_paused() { g_pauseMngr.UnRegister(this); } |
| 120 | + CTimer_paused() { g_pauseMngr.Register(*this); } |
| 121 | + virtual ~CTimer_paused() { g_pauseMngr.UnRegister(*this); } |
135 | 122 | };
|
136 | 123 |
|
137 | 124 | extern XRCORE_API BOOL g_bEnableStatGather;
|
| 125 | + |
138 | 126 | class XRCORE_API CStatTimer {
|
139 | 127 | public:
|
| 128 | + using Duration = CTimerBase::Duration; |
| 129 | + |
140 | 130 | CTimer T;
|
141 |
| - u64 accum; |
| 131 | + Duration accum; |
142 | 132 | float result;
|
143 | 133 | u32 count;
|
144 | 134 |
|
145 |
| -public: |
146 |
| - CStatTimer(); |
| 135 | + CStatTimer() : T(), accum(), result(.0f), count(0) {} |
147 | 136 | void FrameStart();
|
148 | 137 | void FrameEnd();
|
149 | 138 |
|
150 |
| - ICF void Begin() { |
| 139 | + void Begin() { |
151 | 140 | if (!g_bEnableStatGather)
|
152 | 141 | return;
|
153 | 142 | count++;
|
154 | 143 | T.Start();
|
155 | 144 | }
|
156 |
| - ICF void End() { |
| 145 | + |
| 146 | + void End() { |
157 | 147 | if (!g_bEnableStatGather)
|
158 | 148 | return;
|
159 |
| - accum += T.GetElapsed_ticks(); |
| 149 | + accum += T.getElapsedTime(); |
| 150 | + } |
| 151 | + |
| 152 | + Duration getElapsedTime() const { |
| 153 | + return accum; |
160 | 154 | }
|
161 | 155 |
|
162 |
| - ICF u64 GetElapsed_ticks() const { return accum; } |
| 156 | + u64 GetElapsed_ms() const { |
| 157 | + using namespace std::chrono; |
| 158 | + return duration_cast<milliseconds>(getElapsedTime()).count(); |
| 159 | + } |
163 | 160 |
|
164 |
| - IC u32 GetElapsed_ms() const { return u32(GetElapsed_ticks() * u64(1000) / CPU::qpc_freq); } |
165 |
| - IC float GetElapsed_sec() const { |
166 |
| - FPU::m64r(); |
167 |
| - float _result = float(double(GetElapsed_ticks()) / double(CPU::qpc_freq)); |
168 |
| - FPU::m24r(); |
169 |
| - return _result; |
| 161 | + float GetElapsed_sec() const { |
| 162 | + using namespace std::chrono; |
| 163 | + const auto nanos = duration_cast<nanoseconds>(getElapsedTime()).count(); |
| 164 | + return float(nanos) / 1000000000.0; |
170 | 165 | }
|
171 | 166 | };
|
172 |
| - |
173 |
| -#endif // FTimerH |
0 commit comments