DEADSOFTWARE

673b5f9a6f8cbd27f1849af75801ed69e51fef84
[flatwaifu.git] / src / sdl2 / main.c
1 #include <SDL.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <stdlib.h> // srand exit
5 #include <string.h> // strcasecmp
6 #include <assert.h>
7 #include "system.h"
8 #include "input.h"
10 #include "my.h" // fexists
11 #include "player.h" // pl1 pl2
12 #include "menu.h" // G_keyf
13 #include "error.h" // logo
15 #include "files.h" // F_startup F_addwad F_initwads F_allocres
16 #include "config.h" // CFG_args CFG_load CFG_save
17 #include "memory.h" // M_startup
18 #include "game.h" // G_init G_act
19 #include "sound.h" // S_init S_done
20 #include "music.h" // S_initmusic S_updatemusic S_donemusic
21 #include "render.h" // R_init R_draw R_done
23 static int quit = 0;
24 static SDL_Window *window;
25 static SDL_GLContext context;
27 /* --- error.h --- */
29 void logo (const char *s, ...) {
30 va_list ap;
31 va_start(ap, s);
32 vprintf(s, ap);
33 va_end(ap);
34 fflush(stdout);
35 }
37 void logo_gas (int cur, int all) {
38 // stub
39 }
41 void ERR_failinit (char *s, ...) {
42 va_list ap;
43 va_start(ap, s);
44 vprintf(s, ap);
45 va_end(ap);
46 puts("");
47 exit(1);
48 }
50 void ERR_fatal (char *s, ...) {
51 va_list ap;
52 R_done();
53 S_done();
54 S_donemusic();
55 M_shutdown();
56 SDL_Quit();
57 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
58 va_start(ap, s);
59 vprintf(s, ap);
60 va_end(ap);
61 puts("");
62 exit(1);
63 }
65 void ERR_quit (void) {
66 puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
67 //F_loadres(F_getresid("ENDOOM"),p,0,4000);
68 quit = 1;
69 }
71 /* --- system.h --- */
73 int Y_set_videomode (int w, int h, int flags) {
74 assert(w > 0);
75 assert(h > 0);
76 Uint32 f = SDL_WINDOW_HIDDEN;
77 if (flags & SYSTEM_USE_FULLSCREEN) {
78 f = f | SDL_WINDOW_FULLSCREEN;
79 }
80 int res = 0;
81 int x = SDL_WINDOWPOS_CENTERED;
82 int y = SDL_WINDOWPOS_CENTERED;
83 if (flags & SYSTEM_USE_OPENGL) {
84 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
85 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
86 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
87 SDL_Window *win = SDL_CreateWindow("Doom 2D (SDL2)", x, y, w, h, f | SDL_WINDOW_OPENGL);
88 if (win != NULL) {
89 SDL_GLContext ctx = SDL_GL_CreateContext(win);
90 if (ctx != NULL) {
91 Y_unset_videomode();
92 window = win;
93 context = ctx;
94 SDL_GL_MakeCurrent(window, context);
95 SDL_ShowWindow(window);
96 res = 1;
97 } else {
98 SDL_DestroyWindow(win);
99 }
101 } else {
102 // TODO software
104 return res;
107 int Y_videomode_setted (void) {
108 return window != NULL;
111 void Y_unset_videomode (void) {
112 if (window != NULL) {
113 if (context != NULL) {
114 SDL_GL_MakeCurrent(window, NULL);
115 SDL_GL_DeleteContext(context);
116 context = NULL;
118 SDL_DestroyWindow(window);
119 window = NULL;
123 int Y_set_fullscreen (int yes) {
124 if (window != NULL) {
125 SDL_SetWindowFullscreen(window, yes ? SDL_WINDOW_FULLSCREEN : 0);
127 return yes;
130 int Y_get_fullscreen (void) {
131 return (window != NULL) && (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN);
134 void Y_swap_buffers (void) {
135 assert(context != NULL);
136 SDL_GL_SwapWindow(window);
139 void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) {
140 // TODO
143 void Y_set_vga_palette (byte *vgapal) {
144 // TODO
147 void Y_repaint_rect (int x, int y, int w, int h) {
148 // TODO
151 void Y_repaint (void) {
152 // TODO
155 /* --- main --- */
157 static int sdl_to_key (int code) {
158 switch (code) {
159 case SDLK_0: return KEY_0;
160 case SDLK_1: return KEY_1;
161 case SDLK_2: return KEY_2;
162 case SDLK_3: return KEY_3;
163 case SDLK_4: return KEY_4;
164 case SDLK_5: return KEY_5;
165 case SDLK_6: return KEY_6;
166 case SDLK_7: return KEY_7;
167 case SDLK_8: return KEY_8;
168 case SDLK_9: return KEY_9;
169 case SDLK_a: return KEY_A;
170 case SDLK_b: return KEY_B;
171 case SDLK_c: return KEY_C;
172 case SDLK_d: return KEY_D;
173 case SDLK_e: return KEY_E;
174 case SDLK_f: return KEY_F;
175 case SDLK_g: return KEY_G;
176 case SDLK_h: return KEY_H;
177 case SDLK_i: return KEY_I;
178 case SDLK_j: return KEY_J;
179 case SDLK_k: return KEY_K;
180 case SDLK_l: return KEY_L;
181 case SDLK_m: return KEY_M;
182 case SDLK_n: return KEY_N;
183 case SDLK_o: return KEY_O;
184 case SDLK_p: return KEY_P;
185 case SDLK_q: return KEY_Q;
186 case SDLK_r: return KEY_R;
187 case SDLK_s: return KEY_S;
188 case SDLK_t: return KEY_T;
189 case SDLK_u: return KEY_U;
190 case SDLK_v: return KEY_V;
191 case SDLK_w: return KEY_W;
192 case SDLK_x: return KEY_X;
193 case SDLK_y: return KEY_Y;
194 case SDLK_z: return KEY_Z;
195 case SDLK_RETURN: return KEY_RETURN;
196 case SDLK_ESCAPE: return KEY_ESCAPE;
197 case SDLK_BACKSPACE: return KEY_BACKSPACE;
198 case SDLK_TAB: return KEY_TAB;
199 case SDLK_SPACE: return KEY_SPACE;
200 case SDLK_MINUS: return KEY_MINUS;
201 case SDLK_EQUALS: return KEY_EQUALS;
202 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
203 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
204 case SDLK_BACKSLASH: return KEY_BACKSLASH;
205 case SDLK_SEMICOLON: return KEY_SEMICOLON;
206 case SDLK_QUOTE: return KEY_APOSTROPHE;
207 case SDLK_BACKQUOTE: return KEY_GRAVE;
208 case SDLK_COMMA: return KEY_COMMA;
209 case SDLK_PERIOD: return KEY_PERIOD;
210 case SDLK_SLASH: return KEY_SLASH;
211 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
212 case SDLK_F1: return KEY_F1;
213 case SDLK_F2: return KEY_F2;
214 case SDLK_F3: return KEY_F3;
215 case SDLK_F4: return KEY_F4;
216 case SDLK_F5: return KEY_F5;
217 case SDLK_F6: return KEY_F6;
218 case SDLK_F7: return KEY_F7;
219 case SDLK_F8: return KEY_F8;
220 case SDLK_F9: return KEY_F9;
221 case SDLK_F10: return KEY_F10;
222 case SDLK_F11: return KEY_F11;
223 case SDLK_F12: return KEY_F12;
224 case SDLK_PRINTSCREEN: return KEY_PRINTSCREEN;
225 case SDLK_SCROLLLOCK: return KEY_SCROLLLOCK;
226 case SDLK_PAUSE: return KEY_PAUSE;
227 case SDLK_INSERT: return KEY_INSERT;
228 case SDLK_HOME: return KEY_HOME;
229 case SDLK_PAGEUP: return KEY_PAGEUP;
230 case SDLK_DELETE: return KEY_DELETE;
231 case SDLK_END: return KEY_END;
232 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
233 case SDLK_RIGHT: return KEY_RIGHT;
234 case SDLK_LEFT: return KEY_LEFT;
235 case SDLK_DOWN: return KEY_DOWN;
236 case SDLK_UP: return KEY_UP;
237 //case SDLK_NUMLOCK: return KEY_NUMLOCK;
238 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
239 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
240 case SDLK_KP_MINUS: return KEY_KP_MINUS;
241 case SDLK_KP_PLUS: return KEY_KP_PLUS;
242 case SDLK_KP_ENTER: return KEY_KP_ENTER;
243 case SDLK_KP_0: return KEY_KP_0;
244 case SDLK_KP_1: return KEY_KP_1;
245 case SDLK_KP_2: return KEY_KP_2;
246 case SDLK_KP_3: return KEY_KP_3;
247 case SDLK_KP_4: return KEY_KP_4;
248 case SDLK_KP_5: return KEY_KP_5;
249 case SDLK_KP_6: return KEY_KP_6;
250 case SDLK_KP_7: return KEY_KP_7;
251 case SDLK_KP_8: return KEY_KP_8;
252 case SDLK_KP_9: return KEY_KP_9;
253 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
254 case SDLK_SYSREQ: return KEY_SYSREQ;
255 case SDLK_LCTRL: return KEY_LCTRL;
256 case SDLK_LSHIFT: return KEY_LSHIFT;
257 case SDLK_LALT: return KEY_LALT;
258 //case SDLK_LSUPER: return KEY_LSUPER;
259 case SDLK_RCTRL: return KEY_RCTRL;
260 case SDLK_RSHIFT: return KEY_RSHIFT;
261 case SDLK_RALT: return KEY_RALT;
262 //case SDLK_RSUPER: return KEY_RSUPER;
263 default: return KEY_UNKNOWN;
267 static void poll_events (void (*h)(int key, int down)) {
268 int key;
269 SDL_Event ev;
270 while (SDL_PollEvent(&ev)) {
271 switch (ev.type) {
272 case SDL_QUIT:
273 ERR_quit();
274 break;
275 case SDL_KEYDOWN:
276 case SDL_KEYUP:
277 key = sdl_to_key(ev.key.keysym.sym);
278 I_press(key, ev.type == SDL_KEYDOWN);
279 if (h != NULL) {
280 (*h)(key, ev.type == SDL_KEYDOWN);
282 break;
287 int main (int argc, char **argv) {
288 char *pw;
289 Uint32 t, ticks;
290 logo("system: initialize SDL2\n");
291 if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO | SDL_INIT_EVENTS) == -1) {
292 logo("system: failed to init SDL2: %s\n", SDL_GetError());
293 return 1;
295 //SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
296 // Player 1 defaults
297 pl1.ku = KEY_KP_8;
298 pl1.kd = KEY_KP_5;
299 pl1.kl = KEY_KP_4;
300 pl1.kr = KEY_KP_6;
301 pl1.kf = KEY_PAGEDOWN;
302 pl1.kj = KEY_DELETE;
303 pl1.kwl = KEY_HOME;
304 pl1.kwr = KEY_END;
305 pl1.kp = KEY_KP_8;
306 // Player 2 defaults
307 pl2.ku = KEY_E;
308 pl2.kd = KEY_D;
309 pl2.kl = KEY_S;
310 pl2.kr = KEY_F;
311 pl2.kf = KEY_A;
312 pl2.kj = KEY_Q;
313 pl2.kwl = KEY_1;
314 pl2.kwr = KEY_2;
315 pl2.kp = KEY_E;
316 srand(SDL_GetTicks());
317 F_startup();
318 #ifndef WIN32
319 pw = "/usr/share/doom2d-rembo/doom2d.wad";
320 #else
321 pw = "doom2d.wad";
322 #endif
323 if (fexists(pw)) {
324 F_addwad(pw);
325 } else {
326 F_addwad("doom2d.wad");
328 CFG_args(argc, argv);
329 CFG_load();
330 F_initwads();
331 M_startup();
332 F_allocres();
333 S_init();
334 S_initmusic();
335 R_init();
336 G_init();
337 ticks = SDL_GetTicks();
338 while (!quit) {
339 poll_events(&G_keyf);
340 S_updatemusic();
341 t = SDL_GetTicks();
342 if (t - ticks > DELAY) {
343 ticks = t;
344 G_act();
346 R_draw();
348 CFG_save();
349 R_done();
350 S_donemusic();
351 S_done();
352 M_shutdown();
353 SDL_Quit();
354 return 0;