Changeset - r24712:655f7e7d3209
[Not reviewed]
master
0 2 0
Patric Stout - 4 years ago 2021-01-24 10:54:36
truebrain@openttd.org
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.
2 files changed with 49 insertions and 43 deletions:
0 comments (0 inline, 0 general)
src/video/sdl2_v.cpp
Show inline comments
 
@@ -280,56 +280,61 @@ static uint FindStartupDisplay(uint star
 
	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);
src/video/sdl2_v.h
Show inline comments
 
@@ -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. */
0 comments (0 inline, 0 general)