summaryrefslogtreecommitdiff
path: root/platformer/src/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'platformer/src/main.c')
-rw-r--r--platformer/src/main.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/platformer/src/main.c b/platformer/src/main.c
index 4e9e07a..5b11772 100644
--- a/platformer/src/main.c
+++ b/platformer/src/main.c
@@ -65,3 +65,98 @@
* the title bar shows live state/speed.
* ============================================================
*/
+
+#include <stdio.h>
+
+#include "camera.h"
+#include "config.h"
+#include "controls.h"
+#include "player.h"
+#include "render.h"
+#include "window.h"
+#include "world.h"
+
+#define TITLE_UPDATE_INTERVAL 0.25 /* seconds between debug title refreshes */
+
+/* Our "HUD" until text rendering exists: live state in the title bar.
+ * Updating a few times per second keeps it readable (and cheap). */
+static void update_debug_title(const Player *player, const Input *input)
+{
+ char title[128];
+ snprintf(title, sizeof title, "%s | %s | speed %.2f m/s | y %.2f | %s",
+ WINDOW_TITLE,
+ player_state_name(player->state),
+ player->forward_speed,
+ player->body.pos.y,
+ input->using_gamepad ? "pad" : "keyboard");
+ window_set_title(title);
+}
+
+int main(void)
+{
+ if (!window_init())
+ return 1;
+ controls_init();
+ world_init();
+
+ Player player;
+ player_init(&player, world_spawn_point());
+ Camera camera;
+ camera_init(&camera, player.body.pos);
+
+ if (!render_init()) {
+ window_shutdown();
+ return 1;
+ }
+
+ /* FIXED TIMESTEP with an accumulator. Real time comes in uneven chunks
+ * (one display frame at a time), but the game logic always advances in
+ * exact TICK_DT steps. Each frame adds the elapsed time to the
+ * accumulator, then runs as many whole ticks as fit (0, 1 or several);
+ * the leftover carries into the next frame. The payoff: identical jump
+ * heights and speeds no matter the monitor's refresh rate or a dropped
+ * frame, which is exactly what you need when tuning movement. SM64 ran its
+ * logic at 30 Hz; we use TICK_HZ. */
+ Input input = { 0 };
+ double previous = window_time();
+ double accumulator = 0.0;
+ double last_title_time = 0.0;
+
+ while (!window_should_close()) {
+ double now = window_time();
+ double frame_time = now - previous;
+ previous = now;
+ /* After a stall (breakpoint, window drag) don't try to simulate the
+ * whole gap at once: that would take even longer and fall further behind. */
+ if (frame_time > MAX_FRAME_TIME)
+ frame_time = MAX_FRAME_TIME;
+ accumulator += frame_time;
+
+ window_poll_events();
+
+ while (accumulator >= TICK_DT) {
+ controls_poll(&input);
+ if (input.quit_pressed)
+ window_request_close();
+ if (input.reset_pressed)
+ player_reset(&player, world_spawn_point());
+ /* Player before camera, so the camera follows where the player IS,
+ * not where it was a tick ago. SM64 updates Mario before the camera too. */
+ player_update(&player, &input, camera_yaw(&camera), TICK_DT);
+ camera_update(&camera, &input, player.body.pos, TICK_DT);
+ accumulator -= TICK_DT;
+ }
+
+ render_frame(&camera, &player, window_aspect());
+ window_swap();
+
+ if (now - last_title_time >= TITLE_UPDATE_INTERVAL) {
+ update_debug_title(&player, &input);
+ last_title_time = now;
+ }
+ }
+
+ render_shutdown();
+ window_shutdown();
+ return 0;
+}