From 79f736b75fa292c5130ef0a4484b8002ce452465 Mon Sep 17 00:00:00 2001 From: Sally Coolatta Date: Thu, 19 May 2022 16:37:33 -0400 Subject: [PATCH] Use double instead of precise_t for FPS counter Makes the displayed FPS more accurate to what it's actually displaying. Also removed HUDTRANS from it -- stop it fading out during level transitions, it's annoying when I actually want to see the FPS during those bits. --- src/screen.c | 58 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/src/screen.c b/src/screen.c index 9cac11263..ef1e1d323 100644 --- a/src/screen.c +++ b/src/screen.c @@ -454,55 +454,69 @@ boolean SCR_IsAspectCorrect(INT32 width, INT32 height) double averageFPS = 0.0f; -#define FPS_SAMPLE_RATE (50000) // How often to update FPS samples, in microseconds -#define NUM_FPS_SAMPLES 16 // Number of samples to store +#define USE_FPS_SAMPLES + +#ifdef USE_FPS_SAMPLES +#define FPS_SAMPLE_RATE (0.05) // How often to update FPS samples, in seconds +#define NUM_FPS_SAMPLES (16) // Number of samples to store static double fps_samples[NUM_FPS_SAMPLES]; +static double updateElapsed = 0.0; +#endif + +static boolean fps_init = false; +static precise_t fps_enter = 0; void SCR_CalculateFPS(void) { - static boolean init = false; + precise_t fps_finish = 0; - static precise_t startTime = 0; - precise_t endTime = 0; + double frameElapsed = 0.0; - static precise_t updateTime = 0; - int updateElapsed = 0; - int i; - - endTime = I_GetPreciseTime(); - - if (init == false) + if (fps_init == false) { - startTime = updateTime = endTime; - init = true; - return; + fps_enter = I_GetPreciseTime(); + fps_init = true; } - updateElapsed = (endTime - updateTime) / (I_GetPrecisePrecision() / 1000000); + fps_finish = I_GetPreciseTime(); + frameElapsed = (double)((INT64)(fps_finish - fps_enter)) / I_GetPrecisePrecision(); + fps_enter = fps_finish; + +#ifdef USE_FPS_SAMPLES + updateElapsed += frameElapsed; if (updateElapsed >= FPS_SAMPLE_RATE) { static int sampleIndex = 0; - int frameElapsed = (endTime - startTime) / (I_GetPrecisePrecision() / 1000000); + int i; - fps_samples[sampleIndex] = frameElapsed / 1000.0f; + fps_samples[sampleIndex] = frameElapsed; sampleIndex++; if (sampleIndex >= NUM_FPS_SAMPLES) sampleIndex = 0; - averageFPS = 0.0f; + averageFPS = 0.0; for (i = 0; i < NUM_FPS_SAMPLES; i++) { averageFPS += fps_samples[i]; } - averageFPS = 1000.0f / (averageFPS / NUM_FPS_SAMPLES); - updateTime = endTime; + if (averageFPS > 0.0) + { + averageFPS = 1.0 / (averageFPS / NUM_FPS_SAMPLES); + } } - startTime = endTime; + while (updateElapsed >= FPS_SAMPLE_RATE) + { + updateElapsed -= FPS_SAMPLE_RATE; + } +#else + // Direct, unsampled counter. + averageFPS = 1.0 / frameElapsed; +#endif } void SCR_DisplayTicRate(void)