Fix inaccuracies in FPS counter

This commit is contained in:
Gustaf Alhäll 2023-07-11 16:37:45 +02:00
parent 3af1074e17
commit 576262f6c5
No known key found for this signature in database
GPG key ID: 333455C5B7ED2E87

View file

@ -462,11 +462,10 @@ double averageFPS = 0.0f;
#define USE_FPS_SAMPLES #define USE_FPS_SAMPLES
#ifdef 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 (32) // Number of samples to store
#define NUM_FPS_SAMPLES (16) // Number of samples to store
static double fps_samples[NUM_FPS_SAMPLES]; static double fps_samples[NUM_FPS_SAMPLES];
static double updateElapsed = 0.0; static int fps_index;
#endif #endif
static boolean fps_init = false; static boolean fps_init = false;
@ -475,6 +474,7 @@ static precise_t fps_enter = 0;
void SCR_CalculateFPS(void) void SCR_CalculateFPS(void)
{ {
precise_t fps_finish = 0; precise_t fps_finish = 0;
int i;
double frameElapsed = 0.0; double frameElapsed = 0.0;
@ -489,35 +489,13 @@ void SCR_CalculateFPS(void)
fps_enter = fps_finish; fps_enter = fps_finish;
#ifdef USE_FPS_SAMPLES #ifdef USE_FPS_SAMPLES
updateElapsed += frameElapsed; fps_samples[fps_index] = frameElapsed / NUM_FPS_SAMPLES;
fps_index = (fps_index + 1) % NUM_FPS_SAMPLES;
if (updateElapsed >= FPS_SAMPLE_RATE)
{
static int sampleIndex = 0;
int i;
fps_samples[sampleIndex] = frameElapsed;
sampleIndex++;
if (sampleIndex >= NUM_FPS_SAMPLES)
sampleIndex = 0;
averageFPS = 0.0; averageFPS = 0.0;
for (i = 0; i < NUM_FPS_SAMPLES; i++) for (i = 0; i < NUM_FPS_SAMPLES; i++)
{
averageFPS += fps_samples[i]; averageFPS += fps_samples[i];
} averageFPS = 1.0 / averageFPS;
if (averageFPS > 0.0)
{
averageFPS = 1.0 / (averageFPS / NUM_FPS_SAMPLES);
}
}
while (updateElapsed >= FPS_SAMPLE_RATE)
{
updateElapsed -= FPS_SAMPLE_RATE;
}
#else #else
// Direct, unsampled counter. // Direct, unsampled counter.
averageFPS = 1.0 / frameElapsed; averageFPS = 1.0 / frameElapsed;