-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
105 lines (84 loc) · 2.61 KB
/
main.c
File metadata and controls
105 lines (84 loc) · 2.61 KB
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
#include <raylib.h>
#include "gmem.h"
#include "gdict.h"
#include "context.h"
// Scenes
#include "scenes/menu.h"
#include "scenes/play.h"
#include "scenes/editor.h"
// Default style
#include "styles/dark.h"
int main(int argc, char *argv[]) {
/* gdict entries */
gdict_init("textures", "path");
// Create the context
Context ctx = context_new(
window_new("RayLab - ", 60, 600),
(Map) { 0 }, (Player) { 0 }
);
uintptr_t last_scene_uid = 0; // For swapping scenes
int last_window_length = ctx.window.length; // For window resizing
// Initialisation
InitWindow(
ctx.window.length,
ctx.window.length,
ctx.window.title);
SetTargetFPS(ctx.window.fps);
GuiLoadStyleDark();
// Immediately skip to PLAY or EDITOR and load map
Scene scene = SCENE_MENU;
if (argc > 1) {
const char *path = argv[1];
if (strlen(path) != 0) {
// Attempt to load
ctx.map = map_load_filename(path + 1);
if (!ctx.map.data) {
fprintf(stderr, "Failed to load map.\n");
return 1;
} else {
switch (path[0]) {
case 'p':
scene = SCENE_PLAY;
break;
case 'e':
scene = SCENE_EDITOR;
break;
}
}
}
}
// Main loop
while (!WindowShouldClose() && ctx.running) {
/* UPDATE PLAYER */
ctx.player.mouse = GetMousePosition();
/* WINDOW PROPERTIES UPDATE */
if (ctx.window.length != last_window_length) {
SetWindowSize(ctx.window.length, ctx.window.length);
last_window_length = ctx.window.length;
}
/* SCENE SWAP + INITIALISE SCENE */
if (scene.uid != last_scene_uid) {
SetWindowTitle(TextFormat("%s%s", ctx.window.title, scene.subtitle));
if (scene.init)
scene.init(&ctx, &scene, SCENE_INIT);
last_scene_uid = scene.uid;
}
/* UPDATE */
if (scene.update)
scene.update(&ctx, &scene, GetFrameTime());
/* DRAWIN' */
BeginDrawing();
if (scene.draw)
scene.draw(&ctx, &scene);
EndDrawing();
}
// Save the map
if (ctx.map.data)
if (!map_dump_filename(ctx.map, "save.bin"))
printf("Failed to save level!\n");
// Cleanup (gmem handles the rest)
if (scene.init)
scene.init(&ctx, &scene, SCENE_CLEAN);
CloseWindow();
return 0;
}