diff --git a/source/glest_game/global/metrics.cpp b/source/glest_game/global/metrics.cpp index 37a32c03..e9644f2c 100644 --- a/source/glest_game/global/metrics.cpp +++ b/source/glest_game/global/metrics.cpp @@ -24,6 +24,15 @@ namespace Glest{ namespace Game{ // ===================================================== Metrics::Metrics() { + reloadData(); +} + +void Metrics::reload() { + Metrics *metrics = getInstancePtr(); + metrics->reloadData(); +} + +void Metrics::reloadData() { Config &config = Config::getInstance(); virtualW= 1000; @@ -43,9 +52,14 @@ Metrics::Metrics() { displayH= 480; } +Metrics * Metrics::getInstancePtr() { + static Metrics metrics; + return &metrics; +} + const Metrics &Metrics::getInstance(){ - static const Metrics metrics; - return metrics; + Metrics *metrics = getInstancePtr(); + return *metrics; } float Metrics::getAspectRatio() const{ diff --git a/source/glest_game/global/metrics.h b/source/glest_game/global/metrics.h index 0254d854..29ee2703 100644 --- a/source/glest_game/global/metrics.h +++ b/source/glest_game/global/metrics.h @@ -43,6 +43,8 @@ private: private: Metrics(); + static Metrics *getInstancePtr(); + void reloadData(); public: static const Metrics &getInstance(); @@ -66,6 +68,8 @@ public: bool isInDisplay(int x, int y) const; bool isInMinimap(int x, int y) const; + + static void reload(); }; }}//end namespace diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index f67a09ce..507032b2 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -785,15 +785,8 @@ void Program::setDisplaySettings(){ config.setInt("ScreenWidth",screenWidth); config.setInt("ScreenHeight",screenHeight); } - - if(!(changeVideoMode(screenWidth, screenHeight, colorBits, freq) || - changeVideoMode(screenWidth, screenHeight, colorBits, 0))) - { - throw megaglest_runtime_error( - "Error setting video mode: " + - intToStr(screenWidth) + "x" + intToStr(screenHeight) + "x" + intToStr(colorBits)); - } } + changeVideoModeFullScreen(!config.getBool("Windowed")); } void Program::restoreDisplaySettings(){ diff --git a/source/glest_game/menu/menu_state_options.cpp b/source/glest_game/menu/menu_state_options.cpp index f43922f5..b7a69a63 100644 --- a/source/glest_game/menu/menu_state_options.cpp +++ b/source/glest_game/menu/menu_state_options.cpp @@ -22,6 +22,7 @@ #include "menu_state_graphic_info.h" #include "menu_state_keysetup.h" #include "string_utils.h" +#include "metrics.h" #include "leak_dumper.h" using namespace Shared::Util; @@ -859,21 +860,19 @@ void MenuStateOptions::mouseClick(int x, int y, MouseButton mouseButton){ CoreData &coreData= CoreData::getInstance(); SoundRenderer &soundRenderer= SoundRenderer::getInstance(); - if(mainMessageBox.getEnabled()){ + if(mainMessageBox.getEnabled()) { int button= 0; - if(mainMessageBox.mouseClick(x, y, button)) - { + if(mainMessageBox.mouseClick(x, y, button)) { soundRenderer.playFx(coreData.getClickSoundA()); - if(button==0) - { - if(mainMessageBoxState==1) - { + if(button==0) { + if(mainMessageBoxState==1) { mainMessageBox.setEnabled(false); saveConfig(); mainMenu->setState(new MenuStateRoot(program, mainMenu)); } - else + else { mainMessageBox.setEnabled(false); + } } } } @@ -904,15 +903,15 @@ void MenuStateOptions::mouseClick(int x, int y, MouseButton mouseButton){ string currentResolution=config.getString("ScreenWidth")+"x"+config.getString("ScreenHeight")+"-"+intToStr(config.getInt("ColorBits")); string selectedResolution=listBoxScreenModes.getSelectedItem(); - if(currentResolution!=selectedResolution){ - mainMessageBoxState=1; - Lang &lang= Lang::getInstance(); - showMessageBox(lang.get("RestartNeeded"), lang.get("ResolutionChanged"), false); - return; + if(currentResolution != selectedResolution){ + //mainMessageBoxState=1; + //Lang &lang= Lang::getInstance(); + //showMessageBox(lang.get("RestartNeeded"), lang.get("ResolutionChanged"), false); + //return; } string currentFontSizeAdjustment=config.getString("FontSizeAdjustment"); string selectedFontSizeAdjustment=listFontSizeAdjustment.getSelectedItem(); - if(currentFontSizeAdjustment!=selectedFontSizeAdjustment){ + if(currentFontSizeAdjustment != selectedFontSizeAdjustment){ mainMessageBoxState=1; Lang &lang= Lang::getInstance(); showMessageBox(lang.get("RestartNeeded"), lang.get("FontSizeAdjustmentChanged"), false); @@ -921,14 +920,35 @@ void MenuStateOptions::mouseClick(int x, int y, MouseButton mouseButton){ bool currentFullscreenWindowed=config.getBool("Windowed"); bool selectedFullscreenWindowed = checkBoxFullscreenWindowed.getValue(); - if(currentFullscreenWindowed!=selectedFullscreenWindowed){ - mainMessageBoxState=1; - Lang &lang= Lang::getInstance(); - showMessageBox(lang.get("RestartNeeded"), lang.get("DisplaySettingsChanged"), false); - return; + if(currentFullscreenWindowed != selectedFullscreenWindowed) { + //mainMessageBoxState=1; + //Lang &lang= Lang::getInstance(); + //showMessageBox(lang.get("RestartNeeded"), lang.get("DisplaySettingsChanged"), false); + //return; } saveConfig(); + + if(currentResolution != selectedResolution || + currentFullscreenWindowed != selectedFullscreenWindowed) { + + changeVideoModeFullScreen(!config.getBool("Windowed")); + WindowGl *window = this->program->getWindow(); + window->ChangeVideoMode(true, + config.getInt("ScreenWidth"), + config.getInt("ScreenHeight"), + !config.getBool("Windowed"), + config.getInt("ColorBits"), + config.getInt("DepthBits"), + config.getInt("StencilBits"), + config.getBool("HardwareAcceleration","false"), + config.getBool("FullScreenAntiAliasing","false"), + config.getFloat("GammaValue","0.0")); + + Metrics::reload(); + this->mainMenu->init(); + } + mainMenu->setState(new MenuStateRoot(program, mainMenu)); return; } diff --git a/source/shared_lib/include/platform/common/platform_common.h b/source/shared_lib/include/platform/common/platform_common.h index 8b894bcb..cdc9d199 100644 --- a/source/shared_lib/include/platform/common/platform_common.h +++ b/source/shared_lib/include/platform/common/platform_common.h @@ -213,7 +213,7 @@ string extractExtension(const string& filename); void getFullscreenVideoModes(vector *modeinfos,bool isFullscreen); void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight,bool isFullscreen); -bool changeVideoMode(int resH, int resW, int colorBits, int refreshFrequency); +void changeVideoModeFullScreen(bool value); void restoreVideoMode(bool exitingApp=false); bool StartsWith(const std::string &str, const std::string &key); diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index 431ce952..8755fa8b 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -143,6 +143,10 @@ public: Window(); virtual ~Window(); + virtual bool ChangeVideoMode(bool preserveContext,int resWidth, int resHeight, + bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, + bool hardware_acceleration, bool fullscreen_anti_aliasing, + float gammaValue) = 0; //static void setMasterserverMode(bool value) { Window::masterserverMode = value;} //static bool getMasterserverMode() { return Window::masterserverMode;} diff --git a/source/shared_lib/include/platform/sdl/window_gl.h b/source/shared_lib/include/platform/sdl/window_gl.h index ce628c94..ae45557c 100644 --- a/source/shared_lib/include/platform/sdl/window_gl.h +++ b/source/shared_lib/include/platform/sdl/window_gl.h @@ -35,6 +35,11 @@ public: void makeCurrentGl(); void swapBuffersGl(); void setGamma(float gammaValue){context.setGammaValue(gammaValue);} + + virtual bool ChangeVideoMode(bool preserveContext, int resWidth, int resHeight, + bool fullscreenWindow, int colorBits, int depthBits, int stencilBits, + bool hardware_acceleration, bool fullscreen_anti_aliasing, + float gammaValue); }; }}//end namespace diff --git a/source/shared_lib/sources/platform/common/platform_common.cpp b/source/shared_lib/sources/platform/common/platform_common.cpp index 5e12bab1..dd01717f 100644 --- a/source/shared_lib/sources/platform/common/platform_common.cpp +++ b/source/shared_lib/sources/platform/common/platform_common.cpp @@ -1694,9 +1694,8 @@ void getFullscreenVideoModes(vector *modeinfos, bool isFullscreen) { -bool changeVideoMode(int resW, int resH, int colorBits, int ) { - Private::shouldBeFullscreen = true; - return true; +void changeVideoModeFullScreen(bool value) { + Private::shouldBeFullscreen = value; } void restoreVideoMode(bool exitingApp) { diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp index 030f74e3..ed340bfc 100644 --- a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -105,6 +105,8 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, // try different color bits screen = SDL_SetVideoMode(resW, resH, i, flags); if(screen != 0) { + glViewport( 0, 0, resW, resH ) ; + break; } } @@ -116,6 +118,8 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, if(screen != 0) { resW = 800; resH = 600; + + glViewport( 0, 0, resW, resH ) ; break; } } @@ -127,6 +131,8 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, if(screen != 0) { resW = 640; resH = 480; + + glViewport( 0, 0, resW, resH ) ; break; } } @@ -136,6 +142,10 @@ void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits, throw std::runtime_error(msg.str()); } } + else { + glViewport( 0, 0, resW, resH ) ; + //printf("Reset resolution to [%d] x [%d]\n",resW, resH); + } #ifndef WIN32 string mg_icon_file = ""; diff --git a/source/shared_lib/sources/platform/sdl/window_gl.cpp b/source/shared_lib/sources/platform/sdl/window_gl.cpp index 05cfe766..b1d0e3fa 100644 --- a/source/shared_lib/sources/platform/sdl/window_gl.cpp +++ b/source/shared_lib/sources/platform/sdl/window_gl.cpp @@ -12,9 +12,12 @@ #include "gl_wrap.h" #include "graphics_interface.h" +#include "util.h" +#include "platform_util.h" #include "leak_dumper.h" using namespace Shared::Graphics; +using namespace Shared::Util; namespace Shared{ namespace Platform{ @@ -44,4 +47,90 @@ void WindowGl::swapBuffersGl(){ context.swapBuffers(); } +// changes display resolution at any time +bool WindowGl::ChangeVideoMode(bool preserveContext, int resWidth, int resHeight, + bool fullscreenWindow, + int colorBits, int depthBits, int stencilBits, bool hardware_acceleration, + bool fullscreen_anti_aliasing, float gammaValue) { + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("In [%s::%s %d] preserveContext = %d\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__,preserveContext); + +#ifdef WIN32 + if(preserveContext == true) { + SDL_SysWMinfo info; + + // get window handle from SDL + SDL_VERSION(&info.version); + if (SDL_GetWMInfo(&info) == -1) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] SDL_GetWMInfo #1 failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + + // get device context handle + HDC tempDC = GetDC( info.window ); + + // create temporary context + HGLRC tempRC = wglCreateContext( tempDC ); + if (tempRC == NULL) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] wglCreateContext failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + + // share resources to temporary context + SetLastError(0); + if (!wglShareLists(info.hglrc, tempRC)) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] wglShareLists #1 failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + } +#endif + + // set video mode + //if (!SetVideoMode()) + // return false; +// this->initGl(config.getInt("ColorBits"), +// config.getInt("DepthBits"), +// config.getInt("StencilBits"), +// config.getBool("HardwareAcceleration","false"), +// config.getBool("FullScreenAntiAliasing","false"), +// config.getFloat("GammaValue","0.0")); + + + this->setStyle(fullscreenWindow ? wsWindowedFixed: wsFullscreen); + this->setPos(0, 0); + this->setSize(resWidth, resHeight); + + this->initGl(colorBits, depthBits, stencilBits,hardware_acceleration, + fullscreen_anti_aliasing,gammaValue); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + + this->makeCurrentGl(); + +#ifdef WIN32 + if(preserveContext == true) { + // previously used structure may possibly be invalid, to be sure we get it again + SDL_VERSION(&info.version); + if (SDL_GetWMInfo(&info) == -1) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] SDL_GetWMInfo #2 failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + + // share resources to new SDL-created context + if (!wglShareLists(tempRC, info.hglrc)) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] wglShareLists #2 failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + + // we no longer need our temporary context + if (!wglDeleteContext(tempRC)) { + if(SystemFlags::getSystemSettingType(SystemFlags::debugError).enabled) SystemFlags::OutputDebug(SystemFlags::debugError,"In [%s::%s %d] wglDeleteContext failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + return false; + } + } +#endif + + // success + return true; +} + + }}//end namespace