summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-11 09:34:10 +0100
committerMichael Lutz <michi@icosahedron.de>2021-02-22 22:16:07 +0100
commit0d58bc9384cbe95b696e852dbc47c4deaca6a025 (patch)
treeaff9bd7e0bee10f4352024ff22e204c34dcbda37
parent6098811b493c205797d7745e70db1e6f07879f72 (diff)
downloadopenttd-0d58bc9384cbe95b696e852dbc47c4deaca6a025.tar.xz
Codechange: [SDL2] Move SDLSurface code to its own function
This increases readability, and allow future subdrivers to not use SDLSurface to draw.
-rw-r--r--src/video/sdl2_v.cpp42
-rw-r--r--src/video/sdl2_v.h1
2 files changed, 23 insertions, 20 deletions
diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp
index 052396e6b..56923c4cf 100644
--- a/src/video/sdl2_v.cpp
+++ b/src/video/sdl2_v.cpp
@@ -309,19 +309,33 @@ bool VideoDriver_SDL::CreateMainWindow(uint w, uint h)
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);
+ DEBUG(driver, 1, "SDL2: using mode %ux%u", w, h);
if (!this->CreateMainWindow(w, h)) return false;
if (resize) SDL_SetWindowSize(_sdl_window, w, h);
+ if (!this->AllocateBackingStore(w, h, true)) return false;
+
+ /* When in full screen, we will always have the mouse cursor
+ * within the window, even though SDL does not give us the
+ * appropriate event to know this. */
+ if (_fullscreen) _cursor.in_window = true;
+
+ BlitterFactory::GetCurrentBlitter()->PostResize();
+
+ GameSizeChanged();
+ return true;
+}
+
+bool VideoDriver_SDL::AllocateBackingStore(int w, int h, bool force)
+{
+ int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
+
_sdl_real_surface = SDL_GetWindowSurface(_sdl_window);
- if (_sdl_real_surface == nullptr) {
- DEBUG(driver, 0, "SDL2: Couldn't get window surface: %s", SDL_GetError());
- return false;
- }
+ if (_sdl_real_surface == nullptr) usererror("SDL2: Couldn't get window surface: %s", SDL_GetError());
+
+ if (!force && w == _sdl_real_surface->w && h == _sdl_real_surface->h) return false;
/* Free any previously allocated rgb surface. */
if (_sdl_rgb_surface != nullptr) {
@@ -331,11 +345,7 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
if (bpp == 8) {
_sdl_rgb_surface = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
-
- if (_sdl_rgb_surface == nullptr) {
- DEBUG(driver, 0, "SDL2: Couldn't allocate shadow surface: %s", SDL_GetError());
- return false;
- }
+ if (_sdl_rgb_surface == nullptr) usererror("SDL2: Couldn't allocate shadow surface: %s", SDL_GetError());
_sdl_surface = _sdl_rgb_surface;
} else {
@@ -356,14 +366,6 @@ bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
MakePalette();
- /* When in full screen, we will always have the mouse cursor
- * within the window, even though SDL does not give us the
- * appropriate event to know this. */
- if (_fullscreen) _cursor.in_window = true;
-
- BlitterFactory::GetCurrentBlitter()->PostResize();
-
- GameSizeChanged();
return true;
}
diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h
index 35697991f..74d811aaf 100644
--- a/src/video/sdl2_v.h
+++ b/src/video/sdl2_v.h
@@ -57,6 +57,7 @@ private:
bool CreateMainSurface(uint w, uint h, bool resize);
bool CreateMainWindow(uint w, uint h);
const char *Initialize();
+ bool AllocateBackingStore(int w, int h, bool force = false);
#ifdef __EMSCRIPTEN__
/* Convert a constant pointer back to a non-constant pointer to a member function. */