diff --git a/src/screen.c b/src/screen.c index 0c4a7987..fd97e2ca 100644 --- a/src/screen.c +++ b/src/screen.c @@ -402,55 +402,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) @@ -459,10 +473,10 @@ void SCR_DisplayTicRate(void) UINT32 cap = R_GetFramerateCap(); UINT32 benchmark = (cap == 0) ? I_GetRefreshRate() : cap; INT32 x = 318; - double fps = ceil(averageFPS); + double fps = round(averageFPS); // draw "FPS" - V_DrawFixedPatch(306< (benchmark - 5)) ticcntcolor = R_GetTranslationColormap(TC_RAINBOW, SKINCOLOR_MINT, GTC_CACHE); @@ -481,15 +495,15 @@ void SCR_DisplayTicRate(void) } // draw total frame: - V_DrawPingNum(x, 190, V_SNAPTOBOTTOM|V_SNAPTORIGHT|V_HUDTRANS, cap, ticcntcolor); + V_DrawPingNum(x, 190, V_SNAPTOBOTTOM|V_SNAPTORIGHT, cap, ticcntcolor); x -= digits * 4; // draw "/" - V_DrawFixedPatch(x<