Mercurial > repos > blastem
comparison render_sdl.c @ 792:724bbec47f86
Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows).
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 25 Jul 2015 18:22:07 -0700 |
parents | 1b2f8280ba81 |
children | 0b692b5d154b |
comparison
equal
deleted
inserted
replaced
791:60686f8d5e48 | 792:724bbec47f86 |
---|---|
182 glAttachShader(program, fshader); | 182 glAttachShader(program, fshader); |
183 glLinkProgram(program); | 183 glLinkProgram(program); |
184 GLint link_status; | 184 GLint link_status; |
185 glGetProgramiv(program, GL_LINK_STATUS, &link_status); | 185 glGetProgramiv(program, GL_LINK_STATUS, &link_status); |
186 if (!link_status) { | 186 if (!link_status) { |
187 fputs("Failed to link shader program\n", stderr); | 187 fatal_error("Failed to link shader program\n"); |
188 exit(1); | |
189 } | 188 } |
190 un_textures[0] = glGetUniformLocation(program, "textures[0]"); | 189 un_textures[0] = glGetUniformLocation(program, "textures[0]"); |
191 un_textures[1] = glGetUniformLocation(program, "textures[1]"); | 190 un_textures[1] = glGetUniformLocation(program, "textures[1]"); |
192 un_width = glGetUniformLocation(program, "width"); | 191 un_width = glGetUniformLocation(program, "width"); |
193 at_pos = glGetAttribLocation(program, "pos"); | 192 at_pos = glGetAttribLocation(program, "pos"); |
196 char * caption = NULL; | 195 char * caption = NULL; |
197 | 196 |
198 void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen) | 197 void render_init(int width, int height, char * title, uint32_t fps, uint8_t fullscreen) |
199 { | 198 { |
200 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { | 199 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK) < 0) { |
201 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | 200 fatal_error("Unable to init SDL: %s\n", SDL_GetError()); |
202 exit(1); | 201 } |
203 } | 202 atexit(SDL_Quit); |
204 printf("width: %d, height: %d\n", width, height); | 203 printf("width: %d, height: %d\n", width, height); |
205 uint32_t flags = SDL_WINDOW_OPENGL; | 204 uint32_t flags = SDL_WINDOW_OPENGL; |
206 | 205 |
207 | 206 |
208 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); | 207 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); |
220 width = mode.w; | 219 width = mode.w; |
221 height = mode.h; | 220 height = mode.h; |
222 } | 221 } |
223 main_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags); | 222 main_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, flags); |
224 if (!main_window) { | 223 if (!main_window) { |
225 fprintf(stderr, "Unable to create SDL window: %s\n", SDL_GetError()); | 224 fatal_error("Unable to create SDL window: %s\n", SDL_GetError()); |
226 SDL_Quit(); | |
227 exit(1); | |
228 } | 225 } |
229 SDL_GetWindowSize(main_window, &width, &height); | 226 SDL_GetWindowSize(main_window, &width, &height); |
230 printf("Window created with size: %d x %d\n", width, height); | 227 printf("Window created with size: %d x %d\n", width, height); |
231 main_context = SDL_GL_CreateContext(main_window); | 228 main_context = SDL_GL_CreateContext(main_window); |
232 GLenum res = glewInit(); | 229 GLenum res = glewInit(); |
233 if (res != GLEW_OK) { | 230 if (res != GLEW_OK) { |
234 fprintf(stderr, "Initialization of GLEW failed with code %d\n", res); | 231 fatal_error("Initialization of GLEW failed with code %d\n", res); |
235 SDL_Quit(); | |
236 exit(1); | |
237 } | 232 } |
238 if (!GLEW_VERSION_2_0) { | 233 if (!GLEW_VERSION_2_0) { |
239 fputs("BlastEm requires at least OpenGL 2.0, but it is unavailable\n", stderr); | 234 fatal_error("BlastEm requires at least OpenGL 2.0, but it is unavailable\n"); |
240 SDL_Quit(); | |
241 exit(1); | |
242 } | 235 } |
243 float aspect = (float)width / height; | 236 float aspect = (float)width / height; |
244 tern_val def = {.ptrval = "normal"}; | 237 tern_val def = {.ptrval = "normal"}; |
245 if (fabs(aspect - 4.0/3.0) > 0.01 && strcmp(tern_find_path_default(config, "video\0aspect\0", def).ptrval, "stretch")) { | 238 if (fabs(aspect - 4.0/3.0) > 0.01 && strcmp(tern_find_path_default(config, "video\0aspect\0", def).ptrval, "stretch")) { |
246 for (int i = 0; i < 4; i++) | 239 for (int i = 0; i < 4; i++) |
292 desired.samples = samples*2; | 285 desired.samples = samples*2; |
293 desired.callback = audio_callback; | 286 desired.callback = audio_callback; |
294 desired.userdata = NULL; | 287 desired.userdata = NULL; |
295 | 288 |
296 if (SDL_OpenAudio(&desired, &actual) < 0) { | 289 if (SDL_OpenAudio(&desired, &actual) < 0) { |
297 fprintf(stderr, "Unable to open SDL audio: %s\n", SDL_GetError()); | 290 fatal_error("Unable to open SDL audio: %s\n", SDL_GetError()); |
298 SDL_Quit(); | |
299 exit(1); | |
300 } | 291 } |
301 buffer_samples = actual.samples; | 292 buffer_samples = actual.samples; |
302 sample_rate = actual.freq; | 293 sample_rate = actual.freq; |
303 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples); | 294 printf("Initialized audio at frequency %d with a %d sample buffer\n", actual.freq, actual.samples); |
304 SDL_PauseAudio(0); | 295 SDL_PauseAudio(0); |
313 printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy)); | 304 printf("\tNum Axes: %d\n\tNum Buttons: %d\n\tNum Hats: %d\n", SDL_JoystickNumAxes(joy), SDL_JoystickNumButtons(joy), SDL_JoystickNumHats(joy)); |
314 } | 305 } |
315 } | 306 } |
316 SDL_JoystickEventState(SDL_ENABLE); | 307 SDL_JoystickEventState(SDL_ENABLE); |
317 | 308 |
318 atexit(SDL_Quit); | |
319 atexit(render_close_audio); | 309 atexit(render_close_audio); |
320 } | 310 } |
321 | 311 |
322 void render_context(vdp_context * context) | 312 void render_context(vdp_context * context) |
323 { | 313 { |
528 uint32_t render_sample_rate() | 518 uint32_t render_sample_rate() |
529 { | 519 { |
530 return sample_rate; | 520 return sample_rate; |
531 } | 521 } |
532 | 522 |
533 | 523 void render_errorbox(char *title, char *message) |
524 { | |
525 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, title, message, NULL); | |
526 } | |
527 | |
528 void render_warnbox(char *title, char *message) | |
529 { | |
530 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_WARNING, title, message, NULL); | |
531 } | |
532 | |
533 void render_infobox(char *title, char *message) | |
534 { | |
535 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_INFORMATION, title, message, NULL); | |
536 } | |
537 |