Mercurial > molko
comparison libmlk-adventure/adventure/molko.c @ 243:71b3b7036de7
misc: lot of cleanups,
- prefix libraries with libmlk,
- move assets from source directories closes #2520,
- prefix header guards closes #2519
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 28 Nov 2020 22:37:30 +0100 |
parents | libadventure/adventure/molko.c@76afe639fd72 |
children | 16be1ad3ddba |
comparison
equal
deleted
inserted
replaced
242:4c24604efcab | 243:71b3b7036de7 |
---|---|
1 /* | |
2 * molko.c -- main structure for Molko's Adventure | |
3 * | |
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <stddef.h> | |
20 #include <setjmp.h> | |
21 #include <locale.h> | |
22 | |
23 #include <core/clock.h> | |
24 #include <core/core.h> | |
25 #include <core/event.h> | |
26 #include <core/panic.h> | |
27 #include <core/translate.h> | |
28 #include <core/util.h> | |
29 #include <core/window.h> | |
30 | |
31 #include <ui/ui.h> | |
32 | |
33 #include <rpg/rpg.h> | |
34 | |
35 #include <adventure/state/panic.h> | |
36 #include <adventure/state/splashscreen.h> | |
37 #include <adventure/state/mainmenu.h> | |
38 | |
39 #include "molko.h" | |
40 | |
41 #define WINDOW_WIDTH 1280 | |
42 #define WINDOW_HEIGHT 720 | |
43 | |
44 static jmp_buf panic_buf; | |
45 | |
46 struct molko molko; | |
47 | |
48 static void | |
49 crash(void) | |
50 { | |
51 longjmp(panic_buf, 1); | |
52 } | |
53 | |
54 static void | |
55 loop(void) | |
56 { | |
57 struct clock clock = {0}; | |
58 | |
59 while (game.state) { | |
60 unsigned int elapsed = clock_elapsed(&clock); | |
61 | |
62 clock_start(&clock); | |
63 | |
64 for (union event ev; event_poll(&ev); ) { | |
65 switch (ev.type) { | |
66 case EVENT_QUIT: | |
67 return; | |
68 default: | |
69 game_handle(&ev); | |
70 break; | |
71 } | |
72 } | |
73 | |
74 game_update(elapsed); | |
75 game_draw(); | |
76 | |
77 if ((elapsed = clock_elapsed(&clock)) < 20) | |
78 delay(20 - elapsed); | |
79 } | |
80 } | |
81 | |
82 void | |
83 molko_init(void) | |
84 { | |
85 setlocale(LC_ALL, ""); | |
86 | |
87 if (!core_init("fr.malikania", "molko") || !ui_init() || !rpg_init()) | |
88 panic(); | |
89 | |
90 translate_init("libmlk-adventure"); | |
91 | |
92 if (!window_open("Molko's Adventure", WINDOW_WIDTH, WINDOW_HEIGHT)) | |
93 panic(); | |
94 | |
95 /* | |
96 * From here, we can setup our panic state which requires a window | |
97 * to be running. | |
98 */ | |
99 | |
100 /* Init unrecoverable panic state. */ | |
101 panic_state(&molko.states[MOLKO_STATE_PANIC]); | |
102 panic_handler = crash; | |
103 | |
104 /* Init states. */ | |
105 splashscreen_state(&molko.states[MOLKO_STATE_SPLASH], &molko.states[MOLKO_STATE_MAINMENU]); | |
106 mainmenu_state(&molko.states[MOLKO_STATE_MAINMENU]); | |
107 | |
108 /* Start to splash. */ | |
109 game_switch(&molko.states[MOLKO_STATE_SPLASH], true); | |
110 } | |
111 | |
112 void | |
113 molko_run(void) | |
114 { | |
115 if (setjmp(panic_buf) == 0) { | |
116 /* Initial game run. */ | |
117 loop(); | |
118 } else { | |
119 /* Clear event queue to avoid accidental key presses. */ | |
120 for (union event ev; event_poll(&ev); ) | |
121 continue; | |
122 | |
123 game_switch(&molko.states[MOLKO_STATE_PANIC], true); | |
124 loop(); | |
125 } | |
126 } | |
127 | |
128 void | |
129 molko_finish(void) | |
130 { | |
131 for (size_t i = 0; i < MOLKO_STATE_NUM; ++i) | |
132 state_finish(&molko.states[i]); | |
133 | |
134 window_finish(); | |
135 rpg_finish(); | |
136 ui_finish(); | |
137 core_finish(); | |
138 } |