DEADSOFTWARE

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