Merge branch 'fix-inaccurate-fps-counter' into 'next'

Fix inaccuracies in FPS counter

Closes #1047

See merge request STJr/SRB2!2047
This commit is contained in:
Sal 2023-07-24 10:57:27 +00:00
commit 4ccba2a9d9

View file

@ -478,11 +478,11 @@ double averageFPS = 0.0f;
#define USE_FPS_SAMPLES
#ifdef USE_FPS_SAMPLES
#define FPS_SAMPLE_RATE (0.05) // How often to update FPS samples, in seconds
#define MAX_FRAME_TIME 0.05
#define NUM_FPS_SAMPLES (16) // Number of samples to store
static double fps_samples[NUM_FPS_SAMPLES];
static double updateElapsed = 0.0;
static double total_frame_time = 0.0;
static int frame_index;
#endif
static boolean fps_init = false;
@ -505,34 +505,12 @@ void SCR_CalculateFPS(void)
fps_enter = fps_finish;
#ifdef USE_FPS_SAMPLES
updateElapsed += frameElapsed;
if (updateElapsed >= FPS_SAMPLE_RATE)
total_frame_time += frameElapsed;
if (frame_index++ >= NUM_FPS_SAMPLES || 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 (updateElapsed >= FPS_SAMPLE_RATE)
{
updateElapsed -= FPS_SAMPLE_RATE;
averageFPS = 1.0 / (total_frame_time / frame_index);
total_frame_time = 0.0;
frame_index = 0;
}
#else
// Direct, unsampled counter.