DEADSOFTWARE

75ddd86ab9df9c323397df5feea91f3367a35635
[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[] = { arg, R_args(), S_args(), MUS_args() };
97 ARG_parse(argc, argv, 4, list);
98 }
100 static void CFG_load (void) {
101 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
102 CFG_read_config("default.cfg", 4, list);
103 CFG_read_config("doom2d.cfg", 4, list);
106 static void CFG_save (void) {
107 const cfg_t *list[] = { cfg, R_conf(), S_conf(), MUS_conf() };
108 CFG_update_config("doom2d.cfg", "doom2d.cfg", 4, list, "generated by doom2d, do not modify");
111 /* --- error.h --- */
113 void logo (const char *s, ...) {
114 va_list ap;
115 va_start(ap, s);
116 vprintf(s, ap);
117 va_end(ap);
118 fflush(stdout);
121 void logo_gas (int cur, int all) {
122 // stub
125 void ERR_failinit (char *s, ...) {
126 va_list ap;
127 va_start(ap, s);
128 vprintf(s, ap);
129 va_end(ap);
130 puts("");
131 abort();
134 void ERR_fatal (char *s, ...) {
135 va_list ap;
136 R_done();
137 MUS_done();
138 S_done();
139 puts("\nCRITICAL ERROR:");
140 va_start(ap, s);
141 vprintf(s, ap);
142 va_end(ap);
143 puts("");
144 abort();
147 void ERR_quit (void) {
148 quit = 1;
151 /* --- system.h --- */
153 int Y_set_videomode_opengl (int w, int h, int fullscreen) {
154 assert(w > 0);
155 assert(h > 0);
156 mode = MODE_OPENGL;
157 width = w;
158 height = h;
159 return 1;
162 int Y_set_videomode_software (int w, int h, int fullscreen) {
163 assert(w > 0);
164 assert(h > 0);
165 void *buf = softbuffer ? realloc(softbuffer, w * h) : malloc(w * h);
166 if (buf) {
167 mode = MODE_SOFTWARE;
168 softbuffer = buf;
169 width = w;
170 height = h;
172 return buf != NULL;
175 const videomode_t *Y_get_videomode_list_opengl (int fullscreen) {
176 return &vlist;
179 const videomode_t *Y_get_videomode_list_software (int fullscreen) {
180 return &vlist;
183 void Y_get_videomode (int *w, int *h) {
184 if (mode != MODE_NONE) {
185 *w = width;
186 *h = height;
187 } else {
188 *w = 0;
189 *h = 0;
193 int Y_videomode_setted (void) {
194 return mode != MODE_NONE;
197 void Y_unset_videomode (void) {
198 mode = MODE_NONE;
199 free(softbuffer);
200 softbuffer = NULL;
203 void Y_set_fullscreen (int yes) {
204 fullscreen = yes;
207 int Y_get_fullscreen (void) {
208 return (mode != MODE_NONE) && fullscreen;
211 void Y_swap_buffers (void) {
212 assert(mode == MODE_OPENGL);
215 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
216 assert(mode == MODE_SOFTWARE);
217 // *buf = surf->pixels;
218 *buf = NULL; // fix this
219 *w = width;
220 *h = height;
221 *pitch = width;
224 void Y_set_vga_palette (byte *vgapal) {
225 int i;
226 byte *p = vgapal;
227 assert(vgapal != NULL);
228 assert(mode == MODE_SOFTWARE);
231 void Y_repaint_rect (int x, int y, int w, int h) {
232 assert(mode == MODE_SOFTWARE);
235 void Y_repaint (void) {
236 assert(mode == MODE_SOFTWARE);
239 void Y_enable_text_input (void) {
240 text_input = 1;
243 void Y_disable_text_input (void) {
244 text_input = 0;
247 /* --- main --- */
249 static void poll_events (void) {
250 // stub
253 static void step (void) {
254 poll_events();
255 MUS_update();
256 clock_t t = clock();
257 if ((t - ticks) * 1000 / CLOCKS_PER_SEC > DELAY) {
258 ticks = t;
259 G_act();
261 R_draw();
264 int main (int argc, char *argv[]) {
265 logo("main: initialize\n");
266 // Player 1 defaults
267 pl1.ku = KEY_KP_8;
268 pl1.kd = KEY_KP_5;
269 pl1.kl = KEY_KP_4;
270 pl1.kr = KEY_KP_6;
271 pl1.kf = KEY_PAGEDOWN;
272 pl1.kj = KEY_DELETE;
273 pl1.kwl = KEY_HOME;
274 pl1.kwr = KEY_END;
275 pl1.kp = KEY_KP_8;
276 // Player 2 defaults
277 pl2.ku = KEY_E;
278 pl2.kd = KEY_D;
279 pl2.kl = KEY_S;
280 pl2.kr = KEY_F;
281 pl2.kf = KEY_A;
282 pl2.kj = KEY_Q;
283 pl2.kwl = KEY_1;
284 pl2.kwr = KEY_2;
285 pl2.kp = KEY_E;
286 //srand(SDL_GetTicks());
287 F_addwad("doom2d.wad");
288 CFG_args(argc, argv);
289 CFG_load();
290 F_initwads();
291 S_init();
292 MUS_init();
293 R_init();
294 G_init();
295 ticks = clock();
296 #ifdef __EMSCRIPTEN__
297 emscripten_set_main_loop(step, 0, 1);
298 #else
299 while (!quit) {
300 step();
302 #endif
303 CFG_save();
304 R_done();
305 MUS_done();
306 S_done();
307 return 0;