From 19345908cba1bf12a138162b18d6db39662cb762 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 24 Jan 2021 11:54:36 +0100 Subject: Codechange: [SDL2] Split away CreateMainWindow from CreateMainSurface This makes the code a bit more readable, as both intentions are more clear, and there is less nesting in the main function. --- src/video/sdl2_v.cpp | 81 ++++++++++++++++++++++++++++------------------------ src/video/sdl2_v.h | 1 + 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index cb9105357..e4518d5b3 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -280,56 +280,61 @@ static uint FindStartupDisplay(uint startup_display) return 0; } -bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize) +bool VideoDriver_SDL::CreateMainWindow(uint w, uint h) { - int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth(); + if (_sdl_window != nullptr) return true; - GetAvailableVideoMode(&w, &h); + Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; - DEBUG(driver, 1, "SDL2: using mode %ux%ux%d", w, h, bpp); + if (_fullscreen) { + flags |= SDL_WINDOW_FULLSCREEN; + } + + int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; + SDL_Rect r; + if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) { + x = r.x + std::max(0, r.w - static_cast(w)) / 2; + y = r.y + std::max(0, r.h - static_cast(h)) / 4; // decent desktops have taskbars at the bottom + } + + char caption[50]; + seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision); + _sdl_window = SDL_CreateWindow( + caption, + x, y, + w, h, + flags); if (_sdl_window == nullptr) { - Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE; + DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on"); + return false; + } - if (_fullscreen) { - flags |= SDL_WINDOW_FULLSCREEN; - } + std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp"); + if (!icon_path.empty()) { + /* Give the application an icon */ + SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str()); + if (icon != nullptr) { + /* Get the colourkey, which will be magenta */ + uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255); - int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED; - SDL_Rect r; - if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) { - x = r.x + std::max(0, r.w - static_cast(w)) / 2; - y = r.y + std::max(0, r.h - static_cast(h)) / 4; // decent desktops have taskbars at the bottom + SDL_SetColorKey(icon, SDL_TRUE, rgbmap); + SDL_SetWindowIcon(_sdl_window, icon); + SDL_FreeSurface(icon); } + } - char caption[50]; - seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision); - _sdl_window = SDL_CreateWindow( - caption, - x, y, - w, h, - flags); + return true; +} - if (_sdl_window == nullptr) { - DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on"); - return false; - } +bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize) +{ + int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth(); - std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp"); - if (!icon_path.empty()) { - /* Give the application an icon */ - SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str()); - if (icon != nullptr) { - /* Get the colourkey, which will be magenta */ - uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255); - - SDL_SetColorKey(icon, SDL_TRUE, rgbmap); - SDL_SetWindowIcon(_sdl_window, icon); - SDL_FreeSurface(icon); - } - } - } + GetAvailableVideoMode(&w, &h); + DEBUG(driver, 1, "SDL2: using mode %ux%ux%d", w, h, bpp); + if (!this->CreateMainWindow(w, h)) return false; if (resize) SDL_SetWindowSize(_sdl_window, w, h); _sdl_real_surface = SDL_GetWindowSurface(_sdl_window); diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h index de10543b5..a72325674 100644 --- a/src/video/sdl2_v.h +++ b/src/video/sdl2_v.h @@ -49,6 +49,7 @@ private: void LoopOnce(); void MainLoopCleanup(); bool CreateMainSurface(uint w, uint h, bool resize); + bool CreateMainWindow(uint w, uint h); #ifdef __EMSCRIPTEN__ /* Convert a constant pointer back to a non-constant pointer to a member function. */ -- cgit v1.2.3-54-g00ecf