DEADSOFTWARE

portability: avoid errors on some compilers
[flatwaifu.git] / src / stubsys / main.c
1 /* Copyright (C) 2020 SovietPony
2 *
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
16 #ifdef __EMSCRIPTEN__
17 # include <emscripten.h>
18 #endif
20 #include <stdio.h>
21 #include <stdarg.h>
22 #include <stdlib.h> // srand exit
23 #include <assert.h>
24 #include <time.h>
25 #include "system.h"
26 #include "input.h"
28 #include "player.h" // pl1 pl2
29 #include "menu.h" // G_keyf
30 #include "error.h" // logo
31 #include "monster.h" // nomon
33 #include "files.h" // F_startup F_addwad F_initwads F_allocres
34 #include "config.h" // CFG_args CFG_load CFG_save
35 #include "args.h" // ARG_parse
36 #include "memory.h" // M_startup
37 #include "game.h" // G_init G_act
38 #include "sound.h" // S_init S_done
39 #include "music.h" // S_initmusic S_updatemusic S_donemusic
40 #include "render.h" // R_init R_draw R_done
42 #define MODE_NONE 0
43 #define MODE_OPENGL 1
44 #define MODE_SOFTWARE 2
46 static clock_t ticks;
47 static int quit = 0;
48 static int mode = MODE_NONE;
49 static int text_input = 0;
50 static int width = 0;
51 static int height = 0;
52 static int fullscreen = 0;
53 static void *softbuffer = NULL;
54 static videomode_size_t vsize = { 320, 200, 0 };
55 static videomode_t vlist = { 1, &vsize };
57 static const cfg_t arg[] = {
58 {"file", NULL, Y_FILES},
59 {"cheat", &cheat, Y_SW_ON},
60 // {"vga", &shot_vga, Y_SW_ON},
61 // {"musvol", &mus_vol, Y_WORD},
62 {"mon", &nomon, Y_SW_OFF},
63 {"warp", &_warp, Y_BYTE},
64 // {"config", NULL, cfg_file, Y_STRING},
65 {NULL, NULL, 0} // end
66 };
68 static const cfg_t cfg[] = {
69 // {"screenshot", &shot_vga, Y_SW_ON},
70 // {"music_volume", &mus_vol, Y_WORD},
71 // {"music_random", &music_random, Y_SW_ON},
72 // {"music_time", &music_time, Y_DWORD},
73 // {"music_fade", &music_fade, Y_DWORD},
74 {"pl1_left", &pl1.kl, Y_KEY},
75 {"pl1_right",&pl1.kr, Y_KEY},
76 {"pl1_up", &pl1.ku, Y_KEY},
77 {"pl1_down", &pl1.kd, Y_KEY},
78 {"pl1_jump", &pl1.kj, Y_KEY},
79 {"pl1_fire", &pl1.kf, Y_KEY},
80 {"pl1_next", &pl1.kwr, Y_KEY},
81 {"pl1_prev", &pl1.kwl, Y_KEY},
82 {"pl1_use", &pl1.kp, Y_KEY},
83 {"pl2_left", &pl2.kl, Y_KEY},
84 {"pl2_right", &pl2.kr, Y_KEY},
85 {"pl2_up", &pl2.ku, Y_KEY},
86 {"pl2_down", &pl2.kd, Y_KEY},
87 {"pl2_jump", &pl2.kj, Y_KEY},
88 {"pl2_fire", &pl2.kf, Y_KEY},
89 {"pl2_next", &pl2.kwr, Y_KEY},
90 {"pl2_prev", &pl2.kwl, Y_KEY},
91 {"pl2_use", &pl2.kp, Y_KEY},
92 {NULL, NULL, 0} // end
93 };
95 static void CFG_args (int argc, char **argv) {
96 const cfg_t *list[4];
97 list[0] = arg;
98 list[1] = R_args();
99 list[2] = S_args();
100 list[3] = MUS_args();
101 ARG_parse(argc, argv, 4, list);
104 static void CFG_load (void) {
105 const cfg_t *list[4];
106 list[0] = cfg;
107 list[1] = R_conf();
108 list[2] = S_conf();
109 list[3] = MUS_conf();
110 CFG_read_config("default.cfg", 4, list);
111 CFG_read_config("doom2d.cfg", 4, list);
114 static void CFG_save (void) {
115 const cfg_t *list[4];
116 list[0] = cfg;
117 list[1] = R_conf();
118 list[2] = S_conf();
119 list[3] = MUS_conf();
120 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
123 /* --- error.h --- */
125 void logo (const char *s, ...) {
126 va_list ap;
127 va_start(ap, s);
128 vprintf(s, ap);
129 va_end(ap);
130 fflush(stdout);
133 void logo_gas (int cur, int all) {
134 // stub
137 void ERR_failinit (char *s, ...) {
138 va_list ap;
139 va_start(ap, s);
140 vprintf(s, ap);
141 va_end(ap);
142 puts("");
143 abort();
146 void ERR_fatal (char *s, ...) {
147 va_list ap;
148 R_done();
149 MUS_done();
150 S_done();
151 puts("\nCRITICAL ERROR:");
152 va_start(ap, s);
153 vprintf(s, ap);
154 va_end(ap);
155 puts("");
156 abort();
159 void ERR_quit (void) {
160 quit = 1;
163 /* --- system.h --- */
165 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
166 assert(w > 0);
167 assert(h > 0);
168 mode = MODE_OPENGL;
169 width = w;
170 height = h;
171 return 1;
174 int Y_set_videomode_software (int w, int h, int fullscreen) {
175 void *buf;
176 assert(w > 0);
177 assert(h > 0);
178 buf = softbuffer ? realloc(softbuffer, w * h) : malloc(w * h);
179 if (buf) {
180 mode = MODE_SOFTWARE;
181 softbuffer = buf;
182 width = w;
183 height = h;
185 return buf != NULL;
188 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
189 return &vlist;
192 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
193 return &vlist;
196 void Y_get_videomode (int *w, int *h) {
197 if (mode != MODE_NONE) {
198 *w = width;
199 *h = height;
200 } else {
201 *w = 0;
202 *h = 0;
206 int Y_videomode_setted (void) {
207 return mode != MODE_NONE;
210 void Y_unset_videomode (void) {
211 mode = MODE_NONE;
212 free(softbuffer);
213 softbuffer = NULL;
216 void Y_set_fullscreen (int yes) {
217 fullscreen = yes;
220 int Y_get_fullscreen (void) {
221 return (mode != MODE_NONE) && fullscreen;
224 void Y_swap_buffers (void) {
225 assert(mode == MODE_OPENGL);
228 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
229 assert(mode == MODE_SOFTWARE);
230 *buf = softbuffer;
231 *w = width;
232 *h = height;
233 *pitch = width;
236 void Y_set_vga_palette (byte *vgapal) {
237 int i;
238 //byte *p = vgapal;
239 assert(vgapal != NULL);
240 assert(mode == MODE_SOFTWARE);
243 void Y_repaint_rect (int x, int y, int w, int h) {
244 assert(mode == MODE_SOFTWARE);
247 void Y_repaint (void) {
248 assert(mode == MODE_SOFTWARE);
251 void Y_enable_text_input (void) {
252 text_input = 1;
255 void Y_disable_text_input (void) {
256 text_input = 0;
259 /* --- main --- */
261 static void poll_events (void) {
262 // stub
265 static void step (void) {
266 clock_t t;
267 poll_events();
268 MUS_update();
269 t = clock();
270 if ((t - ticks) * 1000 / CLOCKS_PER_SEC > DELAY) {
271 ticks = t;
272 G_act();
274 R_draw();
277 int main (int argc, char *argv[]) {
278 logo("main: initialize\n");
279 // Player 1 defaults
280 pl1.ku = KEY_KP_8;
281 pl1.kd = KEY_KP_5;
282 pl1.kl = KEY_KP_4;
283 pl1.kr = KEY_KP_6;
284 pl1.kf = KEY_PAGEDOWN;
285 pl1.kj = KEY_DELETE;
286 pl1.kwl = KEY_HOME;
287 pl1.kwr = KEY_END;
288 pl1.kp = KEY_KP_8;
289 // Player 2 defaults
290 pl2.ku = KEY_E;
291 pl2.kd = KEY_D;
292 pl2.kl = KEY_S;
293 pl2.kr = KEY_F;
294 pl2.kf = KEY_A;
295 pl2.kj = KEY_Q;
296 pl2.kwl = KEY_1;
297 pl2.kwr = KEY_2;
298 pl2.kp = KEY_E;
299 //srand(SDL_GetTicks());
300 F_addwad("doom2d.wad");
301 CFG_args(argc, argv);
302 CFG_load();
303 F_initwads();
304 S_init();
305 MUS_init();
306 R_init();
307 G_init();
308 ticks = clock();
309 #ifdef __EMSCRIPTEN__
310 emscripten_set_main_loop(step, 0, 1);
311 #else
312 while (!quit) {
313 step();
315 #endif
316 CFG_save();
317 R_done();
318 MUS_done();
319 S_done();
320 return 0;