3 * (C) 2018 by Andreas Eversberg <jolly@eversberg.eu>
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.
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.
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/>.
26 #define GL3_PROTOTYPES 1
29 static int sdl_initialized = 0;
30 static int audio_initialized = 0;
31 static SDL_Window *gl_window = NULL;
32 static SDL_GLContext gl_context = NULL;
33 static void (*keyboard_sdl)(int down, SDL_Keycode sym) = NULL;
34 static void (*audio_sdl)(float *data, int len) = NULL;
36 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
38 float audio_data[len / sizeof(float)];
40 SDL_memset(stream, 0, len);
41 audio_sdl(audio_data, len / sizeof(float));
42 SDL_MixAudio(stream, (Uint8 *)audio_data, len, SDL_MIX_MAXVOLUME);
45 int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk, void (*keyboard)(int down, SDL_Keycode sym), void (*audio)(float *data, int len))
49 keyboard_sdl = keyboard;
52 /* init SDL library */
53 rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
55 fprintf(stderr, "Failed to init SDL\n");
61 gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
63 fprintf(stderr, "Failed to open SDL window: %s\n", SDL_GetError());
68 /* create GL context */
69 gl_context = SDL_GL_CreateContext(gl_window);
71 fprintf(stderr, "Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
76 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
78 fprintf(stderr, "Failed to set SDL's OpenGL context profile\n");
82 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
84 fprintf(stderr, "Failed to set SDL's OpenGL major version\n");
87 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
89 fprintf(stderr, "Failed to set SDL's OpenGL minor version\n");
93 rc = SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
95 fprintf(stderr, "Failed to set SDL's OpenGL doublebuffer\n");
99 rc = SDL_GL_SetSwapInterval(1);
101 fprintf(stderr, "Failed to set SDL's OpenGL swap interval to VBLANK\n");
106 glewExperimental = GL_TRUE;
107 if (glewInit() != GLEW_OK) {
108 fprintf(stderr, "Failed to init GLEW\n");
114 glDisable(GL_DEPTH_TEST);
115 glDisable(GL_CULL_FACE);
118 SDL_AudioSpec want, have;
119 SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
120 want.freq = sound_samplerate;
121 want.format = AUDIO_F32; /* we always use float in this project */
123 want.samples = sound_chunk; /* must be a power of two */
124 want.callback = audio_cb;
125 rc = SDL_OpenAudio(&want, &have);
127 fprintf(stderr, "Failed to open audio\n");
128 } else if (have.format != want.format) {
129 fprintf(stderr, "Failed to open audio with desired audio format\n");
133 audio_initialized = 1;
143 static int key_ctrl = 0, fullscreen = 0;
150 while (SDL_PollEvent(&event)) {
151 if (event.type == SDL_QUIT)
153 if (event.type == SDL_WINDOWEVENT) {
154 if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
155 resize_opengl(event.window.data1, event.window.data2);
157 if (event.type == SDL_KEYDOWN) {
158 switch (event.key.keysym.sym) {
163 SDL_SetWindowFullscreen(gl_window, 0);
165 SDL_SetWindowFullscreen(gl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
175 keyboard_sdl(1, event.key.keysym.sym);
177 if (event.type == SDL_KEYUP) {
178 switch (event.key.keysym.sym) {
184 keyboard_sdl(0, event.key.keysym.sym);
193 SDL_GL_SwapWindow(gl_window);
200 SDL_DestroyWindow(gl_window);
204 /* clear OpenGL context */
206 SDL_GL_DeleteContext(gl_context);
210 /* exit SDL library */
211 if (audio_initialized) {
214 audio_initialized = 0;
216 if (sdl_initialized) {