aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2026-04-30 19:19:27 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2026-04-30 19:44:07 +0200
commitb4d0ad9e95226d225d5361b1182866884aaa6366 (patch)
tree5440b34d97e89a08fc01abd49bb80e5b50dda945 /main.c
parentddde2e8fdf05efac5914ca2b812b7762b78ba9ec (diff)
downloadstalag-b4d0ad9e95226d225d5361b1182866884aaa6366.tar.gz
Structural refactor
Diffstat (limited to 'main.c')
-rw-r--r--main.c143
1 files changed, 90 insertions, 53 deletions
diff --git a/main.c b/main.c
index e497e73..9f32ecc 100644
--- a/main.c
+++ b/main.c
@@ -1,60 +1,97 @@
1#define _POSIX_C_SOURCE 200809L
2#define NONSTD_IMPLEMENTATION 1#define NONSTD_IMPLEMENTATION
3#define VFS_IMPLEMENTATION 2#define VFS_IMPLEMENTATION
4#include "all.h" 3#include "all.h"
4
5#include <getopt.h> 5#include <getopt.h>
6#include <string.h>
7 6
8int main(int argc, char *argv[]) { 7int main(int argc, char *argv[]) {
9 char map_path[256] = "maps/demo3.map"; 8 stringb map_path;
10 bool skip_title = false; 9 sb_init(&map_path, 256);
11 10 sb_append_cstr(&map_path, "maps/demo3.map");
12 static struct option long_options[] = { 11
13 {"map", required_argument, 0, 'm'}, 12 bool skip_title = false;
14 {0, 0, 0, 0} 13
15 }; 14 static struct option long_options[] = {
16 15 {"map", required_argument, 0, 'm'},
17 int opt; 16 {0, 0, 0, 0}
18 int option_index = 0; 17 };
19 while ((opt = getopt_long_only(argc, argv, "m:", long_options, &option_index)) != -1) { 18
20 switch (opt) { 19 int opt;
21 case 'm': 20 int option_index = 0;
22 strncpy(map_path, optarg, sizeof(map_path) - 1); 21 while ((opt = getopt_long_only(argc, argv, "m:", long_options, &option_index)) != -1) {
23 skip_title = true; 22 switch (opt) {
24 break; 23 case 'm':
25 } 24 sb_free(&map_path);
26 } 25 sb_init(&map_path, 256);
27 26 sb_append_cstr(&map_path, optarg);
28 SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI); 27 skip_title = true;
29 InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE); 28 break;
30 29 }
31 int monitor = GetCurrentMonitor(); 30 }
32 SetWindowPosition((GetMonitorWidth(monitor) - GetScreenWidth()) / 2, 31
33 (GetMonitorHeight(monitor) - GetScreenHeight()) / 2); 32 SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI);
34 33 InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE);
35 SetTargetFPS(GetMonitorRefreshRate(monitor)); 34
36 35 int monitor = GetCurrentMonitor();
37 vfs_init("data.pak"); 36 SetWindowPosition((GetMonitorWidth(monitor) - GetScreenWidth()) / 2, (GetMonitorHeight(monitor) - GetScreenHeight()) / 2);
38 InitGame(); 37 SetTargetFPS(GetMonitorRefreshRate(monitor));
39 SetMap(map_path); 38
40 39 vfs_init(VFS_DATA_PAK);
41 if (skip_title) { 40 InitGame();
42 if (LoadMap(game.map_path)) { 41 SetMap(map_path.data);
43 game.mode = STATE_PLAYING; 42
44 game.cursor_captured = true; 43 if (skip_title) {
45 DisableCursor(); 44 if (LoadMap(game.map_path)) {
46 } 45 game.mode = STATE_PLAYING;
47 } 46 game.cursor_captured = true;
48 47 DisableCursor();
49 game.vsync = true; 48 }
50 49 }
51 while (!WindowShouldClose()) { 50
52 UpdateGame(); 51 game.vsync = true;
53 DrawGame(); 52
54 } 53 while (!WindowShouldClose()) {
55 54 // Global Map Switching (Debug/Demo)
56 vfs_shutdown(); 55 if (IsKeyPressed(KEY_ONE)) {
57 CloseWindow(); 56 if (LoadMap("maps/demo1.map")) {
58 57 game.mode = STATE_PLAYING;
59 return 0; 58 game.cursor_captured = true;
59 DisableCursor();
60 }
61 }
62 if (IsKeyPressed(KEY_TWO)) {
63 if (LoadMap("maps/demo2.map")) {
64 game.mode = STATE_PLAYING;
65 game.cursor_captured = true;
66 DisableCursor();
67 }
68 }
69 if (IsKeyPressed(KEY_THREE)) {
70 if (LoadMap("maps/demo3.map")) {
71 game.mode = STATE_PLAYING;
72 game.cursor_captured = true;
73 DisableCursor();
74 }
75 }
76
77 // Game State Switcher
78 switch (game.mode) {
79 case STATE_MENU:
80 UpdateMenu();
81 DrawMenu();
82 break;
83 case STATE_PLAYING:
84 UpdateGame();
85 DrawGame();
86 break;
87 }
88 }
89
90 UnloadMap();
91 UnloadAssets();
92 vfs_shutdown();
93 CloseWindow();
94 sb_free(&map_path);
95
96 return 0;
60} 97}