summaryrefslogtreecommitdiff
path: root/platformer/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--platformer/src/main.c26
1 files changed, 23 insertions, 3 deletions
diff --git a/platformer/src/main.c b/platformer/src/main.c
index 8f5e510..0435737 100644
--- a/platformer/src/main.c
+++ b/platformer/src/main.c
@@ -40,8 +40,9 @@
* window_aspect(), TICK_DT);
* accumulator -= TICK_DT;
*
- * render_frame(&camera, &player, window_aspect());
+ * render_frame(&camera, &player, window_aspect(), fps);
* window_swap();
+ * count the frame toward the FPS reading (below)
*
* WHY the fixed timestep: the game behaves the same (identical jump
* height, identical speeds) no matter the monitor's refresh rate or a
@@ -53,6 +54,11 @@
* far they moved each tick, so a teleport back to spawn would look like a
* 100 m sprint. Re-initializing gives a clean, centered start instead.
*
+ * - FPS counter: count rendered frames, and every FPS_UPDATE_INTERVAL
+ * seconds set fps = frames / elapsed time (rounded), then start counting
+ * again. Averaging over a window keeps the number steady enough to read.
+ * fps starts at 0, which render_frame() takes as "not measured yet".
+ *
* - Debug readout for tuning (a few times per second, not every frame):
* window_set_title() with something like
* "platformer | RUN | speed 7.42 m/s | y 1.83 | pad"
@@ -69,7 +75,8 @@
* DONE WHEN: `make run` opens the window. The player can run, turn and jump
* with a gamepad, the camera orbits and frames them (right stick
* click recenters behind them), Back resets, Start quits, and
- * the title bar shows live state/speed.
+ * the title bar shows live state/speed, and the top-right corner
+ * shows the FPS.
* ============================================================
*/
@@ -84,6 +91,7 @@
#include "world.h"
#define TITLE_UPDATE_INTERVAL 0.25 /* seconds between debug title refreshes */
+#define FPS_UPDATE_INTERVAL 0.5 /* seconds of frames averaged into each FPS reading */
/* Our "HUD" until text rendering exists: live state in the title bar.
* Updating a few times per second keeps it readable (and cheap). */
@@ -128,6 +136,9 @@ int main(void)
double previous = window_time();
double accumulator = 0.0;
double last_title_time = 0.0;
+ double fps_start = previous; /* when the current FPS count began */
+ int fps_frames = 0; /* frames rendered since fps_start */
+ int fps = 0; /* last FPS reading; 0 = not measured yet */
while (!window_should_close()) {
double now = window_time();
@@ -159,9 +170,18 @@ int main(void)
accumulator -= TICK_DT;
}
- render_frame(&camera, &player, window_aspect());
+ render_frame(&camera, &player, window_aspect(), fps);
window_swap();
+ /* FPS averaged over FPS_UPDATE_INTERVAL: a per-frame number would
+ * flicker too fast to read. */
+ fps_frames++;
+ if (now - fps_start >= FPS_UPDATE_INTERVAL) {
+ fps = (int)(fps_frames / (now - fps_start) + 0.5);
+ fps_frames = 0;
+ fps_start = now;
+ }
+
if (now - last_title_time >= TITLE_UPDATE_INTERVAL) {
update_debug_title(&player, &input);
last_title_time = now;