comparison render_sdl.c @ 1402:458df351af06

Allow height to be specified in the config file and properly calculate from the aspect setting if it is not specified
author Michael Pavone <pavone@retrodev.com>
date Thu, 15 Jun 2017 19:24:16 -0700
parents 08116cb5ffaa
children 87493f585c7f
comparison
equal deleted inserted replaced
1401:b56c8c51ca5d 1402:458df351af06
274 SDL_DestroyTexture(sdl_textures[i]); 274 SDL_DestroyTexture(sdl_textures[i]);
275 } 275 }
276 } 276 }
277 } 277 }
278 278
279 static float config_aspect()
280 {
281 static float aspect = 0.0f;
282 if (aspect == 0.0f) {
283 char *config_aspect = tern_find_path_default(config, "video\0aspect\0", (tern_val){.ptrval = "4:3"}, TVAL_PTR).ptrval;
284 if (strcmp("stretch", config_aspect)) {
285 aspect = 4.0f/3.0f;
286 char *end;
287 float aspect_numerator = strtof(config_aspect, &end);
288 if (aspect_numerator > 0.0f && *end == ':') {
289 float aspect_denominator = strtof(end+1, &end);
290 if (aspect_denominator > 0.0f && !*end) {
291 aspect = aspect_numerator / aspect_denominator;
292 }
293 }
294 } else {
295 aspect = -1.0f;
296 }
297 }
298 return aspect;
299 }
300
279 static void update_aspect() 301 static void update_aspect()
280 { 302 {
281 //reset default values 303 //reset default values
282 memcpy(vertex_data, vertex_data_default, sizeof(vertex_data)); 304 memcpy(vertex_data, vertex_data_default, sizeof(vertex_data));
283 main_clip.w = main_width; 305 main_clip.w = main_width;
284 main_clip.h = main_height; 306 main_clip.h = main_height;
285 main_clip.x = main_clip.y = 0; 307 main_clip.x = main_clip.y = 0;
286 //calculate configured aspect ratio 308 if (config_aspect() > 0.0f) {
287 char *config_aspect = tern_find_path_default(config, "video\0aspect\0", (tern_val){.ptrval = "4:3"}, TVAL_PTR).ptrval;
288 if (strcmp("stretch", config_aspect)) {
289 float src_aspect = 4.0f/3.0f;
290 char *end;
291 float aspect_numerator = strtof(config_aspect, &end);
292 if (aspect_numerator > 0.0f && *end == ':') {
293 float aspect_denominator = strtof(end+1, &end);
294 if (aspect_denominator > 0.0f && !*end) {
295 src_aspect = aspect_numerator / aspect_denominator;
296 }
297 }
298 float aspect = (float)main_width / main_height; 309 float aspect = (float)main_width / main_height;
299 if (fabs(aspect - src_aspect) < 0.01f) { 310 if (fabs(aspect - config_aspect()) < 0.01f) {
300 //close enough for government work 311 //close enough for government work
301 return; 312 return;
302 } 313 }
303 #ifndef DISABLE_OPENGL 314 #ifndef DISABLE_OPENGL
304 if (render_gl) { 315 if (render_gl) {
305 for (int i = 0; i < 4; i++) 316 for (int i = 0; i < 4; i++)
306 { 317 {
307 if (aspect > src_aspect) { 318 if (aspect > config_aspect()) {
308 vertex_data[i*2] *= src_aspect/aspect; 319 vertex_data[i*2] *= config_aspect()/aspect;
309 } else { 320 } else {
310 vertex_data[i*2+1] *= aspect/src_aspect; 321 vertex_data[i*2+1] *= aspect/config_aspect();
311 } 322 }
312 } 323 }
313 } else { 324 } else {
314 #endif 325 #endif
315 main_clip.w = aspect > src_aspect ? src_aspect * (float)main_height : main_width; 326 main_clip.w = aspect > config_aspect() ? config_aspect() * (float)main_height : main_width;
316 main_clip.h = aspect > src_aspect ? main_height : main_width / src_aspect; 327 main_clip.h = aspect > config_aspect() ? main_height : main_width / config_aspect();
317 main_clip.x = (main_width - main_clip.w) / 2; 328 main_clip.x = (main_width - main_clip.w) / 2;
318 main_clip.y = (main_height - main_clip.h) / 2; 329 main_clip.y = (main_height - main_clip.h) / 2;
319 #ifndef DISABLE_OPENGL 330 #ifndef DISABLE_OPENGL
320 } 331 }
321 #endif 332 #endif
332 { 343 {
333 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) { 344 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER) < 0) {
334 fatal_error("Unable to init SDL: %s\n", SDL_GetError()); 345 fatal_error("Unable to init SDL: %s\n", SDL_GetError());
335 } 346 }
336 atexit(SDL_Quit); 347 atexit(SDL_Quit);
348 if (height <= 0) {
349 float aspect = config_aspect() > 0.0f ? config_aspect() : 4.0f/3.0f;
350 height = ((float)width / aspect) + 0.5f;
351 }
337 printf("width: %d, height: %d\n", width, height); 352 printf("width: %d, height: %d\n", width, height);
338 windowed_width = width; 353 windowed_width = width;
339 windowed_height = height; 354 windowed_height = height;
340 355
341 uint32_t flags = SDL_WINDOW_RESIZABLE; 356 uint32_t flags = SDL_WINDOW_RESIZABLE;