From d80ca9e0b2822970a44869ad83a1a021c93d6480 Mon Sep 17 00:00:00 2001 From: SoftCoder Date: Sun, 4 Oct 2015 20:34:54 -0700 Subject: [PATCH] - for now make fullscreen mode ONLY use desktop resolution, all other tries seem buggy in SDL2 --- source/glest_game/main/program.cpp | 19 ++++ source/glest_game/main/program.h | 1 + .../menu/menu_state_options_graphics.cpp | 8 +- .../shared_lib/include/platform/sdl/window.h | 9 +- .../include/platform/sdl/window_gl.h | 2 + .../sources/platform/sdl/gl_wrap.cpp | 86 +++++++++++++++---- .../sources/platform/sdl/window.cpp | 12 +-- .../sources/platform/sdl/window_gl.cpp | 8 ++ 8 files changed, 117 insertions(+), 28 deletions(-) diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index bdc4edc0..bb232ca4 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -738,6 +738,23 @@ void Program::exit() { // ==================== PRIVATE ==================== +void Program::initResolution() { + const Metrics &metrics = Metrics::getInstance(); + if(window->getScreenWidth() != metrics.getScreenW() || + window->getScreenHeight() != metrics.getScreenH()) { + + int oldW = metrics.getScreenW(); + int oldH = metrics.getScreenH(); + + Config &config= Config::getInstance(); + config.setInt("ScreenWidth",window->getScreenWidth(),true); + config.setInt("ScreenHeight",window->getScreenHeight(),true); + + metrics.reload(window->getScreenWidth(), window->getScreenHeight()); + printf("MainWindow forced change of resolution to desktop values (%d x %d) instead of (%d x %d)\n",metrics.getScreenW(), metrics.getScreenH(),oldW,oldH); + } +} + void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); @@ -809,6 +826,7 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){ if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); window->makeCurrentGl(); + initResolution(); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); @@ -948,6 +966,7 @@ void Program::reInitGl() { config.getBool("FullScreenAntiAliasing","false"), config.getFloat("GammaValue","0.0")); window->setText(config.getString("WindowTitle","MegaGlest")); + initResolution(); } } diff --git a/source/glest_game/main/program.h b/source/glest_game/main/program.h index d829c060..6b390cd9 100644 --- a/source/glest_game/main/program.h +++ b/source/glest_game/main/program.h @@ -233,6 +233,7 @@ private: void setDisplaySettings(); void restoreDisplaySettings(); void restoreStateFromSystemError(); + void initResolution(); }; }} //end namespace diff --git a/source/glest_game/menu/menu_state_options_graphics.cpp b/source/glest_game/menu/menu_state_options_graphics.cpp index 9e3c521e..27014cae 100644 --- a/source/glest_game/menu/menu_state_options_graphics.cpp +++ b/source/glest_game/menu/menu_state_options_graphics.cpp @@ -523,8 +523,10 @@ void MenuStateOptionsGraphics::revertScreenMode(){ config.getBool("HardwareAcceleration","false"), config.getBool("FullScreenAntiAliasing","false"), config.getFloat("GammaValue","0.0")); + Metrics::reload(this->program->getWindow()->getScreenWidth(), + this->program->getWindow()->getScreenHeight()); window->setText(config.getString("WindowTitle","MegaGlest")); - Metrics::reload(); + this->mainMenu->init(); } @@ -635,7 +637,9 @@ void MenuStateOptionsGraphics::mouseClick(int x, int y, MouseButton mouseButton) config.getBool("FullScreenAntiAliasing","false"), strToFloat(listBoxGammaCorrection.getSelectedItem())); - Metrics::reload(selectedMode->width,selectedMode->height); + Metrics::reload(this->program->getWindow()->getScreenWidth(), + this->program->getWindow()->getScreenHeight()); + this->mainMenu->init(); mainMessageBoxState=1; diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index 99efa58a..8b164f69 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -122,7 +122,7 @@ private: static map mapAllowedKeys; protected: - int w, h; + //int w, h; static bool isActive; static bool no2DMouseRendering; static bool allowAltEnterFullscreenToggle; @@ -149,6 +149,9 @@ public: static void clearAllowedKeys(); static bool isAllowedKey(wchar_t key); + virtual int getScreenWidth() = 0; + virtual int getScreenHeight() = 0; + virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight, bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, bool hardware_acceleration, bool fullscreen_anti_aliasing, @@ -163,8 +166,8 @@ public: string getText(); int getX() { return 0; } int getY() { return 0; } - int getW() { return w; } - int getH() { return h; } + int getW() { return getScreenWidth(); } + int getH() { return getScreenHeight(); } //component state int getClientW() { return getW(); } diff --git a/source/shared_lib/include/platform/sdl/window_gl.h b/source/shared_lib/include/platform/sdl/window_gl.h index 32bab15b..36bf31a1 100644 --- a/source/shared_lib/include/platform/sdl/window_gl.h +++ b/source/shared_lib/include/platform/sdl/window_gl.h @@ -43,6 +43,8 @@ public: SDL_Window * getScreenWindow(); SDL_Surface * getScreenSurface(); + virtual int getScreenWidth(); + virtual int getScreenHeight(); virtual bool ChangeVideoMode(bool preserveContext, int resWidth, int resHeight, bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp index 4868d5c0..3839689a 100644 --- a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -78,10 +78,13 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, //printf("In [%s::%s %d] PlatformCommon::Private::shouldBeFullscreen = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,PlatformCommon::Private::shouldBeFullscreen); - int flags = SDL_WINDOW_OPENGL; + // SDL_WINDOW_FULLSCREEN seems very broken when changing resolutions that differ from the desktop resolution + // For now fullscreen will mean use desktop resolution + int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; if(PlatformCommon::Private::shouldBeFullscreen) { - flags |= SDL_WINDOW_FULLSCREEN; Window::setIsFullScreen(true); + //flags |= SDL_WINDOW_FULLSCREEN; + flags |= SDL_WINDOW_FULLSCREEN_DESKTOP; } else { Window::setIsFullScreen(false); @@ -97,19 +100,44 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to set resolution: %d x %d, colorBits = %d.\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,resW,resH,colorBits); + if(Window::getIsFullScreen() && (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) { + //printf("#1 SDL_WINDOW_FULLSCREEN_DESKTOP\n"); + // TODO: which display? is 0 the designated primary display always? + SDL_Rect display_rect; + SDL_GetDisplayBounds(0, &display_rect); + + if(PlatformCommon::Private::ScreenWidth != display_rect.w || + PlatformCommon::Private::ScreenHeight != display_rect.h) { + printf("Auto Change resolution to (%d x %d) from (%d x %d)\n",display_rect.w,display_rect.h,resW,resH); + resW = display_rect.w; + resH = display_rect.h; + PlatformCommon::Private::ScreenWidth = display_rect.w; + PlatformCommon::Private::ScreenHeight = display_rect.h; + } + } + int windowX = SDL_WINDOWPOS_UNDEFINED; int windowY = SDL_WINDOWPOS_UNDEFINED; string windowTitleText = "MG"; + int windowDisplayID = -1; + if(window != NULL) { SDL_GetWindowPosition(window,&windowX,&windowY); + windowDisplayID = SDL_GetWindowDisplayIndex( window ); + //printf("windowDisplayID = %d\n",windowDisplayID); + if(Window::getIsFullScreen()) { + windowX = SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplayID); + windowY = SDL_WINDOWPOS_CENTERED_DISPLAY(windowDisplayID); + } windowTitleText = SDL_GetWindowTitle(window); - //SDL_FreeSurface(getScreenSurface()); + SDL_DestroyWindow(window); window = NULL; } if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); //screen = SDL_CreateWindow(resW, resH, colorBits, flags); window = SDL_CreateWindow(windowTitleText.c_str(),windowX,windowY,resW, resH, flags); if(window == 0) { @@ -139,20 +167,14 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, } } - if(Window::getIsFullScreen()) { - SDL_SetWindowDisplayMode(window,NULL); - } +// int totalDisplays = SDL_GetNumVideoDisplays(); +// windowDisplayID = SDL_GetWindowDisplayIndex( window ); +// printf("!!! totalDisplays = %d, windowDisplayID = %d\n",totalDisplays,windowDisplayID); - if(glcontext != NULL) { - if(Window::getIsFullScreen()) { - SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN); - } - else { - SDL_SetWindowFullscreen(window,0); - } - } + //SDL_SetWindowDisplayMode(window, NULL); - if(glcontext == NULL) { + bool isNewWindow = (glcontext == NULL); + if(isNewWindow) { glcontext = SDL_GL_CreateContext(window); } else { @@ -162,6 +184,27 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, int h; int w; SDL_GetWindowSize(window, &w, &h); + + if((w != resW || h != resH) && (flags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP) { + //printf("#2 SDL_WINDOW_FULLSCREEN_DESKTOP\n"); + printf("#1 Change resolution mismatch get (%d x %d) desired (%d x %d)\n",w,h,resW,resH); + + PlatformCommon::Private::ScreenWidth = w; + PlatformCommon::Private::ScreenHeight = h; + + resW = PlatformCommon::Private::ScreenWidth; + resH = PlatformCommon::Private::ScreenHeight; + + printf("#2 Change resolution to (%d x %d)\n",resW,resH); + +// SDL_SetWindowFullscreen(window,0); +// SDL_SetWindowSize(window, resW, resH); +// SDL_SetWindowFullscreen(window,flags); +// SDL_SetWindowSize(window, resW, resH); +// +// SDL_GetWindowSize(window, &w, &h); +// printf("#2 Change resolution mismatch get (%d x %d) desired (%d x %d)\n",w,resW,h,resH); + } glViewport( 0, 0, w, h ) ; // There seems to be a bug where if relative mouse mouse is enabled when you create a new window, @@ -287,14 +330,23 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, // SDL_WM_GrabInput(SDL_GRAB_ON); // SDL_WM_GrabInput(SDL_GRAB_OFF); - //SDL_SetRelativeMouseMode(SDL_TRUE); + SDL_SetRelativeMouseMode(SDL_TRUE); SDL_SetRelativeMouseMode(SDL_FALSE); // if(Window::getIsFullScreen()) // SDL_SetWindowGrab(window, SDL_TRUE); // else -// SDL_SetWindowGrab(window, SDL_FALSE); + SDL_SetWindowGrab(window, SDL_FALSE); + +// if(Window::getIsFullScreen()) { +// SDL_SetWindowSize(window, resW, resH); +// //SDL_SetWindowFullscreen(window,SDL_WINDOW_FULLSCREEN); +// SDL_SetWindowSize(window, resW, resH); +// } + +// SDL_SetRelativeMouseMode(SDL_TRUE); +// SDL_SetRelativeMouseMode(SDL_FALSE); } } diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index aed2a6e8..94b31477 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -108,8 +108,8 @@ static bool isUnprintableChar(SDL_keysym key) Window::Window() { this->sdlWindow=0; // Default to 1x1 until set by caller to avoid divide by 0 - this->w = 1; - this->h = 1; + //this->w = 1; + //this->h = 1; for(int idx = 0; idx < mbCount; idx++) { lastMouseDown[idx] = 0; @@ -137,8 +137,8 @@ Window::Window() { Window::Window(SDL_Window *sdlWindow) { this->sdlWindow=sdlWindow; // Default to 1x1 until set by caller to avoid divide by 0 - this->w = 1; - this->h = 1; + //this->w = 1; + //this->h = 1; for(int idx = 0; idx < mbCount; idx++) { lastMouseDown[idx] = 0; @@ -485,8 +485,8 @@ void Window::setText(string text) { } void Window::setSize(int w, int h) { - this->w = w; - this->h = h; + //this->w = w; + //this->h = h; Private::ScreenWidth = w; Private::ScreenHeight = h; } diff --git a/source/shared_lib/sources/platform/sdl/window_gl.cpp b/source/shared_lib/sources/platform/sdl/window_gl.cpp index 9ed30d4e..f5da1799 100644 --- a/source/shared_lib/sources/platform/sdl/window_gl.cpp +++ b/source/shared_lib/sources/platform/sdl/window_gl.cpp @@ -38,6 +38,14 @@ WindowGl::WindowGl(SDL_Window *sdlWindow) : Window(sdlWindow) { WindowGl::~WindowGl() { } +int WindowGl::getScreenWidth() { + return PlatformCommon::Private::ScreenWidth; + +} +int WindowGl::getScreenHeight() { + return PlatformCommon::Private::ScreenHeight; +} + void WindowGl::setGamma(SDL_Window *window,float gammaValue) { //SDL_SetGamma(gammaValue, gammaValue, gammaValue); //SDL_SetWindowGammaRamp(getSDLWindow(), gammaValue, gammaValue, gammaValue);