shithub: ft²

Download patch

ref: a5d7c6f7104a6f4beaa653f24794dcd79a425110
parent: 846ce9d9cad4b46246ea18f3def3112a7e49b802
author: Olav Sørensen <[email protected]>
date: Thu Jul 7 20:38:54 EDT 2022

Fix FPS counter (CTRL+SHIFT+F)

--- a/src/ft2_hpc.c
+++ b/src/ft2_hpc.c
@@ -34,7 +34,7 @@
 	// NtDelayExecution() delays in 100ns-units, and negative value = delay from current time
 	usec *= -10;
 
-	delayInterval.HighPart = 0xFFFFFFFF;
+	delayInterval.HighPart = 0xFFFFFFFF; // negative 64-bit value, we only set the lower dword
 	delayInterval.LowPart = usec;
 	NtDelayExecution(false, &delayInterval);
 }
@@ -61,6 +61,7 @@
 #endif
 	hpcFreq.freq64 = SDL_GetPerformanceFrequency();
 	hpcFreq.dFreq = (double)hpcFreq.freq64;
+	hpcFreq.dFreqMulMs = 1000.0 / hpcFreq.dFreq;
 	hpcFreq.dFreqMulMicro = (1000.0 * 1000.0) / hpcFreq.dFreq;
 }
 
--- a/src/ft2_hpc.h
+++ b/src/ft2_hpc.h
@@ -6,7 +6,7 @@
 typedef struct
 {
 	uint64_t freq64;
-	double dFreq, dFreqMulMicro;
+	double dFreq, dFreqMulMicro, dFreqMulMs;
 } hpcFreq_t;
 
 typedef struct
--- a/src/ft2_video.c
+++ b/src/ft2_video.c
@@ -59,7 +59,7 @@
 
 static char fpsTextBuf[1024];
 static uint64_t frameStartTime;
-static double dRunningFPS, dFrameTime, dAvgFPS;
+static double dRunningFrameDuration, dAvgFPS;
 // ------------------
 
 static void drawReplayerData(void);
@@ -68,8 +68,7 @@
 {
 	editor.framesPassed = 0;
 	fpsTextBuf[0] = '\0';
-	dRunningFPS = VBLANK_HZ;
-	dFrameTime = 1000.0 / VBLANK_HZ;
+	dRunningFrameDuration = 1000.0 / VBLANK_HZ;
 }
 
 void beginFPSCounter(void)
@@ -82,11 +81,11 @@
 {
 	if (editor.framesPassed >= FPS_SCAN_FRAMES && (editor.framesPassed % FPS_SCAN_FRAMES) == 0)
 	{
-		dAvgFPS = dRunningFPS * (1.0 / FPS_SCAN_FRAMES);
+		dAvgFPS = 1000.0 / (dRunningFrameDuration / FPS_SCAN_FRAMES);
 		if (dAvgFPS < 0.0 || dAvgFPS > 99999999.9999)
 			dAvgFPS = 99999999.9999; // prevent number from overflowing text box
 
-		dRunningFPS = 0.0;
+		dRunningFrameDuration = 0.0;
 	}
 
 	clearRect(FPS_RENDER_X+2, FPS_RENDER_Y+2, FPS_RENDER_W, FPS_RENDER_H);
@@ -98,7 +97,7 @@
 	// test if enough data is collected yet
 	if (editor.framesPassed < FPS_SCAN_FRAMES)
 	{
-		textOut(FPS_RENDER_X+53, FPS_RENDER_Y+39, PAL_FORGRND, "Collecting frame information...");
+		textOut(FPS_RENDER_X+53, FPS_RENDER_Y+39, PAL_FORGRND, "Gathering frame information...");
 		return;
 	}
 
@@ -150,9 +149,11 @@
 {
 	if (video.showFPSCounter && frameStartTime > 0)
 	{
-		const uint64_t frameTimeDiff = SDL_GetPerformanceCounter() - frameStartTime;
-		const double dHz = 1000.0 / (frameTimeDiff * editor.dPerfFreqMulMs);
-		dRunningFPS += dHz;
+		uint64_t frameTimeDiff64 = SDL_GetPerformanceCounter() - frameStartTime;
+		if (frameTimeDiff64 > INT32_MAX)
+			frameTimeDiff64 = INT32_MAX;
+
+		dRunningFrameDuration += (int32_t)frameTimeDiff64 / (hpcFreq.dFreq / 1000.0);
 	}
 }