From 5e074a61e627f56600f7152d689cf0ecc19e1a5b Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Sat, 24 Jan 2015 13:16:35 +0000 Subject: [PATCH 1/2] fix fullscreen issues --- src/config.c | 2 +- src/platform/osinterface.c | 101 ++++++++++++++++--------------------- src/platform/platform.h | 1 + src/platform/shared.c | 34 ++++++++++++- 4 files changed, 78 insertions(+), 60 deletions(-) diff --git a/src/config.c b/src/config.c index 5b8dc1cd36..240b4edc40 100644 --- a/src/config.c +++ b/src/config.c @@ -256,7 +256,7 @@ void config_write_ini_general(FILE *fp) if (gGeneral_config.fullscreen_width != -1) fprintf(fp, "fullscreen_width = %d\n", gGeneral_config.fullscreen_width); - if (gGeneral_config.window_height != -1) + if (gGeneral_config.fullscreen_height != -1) fprintf(fp, "fullscreen_height = %d\n", gGeneral_config.fullscreen_height); if (gGeneral_config.window_width != -1) diff --git a/src/platform/osinterface.c b/src/platform/osinterface.c index ce6fba9a26..fe5191bf97 100644 --- a/src/platform/osinterface.c +++ b/src/platform/osinterface.c @@ -182,35 +182,35 @@ static void osinterface_create_window() { SDL_SysWMinfo wmInfo; HWND hWnd; - int width, height; + int width, height, mode; if (SDL_Init(SDL_INIT_VIDEO) < 0) { RCT2_ERROR("SDL_Init %s", SDL_GetError()); exit(-1); } - // stuff - { - osinterface_load_cursors(); - RCT2_CALLPROC_EBPSAFE(0x0068371D); + osinterface_load_cursors(); + RCT2_CALLPROC_EBPSAFE(0x0068371D); - width = gGeneral_config.window_width; - height = gGeneral_config.window_height; - - if (width == -1) width = 640; - if (height == -1) height = 480; - } + // Get window size + width = gGeneral_config.window_width; + height = gGeneral_config.window_height; + if (width == -1) width = 640; + if (height == -1) height = 480; RCT2_GLOBAL(0x009E2D8C, sint32) = 0; - gWindow = SDL_CreateWindow("OpenRCT2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, - _fullscreen_modes[gGeneral_config.fullscreen_mode] | SDL_WINDOW_RESIZABLE); + // Create window in window first rather than fullscreen so we have the display the window is on first + gWindow = SDL_CreateWindow( + "OpenRCT2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_RESIZABLE + ); if (!gWindow) { RCT2_ERROR("SDL_CreateWindow failed %s", SDL_GetError()); exit(-1); } SDL_VERSION(&wmInfo.version); + // Get the HWND context if (SDL_GetWindowWMInfo(gWindow, &wmInfo) != SDL_TRUE) { RCT2_ERROR("SDL_GetWindowWMInfo failed %s", SDL_GetError()); @@ -226,8 +226,38 @@ static void osinterface_create_window() osinterface_resize(width, height); platform_update_fullscreen_resolutions(); + osinterface_set_fullscreen_mode(gGeneral_config.fullscreen_mode); } +void osinterface_set_fullscreen_mode(int mode) +{ + int width, height; + + mode = _fullscreen_modes[mode]; + + // HACK Changing window size when in fullscreen usually has no effect + if (mode == SDL_WINDOW_FULLSCREEN) + SDL_SetWindowFullscreen(gWindow, 0); + + // Set window size + if (mode == SDL_WINDOW_FULLSCREEN) { + platform_update_fullscreen_resolutions(); + platform_get_closest_resolution(gGeneral_config.fullscreen_width, gGeneral_config.fullscreen_height, &width, &height); + SDL_SetWindowSize(gWindow, width, height); + } else if (mode == 0) { + SDL_SetWindowSize(gWindow, gGeneral_config.window_width, gGeneral_config.window_height); + } + + if (SDL_SetWindowFullscreen(gWindow, mode)) { + RCT2_ERROR("SDL_SetWindowFullscreen %s", SDL_GetError()); + exit(1); + + // TODO try another display mode rather than just exiting the game + } + + gGeneral_config.fullscreen_mode = mode; + config_save(); +} static void osinterface_resize(int width, int height) { @@ -526,51 +556,6 @@ void osinterface_free() SDL_Quit(); } -void osinterface_set_fullscreen_mode(int mode) -{ - int i, destinationArea, areaDiff, closestAreaDiff, closestWidth, closestHeight; - - if (mode == SDL_WINDOW_FULLSCREEN) - SDL_SetWindowFullscreen(gWindow, 0); - - if (mode == SDL_WINDOW_FULLSCREEN) { - platform_update_fullscreen_resolutions(); - - closestAreaDiff = -1; - destinationArea = gGeneral_config.fullscreen_width * gGeneral_config.fullscreen_height; - for (i = 0; i < gNumResolutions; i++) { - // Check if exact match - if (gResolutions[i].width == gGeneral_config.fullscreen_width && gResolutions[i].height == gGeneral_config.fullscreen_height) { - closestWidth = gResolutions[i].width; - closestHeight = gResolutions[i].height; - break; - } - - // Check if area is closer to best match - areaDiff = abs((gResolutions[i].width * gResolutions[i].height) - destinationArea); - if (closestAreaDiff == -1 || areaDiff < closestAreaDiff) { - closestAreaDiff = areaDiff; - closestWidth = gResolutions[i].width; - closestHeight = gResolutions[i].height; - } - } - - if (closestAreaDiff != -1) - SDL_SetWindowSize(gWindow, closestWidth, closestHeight); - } else if (mode == 0) { - SDL_SetWindowSize(gWindow, gGeneral_config.window_width, gGeneral_config.window_height); - } - - if (SDL_SetWindowFullscreen(gWindow, _fullscreen_modes[mode])){ - RCT2_ERROR("SDL_SetWindowFullscreen %s", SDL_GetError()); - exit(1); - } - - gGeneral_config.fullscreen_mode = mode; - - config_save(); -} - /** * * rct2: 0x00407978 diff --git a/src/platform/platform.h b/src/platform/platform.h index cbdc7db7c9..8d8ca037e0 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -48,6 +48,7 @@ extern SDL_Window *gWindow; // Platform shared definitions void platform_update_fullscreen_resolutions(); +void platform_get_closest_resolution(int inWidth, int inHeight, int *outWidth, int *outHeight); // Platform specific definitions char platform_get_path_separator(); diff --git a/src/platform/shared.c b/src/platform/shared.c index afcccda54c..989b1857a5 100644 --- a/src/platform/shared.c +++ b/src/platform/shared.c @@ -23,7 +23,7 @@ #include "../config.h" #include "platform.h" -int gNumResolutions; +int gNumResolutions = 0; resolution *gResolutions = NULL; SDL_Window *gWindow; @@ -95,4 +95,36 @@ void platform_update_fullscreen_resolutions() gGeneral_config.fullscreen_width = gResolutions[gNumResolutions - 1].width; gGeneral_config.fullscreen_height = gResolutions[gNumResolutions - 1].height; } +} + +void platform_get_closest_resolution(int inWidth, int inHeight, int *outWidth, int *outHeight) +{ + int i, destinationArea, areaDiff, closestAreaDiff, closestWidth, closestHeight; + + closestAreaDiff = -1; + destinationArea = inWidth * inHeight; + for (i = 0; i < gNumResolutions; i++) { + // Check if exact match + if (gResolutions[i].width == inWidth && gResolutions[i].height == inHeight) { + closestWidth = gResolutions[i].width; + closestHeight = gResolutions[i].height; + break; + } + + // Check if area is closer to best match + areaDiff = abs((gResolutions[i].width * gResolutions[i].height) - destinationArea); + if (closestAreaDiff == -1 || areaDiff < closestAreaDiff) { + closestAreaDiff = areaDiff; + closestWidth = gResolutions[i].width; + closestHeight = gResolutions[i].height; + } + } + + if (closestAreaDiff != -1) { + *outWidth = closestWidth; + *outHeight = closestHeight; + } else { + *outWidth = 640; + *outHeight = 480; + } } \ No newline at end of file From 80959b7db77ba02092f6dc56eaf7fc3887a935c1 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Sat, 24 Jan 2015 15:06:14 +0000 Subject: [PATCH 2/2] fix fullscreen mode saving --- src/platform/osinterface.c | 3 --- src/windows/options.c | 3 +++ 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/platform/osinterface.c b/src/platform/osinterface.c index fe5191bf97..6d34bd5237 100644 --- a/src/platform/osinterface.c +++ b/src/platform/osinterface.c @@ -254,9 +254,6 @@ void osinterface_set_fullscreen_mode(int mode) // TODO try another display mode rather than just exiting the game } - - gGeneral_config.fullscreen_mode = mode; - config_save(); } static void osinterface_resize(int width, int height) diff --git a/src/windows/options.c b/src/windows/options.c index 5507ca53d0..0a234f1272 100644 --- a/src/windows/options.c +++ b/src/windows/options.c @@ -574,6 +574,9 @@ static void window_options_dropdown() w->disabled_widgets &= ~(1 << WIDX_RESOLUTION); } osinterface_set_fullscreen_mode(dropdownIndex); + + gGeneral_config.fullscreen_mode = (uint8)dropdownIndex; + config_save(); } break; case WIDX_TEMPERATURE_DROPDOWN: