Add shadows and light effects
[mercenary-reloaded.git] / src / libsdl / sdl.c
1 /* SDL handling
2  *
3  * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
4  * All Rights Reserved
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdint.h>
22 #include <errno.h>
23 #include "print.h"
24 #include "../../include/keycodes.h"
25 #include "sdl.h"
26 #include <SDL2/SDL.h>
27 #define GLEW_STATIC
28 #include <GL/glew.h>
29
30 static int sdl_initialized = 0;
31 static int audio_initialized = 0;
32 static const char *device_string = NULL;
33 static SDL_AudioDeviceID audio_devid = 0;
34 static SDL_Window *gl_window = NULL;
35 static SDL_GLContext gl_context = NULL;
36 static SDL_Joystick *sdl_joystick = NULL;
37 static void (*keyboard_sdl)(int down, enum keycode keycode) = NULL;
38 static void (*joystick_sdl)(double x, double y, int fire) = NULL;
39 static void (*audio_sdl)(float *data, int len) = NULL;
40 static void (*resize_window_sdl)(int width, int height) = NULL;
41 static int joystick_y_axis_sdl = 1, joystick_x_axis_sdl = 0, joystick_fire_button_sdl = -1;
42
43 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
44 {
45         float audio_data[len / sizeof(float)];
46
47         SDL_memset(stream, 0, len);
48         audio_sdl(audio_data, len / sizeof(float) / 2);
49         SDL_MixAudioFormat(stream, (Uint8 *)audio_data, AUDIO_F32, len, SDL_MIX_MAXVOLUME / 2.0);
50 }
51
52 void sdl_list_joysticks(void)
53 {
54         int num, i, rc;
55
56         rc = SDL_Init(SDL_INIT_JOYSTICK);
57         if (rc < 0) {
58                 print_error("Failed to init SDL\n");
59                 return;
60         }
61
62         num = SDL_NumJoysticks();
63         for (i = 0; i < num; i++)
64                 printf("#%d: %s\n", i, SDL_JoystickNameForIndex(i));
65
66         SDL_Quit();
67 }
68
69 int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), int joysticknum, int joystick_x_axis, int joystick_y_axis, int joystick_fire_button, void (*joystick)(double x, double y, int fire), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync, int rift)
70 {
71         int rc = -EINVAL;
72
73         keyboard_sdl = keyboard;
74         joystick_sdl = joystick;
75         audio_sdl = audio;
76         resize_window_sdl = resize_window;
77         if (joystick_x_axis >= 0)
78                 joystick_x_axis_sdl = joystick_x_axis;
79         if (joystick_y_axis >= 0)
80                 joystick_y_axis_sdl = joystick_y_axis;
81         if (joystick_fire_button >= 0)
82                 joystick_fire_button_sdl = joystick_fire_button;
83
84         /* init SDL library */
85         rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);
86         if (rc < 0) {
87                 print_error("Failed to init SDL\n");
88                 goto error;
89         }
90         sdl_initialized = 1;
91
92         SDL_JoystickEventState(SDL_ENABLE);
93         sdl_joystick = SDL_JoystickOpen((joysticknum < 0) ? 0 : joysticknum);
94         if (!sdl_joystick && joysticknum >= 0) {
95                 rc = -EIO;
96                 print_error("Failed to open joystick #%d, see help!\n", joysticknum);
97                 goto error;
98         }
99
100 retry_without_multisampling:
101         if (multisampling > 1) {
102                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
103                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
104                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
105                 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
106                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
107                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
108                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisampling);
109         }
110
111         /* open window */
112         gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
113         if (!gl_window) {
114                 if (multisampling) {
115                         print_info("Failed to open SDL window: %s (retrying without multisampling)\n", SDL_GetError());
116                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
117                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
118                         multisampling = 0;
119                         goto retry_without_multisampling;
120                 }
121                 print_error("Failed to open SDL window: %s\n", SDL_GetError());
122                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
123                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisampling);
124                 rc = EIO;
125                 goto error;
126         }
127
128         /* create GL context */
129         gl_context = SDL_GL_CreateContext(gl_window);
130         if (!gl_context) {
131                 print_error("Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
132                 rc = EIO;
133                 goto error;
134         }
135
136         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
137         if (rc < 0) {
138                 print_error("Failed to set SDL's OpenGL context profile\n");
139                 goto error;
140         }
141
142         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
143         if (rc < 0) {
144                 print_error("Failed to set SDL's OpenGL major version\n");
145                 goto error;
146         }
147         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
148         if (rc < 0) {
149                 print_error("Failed to set SDL's OpenGL minor version\n");
150                 goto error;
151         }
152
153         rc = SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
154         if (rc < 0) {
155                 print_error("Failed to set SDL's OpenGL doublebuffer\n");
156                 goto error;
157         }
158
159         rc = SDL_GL_SetSwapInterval((vbl_sync) ? 1 : 0);
160         if (rc < 0) {
161                 print_error("Failed to set SDL's OpenGL swap interval to VBLANK\n");
162                 // continue anyway
163         }
164
165 #ifndef __APPLE__
166         glewExperimental = GL_TRUE;
167         if (glewInit() != GLEW_OK) {
168                 print_error("Failed to init GLEW\n");
169                 goto error;
170         }
171 #endif
172
173         /* just in case */
174         glDisable(GL_DEPTH_TEST);
175         glDisable(GL_CULL_FACE);
176         if (multisampling > 1)
177                 glEnable(GL_MULTISAMPLE);
178
179         if (rift) {
180                 int i, count;
181                 count = SDL_GetNumAudioDevices(0);
182                 const char *string;
183                 for(i = 0; i < count; i++) {
184                         string = SDL_GetAudioDeviceName(i, 0);
185                         if (strstr(string, "Rift"))
186                                 device_string = string;
187                 }
188                 if (!device_string)
189                         print_error("Oculus Rift headset not found, falling back to default audio device.\n");
190         }
191
192         /* open audio */
193         SDL_AudioSpec want, have;
194         SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
195         want.freq = sound_samplerate;
196         want.format = AUDIO_F32; /* we always use float in this project */
197         want.channels = 2;
198         want.samples = sound_chunk; /* must be a power of two */
199         want.callback = audio_cb;
200         audio_devid = SDL_OpenAudioDevice(device_string, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
201         if (audio_devid <= 0) {
202                 print_error("Failed to open audio! (No speaker connected?)\n");
203                 rc = -EIO;
204                 goto error;
205         } else if (have.format != want.format) {
206                 print_error("Failed to open audio with desired audio format (want %d, got %d)\n", want.format, have.format);
207                 rc = -EIO;
208                 goto error;
209         } else if (have.freq != want.freq) {
210                 print_error("Failed to open audio with desired sample rate (want %d, got %d)\n", want.freq, have.freq);
211                 rc = -EIO;
212                 goto error;
213         } else if (have.channels != want.channels) {
214                 print_error("Failed to open audio with desired number of channels (want %d, got %d)\n", want.channels, have.channels);
215                 rc = -EIO;
216                 goto error;
217         } else {
218                 SDL_PauseAudioDevice(audio_devid, 0);
219                 audio_initialized = 1;
220         }
221
222         return 0;
223
224 error:
225         exit_sdl();
226         return rc;
227 }
228
229 static enum keycode sdl2keycode(SDL_Keycode sym)
230 {
231         switch (sym) {
232         case SDLK_LCTRL: return KEYCODE_LCTRL;
233         case SDLK_RCTRL: return KEYCODE_RCTRL;
234         case SDLK_LSHIFT: return KEYCODE_LSHIFT;
235         case SDLK_RSHIFT: return KEYCODE_RSHIFT;
236         case SDLK_LALT: return KEYCODE_LALT;
237         case SDLK_RALT: return KEYCODE_RALT;
238         case SDLK_PAUSE: return KEYCODE_PAUSE;
239         case SDLK_LEFT: return KEYCODE_LEFT;
240         case SDLK_RIGHT: return KEYCODE_RIGHT;
241         case SDLK_UP: return KEYCODE_UP;
242         case SDLK_DOWN: return KEYCODE_DOWN;
243         case SDLK_END: return KEYCODE_END;
244         case SDLK_a: return KEYCODE_a;
245         case SDLK_b: return KEYCODE_b;
246         case SDLK_c: return KEYCODE_c;
247         case SDLK_d: return KEYCODE_d;
248         case SDLK_e: return KEYCODE_e;
249         case SDLK_f: return KEYCODE_f;
250         case SDLK_g: return KEYCODE_g;
251         case SDLK_h: return KEYCODE_h;
252         case SDLK_i: return KEYCODE_i;
253         case SDLK_j: return KEYCODE_j;
254         case SDLK_k: return KEYCODE_k;
255         case SDLK_l: return KEYCODE_l;
256         case SDLK_m: return KEYCODE_m;
257         case SDLK_n: return KEYCODE_n;
258         case SDLK_o: return KEYCODE_o;
259         case SDLK_p: return KEYCODE_p;
260         case SDLK_q: return KEYCODE_q;
261         case SDLK_r: return KEYCODE_r;
262         case SDLK_s: return KEYCODE_s;
263         case SDLK_t: return KEYCODE_t;
264         case SDLK_u: return KEYCODE_u;
265         case SDLK_v: return KEYCODE_v;
266         case SDLK_w: return KEYCODE_w;
267         case SDLK_x: return KEYCODE_x;
268         case SDLK_y: return KEYCODE_y;
269         case SDLK_z: return KEYCODE_z;
270         case SDLK_0: return KEYCODE_0;
271         case SDLK_1: return KEYCODE_1;
272         case SDLK_2: return KEYCODE_2;
273         case SDLK_3: return KEYCODE_3;
274         case SDLK_4: return KEYCODE_4;
275         case SDLK_5: return KEYCODE_5;
276         case SDLK_6: return KEYCODE_6;
277         case SDLK_7: return KEYCODE_7;
278         case SDLK_8: return KEYCODE_8;
279         case SDLK_9: return KEYCODE_9;
280         case SDLK_KP_0: return KEYCODE_KP_0;
281         case SDLK_KP_1: return KEYCODE_KP_1;
282         case SDLK_KP_2: return KEYCODE_KP_2;
283         case SDLK_KP_3: return KEYCODE_KP_3;
284         case SDLK_KP_4: return KEYCODE_KP_4;
285         case SDLK_KP_5: return KEYCODE_KP_5;
286         case SDLK_KP_6: return KEYCODE_KP_6;
287         case SDLK_KP_7: return KEYCODE_KP_7;
288         case SDLK_KP_8: return KEYCODE_KP_8;
289         case SDLK_KP_9: return KEYCODE_KP_9;
290         case SDLK_KP_DIVIDE: return KEYCODE_KP_DIVIDE;
291         case SDLK_KP_MULTIPLY: return KEYCODE_KP_MULTIPLY;
292         case SDLK_KP_MINUS: return KEYCODE_KP_MINUS;
293         case SDLK_KP_PLUS: return KEYCODE_KP_PLUS;
294         case SDLK_KP_PERIOD: return KEYCODE_KP_PERIOD;
295         case SDLK_KP_ENTER: return KEYCODE_KP_ENTER;
296         case SDLK_F1: return KEYCODE_F1;
297         case SDLK_F2: return KEYCODE_F2;
298         case SDLK_F3: return KEYCODE_F3;
299         case SDLK_F4: return KEYCODE_F4;
300         case SDLK_F5: return KEYCODE_F5;
301         case SDLK_F6: return KEYCODE_F6;
302         case SDLK_F7: return KEYCODE_F7;
303         case SDLK_F8: return KEYCODE_F8;
304         case SDLK_F9: return KEYCODE_F9;
305         case SDLK_F10: return KEYCODE_F10;
306         case SDLK_SPACE: return KEYCODE_SPACE;
307         case SDLK_BACKSPACE: return KEYCODE_BACKSPACE;
308         case SDLK_TAB: return KEYCODE_TAB;
309         case SDLK_RETURN: return KEYCODE_RETURN;
310         case SDLK_ESCAPE: return KEYCODE_ESCAPE;
311         case SDLK_DELETE: return KEYCODE_DELETE;
312         case SDLK_INSERT: return KEYCODE_INSERT;
313         case SDLK_COMMA: return KEYCODE_COMMA;
314         case SDLK_PERIOD: return KEYCODE_PERIOD;
315         default: return KEYCODE_UNDEFINED;
316         }
317 }
318
319 static double joystick_x = 0, joystick_y = 0;
320 static int joystick_fire = 0;
321
322 static void joystick_event(int axis, int value)
323 {
324         double v = (double)value  / 32767.0;
325
326         switch (axis) {
327         case 0: joystick_y = v; break;
328         case 1: joystick_x = v; break;
329         case 2: joystick_fire = value; break;
330         }
331
332         joystick_sdl(joystick_x, joystick_y, joystick_fire);
333 }
334
335 static int key_ctrl = 0, key_f = 0, fullscreen = 0;
336
337 int event_sdl(void)
338 {
339         int quit = 0;
340         SDL_Event event;
341
342         while (SDL_PollEvent(&event)) {
343                 switch (event.type) {
344                 case SDL_QUIT:
345                         quit = 1;
346                         break;
347                 case SDL_WINDOWEVENT:
348                         if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
349                                 resize_window_sdl(event.window.data1, event.window.data2);
350                         break;
351                 case SDL_KEYDOWN:
352                         switch (event.key.keysym.sym) {
353                         case SDLK_f:
354                                 if (key_ctrl && !key_f) {
355                                         if (fullscreen) {
356                                                 fullscreen = 0;
357                                                 SDL_SetWindowFullscreen(gl_window, 0);
358                                                 SDL_ShowCursor(SDL_ENABLE);
359                                         } else {
360                                                 SDL_SetWindowFullscreen(gl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
361                                                 SDL_ShowCursor(SDL_DISABLE);
362                                                 fullscreen = 1;
363                                         }
364                                 }
365                                 key_f = 1;
366                                 break;
367                         case SDLK_LCTRL:
368                         case SDLK_RCTRL:
369                                 key_ctrl = 1;
370                                 break;
371                         }
372                         keyboard_sdl(1, sdl2keycode(event.key.keysym.sym));
373                         break;
374                 case SDL_KEYUP:
375                         switch (event.key.keysym.sym) {
376                         case SDLK_LCTRL:
377                         case SDLK_RCTRL:
378                                 key_ctrl = 0;
379                                 break;
380                         case SDLK_f:
381                                 key_f = 0;
382                                 break;
383                         }
384                         keyboard_sdl(0, sdl2keycode(event.key.keysym.sym));
385                         break;
386                 case SDL_JOYAXISMOTION:
387                         if (event.jaxis.axis == joystick_y_axis_sdl)
388                                 joystick_event(0, event.jaxis.value);
389                         if (event.jaxis.axis == joystick_x_axis_sdl)
390                                 joystick_event(1, event.jaxis.value);
391                         break;
392                 case SDL_JOYBUTTONDOWN:
393                         if (joystick_fire_button_sdl < 0 || event.jbutton.button == joystick_fire_button_sdl)
394                                 joystick_event(2, 1);
395                         break;
396                 case SDL_JOYBUTTONUP:
397                         if (joystick_fire_button_sdl < 0 || event.jbutton.button == joystick_fire_button_sdl)
398                                 joystick_event(2, 0);
399                         break;
400                 }
401         }
402
403         return quit;
404 }
405
406 void swap_sdl(void)
407 {
408         SDL_GL_SwapWindow(gl_window);
409 }
410
411 void exit_sdl(void)
412 {
413         /* close joystick */
414         if (sdl_joystick) {
415                 SDL_JoystickClose(sdl_joystick);
416                 sdl_joystick = NULL;
417         }
418         /* close window */
419         if (gl_window) {
420                 SDL_DestroyWindow(gl_window);
421                 gl_window = NULL;
422         }
423
424         /* clear OpenGL context */
425         if (gl_context) {
426                 SDL_GL_DeleteContext(gl_context);
427                 gl_context = NULL;
428         }
429
430         /* exit SDL library */
431         if (audio_initialized) {
432                 SDL_PauseAudioDevice(audio_devid, 1);
433                 audio_initialized = 0;
434         }
435         if (audio_devid > 0) {
436                 SDL_CloseAudioDevice(audio_devid);
437                 audio_devid = 0;
438         }
439         if (sdl_initialized) {
440                 SDL_Quit();
441                 sdl_initialized = 0;
442         }
443 }
444
445 uint32_t ticks_sdl(void)
446 {
447         return SDL_GetTicks();
448 }