Use SDL_GetPerformance(Counter|Frequency) in MxStopWatch

This commit is contained in:
Anonymous Maarten 2024-12-25 21:00:40 +01:00
parent d3cdec8b5d
commit 572722ce13

View File

@ -5,6 +5,8 @@
#include <limits.h> // ULONG_MAX #include <limits.h> // ULONG_MAX
#include <math.h> #include <math.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_timer.h>
#include <windows.h> #include <windows.h>
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
@ -29,73 +31,55 @@ class MxStopWatch {
double ElapsedSeconds() const; double ElapsedSeconds() const;
protected: protected:
unsigned long TicksPerSeconds() const; Uint64 TicksPerSeconds() const;
private: private:
LARGE_INTEGER m_startTick; // 0x00 Uint64 m_startTick; // 0x00
// ??? when we provide LARGE_INTEGER arithmetic, use a // ??? when we provide LARGE_INTEGER arithmetic, use a
// LARGE_INTEGER m_elapsedTicks rather than m_elapsedSeconds // LARGE_INTEGER m_elapsedTicks rather than m_elapsedSeconds
double m_elapsedSeconds; // 0x0c double m_elapsedSeconds; // 0x0c
unsigned long m_ticksPerSeconds; // 0x14 Uint64 m_ticksPerSeconds; // 0x14
}; };
// FUNCTION: BETA10 0x100d8ba0 // FUNCTION: BETA10 0x100d8ba0
inline MxStopWatch::MxStopWatch() inline MxStopWatch::MxStopWatch()
{ {
Reset(); Reset();
m_ticksPerSeconds = TicksPerSeconds(); m_ticksPerSeconds = SDL_GetPerformanceCounter();
} }
// FUNCTION: BETA10 0x100d8be0 // FUNCTION: BETA10 0x100d8be0
inline void MxStopWatch::Start() inline void MxStopWatch::Start()
{ {
QueryPerformanceCounter(&m_startTick); m_startTick = SDL_GetPerformanceCounter();
} }
// FUNCTION: BETA10 0x100d8f50 // FUNCTION: BETA10 0x100d8f50
inline void MxStopWatch::Stop() inline void MxStopWatch::Stop()
{ {
LARGE_INTEGER endTick; Uint64 endTick;
BOOL result;
result = QueryPerformanceCounter(&endTick); endTick = SDL_GetPerformanceCounter();
assert(result);
if (endTick.HighPart != m_startTick.HighPart) { m_elapsedSeconds = (double)(endTick - m_startTick) / (double)m_ticksPerSeconds;
// LARGE_INTEGER arithmetic not yet provided
m_elapsedSeconds = HUGE_VAL_IMMEDIATE;
}
else {
m_elapsedSeconds += ((endTick.LowPart - m_startTick.LowPart) / (double) m_ticksPerSeconds);
}
} }
// FUNCTION: BETA10 0x100d8c10 // FUNCTION: BETA10 0x100d8c10
inline void MxStopWatch::Reset() inline void MxStopWatch::Reset()
{ {
m_startTick.LowPart = 0; m_startTick = 0;
m_startTick.HighPart = 0; m_elapsedSeconds = 0.;
m_elapsedSeconds = 0;
} }
// FUNCTION: BETA10 0x100d8c60 // FUNCTION: BETA10 0x100d8c60
inline unsigned long MxStopWatch::TicksPerSeconds() const inline Uint64 MxStopWatch::TicksPerSeconds() const
{ {
LARGE_INTEGER ticksPerSeconds; Uint64 ticksPerSeconds;
BOOL result;
result = QueryPerformanceFrequency(&ticksPerSeconds); ticksPerSeconds = SDL_GetPerformanceFrequency();
assert(result); assert(ticksPerSeconds);
if (ticksPerSeconds.HighPart) { return ticksPerSeconds;
// LARGE_INTEGER arithmetic not yet provided
// timer is too fast (faster than 32bits/s, i.e. faster than 4GHz)
return ULONG_MAX;
}
else {
return ticksPerSeconds.LowPart;
}
} }
// FUNCTION: BETA10 0x100d9020 // FUNCTION: BETA10 0x100d9020