DEADSOFTWARE

move error.c into system layer
[flatwaifu.git] / src / sdl / main.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include <SDL.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h> // srand exit
27 #include <string.h> // strcasecmp
28 #include "input.h"
30 #include "my.h" // fexists
31 #include "player.h" // pl1 pl2
32 #include "menu.h" // G_keyf
33 #include "error.h" // logo
35 #include "files.h" // F_startup F_addwad F_initwads F_allocres
36 #include "config.h" // CFG_args CFG_load CFG_save
37 #include "memory.h" // M_startup
38 #include "game.h" // G_init G_act
39 #include "sound.h" // S_init S_done
40 #include "music.h" // S_initmusic S_updatemusic S_donemusic
41 #include "render.h" // R_init R_draw R_done
43 static int quit = 0;
45 void logo (const char *s, ...) {
46 va_list ap;
47 va_start(ap, s);
48 vprintf(s, ap);
49 va_end(ap);
50 fflush(stdout);
51 }
53 void logo_gas (int cur, int all) {
54 // stub
55 }
57 void ERR_failinit (char *s, ...) {
58 va_list ap;
59 va_start(ap, s);
60 vprintf(s, ap);
61 va_end(ap);
62 puts("");
63 exit(1);
64 }
66 void ERR_fatal (char *s, ...) {
67 va_list ap;
68 R_done();
69 S_done();
70 S_donemusic();
71 M_shutdown();
72 SDL_Quit();
73 puts("\nКРИТИЧЕСКАЯ ОШИБКА:");
74 va_start(ap, s);
75 vprintf(s, ap);
76 va_end(ap);
77 puts("");
78 exit(1);
79 }
81 void ERR_quit (void) {
82 puts("Спасибо за то, что вы играли в Операцию \"Смятка\"!");
83 //F_loadres(F_getresid("ENDOOM"),p,0,4000);
84 quit = 1;
85 }
87 static int sdl_to_key (int code) {
88 switch (code) {
89 case SDLK_0: return KEY_0;
90 case SDLK_1: return KEY_1;
91 case SDLK_2: return KEY_2;
92 case SDLK_3: return KEY_3;
93 case SDLK_4: return KEY_4;
94 case SDLK_5: return KEY_5;
95 case SDLK_6: return KEY_6;
96 case SDLK_7: return KEY_7;
97 case SDLK_8: return KEY_8;
98 case SDLK_9: return KEY_9;
99 case SDLK_a: return KEY_A;
100 case SDLK_b: return KEY_B;
101 case SDLK_c: return KEY_C;
102 case SDLK_d: return KEY_D;
103 case SDLK_e: return KEY_E;
104 case SDLK_f: return KEY_F;
105 case SDLK_g: return KEY_G;
106 case SDLK_h: return KEY_H;
107 case SDLK_i: return KEY_I;
108 case SDLK_j: return KEY_J;
109 case SDLK_k: return KEY_K;
110 case SDLK_l: return KEY_L;
111 case SDLK_m: return KEY_M;
112 case SDLK_n: return KEY_N;
113 case SDLK_o: return KEY_O;
114 case SDLK_p: return KEY_P;
115 case SDLK_q: return KEY_Q;
116 case SDLK_r: return KEY_R;
117 case SDLK_s: return KEY_S;
118 case SDLK_t: return KEY_T;
119 case SDLK_u: return KEY_U;
120 case SDLK_v: return KEY_V;
121 case SDLK_w: return KEY_W;
122 case SDLK_x: return KEY_X;
123 case SDLK_y: return KEY_Y;
124 case SDLK_z: return KEY_Z;
125 case SDLK_RETURN: return KEY_RETURN;
126 case SDLK_ESCAPE: return KEY_ESCAPE;
127 case SDLK_BACKSPACE: return KEY_BACKSPACE;
128 case SDLK_TAB: return KEY_TAB;
129 case SDLK_SPACE: return KEY_SPACE;
130 case SDLK_MINUS: return KEY_MINUS;
131 case SDLK_EQUALS: return KEY_EQUALS;
132 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
133 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
134 case SDLK_BACKSLASH: return KEY_BACKSLASH;
135 case SDLK_SEMICOLON: return KEY_SEMICOLON;
136 case SDLK_QUOTE: return KEY_APOSTROPHE;
137 case SDLK_BACKQUOTE: return KEY_GRAVE;
138 case SDLK_COMMA: return KEY_COMMA;
139 case SDLK_PERIOD: return KEY_PERIOD;
140 case SDLK_SLASH: return KEY_SLASH;
141 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
142 case SDLK_F1: return KEY_F1;
143 case SDLK_F2: return KEY_F2;
144 case SDLK_F3: return KEY_F3;
145 case SDLK_F4: return KEY_F4;
146 case SDLK_F5: return KEY_F5;
147 case SDLK_F6: return KEY_F6;
148 case SDLK_F7: return KEY_F7;
149 case SDLK_F8: return KEY_F8;
150 case SDLK_F9: return KEY_F9;
151 case SDLK_F10: return KEY_F10;
152 case SDLK_F11: return KEY_F11;
153 case SDLK_F12: return KEY_F12;
154 case SDLK_PRINT: return KEY_PRINTSCREEN;
155 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
156 case SDLK_PAUSE: return KEY_PAUSE;
157 case SDLK_INSERT: return KEY_INSERT;
158 case SDLK_HOME: return KEY_HOME;
159 case SDLK_PAGEUP: return KEY_PAGEUP;
160 case SDLK_DELETE: return KEY_DELETE;
161 case SDLK_END: return KEY_END;
162 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
163 case SDLK_RIGHT: return KEY_RIGHT;
164 case SDLK_LEFT: return KEY_LEFT;
165 case SDLK_DOWN: return KEY_DOWN;
166 case SDLK_UP: return KEY_UP;
167 case SDLK_NUMLOCK: return KEY_NUMLOCK;
168 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
169 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
170 case SDLK_KP_MINUS: return KEY_KP_MINUS;
171 case SDLK_KP_PLUS: return KEY_KP_PLUS;
172 case SDLK_KP_ENTER: return KEY_KP_ENTER;
173 case SDLK_KP0: return KEY_KP_0;
174 case SDLK_KP1: return KEY_KP_1;
175 case SDLK_KP2: return KEY_KP_2;
176 case SDLK_KP3: return KEY_KP_3;
177 case SDLK_KP4: return KEY_KP_4;
178 case SDLK_KP5: return KEY_KP_5;
179 case SDLK_KP6: return KEY_KP_6;
180 case SDLK_KP7: return KEY_KP_7;
181 case SDLK_KP8: return KEY_KP_8;
182 case SDLK_KP9: return KEY_KP_9;
183 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
184 case SDLK_SYSREQ: return KEY_SYSREQ;
185 case SDLK_LCTRL: return KEY_LCTRL;
186 case SDLK_LSHIFT: return KEY_LSHIFT;
187 case SDLK_LALT: return KEY_LALT;
188 case SDLK_LSUPER: return KEY_LSUPER;
189 case SDLK_RCTRL: return KEY_RCTRL;
190 case SDLK_RSHIFT: return KEY_RSHIFT;
191 case SDLK_RALT: return KEY_RALT;
192 case SDLK_RSUPER: return KEY_RSUPER;
193 default: return KEY_UNKNOWN;
197 static void poll_events (void (*h)(int key, int down)) {
198 int key;
199 SDL_Event ev;
200 while (SDL_PollEvent(&ev)) {
201 switch (ev.type) {
202 case SDL_QUIT:
203 ERR_quit();
204 break;
205 case SDL_KEYDOWN:
206 case SDL_KEYUP:
207 key = sdl_to_key(ev.key.keysym.sym);
208 I_press(key, ev.type == SDL_KEYDOWN);
209 if (h != NULL) {
210 (*h)(key, ev.type == SDL_KEYDOWN);
212 break;
217 int SDL_main (int argc, char *argv[]) {
218 char *pw;
219 Uint32 t, ticks;
220 logo("main: initialize SDL\n");
221 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
222 logo("main: failed to init SDL: %s\n", SDL_GetError());
223 return 1;
225 SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
226 // Player 1 defaults
227 pl1.ku = KEY_KP_8;
228 pl1.kd = KEY_KP_5;
229 pl1.kl = KEY_KP_4;
230 pl1.kr = KEY_KP_6;
231 pl1.kf = KEY_PAGEDOWN;
232 pl1.kj = KEY_DELETE;
233 pl1.kwl = KEY_HOME;
234 pl1.kwr = KEY_END;
235 pl1.kp = KEY_KP_8;
236 // Player 2 defaults
237 pl2.ku = KEY_E;
238 pl2.kd = KEY_D;
239 pl2.kl = KEY_S;
240 pl2.kr = KEY_F;
241 pl2.kf = KEY_A;
242 pl2.kj = KEY_Q;
243 pl2.kwl = KEY_1;
244 pl2.kwr = KEY_2;
245 pl2.kp = KEY_E;
246 srand(SDL_GetTicks());
247 F_startup();
248 #ifndef WIN32
249 pw = "/usr/share/doom2d-rembo/doom2d.wad";
250 #else
251 pw = "doom2d.wad";
252 #endif
253 if (fexists(pw)) {
254 F_addwad(pw);
255 } else {
256 F_addwad("doom2d.wad");
258 CFG_args(argc, argv);
259 CFG_load();
260 F_initwads();
261 M_startup();
262 F_allocres();
263 S_init();
264 S_initmusic();
265 R_init();
266 G_init();
267 ticks = SDL_GetTicks();
268 while (!quit) {
269 poll_events(&G_keyf);
270 S_updatemusic();
271 t = SDL_GetTicks();
272 if (t - ticks > DELAY) {
273 ticks = t;
274 G_act();
276 R_draw();
278 CFG_save();
279 R_done();
280 S_done();
281 S_donemusic();
282 M_shutdown();
283 SDL_Quit();
284 return 0;