* Improve timer accuracy, thanks to @Kleadron on GitHub

This commit is contained in:
iProgramInCpp
2023-07-31 22:42:16 +03:00
parent 65406ef97e
commit f6b1856ea9
2 changed files with 47 additions and 0 deletions

View File

@@ -9,9 +9,42 @@
#include "Timer.hpp"
#include "Utils.hpp"
#if !defined(_WIN32) && defined(USE_ACCURATE_TIMER)
#error "Implement getAccurateTimeMs() for your platform!"
#endif
#if defined(_WIN32) && defined(USE_ACCURATE_TIMER)
static LARGE_INTEGER s_StartTime;
static bool s_Initted;
double getAccurateTimeMs()
{
// Thanks to @Kleadron for helping out with this!
if (!s_Initted)
{
s_Initted = true;
QueryPerformanceCounter(&s_StartTime);
}
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
LARGE_INTEGER currentCounter;
QueryPerformanceCounter(&currentCounter);
LONGLONG diff = currentCounter.QuadPart - s_StartTime.QuadPart;
return double(diff) / double(frequency.QuadPart) * 1000.0;
}
#endif
void Timer::advanceTime()
{
#ifdef USE_ACCURATE_TIMER
double timeMs = getAccurateTimeMs();
#else
int timeMs = getTimeMs();
#endif
if (timeMs - field_4 <= 1000)
{
if (timeMs - field_4 < 0)
@@ -21,8 +54,13 @@ void Timer::advanceTime()
}
else
{
#ifdef USE_ACCURATE_TIMER
double diff1 = timeMs - field_4;
double diff2 = timeMs - field_8;
#else
int diff1 = timeMs - field_4;
int diff2 = timeMs - field_8;
#endif
field_C += ((float(diff1) / float(diff2)) - field_C) * 0.2f;
}

View File

@@ -8,6 +8,10 @@
#pragma once
#if defined(_WIN32)
#define USE_ACCURATE_TIMER
#endif
class Timer
{
public:
@@ -17,8 +21,13 @@ public:
public:
float field_0 = 0;
#ifndef USE_ACCURATE_TIMER
int field_4 = 0;
int field_8 = 0;
#else
double field_4 = 0;
double field_8 = 0;
#endif
float field_C = 1.0f;
float field_10 = 20.0f;
int field_14 = 0;