DEADSOFTWARE

files: cleanup
[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 puts("\nCRITICAL ERROR:");
142 va_start(ap, s);
143 vprintf(s, ap);
144 va_end(ap);
145 puts("");
146 while (1) ;
147 // exit(1);
150 void ERR_quit (void) {
151 quit = 1;
154 /* --- system.h --- */
156 static int Y_resize_window (int w, int h, int fullscreen) {
157 assert(w > 0);
158 assert(h > 0);
159 if (buf != NULL) {
160 return Y_set_videomode_software(w, h, fullscreen);
162 return 0;
165 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
166 assert(w > 0);
167 assert(h > 0);
168 // TODO
169 return 0;
172 static void SetupWindow (int w, int h, const char *title) {
173 int flags = KOS32_WIN_FLAG_CAPTION | KOS32_WIN_FLAG_RELATIVE | KOS32_WIN_FLAG_NOFILL;
174 int skin_h = GetSkinHeight();
175 CreateWindow(0, 0, w + 5*2, h + 5 + skin_h, KOS32_WIN_STYLE_FIXED, flags, 0x000000, 0x000000, title);
178 int Y_set_videomode_software (int w, int h, int fullscreen) {
179 assert(w > 0);
180 assert(h > 0);
181 int size = w * h;
182 byte *new_buf = malloc(size);
183 if (new_buf != NULL) {
184 Y_unset_videomode();
185 memset(new_buf, 0, size);
186 buf = new_buf;
187 buf_w = w;
188 buf_h = h;
189 BeginDraw();
190 SetupWindow(w, h, "Doom2D (software render)");
191 EndDraw();
193 return buf != NULL;
196 void Y_get_videomode (int *w, int *h) {
197 *w = buf_w;
198 *h = buf_h;
201 int Y_videomode_setted (void) {
202 return buf != NULL;
205 void Y_unset_videomode (void) {
206 if (buf != NULL) {
207 free(buf);
208 buf = NULL;
209 buf_w = 0;
210 buf_h = 0;
214 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
215 return &vlist;
218 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
219 return &vlist;
222 void Y_set_fullscreen (int yes) {
223 // TODO
226 int Y_get_fullscreen (void) {
227 // TODO
228 return 0;
231 void Y_swap_buffers (void) {
232 // TODO
235 void Y_get_buffer (byte **buf_ref, int *w, int *h, int *pitch) {
236 assert(buf_ref != NULL);
237 assert(w != NULL);
238 assert(h != NULL);
239 assert(pitch != NULL);
240 *buf_ref = buf;
241 *w = buf_w;
242 *h = buf_h;
243 *pitch = buf_w;
246 void Y_set_vga_palette (byte *vgapal) {
247 int i;
248 byte *p = vgapal;
249 assert(buf != NULL);
250 assert(vgapal != NULL);
251 for (i = 0; i < 256; i++) {
252 rgbpal[i].r = p[0] * 255 / 63;
253 rgbpal[i].g = p[1] * 255 / 63;
254 rgbpal[i].b = p[2] * 255 / 63;
255 p += 3;
259 void Y_repaint_rect (int x, int y, int w, int h) {
260 Y_repaint();
263 void Y_repaint (void) {
264 assert(buf != NULL);
265 BeginDraw();
266 SetupWindow(buf_w, buf_h, NULL);
267 PutImageExt(buf, buf_w, buf_h, 0, 0, 8, rgbpal, 0);
268 EndDraw();
271 void Y_enable_text_input (void) {
272 SetInputMode(KOS32_INPUT_MODE_ASCII);
275 void Y_disable_text_input (void) {
276 SetInputMode(KOS32_INPUT_MODE_SCANCODE);
279 /* --- main --- */
281 static int scancode_to_key (int scancode) {
282 switch (scancode) {
283 case KOS32_SC_0: return KEY_0;
284 case KOS32_SC_1: return KEY_1;
285 case KOS32_SC_2: return KEY_2;
286 case KOS32_SC_3: return KEY_3;
287 case KOS32_SC_4: return KEY_4;
288 case KOS32_SC_5: return KEY_5;
289 case KOS32_SC_6: return KEY_6;
290 case KOS32_SC_7: return KEY_7;
291 case KOS32_SC_8: return KEY_8;
292 case KOS32_SC_9: return KEY_9;
293 case KOS32_SC_A: return KEY_A;
294 case KOS32_SC_B: return KEY_B;
295 case KOS32_SC_C: return KEY_C;
296 case KOS32_SC_D: return KEY_D;
297 case KOS32_SC_E: return KEY_E;
298 case KOS32_SC_F: return KEY_F;
299 case KOS32_SC_G: return KEY_G;
300 case KOS32_SC_H: return KEY_H;
301 case KOS32_SC_I: return KEY_I;
302 case KOS32_SC_J: return KEY_J;
303 case KOS32_SC_K: return KEY_K;
304 case KOS32_SC_L: return KEY_L;
305 case KOS32_SC_M: return KEY_M;
306 case KOS32_SC_N: return KEY_N;
307 case KOS32_SC_O: return KEY_O;
308 case KOS32_SC_P: return KEY_P;
309 case KOS32_SC_Q: return KEY_Q;
310 case KOS32_SC_R: return KEY_R;
311 case KOS32_SC_S: return KEY_S;
312 case KOS32_SC_T: return KEY_T;
313 case KOS32_SC_U: return KEY_U;
314 case KOS32_SC_V: return KEY_V;
315 case KOS32_SC_W: return KEY_W;
316 case KOS32_SC_X: return KEY_X;
317 case KOS32_SC_Y: return KEY_Y;
318 case KOS32_SC_Z: return KEY_Z;
319 case KOS32_SC_RETURN: return KEY_RETURN;
320 case KOS32_SC_ESCAPE: return KEY_ESCAPE;
321 case KOS32_SC_BACKSPACE: return KEY_BACKSPACE;
322 case KOS32_SC_TAB: return KEY_TAB;
323 case KOS32_SC_SPACE: return KEY_SPACE;
324 case KOS32_SC_MINUS: return KEY_MINUS;
325 case KOS32_SC_EQUALS: return KEY_EQUALS;
326 case KOS32_SC_LEFTBRACKET: return KEY_LEFTBRACKET;
327 case KOS32_SC_RIGHTBRACKET: return KEY_RIGHTBRACKET;
328 case KOS32_SC_BACKSLASH: return KEY_BACKSLASH;
329 case KOS32_SC_SEMICOLON: return KEY_SEMICOLON;
330 case KOS32_SC_APOSTROPHE: return KEY_APOSTROPHE;
331 case KOS32_SC_GRAVE: return KEY_GRAVE;
332 case KOS32_SC_COMMA: return KEY_COMMA;
333 case KOS32_SC_PERIOD: return KEY_PERIOD;
334 case KOS32_SC_SLASH: return KEY_SLASH;
335 case KOS32_SC_CAPSLOCK: return KEY_CAPSLOCK;
336 case KOS32_SC_F1: return KEY_F1;
337 case KOS32_SC_F2: return KEY_F2;
338 case KOS32_SC_F3: return KEY_F3;
339 case KOS32_SC_F4: return KEY_F4;
340 case KOS32_SC_F5: return KEY_F5;
341 case KOS32_SC_F6: return KEY_F6;
342 case KOS32_SC_F7: return KEY_F7;
343 case KOS32_SC_F8: return KEY_F8;
344 case KOS32_SC_F9: return KEY_F9;
345 case KOS32_SC_F10: return KEY_F10;
346 case KOS32_SC_F11: return KEY_F11;
347 case KOS32_SC_F12: return KEY_F12;
348 case KOS32_SC_SCROLLLOCK: return KEY_SCROLLLOCK;
349 case KOS32_SC_NUMLOCK: return KEY_NUMLOCK;
350 case KOS32_SC_KP_MULTIPLY: return KEY_KP_MULTIPLY;
351 case KOS32_SC_KP_MINUS: return KEY_KP_MINUS;
352 case KOS32_SC_KP_PLUS: return KEY_KP_PLUS;
353 case KOS32_SC_KP_0: return KEY_KP_0;
354 case KOS32_SC_KP_1: return KEY_KP_1;
355 case KOS32_SC_KP_2: return KEY_KP_2;
356 case KOS32_SC_KP_3: return KEY_KP_3;
357 case KOS32_SC_KP_4: return KEY_KP_4;
358 case KOS32_SC_KP_5: return KEY_KP_5;
359 case KOS32_SC_KP_6: return KEY_KP_6;
360 case KOS32_SC_KP_7: return KEY_KP_7;
361 case KOS32_SC_KP_8: return KEY_KP_8;
362 case KOS32_SC_KP_9: return KEY_KP_9;
363 case KOS32_SC_KP_PERIOD: return KEY_KP_PERIOD;
364 case KOS32_SC_LCTRL: return KEY_LCTRL;
365 case KOS32_SC_LSHIFT: return KEY_LSHIFT;
366 case KOS32_SC_LALT: return KEY_LALT;
367 default: return KEY_UNKNOWN;
371 static int ext_scancode_to_key (int scancode) {
372 switch (scancode) {
373 case KOS32_SC_INSERT: return KEY_INSERT;
374 case KOS32_SC_HOME: return KEY_HOME;
375 case KOS32_SC_PAGEUP: return KEY_PAGEUP;
376 case KOS32_SC_DELETE: return KEY_DELETE;
377 case KOS32_SC_END: return KEY_END;
378 case KOS32_SC_PAGEDOWN: return KEY_PAGEDOWN;
379 case KOS32_SC_RIGHT: return KEY_RIGHT;
380 case KOS32_SC_LEFT: return KEY_LEFT;
381 case KOS32_SC_DOWN: return KEY_DOWN;
382 case KOS32_SC_UP: return KEY_UP;
383 case KOS32_SC_KP_DIVIDE: return KEY_KP_DIVIDE;
384 case KOS32_SC_KP_ENTER: return KEY_KP_ENTER;
385 case KOS32_SC_LSUPER: return KEY_LSUPER;
386 case KOS32_SC_RCTRL: return KEY_RCTRL;
387 case KOS32_SC_RSHIFT: return KEY_RSHIFT;
388 case KOS32_SC_RALT: return KEY_RALT;
389 case KOS32_SC_RSUPER: return KEY_RSUPER;
390 default: return KEY_UNKNOWN;
394 static void handle_scancode (int code) {
395 static enum {
396 ST_std, ST_ext,
397 ST_print_down_1, ST_print_down_2,
398 ST_print_up_1, ST_print_up_2,
399 ST_pause_1, ST_pause_2, ST_pause_3, ST_pause_4, ST_pause_5,
400 ST_ok
401 } state = ST_std;
402 int k, down;
403 // logo("scancode >> 0x%x\n", code);
404 switch (state) {
405 case ST_std:
406 if (code == KOS32_SC_EXTENDED) {
407 state = ST_ext;
408 } else if (code == KOS32_SC_EXTENDED_PAUSE) {
409 state = ST_pause_1;
410 } else {
411 state = ST_ok;
412 k = scancode_to_key(code & 0x7f);
413 down = !((code >> 7) & 1);
415 break;
416 case ST_ext:
417 if (code == 0x2A) {
418 state = ST_print_down_1;
419 } else if (code == 0xB7) {
420 state = ST_print_up_1;
421 } else {
422 state = ST_ok;
423 k = ext_scancode_to_key(code & 0x7f);
424 down = !((code >> 7) & 1);
426 break;
427 case ST_print_down_1:
428 assert(code == 0xE0);
429 state = ST_print_down_2;
430 break;
431 case ST_print_down_2:
432 assert(code == 0x37);
433 state = ST_ok;
434 k = KEY_PRINTSCREEN;
435 down = 1;
436 break;
437 case ST_print_up_1:
438 assert(code == 0xE0);
439 state = ST_print_up_2;
440 break;
441 case ST_print_up_2:
442 assert(code == 0xAA);
443 state = ST_ok;
444 k = KEY_PRINTSCREEN;
445 down = 0;
446 break;
447 case ST_pause_1:
448 assert(code == 0x1D);
449 state = ST_pause_2;
450 break;
451 case ST_pause_2:
452 assert(code == 0x45);
453 state = ST_pause_3;
454 break;
455 case ST_pause_3:
456 assert(code == 0xE1);
457 state = ST_pause_4;
458 break;
459 case ST_pause_4:
460 assert(code == 0x9D);
461 state = ST_pause_5;
462 break;
463 case ST_pause_5:
464 assert(code == 0xC5);
465 state = ST_ok;
466 k = KEY_PAUSE;
467 down = 1;
468 break;
469 default:
470 ERR_fatal("handle_scancode: invalid state: %i\n", state);
472 if (state == ST_ok) {
473 state = ST_std;
474 // logo("key: %s (%i, %s)\n", I_key_to_string(k), k, down ? "down" : "up");
475 I_press(k, down);
476 GM_key(k, down);
477 if (k = KEY_PAUSE) {
478 I_press(k, 0);
479 GM_key(k, 0);
484 static void poll_events (void) {
485 int ev, key, button, code, ch, k;
486 while((ev = CheckEvent()) != KOS32_EVENT_NONE) {
487 switch (ev) {
488 case KOS32_EVENT_REDRAW:
489 if (buf != NULL) {
490 Y_repaint(); /* redraw window */
492 break;
493 case KOS32_EVENT_KEYBOARD:
494 key = GetKey();
495 if ((key & 0xff) == 0) {
496 switch (GetInputMode()) {
497 case KOS32_INPUT_MODE_ASCII:
498 ch = (key >> 8) & 0xff;
499 code = (key >> 16) & 0x7f;
500 k = scancode_to_key(code);
501 I_press(k, 1);
502 GM_key(k, 1);
503 GM_input(ch);
504 I_press(k, 0);
505 GM_key(k, 0);
506 break;
507 case KOS32_INPUT_MODE_SCANCODE:
508 code = key >> 8;
509 handle_scancode(code);
510 break;
513 break;
514 case KOS32_EVENT_BUTTON:
515 button = GetButton();
516 quit = button == 256; /* close button + left click */
517 break;
518 default:
519 ERR_fatal("poll_events: unhandled event: %i\n", ev);
524 static void game_loop (void) {
525 static long ticks; /* ns */
526 ticks = GetTimeCountPro();
527 while (!quit) {
528 poll_events();
529 MUS_update();
530 long t = GetTimeCountPro(); /* ns */
531 int n = (t - ticks) / ((DELAY + 1) * 1000000);
532 ticks = ticks + n * ((DELAY + 1) * 1000000);
533 if (n > 0) {
534 while (n) {
535 G_act();
536 n -= 1;
538 R_draw();
540 Delay(1);
544 int main (int argc, char **argv) {
545 CFG_args(argc, argv);
546 logo("system: initialize engine\n");
547 SetEventsMask(KOS32_EVENT_FLAG_REDRAW | KOS32_EVENT_FLAG_KEYBOARD | KOS32_EVENT_FLAG_BUTTON);
548 Y_disable_text_input();
549 // Player 1 defaults
550 pl1.ku = KEY_KP_8;
551 pl1.kd = KEY_KP_5;
552 pl1.kl = KEY_KP_4;
553 pl1.kr = KEY_KP_6;
554 pl1.kf = KEY_PAGEDOWN;
555 pl1.kj = KEY_DELETE;
556 pl1.kwl = KEY_HOME;
557 pl1.kwr = KEY_END;
558 pl1.kp = KEY_KP_8;
559 // Player 2 defaults
560 pl2.ku = KEY_E;
561 pl2.kd = KEY_D;
562 pl2.kl = KEY_S;
563 pl2.kr = KEY_F;
564 pl2.kf = KEY_A;
565 pl2.kj = KEY_Q;
566 pl2.kwl = KEY_1;
567 pl2.kwr = KEY_2;
568 pl2.kp = KEY_E;
569 srand(GetIdleCount());
570 CFG_load();
571 F_addwad("doom2d.wad");
572 F_initwads();
573 S_init();
574 MUS_init();
575 R_init();
576 G_init();
577 logo("system: game loop\n");
578 game_loop();
579 logo("system: finalize engine\n");
580 CFG_save();
581 R_done();
582 MUS_done();
583 S_done();
584 logo("system: halt\n");
585 return 0;