From 1e7cd63330f839b2ff94b157cd57d6e452a75ece Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Tue, 23 Mar 2010 06:03:16 +0000 Subject: [PATCH] - Some improvements for setting max videomode when AutoMaxFullScreen=true and Windowed=false - Added better logic to try to restore video when crashing --- source/glest_game/main/main.cpp | 3 +- .../include/platform/sdl/platform_util.h | 2 +- .../include/platform/win32/platform_util.h | 2 +- .../sources/platform/sdl/gl_wrap.cpp | 128 ++++++++++++++++++ .../sources/platform/sdl/platform_util.cpp | 61 ++++++++- .../sources/platform/win32/platform_util.cpp | 2 +- 6 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 source/shared_lib/sources/platform/sdl/gl_wrap.cpp diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 69ed9ad10..17a1435d4 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -57,6 +57,7 @@ public: message("An error ocurred and Glest will close.\nError msg = [" + (msg != NULL ? string(msg) : string("?")) + "]\n\nPlease report this bug to "+mailString+", attaching the generated "+getCrashDumpFileName()+" file."); } + restoreVideoMode(true); exit(0); } @@ -71,6 +72,7 @@ public: } if(exitApp == true) { + restoreVideoMode(true); exit(0); } @@ -203,7 +205,6 @@ int glestMain(int argc, char** argv){ } } catch(const exception &e){ - restoreVideoMode(); //exceptionMessage(e); ExceptionHandler::handleRuntimeError(e.what()); } diff --git a/source/shared_lib/include/platform/sdl/platform_util.h b/source/shared_lib/include/platform/sdl/platform_util.h index 8b49988d5..5ceb2687b 100644 --- a/source/shared_lib/include/platform/sdl/platform_util.h +++ b/source/shared_lib/include/platform/sdl/platform_util.h @@ -99,7 +99,7 @@ string extractDirectoryPathFromFile(string filename); void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight); bool changeVideoMode(int resH, int resW, int colorBits, int refreshFrequency); -void restoreVideoMode(); +void restoreVideoMode(bool exitingApp=false); bool EndsWith(const string &str, const string& key); void message(string message); diff --git a/source/shared_lib/include/platform/win32/platform_util.h b/source/shared_lib/include/platform/win32/platform_util.h index 23cea5196..12eba47d6 100755 --- a/source/shared_lib/include/platform/win32/platform_util.h +++ b/source/shared_lib/include/platform/win32/platform_util.h @@ -108,7 +108,7 @@ string extractDirectoryPathFromFile(string filename); void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight); bool changeVideoMode(int resH, int resW, int colorBits, int refreshFrequency); -void restoreVideoMode(); +void restoreVideoMode(bool exitingApp=false); bool EndsWith(const string &str, const string& key); void message(string message); diff --git a/source/shared_lib/sources/platform/sdl/gl_wrap.cpp b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp new file mode 100644 index 000000000..9a87870a4 --- /dev/null +++ b/source/shared_lib/sources/platform/sdl/gl_wrap.cpp @@ -0,0 +1,128 @@ +//This file is part of Glest Shared Library (www.glest.org) +//Copyright (C) 2005 Matthias Braun + +//You can redistribute this code and/or modify it under +//the terms of the GNU General Public License as published by the Free Software +//Foundation; either version 2 of the License, or (at your option) any later +//version. +#include "gl_wrap.h" + +#include +#include +#include +#include + +#include +#ifdef X11_AVAILABLE +#include +#endif + +#include "opengl.h" +#include "sdl_private.h" +#include "noimpl.h" +#include "util.h" + +#include "leak_dumper.h" + +using namespace Shared::Graphics::Gl; +using namespace Shared::Util; + +namespace Shared{ namespace Platform{ + +// ====================================== +// class PlatformContextGl +// ====================================== + +void PlatformContextGl::init(int colorBits, int depthBits, int stencilBits) { + + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 1); + SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 1); + SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 1); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, stencilBits); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, depthBits); + int flags = SDL_OPENGL; + if(Private::shouldBeFullscreen) + flags |= SDL_FULLSCREEN; + + int resW = Private::ScreenWidth; + int resH = Private::ScreenHeight; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] about to set resolution: %d x %d, colorBits = %d.\n",__FILE__,__FUNCTION__,__LINE__,resW,resH,colorBits); + + SDL_Surface* screen = SDL_SetVideoMode(resW, resH, colorBits, flags); + if(screen == 0) { + std::ostringstream msg; + msg << "Couldn't set video mode " + << resW << "x" << resH << " (" << colorBits + << "bpp " << stencilBits << " stencil " + << depthBits << " depth-buffer). SDL Error is: " << SDL_GetError(); + throw std::runtime_error(msg.str()); + } +} + +void PlatformContextGl::end() { +} + +void PlatformContextGl::makeCurrent() { +} + +void PlatformContextGl::swapBuffers() { + SDL_GL_SwapBuffers(); +} + +// ====================================== +// Global Fcs +// ====================================== + +void createGlFontBitmaps(uint32 &base, const string &type, int size, int width, + int charCount, FontMetrics &metrics) { +#ifdef X11_AVAILABLE + Display* display = glXGetCurrentDisplay(); + if(display == 0) { + throw std::runtime_error("Couldn't create font: display is 0"); + } + XFontStruct* fontInfo = XLoadQueryFont(display, type.c_str()); + if(!fontInfo) { + throw std::runtime_error("Font not found."); + } + + // we need the height of 'a' which sould ~ be half ascent+descent + metrics.setHeight(static_cast + (fontInfo->ascent + fontInfo->descent) / 2); + for(unsigned int i = 0; i < static_cast (charCount); ++i) { + if(i < fontInfo->min_char_or_byte2 || + i > fontInfo->max_char_or_byte2) { + metrics.setWidth(i, static_cast(6)); + } else { + int p = i - fontInfo->min_char_or_byte2; + metrics.setWidth(i, static_cast ( + fontInfo->per_char[p].rbearing + - fontInfo->per_char[p].lbearing)); + } + } + + glXUseXFont(fontInfo->fid, 0, charCount, base); + XFreeFont(display, fontInfo); +#else + // we badly need a solution portable to more than just glx + NOIMPL; +#endif +} + +void createGlFontOutlines(uint32 &base, const string &type, int width, + float depth, int charCount, FontMetrics &metrics) { + NOIMPL; +} + +const char *getPlatformExtensions(const PlatformContextGl *pcgl) { + return ""; +} + +void *getGlProcAddress(const char *procName) { + void* proc = SDL_GL_GetProcAddress(procName); + assert(proc!=NULL); + return proc; +} + +}}//end namespace diff --git a/source/shared_lib/sources/platform/sdl/platform_util.cpp b/source/shared_lib/sources/platform/sdl/platform_util.cpp index 209ae4ae7..30d826faa 100644 --- a/source/shared_lib/sources/platform/sdl/platform_util.cpp +++ b/source/shared_lib/sources/platform/sdl/platform_util.cpp @@ -437,10 +437,60 @@ void createDirectoryPaths(string Path) void getFullscreenVideoInfo(int &colorBits,int &screenWidth,int &screenHeight) { // Get the current video hardware information - const SDL_VideoInfo* vidInfo = SDL_GetVideoInfo(); - colorBits = vidInfo->vfmt->BitsPerPixel; - screenWidth = vidInfo->current_w; - screenHeight = vidInfo->current_h; + //const SDL_VideoInfo* vidInfo = SDL_GetVideoInfo(); + //colorBits = vidInfo->vfmt->BitsPerPixel; + //screenWidth = vidInfo->current_w; + //screenHeight = vidInfo->current_h; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + /* Get available fullscreen/hardware modes */ + SDL_Rect**modes = SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE); + + /* Check if there are any modes available */ + if (modes == (SDL_Rect**)0) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] no hardware modes available.\n",__FILE__,__FUNCTION__,__LINE__); + + const SDL_VideoInfo* vidInfo = SDL_GetVideoInfo(); + colorBits = vidInfo->vfmt->BitsPerPixel; + screenWidth = vidInfo->current_w; + screenHeight = vidInfo->current_h; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] using current resolution: %d x %d.\n",__FILE__,__FUNCTION__,__LINE__,screenWidth,screenHeight); + } + /* Check if our resolution is restricted */ + else if (modes == (SDL_Rect**)-1) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] all resolutions available.\n",__FILE__,__FUNCTION__,__LINE__); + + const SDL_VideoInfo* vidInfo = SDL_GetVideoInfo(); + colorBits = vidInfo->vfmt->BitsPerPixel; + screenWidth = vidInfo->current_w; + screenHeight = vidInfo->current_h; + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] using current resolution: %d x %d.\n",__FILE__,__FUNCTION__,__LINE__,screenWidth,screenHeight); + } + else{ + /* Print valid modes */ + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] available Modes are:\n",__FILE__,__FUNCTION__,__LINE__); + + int bestW = -1; + int bestH = -1; + for(int i=0; modes[i]; ++i) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"%d x %d\n",modes[i]->w, modes[i]->h); + + if(bestW < modes[i]->w) { + bestW = modes[i]->w; + bestH = modes[i]->h; + } + } + + if(bestW > screenWidth) { + screenWidth = bestW; + screenHeight = bestH; + } + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] using current resolution: %d x %d.\n",__FILE__,__FUNCTION__,__LINE__,screenWidth,screenHeight); + } } bool changeVideoMode(int resW, int resH, int colorBits, int ) { @@ -448,7 +498,8 @@ bool changeVideoMode(int resW, int resH, int colorBits, int ) { return true; } -void restoreVideoMode() { +void restoreVideoMode(bool exitingApp) { + SDL_Quit(); } void message(string message) { diff --git a/source/shared_lib/sources/platform/win32/platform_util.cpp b/source/shared_lib/sources/platform/win32/platform_util.cpp index 153f3c975..20ada2b95 100644 --- a/source/shared_lib/sources/platform/win32/platform_util.cpp +++ b/source/shared_lib/sources/platform/win32/platform_util.cpp @@ -535,7 +535,7 @@ bool changeVideoMode(int resW, int resH, int colorBits, int refreshFrequency){ return false; } -void restoreVideoMode(){ +void restoreVideoMode(bool exitingApp) { int dispChangeErr= ChangeDisplaySettings(NULL, 0); assert(dispChangeErr==DISP_CHANGE_SUCCESSFUL); }