/* * ============================================================ * 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()); * 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(); * * 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. * * - 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, Back resets, Start quits, and * the title bar shows live state/speed. * ============================================================ */