From 82a6edce5da2473f3a7561460ebbd9f13bd4127f Mon Sep 17 00:00:00 2001 From: NepDisk Date: Thu, 9 Oct 2025 02:34:02 -0400 Subject: [PATCH] Port old inaccurate fps sample as toggle For Jon-sama :face_holding_back_tears: --- src/d_main.cpp | 1 + src/screen.c | 46 ++++++++++++++++++++++++++++++++++++++++++---- src/screen.h | 1 + 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/d_main.cpp b/src/d_main.cpp index 10bf744d9..b0c84ce87 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -1627,6 +1627,7 @@ void D_SRB2Main(void) if (!dedicated) { CV_RegisterVar(&cv_ticrate); + CV_RegisterVar(&cv_accuratefps); CV_RegisterVar(&cv_constextsize); } diff --git a/src/screen.c b/src/screen.c index c1c22e720..4fb4965e2 100644 --- a/src/screen.c +++ b/src/screen.c @@ -87,6 +87,9 @@ static void SCR_ChangeFullscreen(void); static CV_PossibleValue_t fullscreen_cons_t[] = {{0, "No"}, {1, "Yes"}, {2, "Borderless Window"}, {0, NULL}}; consvar_t cv_fullscreen = CVAR_INIT ("fullscreen", "Yes", CV_SAVE|CV_CALL, fullscreen_cons_t, SCR_ChangeFullscreen); +static CV_PossibleValue_t accuratefps_cons_t[] = {{0, "Inaccurate"}, {1, "Accurate"}, {0, NULL}}; +consvar_t cv_accuratefps = CVAR_INIT ("fpssampling", "Accurate", CV_SAVE, accuratefps_cons_t, NULL); + // ========================================================================= // SCREEN VARIABLES // ========================================================================= @@ -526,6 +529,8 @@ double averageFPS = 0.0f; static double total_frame_time = 0.0; static int frame_index; + +static double fps_samples[NUM_FPS_SAMPLES]; #endif static boolean fps_init = false; @@ -549,11 +554,44 @@ void SCR_CalculateFPS(void) #ifdef USE_FPS_SAMPLES total_frame_time += frameElapsed; - if (frame_index++ >= NUM_FPS_SAMPLES || total_frame_time >= MAX_FRAME_TIME) + + if (cv_accuratefps.value) { - averageFPS = 1.0 / (total_frame_time / frame_index); - total_frame_time = 0.0; - frame_index = 0; + if (frame_index++ >= NUM_FPS_SAMPLES || total_frame_time >= MAX_FRAME_TIME) + { + averageFPS = 1.0 / (total_frame_time / frame_index); + total_frame_time = 0.0; + frame_index = 0; + } + } + else + { + if (total_frame_time >= MAX_FRAME_TIME) + { + static int sampleIndex = 0; + + fps_samples[sampleIndex] = frameElapsed; + + sampleIndex++; + if (sampleIndex >= NUM_FPS_SAMPLES) + sampleIndex = 0; + + averageFPS = 0.0; + for (int 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. diff --git a/src/screen.h b/src/screen.h index 35519f8af..70b19d063 100644 --- a/src/screen.h +++ b/src/screen.h @@ -135,6 +135,7 @@ extern UINT8 *scr_borderpatch; // patch used to fill the view borders extern consvar_t cv_scr_width, cv_scr_height, cv_scr_depth, cv_renderview, cv_renderer, cv_renderhitbox, cv_fullscreen; extern consvar_t cv_vhseffect, cv_shittyscreen, cv_votebgscaling; extern consvar_t cv_parallelsoftware; +extern consvar_t cv_accuratefps; // wait for page flipping to end or not extern consvar_t cv_vidwait;