DEADSOFTWARE

557505df4a386b903e0a9df025574985df6b94a9
[flatwaifu.git] / src / kos32 / 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 #include "kos32.h"
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <stdlib.h> // srand exit
21 #include <string.h>
22 #include <assert.h>
23 #include "system.h"
24 #include "input.h"
26 #include "my.h" // fexists
27 #include "player.h" // pl1 pl2
28 #include "menu.h" // G_keyf
29 #include "error.h" // logo
30 #include "monster.h" // nomon
32 #include "files.h" // F_startup F_addwad F_initwads F_allocres
33 #include "config.h" // CFG_args CFG_load CFG_save
34 #include "args.h" // ARG_parse
35 #include "memory.h" // M_startup
36 #include "game.h" // G_init G_act
37 #include "sound.h" // S_init S_done
38 #include "music.h" // S_initmusic S_updatemusic S_donemusic
39 #include "render.h" // R_init R_draw R_done
41 static int quit = 0;
42 static videomode_size_t wlist[3] = {
43 { 320, 200, 0 },
44 { 640, 400, 0 },
45 { 800, 600, 0 },
46 };
47 static videomode_t vlist = {
48 3,
49 wlist
50 };
52 static byte *buf = NULL;
53 static int buf_w = 0;
54 static int buf_h = 0;
55 static struct rgb_pal {
56 byte b, g, r, a;
57 } rgbpal[256];
59 static const cfg_t arg[] = {
60 {"file", NULL, Y_FILES},
61 {"cheat", &cheat, Y_SW_ON},
62 // {"vga", &shot_vga, Y_SW_ON},
63 // {"musvol", &mus_vol, Y_WORD},
64 {"mon", &nomon, Y_SW_OFF},
65 {"warp", &_warp, Y_BYTE},
66 // {"config", NULL, cfg_file, Y_STRING},
67 {NULL, NULL, 0} // end
68 };
70 static const cfg_t cfg[] = {
71 // {"screenshot", &shot_vga, Y_SW_ON},
72 // {"music_volume", &mus_vol, Y_WORD},
73 // {"music_random", &music_random, Y_SW_ON},
74 // {"music_time", &music_time, Y_DWORD},
75 // {"music_fade", &music_fade, Y_DWORD},
76 {"pl1_left", &pl1.kl, Y_KEY},
77 {"pl1_right",&pl1.kr, Y_KEY},
78 {"pl1_up", &pl1.ku, Y_KEY},
79 {"pl1_down", &pl1.kd, Y_KEY},
80 {"pl1_jump", &pl1.kj, Y_KEY},
81 {"pl1_fire", &pl1.kf, Y_KEY},
82 {"pl1_next", &pl1.kwr, Y_KEY},
83 {"pl1_prev", &pl1.kwl, Y_KEY},
84 {"pl1_use", &pl1.kp, Y_KEY},
85 {"pl2_left", &pl2.kl, Y_KEY},
86 {"pl2_right", &pl2.kr, Y_KEY},
87 {"pl2_up", &pl2.ku, Y_KEY},
88 {"pl2_down", &pl2.kd, Y_KEY},
89 {"pl2_jump", &pl2.kj, Y_KEY},
90 {"pl2_fire", &pl2.kf, Y_KEY},
91 {"pl2_next", &pl2.kwr, Y_KEY},
92 {"pl2_prev", &pl2.kwl, Y_KEY},
93 {"pl2_use", &pl2.kp, Y_KEY},
94 {NULL, NULL, 0} // end
95 };
97 static void CFG_args (int argc, char **argv) {
98 const cfg_t *list[] = { arg, R_args(), S_args(), MUS_args() };
99 ARG_parse(argc, argv, 4, list);
102 static void CFG_load (void) {
103 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
104 CFG_read_config("default.cfg", 4, list);
105 CFG_read_config("doom2d.cfg", 4, list);
108 static void CFG_save (void) {
109 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
110 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
113 /* --- error.h --- */
115 void logo (const char *s, ...) {
116 va_list ap;
117 va_start(ap, s);
118 vprintf(s, ap);
119 va_end(ap);
120 // fflush(stdout);
123 void logo_gas (int cur, int all) {
124 // stub
127 void ERR_failinit (char *s, ...) {
128 va_list ap;
129 va_start(ap, s);
130 vprintf(s, ap);
131 va_end(ap);
132 puts("");
133 while (1) ;
134 // exit(1);
137 void ERR_fatal (char *s, ...) {
138 va_list ap;
139 R_done();
140 MUS_done();
141 S_done();
142 M_shutdown();
143 puts("\nCRITICAL ERROR:");
144 va_start(ap, s);
145 vprintf(s, ap);
146 va_end(ap);
147 puts("");
148 while (1) ;
149 // exit(1);
152 void ERR_quit (void) {
153 quit = 1;
156 /* --- system.h --- */
158 static int Y_resize_window (int w, int h, int fullscreen) {
159 assert(w > 0);
160 assert(h > 0);
161 if (buf != NULL) {
162 return Y_set_videomode_software(w, h, fullscreen);
164 return 0;
167 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
168 assert(w > 0);
169 assert(h > 0);
170 // TODO
171 return 0;
174 static void SetupWindow (int w, int h, const char *title) {
175 int flags = KOS32_WIN_FLAG_CAPTION | KOS32_WIN_FLAG_RELATIVE | KOS32_WIN_FLAG_NOFILL;
176 int skin_h = GetSkinHeight();
177 CreateWindow(0, 0, w + 5*2, h + 5 + skin_h, KOS32_WIN_STYLE_FIXED, flags, 0x000000, 0x000000, title);
180 int Y_set_videomode_software (int w, int h, int fullscreen) {
181 assert(w > 0);
182 assert(h > 0);
183 int size = w * h;
184 byte *new_buf = malloc(size);
185 if (new_buf != NULL) {
186 Y_unset_videomode();
187 memset(new_buf, 0, size);
188 buf = new_buf;
189 buf_w = w;
190 buf_h = h;
191 BeginDraw();
192 SetupWindow(w, h, "Doom2D (software render)");
193 EndDraw();
195 return buf != NULL;
198 void Y_get_videomode (int *w, int *h) {
199 *w = buf_w;
200 *h = buf_h;
203 int Y_videomode_setted (void) {
204 return buf != NULL;
207 void Y_unset_videomode (void) {
208 if (buf != NULL) {
209 free(buf);
210 buf = NULL;
211 buf_w = 0;
212 buf_h = 0;
216 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
217 return &vlist;
220 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
221 return &vlist;
224 void Y_set_fullscreen (int yes) {
225 // TODO
228 int Y_get_fullscreen (void) {
229 // TODO
230 return 0;
233 void Y_swap_buffers (void) {
234 // TODO
237 void Y_get_buffer (byte **buf_ref, int *w, int *h, int *pitch) {
238 assert(buf_ref != NULL);
239 assert(w != NULL);
240 assert(h != NULL);
241 assert(pitch != NULL);
242 *buf_ref = buf;
243 *w = buf_w;
244 *h = buf_h;
245 *pitch = buf_w;
248 void Y_set_vga_palette (byte *vgapal) {
249 int i;
250 byte *p = vgapal;
251 assert(buf != NULL);
252 assert(vgapal != NULL);
253 for (i = 0; i < 256; i++) {
254 rgbpal[i].r = p[0] * 255 / 63;
255 rgbpal[i].g = p[1] * 255 / 63;
256 rgbpal[i].b = p[2] * 255 / 63;
257 p += 3;
261 void Y_repaint_rect (int x, int y, int w, int h) {
262 Y_repaint();
265 void Y_repaint (void) {
266 assert(buf != NULL);
267 BeginDraw();
268 SetupWindow(buf_w, buf_h, NULL);
269 PutImageExt(buf, buf_w, buf_h, 0, 0, 8, rgbpal, 0);
270 EndDraw();
273 void Y_enable_text_input (void) {
274 SetInputMode(KOS32_INPUT_MODE_ASCII);
277 void Y_disable_text_input (void) {
278 SetInputMode(KOS32_INPUT_MODE_SCANCODE);
281 /* --- main --- */
283 static int scancode_to_key (int scancode) {
284 switch (scancode) {
285 case KOS32_SC_0: return KEY_0;
286 case KOS32_SC_1: return KEY_1;
287 case KOS32_SC_2: return KEY_2;
288 case KOS32_SC_3: return KEY_3;
289 case KOS32_SC_4: return KEY_4;
290 case KOS32_SC_5: return KEY_5;
291 case KOS32_SC_6: return KEY_6;
292 case KOS32_SC_7: return KEY_7;
293 case KOS32_SC_8: return KEY_8;
294 case KOS32_SC_9: return KEY_9;
295 case KOS32_SC_A: return KEY_A;
296 case KOS32_SC_B: return KEY_B;
297 case KOS32_SC_C: return KEY_C;
298 case KOS32_SC_D: return KEY_D;
299 case KOS32_SC_E: return KEY_E;
300 case KOS32_SC_F: return KEY_F;
301 case KOS32_SC_G: return KEY_G;
302 case KOS32_SC_H: return KEY_H;
303 case KOS32_SC_I: return KEY_I;
304 case KOS32_SC_J: return KEY_J;
305 case KOS32_SC_K: return KEY_K;
306 case KOS32_SC_L: return KEY_L;
307 case KOS32_SC_M: return KEY_M;
308 case KOS32_SC_N: return KEY_N;
309 case KOS32_SC_O: return KEY_O;
310 case KOS32_SC_P: return KEY_P;
311 case KOS32_SC_Q: return KEY_Q;
312 case KOS32_SC_R: return KEY_R;
313 case KOS32_SC_S: return KEY_S;
314 case KOS32_SC_T: return KEY_T;
315 case KOS32_SC_U: return KEY_U;
316 case KOS32_SC_V: return KEY_V;
317 case KOS32_SC_W: return KEY_W;
318 case KOS32_SC_X: return KEY_X;
319 case KOS32_SC_Y: return KEY_Y;
320 case KOS32_SC_Z: return KEY_Z;
321 case KOS32_SC_RETURN: return KEY_RETURN;
322 case KOS32_SC_ESCAPE: return KEY_ESCAPE;
323 case KOS32_SC_BACKSPACE: return KEY_BACKSPACE;
324 case KOS32_SC_TAB: return KEY_TAB;
325 case KOS32_SC_SPACE: return KEY_SPACE;
326 case KOS32_SC_MINUS: return KEY_MINUS;
327 case KOS32_SC_EQUALS: return KEY_EQUALS;
328 case KOS32_SC_LEFTBRACKET: return KEY_LEFTBRACKET;
329 case KOS32_SC_RIGHTBRACKET: return KEY_RIGHTBRACKET;
330 case KOS32_SC_BACKSLASH: return KEY_BACKSLASH;
331 case KOS32_SC_SEMICOLON: return KEY_SEMICOLON;
332 case KOS32_SC_APOSTROPHE: return KEY_APOSTROPHE;
333 case KOS32_SC_GRAVE: return KEY_GRAVE;
334 case KOS32_SC_COMMA: return KEY_COMMA;
335 case KOS32_SC_PERIOD: return KEY_PERIOD;
336 case KOS32_SC_SLASH: return KEY_SLASH;
337 case KOS32_SC_CAPSLOCK: return KEY_CAPSLOCK;
338 case KOS32_SC_F1: return KEY_F1;
339 case KOS32_SC_F2: return KEY_F2;
340 case KOS32_SC_F3: return KEY_F3;
341 case KOS32_SC_F4: return KEY_F4;
342 case KOS32_SC_F5: return KEY_F5;
343 case KOS32_SC_F6: return KEY_F6;
344 case KOS32_SC_F7: return KEY_F7;
345 case KOS32_SC_F8: return KEY_F8;
346 case KOS32_SC_F9: return KEY_F9;
347 case KOS32_SC_F10: return KEY_F10;
348 case KOS32_SC_F11: return KEY_F11;
349 case KOS32_SC_F12: return KEY_F12;
350 case KOS32_SC_SCROLLLOCK: return KEY_SCROLLLOCK;
351 case KOS32_SC_NUMLOCK: return KEY_NUMLOCK;
352 case KOS32_SC_KP_MULTIPLY: return KEY_KP_MULTIPLY;
353 case KOS32_SC_KP_MINUS: return KEY_KP_MINUS;
354 case KOS32_SC_KP_PLUS: return KEY_KP_PLUS;
355 case KOS32_SC_KP_0: return KEY_KP_0;
356 case KOS32_SC_KP_1: return KEY_KP_1;
357 case KOS32_SC_KP_2: return KEY_KP_2;
358 case KOS32_SC_KP_3: return KEY_KP_3;
359 case KOS32_SC_KP_4: return KEY_KP_4;
360 case KOS32_SC_KP_5: return KEY_KP_5;
361 case KOS32_SC_KP_6: return KEY_KP_6;
362 case KOS32_SC_KP_7: return KEY_KP_7;
363 case KOS32_SC_KP_8: return KEY_KP_8;
364 case KOS32_SC_KP_9: return KEY_KP_9;
365 case KOS32_SC_KP_PERIOD: return KEY_KP_PERIOD;
366 case KOS32_SC_LCTRL: return KEY_LCTRL;
367 case KOS32_SC_LSHIFT: return KEY_LSHIFT;
368 case KOS32_SC_LALT: return KEY_LALT;
369 default: return KEY_UNKNOWN;
373 static int ext_scancode_to_key (int scancode) {
374 switch (scancode) {
375 case KOS32_SC_INSERT: return KEY_INSERT;
376 case KOS32_SC_HOME: return KEY_HOME;
377 case KOS32_SC_PAGEUP: return KEY_PAGEUP;
378 case KOS32_SC_DELETE: return KEY_DELETE;
379 case KOS32_SC_END: return KEY_END;
380 case KOS32_SC_PAGEDOWN: return KEY_PAGEDOWN;
381 case KOS32_SC_RIGHT: return KEY_RIGHT;
382 case KOS32_SC_LEFT: return KEY_LEFT;
383 case KOS32_SC_DOWN: return KEY_DOWN;
384 case KOS32_SC_UP: return KEY_UP;
385 case KOS32_SC_KP_DIVIDE: return KEY_KP_DIVIDE;
386 case KOS32_SC_KP_ENTER: return KEY_KP_ENTER;
387 case KOS32_SC_LSUPER: return KEY_LSUPER;
388 case KOS32_SC_RCTRL: return KEY_RCTRL;
389 case KOS32_SC_RSHIFT: return KEY_RSHIFT;
390 case KOS32_SC_RALT: return KEY_RALT;
391 case KOS32_SC_RSUPER: return KEY_RSUPER;
392 default: return KEY_UNKNOWN;
396 static void handle_scancode (int code) {
397 static enum {
398 ST_std, ST_ext,
399 ST_print_down_1, ST_print_down_2,
400 ST_print_up_1, ST_print_up_2,
401 ST_pause_1, ST_pause_2, ST_pause_3, ST_pause_4, ST_pause_5,
402 ST_ok
403 } state = ST_std;
404 int k, down;
405 // logo("scancode >> 0x%x\n", code);
406 switch (state) {
407 case ST_std:
408 if (code == KOS32_SC_EXTENDED) {
409 state = ST_ext;
410 } else if (code == KOS32_SC_EXTENDED_PAUSE) {
411 state = ST_pause_1;
412 } else {
413 state = ST_ok;
414 k = scancode_to_key(code & 0x7f);
415 down = !((code >> 7) & 1);
417 break;
418 case ST_ext:
419 if (code == 0x2A) {
420 state = ST_print_down_1;
421 } else if (code == 0xB7) {
422 state = ST_print_up_1;
423 } else {
424 state = ST_ok;
425 k = ext_scancode_to_key(code & 0x7f);
426 down = !((code >> 7) & 1);
428 break;
429 case ST_print_down_1:
430 assert(code == 0xE0);
431 state = ST_print_down_2;
432 break;
433 case ST_print_down_2:
434 assert(code == 0x37);
435 state = ST_ok;
436 k = KEY_PRINTSCREEN;
437 down = 1;
438 break;
439 case ST_print_up_1:
440 assert(code == 0xE0);
441 state = ST_print_up_2;
442 break;
443 case ST_print_up_2:
444 assert(code == 0xAA);
445 state = ST_ok;
446 k = KEY_PRINTSCREEN;
447 down = 0;
448 break;
449 case ST_pause_1:
450 assert(code == 0x1D);
451 state = ST_pause_2;
452 break;
453 case ST_pause_2:
454 assert(code == 0x45);
455 state = ST_pause_3;
456 break;
457 case ST_pause_3:
458 assert(code == 0xE1);
459 state = ST_pause_4;
460 break;
461 case ST_pause_4:
462 assert(code == 0x9D);
463 state = ST_pause_5;
464 break;
465 case ST_pause_5:
466 assert(code == 0xC5);
467 state = ST_ok;
468 k = KEY_PAUSE;
469 down = 1;
470 break;
471 default:
472 ERR_fatal("handle_scancode: invalid state: %i\n", state);
474 if (state == ST_ok) {
475 state = ST_std;
476 // logo("key: %s (%i, %s)\n", I_key_to_string(k), k, down ? "down" : "up");
477 I_press(k, down);
478 GM_key(k, down);
479 if (k = KEY_PAUSE) {
480 I_press(k, 0);
481 GM_key(k, 0);
486 static void poll_events (void) {
487 int ev, key, button, code, ch, k;
488 while((ev = CheckEvent()) != KOS32_EVENT_NONE) {
489 switch (ev) {
490 case KOS32_EVENT_REDRAW:
491 if (buf != NULL) {
492 Y_repaint(); /* redraw window */
494 break;
495 case KOS32_EVENT_KEYBOARD:
496 key = GetKey();
497 if ((key & 0xff) == 0) {
498 switch (GetInputMode()) {
499 case KOS32_INPUT_MODE_ASCII:
500 ch = (key >> 8) & 0xff;
501 code = (key >> 16) & 0x7f;
502 k = scancode_to_key(code);
503 I_press(k, 1);
504 GM_key(k, 1);
505 GM_input(ch);
506 I_press(k, 0);
507 GM_key(k, 0);
508 break;
509 case KOS32_INPUT_MODE_SCANCODE:
510 code = key >> 8;
511 handle_scancode(code);
512 break;
515 break;
516 case KOS32_EVENT_BUTTON:
517 button = GetButton();
518 quit = button == 256; /* close button + left click */
519 break;
520 default:
521 ERR_fatal("poll_events: unhandled event: %i\n", ev);
526 static void game_loop (void) {
527 static long ticks; /* ns */
528 ticks = GetTimeCountPro();
529 while (!quit) {
530 poll_events();
531 MUS_update();
532 long t = GetTimeCountPro(); /* ns */
533 int n = (t - ticks) / ((DELAY + 1) * 1000000);
534 ticks = ticks + n * ((DELAY + 1) * 1000000);
535 if (n > 0) {
536 while (n) {
537 G_act();
538 n -= 1;
540 R_draw();
542 Delay(1);
546 int main (int argc, char **argv) {
547 CFG_args(argc, argv);
548 logo("system: initialize engine\n");
549 SetEventsMask(KOS32_EVENT_FLAG_REDRAW | KOS32_EVENT_FLAG_KEYBOARD | KOS32_EVENT_FLAG_BUTTON);
550 Y_disable_text_input();
551 // Player 1 defaults
552 pl1.ku = KEY_KP_8;
553 pl1.kd = KEY_KP_5;
554 pl1.kl = KEY_KP_4;
555 pl1.kr = KEY_KP_6;
556 pl1.kf = KEY_PAGEDOWN;
557 pl1.kj = KEY_DELETE;
558 pl1.kwl = KEY_HOME;
559 pl1.kwr = KEY_END;
560 pl1.kp = KEY_KP_8;
561 // Player 2 defaults
562 pl2.ku = KEY_E;
563 pl2.kd = KEY_D;
564 pl2.kl = KEY_S;
565 pl2.kr = KEY_F;
566 pl2.kf = KEY_A;
567 pl2.kj = KEY_Q;
568 pl2.kwl = KEY_1;
569 pl2.kwr = KEY_2;
570 pl2.kp = KEY_E;
571 srand(GetIdleCount());
572 F_startup();
573 CFG_load();
574 F_addwad("doom2d.wad");
575 F_initwads();
576 M_startup();
577 F_allocres();
578 S_init();
579 MUS_init();
580 R_init();
581 G_init();
582 logo("system: game loop\n");
583 game_loop();
584 logo("system: finalize engine\n");
585 CFG_save();
586 R_done();
587 MUS_done();
588 S_done();
589 M_shutdown();
590 logo("system: halt\n");
591 return 0;