This timing stuff actually works

It doesn't help when it turns out the compiler is broken and implementing std::chrono::high_resolution_clock as something that only counts in ms instead of properly aliasing it to std::chrono::steady_clock!

git-svn-id: https://svn.eduke32.com/eduke32@8002 1a8010ca-5511-0410-912e-c29ae57300e0

# Conflicts:
#	source/build/src/sdlayer.cpp
This commit is contained in:
terminx 2019-08-14 14:26:48 +00:00 committed by Christoph Oelckers
parent 3b504975a2
commit c72a69ff6f
4 changed files with 27 additions and 27 deletions

View file

@ -5,14 +5,17 @@
#include "compat.h" #include "compat.h"
int32_t timerInit(int32_t); // for compatibility
void timerUninit(void); #define timerUninit()
int timerInit(int const tickspersecond);
void timerUpdate(void); void timerUpdate(void);
int32_t timerGetRate(void); int timerGetRate(void);
uint64_t timerGetTicksU64(void); uint64_t timerGetTicksU64(void);
uint64_t timerGetFreqU64(void); uint64_t timerGetFreqU64(void);
double timerGetHiTicks(void); double timerGetHiTicks(void);
uint32_t timerGetTicks(void); uint32_t timerGetTicks(void);
void (*timerSetCallback(void (*callback)(void)))(void); void (*timerSetCallback(void (*callback)(void)))(void);
#endif // timer_h__ #endif // timer_h__

View file

@ -9,41 +9,38 @@
using namespace std; using namespace std;
using namespace chrono; using namespace chrono;
static int32_t timerlastsample; static time_point<steady_clock> timerlastsample;
static int32_t timerticspersec=0; static int timerticspersec;
static void(*usertimercallback)(void) = NULL; static void(*usertimercallback)(void) = NULL;
int32_t timerGetRate(void) { return timerticspersec; } int timerGetRate(void) { return timerticspersec; }
void timerUninit(void) { timerticspersec = 0; } uint32_t timerGetTicks(void) { return duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count(); }
uint64_t timerGetTicksU64(void) { return steady_clock::now().time_since_epoch().count(); }
uint32_t timerGetTicks(void) { return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); } uint64_t timerGetFreqU64(void) { return steady_clock::period::den; }
uint64_t timerGetTicksU64(void) { return high_resolution_clock::now().time_since_epoch().count(); }
uint64_t timerGetFreqU64(void) { return high_resolution_clock::period::den; }
// Returns the time since an unspecified starting time in milliseconds. // Returns the time since an unspecified starting time in milliseconds.
// (May be not monotonic for certain configurations.) // (May be not monotonic for certain configurations.)
ATTRIBUTE((flatten)) double timerGetHiTicks(void) { return duration<double, nano>(steady_clock::now().time_since_epoch()).count() / 1000000.0; }
double timerGetHiTicks(void) { return duration<double, milli>(high_resolution_clock::now().time_since_epoch()).count(); }
int32_t timerInit(int32_t const tickspersecond) int timerInit(int const tickspersecond)
{ {
timerticspersec = tickspersecond; timerticspersec = tickspersecond;
timerlastsample = timerGetTicksU64() * timerticspersec / timerGetFreqU64(); timerlastsample = steady_clock::now();
usertimercallback = NULL; usertimercallback = NULL;
return 0; return 0;
} }
void timerUpdate(void) ATTRIBUTE((flatten)) void timerUpdate(void)
{ {
if (!timerticspersec) return; auto time = steady_clock::now();
int n = (time - timerlastsample).count() * (double)timerticspersec / timerGetFreqU64();
uint64_t n = (timerGetTicksU64() * timerticspersec / timerGetFreqU64()) - timerlastsample;
if (n <= 0) return; if (n <= 0) return;
totalclock += n; totalclock += n;
timerlastsample += n; timerlastsample = time;
if (usertimercallback) if (usertimercallback)
for (; n > 0; n--) usertimercallback(); for (; n > 0; n--) usertimercallback();

View file

@ -6339,15 +6339,15 @@ int G_FPSLimit(void)
return 1; return 1;
static double nextPageDelay; static double nextPageDelay;
static uint64_t lastFrameTicks; static double lastFrameTicks;
uint64_t const frameTicks = timerGetTicksU64(); double const frameTicks = timerGetTicksU64();
int64_t const elapsedTime = frameTicks - lastFrameTicks; double const elapsedTime = frameTicks-lastFrameTicks;
if (elapsedTime >= lrint(floor(nextPageDelay))) if (elapsedTime >= nextPageDelay)
{ {
if (elapsedTime <= lrint(floor(nextPageDelay + g_frameDelay))) if (elapsedTime <= nextPageDelay+g_frameDelay)
nextPageDelay = nearbyint(nextPageDelay + g_frameDelay - (double)elapsedTime); nextPageDelay += g_frameDelay-elapsedTime;
lastFrameTicks = frameTicks; lastFrameTicks = frameTicks;

View file

@ -350,7 +350,7 @@ extern palette_t CrosshairColors;
extern palette_t DefaultCrosshairColors; extern palette_t DefaultCrosshairColors;
extern double g_frameDelay; extern double g_frameDelay;
static inline double calcFrameDelay(int const maxFPS) { return maxFPS > 0 ? nearbyint((double)timerGetFreqU64()/maxFPS) : 0.0; } static inline double calcFrameDelay(int const maxFPS) { return maxFPS > 0 ? (timerGetFreqU64()/(double)maxFPS) : 0.0; }
int32_t A_CheckInventorySprite(spritetype *s); int32_t A_CheckInventorySprite(spritetype *s);
int32_t A_InsertSprite(int16_t whatsect, int32_t s_x, int32_t s_y, int32_t s_z, int16_t s_pn, int8_t s_s, uint8_t s_xr, int32_t A_InsertSprite(int16_t whatsect, int32_t s_x, int32_t s_y, int32_t s_z, int16_t s_pn, int8_t s_s, uint8_t s_xr,