From b4d0ad9e95226d225d5361b1182866884aaa6366 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Thu, 30 Apr 2026 19:19:27 +0200 Subject: Structural refactor --- main.c | 143 +++++++++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 90 insertions(+), 53 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index e497e73..9f32ecc 100644 --- a/main.c +++ b/main.c @@ -1,60 +1,97 @@ -#define _POSIX_C_SOURCE 200809L #define NONSTD_IMPLEMENTATION #define VFS_IMPLEMENTATION #include "all.h" + #include -#include int main(int argc, char *argv[]) { - char map_path[256] = "maps/demo3.map"; - bool skip_title = false; - - static struct option long_options[] = { - {"map", required_argument, 0, 'm'}, - {0, 0, 0, 0} - }; - - int opt; - int option_index = 0; - while ((opt = getopt_long_only(argc, argv, "m:", long_options, &option_index)) != -1) { - switch (opt) { - case 'm': - strncpy(map_path, optarg, sizeof(map_path) - 1); - skip_title = true; - break; - } - } - - SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI); - InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE); - - int monitor = GetCurrentMonitor(); - SetWindowPosition((GetMonitorWidth(monitor) - GetScreenWidth()) / 2, - (GetMonitorHeight(monitor) - GetScreenHeight()) / 2); - - SetTargetFPS(GetMonitorRefreshRate(monitor)); - - vfs_init("data.pak"); - InitGame(); - SetMap(map_path); - - if (skip_title) { - if (LoadMap(game.map_path)) { - game.mode = STATE_PLAYING; - game.cursor_captured = true; - DisableCursor(); - } - } - - game.vsync = true; - - while (!WindowShouldClose()) { - UpdateGame(); - DrawGame(); - } - - vfs_shutdown(); - CloseWindow(); - - return 0; + stringb map_path; + sb_init(&map_path, 256); + sb_append_cstr(&map_path, "maps/demo3.map"); + + bool skip_title = false; + + static struct option long_options[] = { + {"map", required_argument, 0, 'm'}, + {0, 0, 0, 0} + }; + + int opt; + int option_index = 0; + while ((opt = getopt_long_only(argc, argv, "m:", long_options, &option_index)) != -1) { + switch (opt) { + case 'm': + sb_free(&map_path); + sb_init(&map_path, 256); + sb_append_cstr(&map_path, optarg); + skip_title = true; + break; + } + } + + SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI); + InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE); + + int monitor = GetCurrentMonitor(); + SetWindowPosition((GetMonitorWidth(monitor) - GetScreenWidth()) / 2, (GetMonitorHeight(monitor) - GetScreenHeight()) / 2); + SetTargetFPS(GetMonitorRefreshRate(monitor)); + + vfs_init(VFS_DATA_PAK); + InitGame(); + SetMap(map_path.data); + + if (skip_title) { + if (LoadMap(game.map_path)) { + game.mode = STATE_PLAYING; + game.cursor_captured = true; + DisableCursor(); + } + } + + game.vsync = true; + + while (!WindowShouldClose()) { + // Global Map Switching (Debug/Demo) + if (IsKeyPressed(KEY_ONE)) { + if (LoadMap("maps/demo1.map")) { + game.mode = STATE_PLAYING; + game.cursor_captured = true; + DisableCursor(); + } + } + if (IsKeyPressed(KEY_TWO)) { + if (LoadMap("maps/demo2.map")) { + game.mode = STATE_PLAYING; + game.cursor_captured = true; + DisableCursor(); + } + } + if (IsKeyPressed(KEY_THREE)) { + if (LoadMap("maps/demo3.map")) { + game.mode = STATE_PLAYING; + game.cursor_captured = true; + DisableCursor(); + } + } + + // Game State Switcher + switch (game.mode) { + case STATE_MENU: + UpdateMenu(); + DrawMenu(); + break; + case STATE_PLAYING: + UpdateGame(); + DrawGame(); + break; + } + } + + UnloadMap(); + UnloadAssets(); + vfs_shutdown(); + CloseWindow(); + sb_free(&map_path); + + return 0; } -- cgit v1.2.3