If SDL window fails, retry without MSAA (multisample antialiasing)
[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 "opengl.h"
27
28 #include <SDL2/SDL.h>
29 #define GL3_PROTOTYPES 1
30 #include <GL/glew.h>
31
32 static int sdl_initialized = 0;
33 static int audio_initialized = 0;
34 static const char *device_string = NULL;
35 static SDL_AudioDeviceID audio_devid = 0;
36 static SDL_Window *gl_window = NULL;
37 static SDL_GLContext gl_context = NULL;
38 static void (*keyboard_sdl)(int down, enum keycode keycode) = NULL;
39 static void (*audio_sdl)(float *data, int len) = NULL;
40 static void (*resize_window_sdl)(int width, int height) = NULL;
41
42 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
43 {
44         float audio_data[len / sizeof(float)];
45
46         SDL_memset(stream, 0, len);
47         audio_sdl(audio_data, len / sizeof(float) / 2);
48         SDL_MixAudioFormat(stream, (Uint8 *)audio_data, AUDIO_F32, len, SDL_MIX_MAXVOLUME / 2.0);
49 }
50
51 int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, enum keycode keycode), void (*audio)(float *data, int len), void (*resize_window)(int width, int height), int multisampling, int vbl_sync, int rift)
52 {
53         int rc;
54
55         keyboard_sdl = keyboard;
56         audio_sdl = audio;
57         resize_window_sdl = resize_window;
58
59         /* init SDL library */
60         rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
61         if (rc < 0) {
62                 print_error("Failed to init SDL\n");
63                 goto error;
64         }
65         sdl_initialized = 1;
66
67 retry_without_multisampling:
68         if (multisampling > 1) {
69                 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
70                 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
71                 SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
72                 SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
73                 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
74                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
75                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisampling);
76         }
77
78         /* open window */
79         gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
80         if (!gl_window) {
81                 if (multisampling) {
82                         print_info("Failed to open SDL window: %s (retrying without multisampling)\n", SDL_GetError());
83                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
84                         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
85                         multisampling = 0;
86                         goto retry_without_multisampling;
87                 }
88                 print_error("Failed to open SDL window: %s\n", SDL_GetError());
89                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
90                 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, multisampling);
91                 rc = EIO;
92                 goto error;
93         }
94
95         /* create GL context */
96         gl_context = SDL_GL_CreateContext(gl_window);
97         if (!gl_context) {
98                 print_error("Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
99                 rc = EIO;
100                 goto error;
101         }
102
103         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
104         if (rc < 0) {
105                 print_error("Failed to set SDL's OpenGL context profile\n");
106                 goto error;
107         }
108
109         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
110         if (rc < 0) {
111                 print_error("Failed to set SDL's OpenGL major version\n");
112                 goto error;
113         }
114         rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
115         if (rc < 0) {
116                 print_error("Failed to set SDL's OpenGL minor version\n");
117                 goto error;
118         }
119
120         rc = SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
121         if (rc < 0) {
122                 print_error("Failed to set SDL's OpenGL doublebuffer\n");
123                 goto error;
124         }
125
126         rc = SDL_GL_SetSwapInterval((vbl_sync) ? 1 : 0);
127         if (rc < 0) {
128                 print_error("Failed to set SDL's OpenGL swap interval to VBLANK\n");
129                 // continue anyway
130         }
131
132 // seems like a hack to me. do we need this?
133 #ifndef __APPLE__
134 #if !defined(_WIN32)
135         glewExperimental = GL_TRUE;
136         if (glewInit() != GLEW_OK) {
137                 print_error("Failed to init GLEW\n");
138                 goto error;
139         }
140 #endif
141 #endif
142
143         /* just in case */
144         glDisable(GL_DEPTH_TEST);
145         glDisable(GL_CULL_FACE);
146         if (multisampling > 1)
147                 glEnable(GL_MULTISAMPLE);
148
149         if (rift) {
150                 int i, count;
151                 count = SDL_GetNumAudioDevices(0);
152                 const char *string;
153                 for(i = 0; i < count; i++) {
154                         string = SDL_GetAudioDeviceName(i, 0);
155                         if (strstr(string, "Rift"))
156                                 device_string = string;
157                 }
158                 if (!device_string)
159                         print_error("Oculus Rift headset not found, falling back to default audio device.\n");
160         }
161
162         /* open audio */
163         SDL_AudioSpec want, have;
164         SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
165         want.freq = sound_samplerate;
166         want.format = AUDIO_F32; /* we always use float in this project */
167         want.channels = 2;
168         want.samples = sound_chunk; /* must be a power of two */
169         want.callback = audio_cb;
170         audio_devid = SDL_OpenAudioDevice(device_string, 0, &want, &have, SDL_AUDIO_ALLOW_ANY_CHANGE);
171         if (audio_devid <= 0) {
172                 print_error("Failed to open audio! (No speaker connected?)\n");
173                 rc = -EIO;
174                 goto error;
175         } else if (have.format != want.format) {
176                 print_error("Failed to open audio with desired audio format (want %d, got %d)\n", want.format, have.format);
177                 rc = -EIO;
178                 goto error;
179         } else if (have.freq != want.freq) {
180                 print_error("Failed to open audio with desired sample rate (want %d, got %d)\n", want.freq, have.freq);
181                 rc = -EIO;
182                 goto error;
183         } else if (have.channels != want.channels) {
184                 print_error("Failed to open audio with desired number of channels (want %d, got %d)\n", want.channels, have.channels);
185                 rc = -EIO;
186                 goto error;
187         } else {
188                 SDL_PauseAudioDevice(audio_devid, 0);
189                 audio_initialized = 1;
190         }
191
192         return 0;
193
194 error:
195         exit_sdl();
196         return rc;
197 }
198
199 static enum keycode sdl2keycode(SDL_Keycode sym)
200 {
201         switch (sym) {
202         case SDLK_LCTRL: return KEYCODE_LCTRL;
203         case SDLK_RCTRL: return KEYCODE_RCTRL;
204         case SDLK_LSHIFT: return KEYCODE_LSHIFT;
205         case SDLK_RSHIFT: return KEYCODE_RSHIFT;
206         case SDLK_PAUSE: return KEYCODE_PAUSE;
207         case SDLK_LEFT: return KEYCODE_LEFT;
208         case SDLK_RIGHT: return KEYCODE_RIGHT;
209         case SDLK_UP: return KEYCODE_UP;
210         case SDLK_DOWN: return KEYCODE_DOWN;
211         case SDLK_END: return KEYCODE_END;
212         case SDLK_a: return KEYCODE_a;
213         case SDLK_b: return KEYCODE_b;
214         case SDLK_c: return KEYCODE_c;
215         case SDLK_d: return KEYCODE_d;
216         case SDLK_e: return KEYCODE_e;
217         case SDLK_f: return KEYCODE_f;
218         case SDLK_g: return KEYCODE_g;
219         case SDLK_h: return KEYCODE_h;
220         case SDLK_i: return KEYCODE_i;
221         case SDLK_j: return KEYCODE_j;
222         case SDLK_k: return KEYCODE_k;
223         case SDLK_l: return KEYCODE_l;
224         case SDLK_m: return KEYCODE_m;
225         case SDLK_n: return KEYCODE_n;
226         case SDLK_o: return KEYCODE_o;
227         case SDLK_p: return KEYCODE_p;
228         case SDLK_q: return KEYCODE_q;
229         case SDLK_r: return KEYCODE_r;
230         case SDLK_s: return KEYCODE_s;
231         case SDLK_t: return KEYCODE_t;
232         case SDLK_u: return KEYCODE_u;
233         case SDLK_v: return KEYCODE_v;
234         case SDLK_w: return KEYCODE_w;
235         case SDLK_x: return KEYCODE_x;
236         case SDLK_y: return KEYCODE_y;
237         case SDLK_z: return KEYCODE_z;
238         case SDLK_0: return KEYCODE_0;
239         case SDLK_1: return KEYCODE_1;
240         case SDLK_2: return KEYCODE_2;
241         case SDLK_3: return KEYCODE_3;
242         case SDLK_4: return KEYCODE_4;
243         case SDLK_5: return KEYCODE_5;
244         case SDLK_6: return KEYCODE_6;
245         case SDLK_7: return KEYCODE_7;
246         case SDLK_8: return KEYCODE_8;
247         case SDLK_9: return KEYCODE_9;
248         case SDLK_KP_0: return KEYCODE_KP_0;
249         case SDLK_KP_1: return KEYCODE_KP_1;
250         case SDLK_KP_2: return KEYCODE_KP_2;
251         case SDLK_KP_3: return KEYCODE_KP_3;
252         case SDLK_KP_4: return KEYCODE_KP_4;
253         case SDLK_KP_5: return KEYCODE_KP_5;
254         case SDLK_KP_6: return KEYCODE_KP_6;
255         case SDLK_KP_7: return KEYCODE_KP_7;
256         case SDLK_KP_8: return KEYCODE_KP_8;
257         case SDLK_KP_9: return KEYCODE_KP_9;
258         case SDLK_KP_DIVIDE: return KEYCODE_KP_DIVIDE;
259         case SDLK_KP_MULTIPLY: return KEYCODE_KP_MULTIPLY;
260         case SDLK_KP_MINUS: return KEYCODE_KP_MINUS;
261         case SDLK_KP_PLUS: return KEYCODE_KP_PLUS;
262         case SDLK_KP_PERIOD: return KEYCODE_KP_PERIOD;
263         case SDLK_KP_ENTER: return KEYCODE_KP_ENTER;
264         case SDLK_F1: return KEYCODE_F1;
265         case SDLK_F2: return KEYCODE_F2;
266         case SDLK_F3: return KEYCODE_F3;
267         case SDLK_F4: return KEYCODE_F4;
268         case SDLK_F5: return KEYCODE_F5;
269         case SDLK_F6: return KEYCODE_F6;
270         case SDLK_F7: return KEYCODE_F7;
271         case SDLK_F8: return KEYCODE_F8;
272         case SDLK_F9: return KEYCODE_F9;
273         case SDLK_F10: return KEYCODE_F10;
274         case SDLK_SPACE: return KEYCODE_SPACE;
275         case SDLK_BACKSPACE: return KEYCODE_BACKSPACE;
276         case SDLK_TAB: return KEYCODE_TAB;
277         case SDLK_RETURN: return KEYCODE_RETURN;
278         case SDLK_ESCAPE: return KEYCODE_ESCAPE;
279         case SDLK_DELETE: return KEYCODE_DELETE;
280         case SDLK_INSERT: return KEYCODE_INSERT;
281         case SDLK_COMMA: return KEYCODE_COMMA;
282         case SDLK_PERIOD: return KEYCODE_PERIOD;
283         default: return KEYCODE_UNDEFINED;
284         }
285 }
286
287 static int key_ctrl = 0, key_f = 0, fullscreen = 0;
288
289 int event_sdl(void)
290 {
291         int quit = 0;
292         SDL_Event event;
293
294         while (SDL_PollEvent(&event)) {
295                 if (event.type == SDL_QUIT)
296                         quit = 1;
297                 if (event.type == SDL_WINDOWEVENT) {
298                         if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
299                                 resize_window_sdl(event.window.data1, event.window.data2);
300                 }
301                 if (event.type == SDL_KEYDOWN) {
302                         switch (event.key.keysym.sym) {
303                         case SDLK_f:
304                                 if (key_ctrl && !key_f) {
305                                         if (fullscreen) {
306                                                 fullscreen = 0;
307                                                 SDL_SetWindowFullscreen(gl_window, 0);
308                                                 SDL_ShowCursor(SDL_ENABLE);
309                                         } else {
310                                                 SDL_SetWindowFullscreen(gl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
311                                                 SDL_ShowCursor(SDL_DISABLE);
312                                                 fullscreen = 1;
313                                         }
314                                 }
315                                 key_f = 1;
316                                 break;
317                         case SDLK_LCTRL:
318                         case SDLK_RCTRL:
319                                 key_ctrl = 1;
320                                 break;
321                         }
322                         keyboard_sdl(1, sdl2keycode(event.key.keysym.sym));
323                 }
324                 if (event.type == SDL_KEYUP) {
325                         switch (event.key.keysym.sym) {
326                         case SDLK_LCTRL:
327                         case SDLK_RCTRL:
328                                 key_ctrl = 0;
329                                 break;
330                         case SDLK_f:
331                                 key_f = 0;
332                                 break;
333                         }
334                         keyboard_sdl(0, sdl2keycode(event.key.keysym.sym));
335                 }
336         }
337
338         return quit;
339 }
340
341 void swap_sdl(void)
342 {
343         SDL_GL_SwapWindow(gl_window);
344 }
345
346 void exit_sdl(void)
347 {
348         /* close window */
349         if (gl_window) {
350                 SDL_DestroyWindow(gl_window);
351                 gl_window = NULL;
352         }
353
354         /* clear OpenGL context */
355         if (gl_context) {
356                 SDL_GL_DeleteContext(gl_context);
357                 gl_context = NULL;
358         }
359
360         /* exit SDL library */
361         if (audio_initialized) {
362                 SDL_PauseAudioDevice(audio_devid, 1);
363                 audio_initialized = 0;
364         }
365         if (audio_devid > 0) {
366                 SDL_CloseAudioDevice(audio_devid);
367                 audio_devid = 0;
368         }
369         if (sdl_initialized) {
370                 SDL_Quit();
371                 sdl_initialized = 0;
372         }
373 }
374
375 uint32_t ticks_sdl(void)
376 {
377         return SDL_GetTicks();
378 }