/* * ============================================================ * FILE: main.c * * PURPOSE: Entry point. Starts every module, runs the game loop, and shuts * everything down. It should read like a table of contents for the * game, with NO gameplay logic of its own. * * FITS IN: The top of the dependency graph. Includes window.h, controls.h, * world.h, player.h, camera.h, render.h, config.h. * * IMPLEMENT: * - Startup (in this order, and if any step fails, clean up what * already started and return 1): * window_init() -> controls_init() -> world_init() * player_init(&player, world_spawn_point()) * camera_init(&camera, player.body.pos) * render_init() * * - Game loop with a FIXED TIMESTEP + accumulator (explain it in a comment; * this is the most important idea in the file): * * previous = window_time(); accumulator = 0; * while (!window_should_close()): * now = window_time(); * frame_time = now - previous; previous = now; * if frame_time > MAX_FRAME_TIME: frame_time = MAX_FRAME_TIME * accumulator += frame_time; * * window_poll_events(); * * while accumulator >= TICK_DT: // 0, 1 or several ticks * controls_poll(&input); * if input.quit_pressed: window_request_close(); * if input.reset_pressed: * player_reset(&player, world_spawn_point()); * camera_init(&camera, player.body.pos); // see WHY below * player_update(&player, &input, camera_yaw(&camera), TICK_DT); * camera_update(&camera, &input, player.body.pos, player.facing_yaw, * window_aspect(), TICK_DT); * accumulator -= TICK_DT; * * render_frame(&camera, &player, window_aspect()); * window_swap(); * * WHY the fixed timestep: the game behaves the same (identical jump * height, identical speeds) no matter the monitor's refresh rate or a * dropped frame, which is exactly what you need when tuning movement. SM64 * ran logic at 30 Hz; we use TICK_HZ (60). * WHY player before camera: the camera then follows where the player IS, * not where it was a tick ago. SM64 updates Mario before the camera too. * WHY reset the camera too: the camera frames the player by tracking how * 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. * * - 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" * using player_state_name(), forward_speed, body.pos.y and * input.using_gamepad. This is our "HUD" until text rendering exists. * * - Shutdown in reverse order: render_shutdown() -> window_shutdown(). * * CONVENTIONS: `int main(void)`. Game objects (Player, Camera, Input) are * local variables in main, passed by pointer. No globals. * * NOT IN SCOPE: Menus, pause, command-line args, state interpolation. * * 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. * ============================================================ */ #include #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) { /* The camera tracks per-tick movement, so a teleport needs a * fresh camera rather than a 100 m "sprint" to follow. */ player_reset(&player, world_spawn_point()); camera_init(&camera, player.body.pos); } /* 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, player.facing_yaw, window_aspect(), 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; }