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.
This commit is contained in:
Patric Stout 2021-01-24 11:54:36 +01:00 committed by Patric Stout
parent 8c37e5c526
commit 19345908cb
2 changed files with 49 additions and 43 deletions

View File

@ -280,56 +280,61 @@ static uint FindStartupDisplay(uint startup_display)
return 0;
}
bool VideoDriver_SDL::CreateMainWindow(uint w, uint h)
{
if (_sdl_window != nullptr) return true;
Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
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<int>(w)) / 2;
y = r.y + std::max(0, r.h - static_cast<int>(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) {
DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on");
return false;
}
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);
}
}
return true;
}
bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
{
int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
GetAvailableVideoMode(&w, &h);
DEBUG(driver, 1, "SDL2: using mode %ux%ux%d", w, h, bpp);
if (_sdl_window == nullptr) {
Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
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<int>(w)) / 2;
y = r.y + std::max(0, r.h - static_cast<int>(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) {
DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on");
return false;
}
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);
}
}
}
if (!this->CreateMainWindow(w, h)) return false;
if (resize) SDL_SetWindowSize(_sdl_window, w, h);
_sdl_real_surface = SDL_GetWindowSurface(_sdl_window);

View File

@ -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. */