@@ -26,40 +26,48 @@ extern XRCORE_API pauseMngr& g_pauseMngr();
2626
2727class XRCORE_API CTimerBase
2828{
29+ public:
30+ using Clock = std::chrono::high_resolution_clock;
31+ using Time = std::chrono::time_point<Clock>;
32+ using Duration = Time::duration;
33+
2934protected:
30- u64 startTime;
31- u64 pauseDuration;
32- u64 pauseAccum;
35+ Time startTime;
36+ Duration pauseDuration;
37+ Duration pauseAccum;
3338 bool paused;
3439
3540public:
36- constexpr CTimerBase () noexcept : startTime(0 ), pauseDuration(0 ), pauseAccum(0 ), paused(false ) {}
41+ constexpr CTimerBase () noexcept : startTime(), pauseDuration(), pauseAccum(), paused(false ) {}
3742
3843 ICF void Start ()
3944 {
4045 if (paused)
4146 return ;
42- startTime = CPU::QPC () - pauseAccum;
47+ startTime = Now () - pauseAccum;
4348 }
44- ICF u64 GetElapsed_ticks () const
49+
50+ virtual Duration getElapsedTime () const
4551 {
4652 if (paused)
4753 return pauseDuration;
48- else
49- return CPU::QPC () - startTime - CPU::qpc_overhead - pauseAccum;
54+ return Now () - startTime - pauseAccum;
55+ }
56+
57+ u64 GetElapsed_ms () const
58+ {
59+ using namespace std ::chrono;
60+ return duration_cast<milliseconds>(getElapsedTime ()).count ();
5061 }
51- IC u32 GetElapsed_ms () const { return u32 ( GetElapsed_ticks () * u64 ( 1000 ) / CPU::qpc_freq); }
62+
5263 IC float GetElapsed_sec () const
5364 {
54- #ifndef _EDITOR
55- FPU::m64r ();
56- #endif
57- float _result = float (double (GetElapsed_ticks ()) / double (CPU::qpc_freq));
58- #ifndef _EDITOR
59- FPU::m24r ();
60- #endif
61- return _result;
65+ using namespace std ::chrono;
66+ return duration_cast<duration<float >>(getElapsedTime ()).count ();
6267 }
68+
69+ Time Now () const { return Clock::now (); }
70+
6371 IC void Dump () const { Msg (" * Elapsed time (sec): %f" , GetElapsed_sec ()); }
6472};
6573
@@ -68,74 +76,49 @@ class XRCORE_API CTimer : public CTimerBase
6876 using inherited = CTimerBase;
6977
7078 float m_time_factor;
71- u64 m_real_ticks ;
72- u64 m_ticks ;
79+ Duration realTime ;
80+ Duration time ;
7381
74- IC u64 GetElapsed_ticks (const u64 & current_ticks ) const
82+ inline Duration getElapsedTime (const Duration& current ) const
7583 {
76- u64 delta = current_ticks - m_real_ticks;
77- double delta_d = (double )delta;
78- double time_factor_d = time_factor ();
79- double time = delta_d * time_factor_d + .5 ;
80- u64 result = (u64 )time;
81- return (m_ticks + result);
84+ const auto delta = current - realTime;
85+ const double deltaD = double (delta.count ());
86+ const double time = deltaD * m_time_factor + .5 ;
87+ const auto result = u64 (time);
88+ return Duration (this ->time .count () + result);
8289 }
8390
8491public:
85- constexpr CTimer () noexcept : m_time_factor(1 .f), m_real_ticks(0 ), m_ticks(0 ) {}
86- ICF void Start () noexcept
92+ constexpr CTimer () noexcept : m_time_factor(1 .f), realTime(0 ), time(0 ) {}
93+
94+ void Start () noexcept
8795 {
8896 if (paused)
8997 return ;
9098
99+ realTime = std::chrono::nanoseconds (0 );
100+ time = std::chrono::nanoseconds (0 );
91101 inherited::Start ();
92- m_real_ticks = 0 ;
93- m_ticks = 0 ;
94102 }
95103
96104 float time_factor () const noexcept { return m_time_factor; }
97105 void time_factor (const float time_factor) noexcept
98106 {
99- u64 current = inherited::GetElapsed_ticks ();
100- m_ticks = GetElapsed_ticks (current);
101- m_real_ticks = current;
107+ const Duration current = inherited::getElapsedTime ();
108+ time = getElapsedTime (current);
109+ realTime = current;
102110 m_time_factor = time_factor;
103111 }
104112
105- u64 GetElapsed_ticks () const
106- {
107- #ifndef _EDITOR
108- FPU::m64r ();
109- #endif // _EDITOR
110-
111- u64 result = GetElapsed_ticks (inherited::GetElapsed_ticks ());
112-
113- #ifndef _EDITOR
114- FPU::m24r ();
115- #endif // _EDITOR
116-
117- return (result);
118- }
119-
120- IC u32 GetElapsed_ms () const { return (u32 (GetElapsed_ticks () * u64 (1000 ) / CPU::qpc_freq)); }
121- IC float GetElapsed_sec () const
113+ virtual Duration getElapsedTime () const
122114 {
123- #ifndef _EDITOR
124- FPU::m64r ();
125- #endif
126- float result = float (double (GetElapsed_ticks ()) / double (CPU::qpc_freq));
127- #ifndef _EDITOR
128- FPU::m24r ();
129- #endif
130- return (result);
115+ return getElapsedTime (inherited::getElapsedTime ());
131116 }
132-
133- void Dump () const { Msg (" * Elapsed time (sec): %f" , GetElapsed_sec ()); }
134117};
135118
136119class XRCORE_API CTimer_paused_ex : public CTimer
137120{
138- u64 save_clock;
121+ Time save_clock;
139122
140123public:
141124 CTimer_paused_ex () noexcept : save_clock() {}
@@ -146,15 +129,15 @@ class XRCORE_API CTimer_paused_ex : public CTimer
146129 if (paused == b)
147130 return ;
148131
149- u64 _current = CPU::QPC () - CPU::qpc_overhead ;
132+ const auto current = Now () ;
150133 if (b)
151134 {
152- save_clock = _current ;
153- pauseDuration = CTimerBase::GetElapsed_ticks ();
135+ save_clock = current ;
136+ pauseDuration = CTimerBase::getElapsedTime ();
154137 }
155138 else
156139 {
157- pauseAccum += _current - save_clock;
140+ pauseAccum += current - save_clock;
158141 }
159142 paused = b;
160143 }
@@ -170,13 +153,15 @@ class XRCORE_API CTimer_paused : public CTimer_paused_ex
170153extern XRCORE_API bool g_bEnableStatGather;
171154class XRCORE_API CStatTimer
172155{
156+ using Duration = CTimerBase::Duration;
157+
173158public:
174159 CTimer T;
175- u64 accum;
160+ Duration accum;
176161 float result;
177162 u32 count;
178163
179- CStatTimer ();
164+ CStatTimer () : T(), accum(), result(. 0f ), count( 0 ) {}
180165 void FrameStart ();
181166 void FrameEnd ();
182167
@@ -192,20 +177,19 @@ class XRCORE_API CStatTimer
192177 {
193178 if (!g_bEnableStatGather)
194179 return ;
195- accum += T.GetElapsed_ticks ();
180+ accum += T.getElapsedTime ();
196181 }
197182
198- ICF u64 GetElapsed_ticks () const { return accum; }
199- IC u32 GetElapsed_ms () const { return u32 (GetElapsed_ticks () * u64 (1000 ) / CPU::qpc_freq); }
200- IC float GetElapsed_sec () const
183+ Duration getElapsedTime () const { return accum; }
184+ u64 GetElapsed_ms () const
185+ {
186+ using namespace std ::chrono;
187+ return duration_cast<milliseconds>(getElapsedTime ()).count ();
188+ }
189+
190+ float GetElapsed_sec () const
201191 {
202- #ifndef _EDITOR
203- FPU::m64r ();
204- #endif
205- float _result = float (double (GetElapsed_ticks ()) / double (CPU::qpc_freq));
206- #ifndef _EDITOR
207- FPU::m24r ();
208- #endif
209- return _result;
192+ using namespace std ::chrono;
193+ return duration_cast<duration<float >>(getElapsedTime ()).count ();
210194 }
211195};
0 commit comments