DEADSOFTWARE

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