aboutsummaryrefslogtreecommitdiff
path: root/main.c
blob: cc96b3b2fdc27b32f9541c5c04efc58e399f4e35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#define NONSTD_IMPLEMENTATION
#define VFS_IMPLEMENTATION
#include "all.h"

#include <getopt.h>

int main(int argc, char *argv[]) {
	stringb map_path;
	sb_init(&map_path, 256);
	sb_append_cstr(&map_path, "maps/demo3.map");

	bool skip_title = false;
	int target_fps = -1;
	int width = WINDOW_WIDTH;
	int height = WINDOW_HEIGHT;

	static struct option long_options[] = {
		{"map", required_argument, 0, 'm'},
		{"fps", required_argument, 0, 'f'},
		{"width", required_argument, 0, 'w'},
		{"height", required_argument, 0, 'h'},
		{0, 0, 0, 0}
	};

	int opt;
	int option_index = 0;
	while ((opt = getopt_long_only(argc, argv, "m:f:w:h:", 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;
			case 'f':
				target_fps = atoi(optarg);
				break;
			case 'w':
				width = atoi(optarg);
				break;
			case 'h':
				height = atoi(optarg);
				break;
		}
	}

	unsigned int flags = FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI;
	if (target_fps < 0) flags |= FLAG_VSYNC_HINT;

	SetConfigFlags(flags);
	InitWindow(width, height, WINDOW_TITLE);

	int monitor = GetCurrentMonitor();
	SetWindowPosition((GetMonitorWidth(monitor) - GetScreenWidth()) / 2, (GetMonitorHeight(monitor) - GetScreenHeight()) / 2);

	vfs_init(VFS_DATA_PAK);
	InitGame();
	game.screen_width = width;
	game.screen_height = height;
	SetMap(map_path.data);

	if (target_fps >= 0) {
		game.target_fps = target_fps;
		game.vsync = false;
		SetTargetFPS(game.target_fps);
	} else {
		game.vsync = true;
		SetTargetFPS(GetMonitorRefreshRate(monitor));
	}

	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;
}