From dfb6006f9c3f23cddd133a2bc33bacaa580f4020 Mon Sep 17 00:00:00 2001 From: Alug Date: Wed, 2 Oct 2024 12:14:31 +0200 Subject: [PATCH] fix up old fps counter --- src/screen.c | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/screen.c b/src/screen.c index 8fe00e821..b7777fc32 100644 --- a/src/screen.c +++ b/src/screen.c @@ -366,15 +366,13 @@ double averageFPS = 0.0f; #define USE_FPS_SAMPLES #ifdef USE_FPS_SAMPLES -#define MAX_FRAME_TIME 0.05 +#define MAX_FRAME_TIME (0.05) #define NUM_FPS_SAMPLES (16) // Number of samples to store -#define NUM_FPS_SAMPLES2 (32) // Number of samples to store for inaccurate counter static double total_frame_time = 0.0; static int frame_index; -static double fps_samples[NUM_FPS_SAMPLES2]; -static int fps_index; +static double fps_samples[NUM_FPS_SAMPLES]; #endif static boolean fps_init = false; @@ -383,7 +381,6 @@ static precise_t fps_enter = 0; void SCR_CalculateFPS(void) { precise_t fps_finish = 0; - int i; double frameElapsed = 0.0; @@ -398,7 +395,6 @@ void SCR_CalculateFPS(void) fps_enter = fps_finish; #ifdef USE_FPS_SAMPLES - if (cv_accuratefps.value) { total_frame_time += frameElapsed; @@ -411,13 +407,35 @@ void SCR_CalculateFPS(void) } else { - fps_samples[fps_index] = frameElapsed / NUM_FPS_SAMPLES2; - fps_index = (fps_index + 1) % NUM_FPS_SAMPLES2; + total_frame_time += frameElapsed; - averageFPS = 0.0; - for (i = 0; i < NUM_FPS_SAMPLES2; i++) - averageFPS += fps_samples[i]; - averageFPS = 1.0 / averageFPS; + if (total_frame_time >= MAX_FRAME_TIME) + { + static int sampleIndex = 0; + int i; + + fps_samples[sampleIndex] = frameElapsed; + + sampleIndex++; + if (sampleIndex >= NUM_FPS_SAMPLES) + sampleIndex = 0; + + averageFPS = 0.0; + for (i = 0; i < NUM_FPS_SAMPLES; i++) + { + averageFPS += fps_samples[i]; + } + + if (averageFPS > 0.0) + { + averageFPS = 1.0 / (averageFPS / NUM_FPS_SAMPLES); + } + } + + while (total_frame_time >= MAX_FRAME_TIME) + { + total_frame_time -= MAX_FRAME_TIME; + } } #else // Direct, unsampled counter.