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;
34 static void audio_cb(void __attribute__((unused)) *userdata, Uint8 *stream, int len)
36 float audio_data[len / sizeof(float)];
38 SDL_memset(stream, 0, len);
39 audio_sdl(audio_data, len / sizeof(float));
40 SDL_MixAudio(stream, (Uint8 *)audio_data, len, SDL_MIX_MAXVOLUME);
43 int init_sdl(const char *progname, int width, int height, int sound_samplerate, int sound_chunk)
47 /* init SDL library */
48 rc = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
50 fprintf(stderr, "Failed to init SDL\n");
56 gl_window = SDL_CreateWindow(progname, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
58 fprintf(stderr, "Failed to open SDL window: %s\n", SDL_GetError());
63 /* create GL context */
64 gl_context = SDL_GL_CreateContext(gl_window);
66 fprintf(stderr, "Failed to create SDL's OpenGL context: %s\n", SDL_GetError());
71 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
73 fprintf(stderr, "Failed to set SDL's OpenGL context profile\n");
77 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
79 fprintf(stderr, "Failed to set SDL's OpenGL major version\n");
82 rc = SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
84 fprintf(stderr, "Failed to set SDL's OpenGL minor version\n");
88 rc = SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
90 fprintf(stderr, "Failed to set SDL's OpenGL doublebuffer\n");
94 rc = SDL_GL_SetSwapInterval(1);
96 fprintf(stderr, "Failed to set SDL's OpenGL swap interval to VBLANK\n");
101 glewExperimental = GL_TRUE;
102 if (glewInit() != GLEW_OK) {
103 fprintf(stderr, "Failed to init GLEW\n");
109 glDisable(GL_DEPTH_TEST);
110 glDisable(GL_CULL_FACE);
113 SDL_AudioSpec want, have;
114 SDL_memset(&want, 0, sizeof(want)); /* or SDL_zero(want) */
115 want.freq = sound_samplerate;
116 want.format = AUDIO_F32; /* we always use float in this project */
118 want.samples = sound_chunk; /* must be a power of two */
119 want.callback = audio_cb;
120 rc = SDL_OpenAudio(&want, &have);
122 fprintf(stderr, "Failed to open audio\n");
123 } else if (have.format != want.format) {
124 fprintf(stderr, "Failed to open audio with desired audio format\n");
128 audio_initialized = 1;
138 static int key_ctrl = 0, fullscreen = 0;
145 while (SDL_PollEvent(&event)) {
146 if (event.type == SDL_QUIT)
148 if (event.type == SDL_WINDOWEVENT) {
149 if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
150 resize_opengl(event.window.data1, event.window.data2);
152 if (event.type == SDL_KEYDOWN) {
153 switch (event.key.keysym.sym) {
158 SDL_SetWindowFullscreen(gl_window, 0);
160 SDL_SetWindowFullscreen(gl_window, SDL_WINDOW_FULLSCREEN_DESKTOP);
170 keyboard_sdl(1, event.key.keysym.sym);
172 if (event.type == SDL_KEYUP) {
173 switch (event.key.keysym.sym) {
179 keyboard_sdl(0, event.key.keysym.sym);
188 SDL_GL_SwapWindow(gl_window);
195 SDL_DestroyWindow(gl_window);
199 /* clear OpenGL context */
201 SDL_GL_DeleteContext(gl_context);
205 /* exit SDL library */
206 if (audio_initialized) {
209 audio_initialized = 0;
211 if (sdl_initialized) {