DEADSOFTWARE

3aaad80c00583e818a1401faae5c9793107c6f4a
[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 <stdlib.h> // srand
25 #include <string.h> // strcasecmp
26 #include "input.h"
28 #include "my.h" // fexists
29 #include "player.h" // pl1 pl2
30 #include "menu.h" // GM_init G_keyf
31 #include "error.h" // logo
33 #include "files.h" // F_startup F_addwad F_initwads F_allocres
34 #include "config.h" // CFG_args CFG_load
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 quit = 0;
43 static int sdl_to_key (int code) {
44 switch (code) {
45 case SDLK_0: return KEY_0;
46 case SDLK_1: return KEY_1;
47 case SDLK_2: return KEY_2;
48 case SDLK_3: return KEY_3;
49 case SDLK_4: return KEY_4;
50 case SDLK_5: return KEY_5;
51 case SDLK_6: return KEY_6;
52 case SDLK_7: return KEY_7;
53 case SDLK_8: return KEY_8;
54 case SDLK_9: return KEY_9;
55 case SDLK_a: return KEY_A;
56 case SDLK_b: return KEY_B;
57 case SDLK_c: return KEY_C;
58 case SDLK_d: return KEY_D;
59 case SDLK_e: return KEY_E;
60 case SDLK_f: return KEY_F;
61 case SDLK_g: return KEY_G;
62 case SDLK_h: return KEY_H;
63 case SDLK_i: return KEY_I;
64 case SDLK_j: return KEY_J;
65 case SDLK_k: return KEY_K;
66 case SDLK_l: return KEY_L;
67 case SDLK_m: return KEY_M;
68 case SDLK_n: return KEY_N;
69 case SDLK_o: return KEY_O;
70 case SDLK_p: return KEY_P;
71 case SDLK_q: return KEY_Q;
72 case SDLK_r: return KEY_R;
73 case SDLK_s: return KEY_S;
74 case SDLK_t: return KEY_T;
75 case SDLK_u: return KEY_U;
76 case SDLK_v: return KEY_V;
77 case SDLK_w: return KEY_W;
78 case SDLK_x: return KEY_X;
79 case SDLK_y: return KEY_Y;
80 case SDLK_z: return KEY_Z;
81 case SDLK_RETURN: return KEY_RETURN;
82 case SDLK_ESCAPE: return KEY_ESCAPE;
83 case SDLK_BACKSPACE: return KEY_BACKSPACE;
84 case SDLK_TAB: return KEY_TAB;
85 case SDLK_SPACE: return KEY_SPACE;
86 case SDLK_MINUS: return KEY_MINUS;
87 case SDLK_EQUALS: return KEY_EQUALS;
88 case SDLK_LEFTBRACKET: return KEY_LEFTBRACKET;
89 case SDLK_RIGHTBRACKET: return KEY_RIGHTBRACKET;
90 case SDLK_BACKSLASH: return KEY_BACKSLASH;
91 case SDLK_SEMICOLON: return KEY_SEMICOLON;
92 case SDLK_QUOTE: return KEY_APOSTROPHE;
93 case SDLK_BACKQUOTE: return KEY_GRAVE;
94 case SDLK_COMMA: return KEY_COMMA;
95 case SDLK_PERIOD: return KEY_PERIOD;
96 case SDLK_SLASH: return KEY_SLASH;
97 case SDLK_CAPSLOCK: return KEY_CAPSLOCK;
98 case SDLK_F1: return KEY_F1;
99 case SDLK_F2: return KEY_F2;
100 case SDLK_F3: return KEY_F3;
101 case SDLK_F4: return KEY_F4;
102 case SDLK_F5: return KEY_F5;
103 case SDLK_F6: return KEY_F6;
104 case SDLK_F7: return KEY_F7;
105 case SDLK_F8: return KEY_F8;
106 case SDLK_F9: return KEY_F9;
107 case SDLK_F10: return KEY_F10;
108 case SDLK_F11: return KEY_F11;
109 case SDLK_F12: return KEY_F12;
110 case SDLK_PRINT: return KEY_PRINTSCREEN;
111 case SDLK_SCROLLOCK: return KEY_SCROLLLOCK;
112 case SDLK_PAUSE: return KEY_PAUSE;
113 case SDLK_INSERT: return KEY_INSERT;
114 case SDLK_HOME: return KEY_HOME;
115 case SDLK_PAGEUP: return KEY_PAGEUP;
116 case SDLK_DELETE: return KEY_DELETE;
117 case SDLK_END: return KEY_END;
118 case SDLK_PAGEDOWN: return KEY_PAGEDOWN;
119 case SDLK_RIGHT: return KEY_RIGHT;
120 case SDLK_LEFT: return KEY_LEFT;
121 case SDLK_DOWN: return KEY_DOWN;
122 case SDLK_UP: return KEY_UP;
123 case SDLK_NUMLOCK: return KEY_NUMLOCK;
124 case SDLK_KP_DIVIDE: return KEY_KP_DIVIDE;
125 case SDLK_KP_MULTIPLY: return KEY_KP_MULTIPLY;
126 case SDLK_KP_MINUS: return KEY_KP_MINUS;
127 case SDLK_KP_PLUS: return KEY_KP_PLUS;
128 case SDLK_KP_ENTER: return KEY_KP_ENTER;
129 case SDLK_KP0: return KEY_KP_0;
130 case SDLK_KP1: return KEY_KP_1;
131 case SDLK_KP2: return KEY_KP_2;
132 case SDLK_KP3: return KEY_KP_3;
133 case SDLK_KP4: return KEY_KP_4;
134 case SDLK_KP5: return KEY_KP_5;
135 case SDLK_KP6: return KEY_KP_6;
136 case SDLK_KP7: return KEY_KP_7;
137 case SDLK_KP8: return KEY_KP_8;
138 case SDLK_KP9: return KEY_KP_9;
139 case SDLK_KP_PERIOD: return KEY_KP_PERIOD;
140 case SDLK_SYSREQ: return KEY_SYSREQ;
141 case SDLK_LCTRL: return KEY_LCTRL;
142 case SDLK_LSHIFT: return KEY_LSHIFT;
143 case SDLK_LALT: return KEY_LALT;
144 case SDLK_LSUPER: return KEY_LSUPER;
145 case SDLK_RCTRL: return KEY_RCTRL;
146 case SDLK_RSHIFT: return KEY_RSHIFT;
147 case SDLK_RALT: return KEY_RALT;
148 case SDLK_RSUPER: return KEY_RSUPER;
149 default: return KEY_UNKNOWN;
153 static void poll_events (void (*h)(int key, int down)) {
154 int key;
155 SDL_Event ev;
156 while (SDL_PollEvent(&ev)) {
157 switch (ev.type) {
158 case SDL_QUIT:
159 ERR_quit();
160 break;
161 case SDL_KEYDOWN:
162 case SDL_KEYUP:
163 key = sdl_to_key(ev.key.keysym.sym);
164 I_press(key, ev.type == SDL_KEYDOWN);
165 if (h != NULL) {
166 (*h)(key, ev.type == SDL_KEYDOWN);
168 break;
173 int SDL_main (int argc, char *argv[]) {
174 char *pw;
175 Uint32 t, ticks;
176 logo("main: initialize SDL\n");
177 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
178 logo("main: failed to init SDL: %s\n", SDL_GetError());
179 return 1;
181 SDL_WM_SetCaption("Doom 2D v1.351", "Doom 2D");
182 pl1.id = -1; // TODO move to generic code
183 pl2.id = -2; // TODO move to generic code
184 // Player 1 defaults
185 pl1.ku = KEY_KP_8;
186 pl1.kd = KEY_KP_5;
187 pl1.kl = KEY_KP_4;
188 pl1.kr = KEY_KP_6;
189 pl1.kf = KEY_PAGEDOWN;
190 pl1.kj = KEY_DELETE;
191 pl1.kwl = KEY_HOME;
192 pl1.kwr = KEY_END;
193 pl1.kp = KEY_KP_8;
194 // Player 2 defaults
195 pl2.ku = KEY_E;
196 pl2.kd = KEY_D;
197 pl2.kl = KEY_S;
198 pl2.kr = KEY_F;
199 pl2.kf = KEY_A;
200 pl2.kj = KEY_Q;
201 pl2.kwl = KEY_1;
202 pl2.kwr = KEY_2;
203 pl2.kp = KEY_E;
204 srand(SDL_GetTicks());
205 F_startup();
206 #ifndef WIN32
207 pw = "/usr/share/doom2d-rembo/doom2d.wad";
208 #else
209 pw = "doom2d.wad";
210 #endif
211 if (fexists(pw)) {
212 F_addwad(pw);
213 } else {
214 F_addwad("doom2d.wad");
216 CFG_args(argc, argv);
217 CFG_load();
218 F_initwads();
219 M_startup();
220 F_allocres();
221 G_init();
222 S_init();
223 S_initmusic();
224 R_init();
225 GM_init(); // TODO move to game
226 F_loadmus("MENU"); // TODO move to menu
227 S_startmusic(0); // TODO move to menu
228 ticks = SDL_GetTicks();
229 while (!quit) {
230 poll_events(&G_keyf);
231 S_updatemusic();
232 t = SDL_GetTicks();
233 if (t - ticks > DELAY) {
234 ticks = t;
235 G_act();
237 R_draw();
239 R_done();
240 S_donemusic();
241 S_done();
242 M_shutdown();
243 SDL_Quit();
244 return 0;