comparison render_sdl.c @ 1004:fc000f245cc8

Set vsync state based on config file rather than just using whatever the system decides for us.
author Michael Pavone <pavone@retrodev.com>
date Sun, 01 May 2016 14:59:40 -0700
parents 534f522a1162
children 7267bc1ab547
comparison
equal deleted inserted replaced
1003:534f522a1162 1004:fc000f245cc8
266 main_width = width; 266 main_width = width;
267 main_height = height; 267 main_height = height;
268 is_fullscreen = fullscreen; 268 is_fullscreen = fullscreen;
269 269
270 render_gl = 0; 270 render_gl = 0;
271 tern_val def = {.ptrval = "off"};
272 char *vsync = tern_find_path_default(config, "video\0vsync\0", def).ptrval;
271 273
272 #ifndef DISABLE_OPENGL 274 #ifndef DISABLE_OPENGL
273 flags |= SDL_WINDOW_OPENGL; 275 flags |= SDL_WINDOW_OPENGL;
274 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5); 276 SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
275 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5); 277 SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
286 warning("Initialization of GLEW failed with code %d\n", res); 288 warning("Initialization of GLEW failed with code %d\n", res);
287 } 289 }
288 290
289 if (res == GLEW_OK && GLEW_VERSION_2_0) { 291 if (res == GLEW_OK && GLEW_VERSION_2_0) {
290 render_gl = 1; 292 render_gl = 1;
293 if (!strcmp("tear", vsync)) {
294 if (SDL_GL_SetSwapInterval(-1) < 0) {
295 warning("late tear is not available (%s), using normal vsync\n", SDL_GetError());
296 vsync = "on";
297 } else {
298 vsync = NULL;
299 }
300 }
301 if (vsync) {
302 if (SDL_GL_SetSwapInterval(!strcmp("on", vsync)) < 0) {
303 warning("Failed to set vsync to %s: %s\n", vsync, SDL_GetError());
304 }
305 }
291 } else { 306 } else {
292 SDL_DestroyWindow(main_window); 307 warning("OpenGL 2.0 is unavailable, falling back to SDL2 renderer\n");
293 warning("OpenGL 2.0 is unavailable, falling back to SDL2 renderer\n", stderr); 308 #endif
294 #endif 309 flags = SDL_RENDERER_ACCELERATED;
295 SDL_CreateWindowAndRenderer(width, height, flags, &main_window, &main_renderer); 310 if (!strcmp("on", vsync) || !strcmp("tear", vsync)) {
311 flags |= SDL_RENDERER_PRESENTVSYNC;
312 }
313 main_renderer = SDL_CreateRenderer(main_window, -1, flags);
296 314
297 if (!main_window || !main_renderer) { 315 if (!main_window || !main_renderer) {
298 fatal_error("unable to create SDL window: %s\n", SDL_GetError()); 316 fatal_error("unable to create SDL window: %s\n", SDL_GetError());
299 } 317 }
300 main_clip.x = main_clip.y = 0; 318 main_clip.x = main_clip.y = 0;
306 324
307 SDL_GetWindowSize(main_window, &width, &height); 325 SDL_GetWindowSize(main_window, &width, &height);
308 printf("Window created with size: %d x %d\n", width, height); 326 printf("Window created with size: %d x %d\n", width, height);
309 float src_aspect = 4.0/3.0; 327 float src_aspect = 4.0/3.0;
310 float aspect = (float)width / height; 328 float aspect = (float)width / height;
311 tern_val def = {.ptrval = "normal"}; 329 def.ptrval = "normal";
312 int stretch = fabs(aspect - src_aspect) > 0.01 && !strcmp(tern_find_path_default(config, "video\0aspect\0", def).ptrval, "stretch"); 330 int stretch = fabs(aspect - src_aspect) > 0.01 && !strcmp(tern_find_path_default(config, "video\0aspect\0", def).ptrval, "stretch");
313 331
314 if (!stretch) { 332 if (!stretch) {
315 #ifndef DISABLE_OPENGL 333 #ifndef DISABLE_OPENGL
316 if (render_gl) { 334 if (render_gl) {
333 main_clip.y = (height - main_clip.h) / 2; 351 main_clip.y = (height - main_clip.h) / 2;
334 #ifndef DISABLE_OPENGL 352 #ifndef DISABLE_OPENGL
335 } 353 }
336 #endif 354 #endif
337 } 355 }
338 def.ptrval = "false"; 356 def.ptrval = "off";
339 scanlines = !strcmp(tern_find_path_default(config, "video\0scanlines\0", def).ptrval, "true"); 357 scanlines = !strcmp(tern_find_path_default(config, "video\0scanlines\0", def).ptrval, "on");
340 358
341 caption = title; 359 caption = title;
342 min_delay = 0; 360 min_delay = 0;
343 for (int i = 0; i < 100; i++) { 361 for (int i = 0; i < 100; i++) {
344 uint32_t start = SDL_GetTicks(); 362 uint32_t start = SDL_GetTicks();