Fix #10490: Allow ships to exit depots if another is not moving at the exit point...
[openttd-github.git] / src / video / sdl2_v.cpp
blob2bb381f5ecbd910af39843847a1f92bfb7af144e
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file sdl2_v.cpp Implementation of the SDL2 video driver. */
10 #include "../stdafx.h"
11 #include "../openttd.h"
12 #include "../gfx_func.h"
13 #include "../blitter/factory.hpp"
14 #include "../thread.h"
15 #include "../progress.h"
16 #include "../core/random_func.hpp"
17 #include "../core/math_func.hpp"
18 #include "../core/mem_func.hpp"
19 #include "../core/geometry_func.hpp"
20 #include "../fileio_func.h"
21 #include "../framerate_type.h"
22 #include "../window_func.h"
23 #include "sdl2_v.h"
24 #include <SDL.h>
25 #ifdef __EMSCRIPTEN__
26 # include <emscripten.h>
27 # include <emscripten/html5.h>
28 #endif
30 #include "../safeguards.h"
32 void VideoDriver_SDL_Base::MakeDirty(int left, int top, int width, int height)
34 Rect r = {left, top, left + width, top + height};
35 this->dirty_rect = BoundingRect(this->dirty_rect, r);
38 void VideoDriver_SDL_Base::CheckPaletteAnim()
40 if (!CopyPalette(this->local_palette)) return;
41 this->MakeDirty(0, 0, _screen.width, _screen.height);
44 static const Dimension default_resolutions[] = {
45 { 640, 480 },
46 { 800, 600 },
47 { 1024, 768 },
48 { 1152, 864 },
49 { 1280, 800 },
50 { 1280, 960 },
51 { 1280, 1024 },
52 { 1400, 1050 },
53 { 1600, 1200 },
54 { 1680, 1050 },
55 { 1920, 1200 }
58 static void FindResolutions()
60 _resolutions.clear();
62 for (int display = 0; display < SDL_GetNumVideoDisplays(); display++) {
63 for (int i = 0; i < SDL_GetNumDisplayModes(display); i++) {
64 SDL_DisplayMode mode;
65 SDL_GetDisplayMode(display, i, &mode);
67 if (mode.w < 640 || mode.h < 480) continue;
68 if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
69 _resolutions.emplace_back(mode.w, mode.h);
73 /* We have found no resolutions, show the default list */
74 if (_resolutions.empty()) {
75 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
78 SortResolutions();
81 static void GetAvailableVideoMode(uint *w, uint *h)
83 /* All modes available? */
84 if (!_fullscreen || _resolutions.empty()) return;
86 /* Is the wanted mode among the available modes? */
87 if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
89 /* Use the closest possible resolution */
90 uint best = 0;
91 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
92 for (uint i = 1; i != _resolutions.size(); ++i) {
93 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
94 if (newdelta < delta) {
95 best = i;
96 delta = newdelta;
99 *w = _resolutions[best].width;
100 *h = _resolutions[best].height;
103 static uint FindStartupDisplay(uint startup_display)
105 int num_displays = SDL_GetNumVideoDisplays();
107 /* If the user indicated a valid monitor, use that. */
108 if (IsInsideBS(startup_display, 0, num_displays)) return startup_display;
110 /* Mouse position decides which display to use. */
111 int mx, my;
112 SDL_GetGlobalMouseState(&mx, &my);
113 for (int display = 0; display < num_displays; ++display) {
114 SDL_Rect r;
115 if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
116 Debug(driver, 1, "SDL2: Mouse is at ({}, {}), use display {} ({}, {}, {}, {})", mx, my, display, r.x, r.y, r.w, r.h);
117 return display;
121 return 0;
124 void VideoDriver_SDL_Base::ClientSizeChanged(int w, int h, bool force)
126 /* Allocate backing store of the new size. */
127 if (this->AllocateBackingStore(w, h, force)) {
128 CopyPalette(this->local_palette, true);
130 BlitterFactory::GetCurrentBlitter()->PostResize();
132 GameSizeChanged();
136 bool VideoDriver_SDL_Base::CreateMainWindow(uint w, uint h, uint flags)
138 if (this->sdl_window != nullptr) return true;
140 flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
142 if (_fullscreen) {
143 flags |= SDL_WINDOW_FULLSCREEN;
146 int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
147 SDL_Rect r;
148 if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
149 x = r.x + std::max(0, r.w - static_cast<int>(w)) / 2;
150 y = r.y + std::max(0, r.h - static_cast<int>(h)) / 4; // decent desktops have taskbars at the bottom
153 std::string caption = VideoDriver::GetCaption();
154 this->sdl_window = SDL_CreateWindow(
155 caption.c_str(),
156 x, y,
157 w, h,
158 flags);
160 if (this->sdl_window == nullptr) {
161 Debug(driver, 0, "SDL2: Couldn't allocate a window to draw on: {}", SDL_GetError());
162 return false;
165 std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
166 if (!icon_path.empty()) {
167 /* Give the application an icon */
168 SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
169 if (icon != nullptr) {
170 /* Get the colourkey, which will be magenta */
171 uint32_t rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
173 SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
174 SDL_SetWindowIcon(this->sdl_window, icon);
175 SDL_FreeSurface(icon);
179 return true;
182 bool VideoDriver_SDL_Base::CreateMainSurface(uint w, uint h, bool resize)
184 GetAvailableVideoMode(&w, &h);
185 Debug(driver, 1, "SDL2: using mode {}x{}", w, h);
187 if (!this->CreateMainWindow(w, h)) return false;
188 if (resize) SDL_SetWindowSize(this->sdl_window, w, h);
189 this->ClientSizeChanged(w, h, true);
191 /* When in full screen, we will always have the mouse cursor
192 * within the window, even though SDL does not give us the
193 * appropriate event to know this. */
194 if (_fullscreen) _cursor.in_window = true;
196 return true;
199 bool VideoDriver_SDL_Base::ClaimMousePointer()
201 /* Emscripten never claims the pointer, so we do not need to change the cursor visibility. */
202 #ifndef __EMSCRIPTEN__
203 SDL_ShowCursor(0);
204 #endif
205 return true;
209 * This is called to indicate that an edit box has gained focus, text input mode should be enabled.
211 void VideoDriver_SDL_Base::EditBoxGainedFocus()
213 if (!this->edit_box_focused) {
214 SDL_StartTextInput();
215 this->edit_box_focused = true;
220 * This is called to indicate that an edit box has lost focus, text input mode should be disabled.
222 void VideoDriver_SDL_Base::EditBoxLostFocus()
224 if (this->edit_box_focused) {
225 SDL_StopTextInput();
226 this->edit_box_focused = false;
230 std::vector<int> VideoDriver_SDL_Base::GetListOfMonitorRefreshRates()
232 std::vector<int> rates = {};
233 for (int i = 0; i < SDL_GetNumVideoDisplays(); i++) {
234 SDL_DisplayMode mode = {};
235 if (SDL_GetDisplayMode(i, 0, &mode) != 0) continue;
236 if (mode.refresh_rate != 0) rates.push_back(mode.refresh_rate);
238 return rates;
242 struct SDLVkMapping {
243 SDL_Keycode vk_from;
244 byte vk_count;
245 byte map_to;
246 bool unprintable;
249 #define AS(x, z) {x, 0, z, false}
250 #define AM(x, y, z, w) {x, (byte)(y - x), z, false}
251 #define AS_UP(x, z) {x, 0, z, true}
252 #define AM_UP(x, y, z, w) {x, (byte)(y - x), z, true}
254 static const SDLVkMapping _vk_mapping[] = {
255 /* Pageup stuff + up/down */
256 AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
257 AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
258 AS_UP(SDLK_UP, WKC_UP),
259 AS_UP(SDLK_DOWN, WKC_DOWN),
260 AS_UP(SDLK_LEFT, WKC_LEFT),
261 AS_UP(SDLK_RIGHT, WKC_RIGHT),
263 AS_UP(SDLK_HOME, WKC_HOME),
264 AS_UP(SDLK_END, WKC_END),
266 AS_UP(SDLK_INSERT, WKC_INSERT),
267 AS_UP(SDLK_DELETE, WKC_DELETE),
269 /* Map letters & digits */
270 AM(SDLK_a, SDLK_z, 'A', 'Z'),
271 AM(SDLK_0, SDLK_9, '0', '9'),
273 AS_UP(SDLK_ESCAPE, WKC_ESC),
274 AS_UP(SDLK_PAUSE, WKC_PAUSE),
275 AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
277 AS(SDLK_SPACE, WKC_SPACE),
278 AS(SDLK_RETURN, WKC_RETURN),
279 AS(SDLK_TAB, WKC_TAB),
281 /* Function keys */
282 AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
284 /* Numeric part. */
285 AM(SDLK_KP_0, SDLK_KP_9, '0', '9'),
286 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
287 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
288 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
289 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
290 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
291 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
293 /* Other non-letter keys */
294 AS(SDLK_SLASH, WKC_SLASH),
295 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
296 AS(SDLK_EQUALS, WKC_EQUALS),
297 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
298 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
299 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
301 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
302 AS(SDLK_COMMA, WKC_COMMA),
303 AS(SDLK_MINUS, WKC_MINUS),
304 AS(SDLK_PERIOD, WKC_PERIOD)
307 static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, char32_t *character)
309 const SDLVkMapping *map;
310 uint key = 0;
311 bool unprintable = false;
313 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
314 if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
315 key = sym->sym - map->vk_from + map->map_to;
316 unprintable = map->unprintable;
317 break;
321 /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
322 if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
324 /* META are the command keys on mac */
325 if (sym->mod & KMOD_GUI) key |= WKC_META;
326 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
327 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
328 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
330 /* The mod keys have no character. Prevent '?' */
331 if (sym->mod & KMOD_GUI ||
332 sym->mod & KMOD_CTRL ||
333 sym->mod & KMOD_ALT ||
334 unprintable) {
335 *character = WKC_NONE;
336 } else {
337 *character = sym->sym;
340 return key;
344 * Like ConvertSdlKeyIntoMy(), but takes an SDL_Keycode as input
345 * instead of an SDL_Keysym.
347 static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
349 const SDLVkMapping *map;
350 uint key = 0;
352 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
353 if ((uint)(kc - map->vk_from) <= map->vk_count) {
354 key = kc - map->vk_from + map->map_to;
355 break;
359 /* check scancode for BACKQUOTE key, because we want the key left
360 * of "1", not anything else (on non-US keyboards) */
361 SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
362 if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
364 return key;
367 bool VideoDriver_SDL_Base::PollEvent()
369 SDL_Event ev;
371 if (!SDL_PollEvent(&ev)) return false;
373 switch (ev.type) {
374 case SDL_MOUSEMOTION: {
375 int32_t x = ev.motion.x;
376 int32_t y = ev.motion.y;
378 if (_cursor.fix_at) {
379 /* Get all queued mouse events now in case we have to warp the cursor. In the
380 * end, we only care about the current mouse position and not bygone events. */
381 while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION)) {
382 x = ev.motion.x;
383 y = ev.motion.y;
387 if (_cursor.UpdateCursorPosition(x, y)) {
388 SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y);
390 HandleMouseEvents();
391 break;
394 case SDL_MOUSEWHEEL:
395 if (ev.wheel.y > 0) {
396 _cursor.wheel--;
397 } else if (ev.wheel.y < 0) {
398 _cursor.wheel++;
400 break;
402 case SDL_MOUSEBUTTONDOWN:
403 if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
404 ev.button.button = SDL_BUTTON_RIGHT;
407 switch (ev.button.button) {
408 case SDL_BUTTON_LEFT:
409 _left_button_down = true;
410 break;
412 case SDL_BUTTON_RIGHT:
413 _right_button_down = true;
414 _right_button_clicked = true;
415 break;
417 default: break;
419 HandleMouseEvents();
420 break;
422 case SDL_MOUSEBUTTONUP:
423 if (_rightclick_emulate) {
424 _right_button_down = false;
425 _left_button_down = false;
426 _left_button_clicked = false;
427 } else if (ev.button.button == SDL_BUTTON_LEFT) {
428 _left_button_down = false;
429 _left_button_clicked = false;
430 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
431 _right_button_down = false;
433 HandleMouseEvents();
434 break;
436 case SDL_QUIT:
437 HandleExitGameRequest();
438 break;
440 case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
441 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
442 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
443 if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
444 } else {
445 char32_t character;
447 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
448 // Only handle non-text keys here. Text is handled in
449 // SDL_TEXTINPUT below.
450 if (!this->edit_box_focused ||
451 keycode == WKC_DELETE ||
452 keycode == WKC_NUM_ENTER ||
453 keycode == WKC_LEFT ||
454 keycode == WKC_RIGHT ||
455 keycode == WKC_UP ||
456 keycode == WKC_DOWN ||
457 keycode == WKC_HOME ||
458 keycode == WKC_END ||
459 keycode & WKC_META ||
460 keycode & WKC_CTRL ||
461 keycode & WKC_ALT ||
462 (keycode >= WKC_F1 && keycode <= WKC_F12) ||
463 !IsValidChar(character, CS_ALPHANUMERAL)) {
464 HandleKeypress(keycode, character);
467 break;
469 case SDL_TEXTINPUT: {
470 if (!this->edit_box_focused) break;
471 SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
472 uint keycode = ConvertSdlKeycodeIntoMy(kc);
474 if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
475 char32_t character;
476 Utf8Decode(&character, ev.text.text);
477 HandleKeypress(keycode, character);
478 } else {
479 HandleTextInput(ev.text.text);
481 break;
483 case SDL_WINDOWEVENT: {
484 if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
485 // Force a redraw of the entire screen.
486 this->MakeDirty(0, 0, _screen.width, _screen.height);
487 } else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
488 int w = std::max(ev.window.data1, 64);
489 int h = std::max(ev.window.data2, 64);
490 CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
491 } else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
492 // mouse entered the window, enable cursor
493 _cursor.in_window = true;
494 /* Ensure pointer lock will not occur. */
495 SDL_SetRelativeMouseMode(SDL_FALSE);
496 } else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
497 // mouse left the window, undraw cursor
498 UndrawMouseCursor();
499 _cursor.in_window = false;
501 break;
505 return true;
508 static const char *InitializeSDL()
510 /* Check if the video-driver is already initialized. */
511 if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return nullptr;
513 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) return SDL_GetError();
514 return nullptr;
517 const char *VideoDriver_SDL_Base::Initialize()
519 this->UpdateAutoResolution();
521 const char *error = InitializeSDL();
522 if (error != nullptr) return error;
524 FindResolutions();
525 Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
527 return nullptr;
530 const char *VideoDriver_SDL_Base::Start(const StringList &param)
532 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
534 const char *error = this->Initialize();
535 if (error != nullptr) return error;
537 this->startup_display = FindStartupDisplay(GetDriverParamInt(param, "display", -1));
539 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
540 return SDL_GetError();
543 const char *dname = SDL_GetCurrentVideoDriver();
544 Debug(driver, 1, "SDL2: using driver '{}'", dname);
546 this->driver_info = this->GetName();
547 this->driver_info += " (";
548 this->driver_info += dname;
549 this->driver_info += ")";
551 MarkWholeScreenDirty();
553 SDL_StopTextInput();
554 this->edit_box_focused = false;
556 #ifdef __EMSCRIPTEN__
557 this->is_game_threaded = false;
558 #else
559 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
560 #endif
562 return nullptr;
565 void VideoDriver_SDL_Base::Stop()
567 SDL_QuitSubSystem(SDL_INIT_VIDEO);
568 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
569 SDL_Quit(); // If there's nothing left, quit SDL
573 void VideoDriver_SDL_Base::InputLoop()
575 uint32_t mod = SDL_GetModState();
576 const Uint8 *keys = SDL_GetKeyboardState(nullptr);
578 bool old_ctrl_pressed = _ctrl_pressed;
580 _ctrl_pressed = !!(mod & KMOD_CTRL);
581 _shift_pressed = !!(mod & KMOD_SHIFT);
583 /* Speedup when pressing tab, except when using ALT+TAB
584 * to switch to another application. */
585 this->fast_forward_key_pressed = keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0;
587 /* Determine which directional keys are down. */
588 _dirkeys =
589 (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
590 (keys[SDL_SCANCODE_UP] ? 2 : 0) |
591 (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
592 (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
594 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
597 void VideoDriver_SDL_Base::LoopOnce()
599 if (_exit_game) {
600 #ifdef __EMSCRIPTEN__
601 /* Emscripten is event-driven, and as such the main loop is inside
602 * the browser. So if _exit_game goes true, the main loop ends (the
603 * cancel call), but we still have to call the cleanup that is
604 * normally done at the end of the main loop for non-Emscripten.
605 * After that, Emscripten just halts, and the HTML shows a nice
606 * "bye, see you next time" message. */
607 extern void PostMainLoop();
608 PostMainLoop();
610 emscripten_cancel_main_loop();
611 emscripten_exit_pointerlock();
612 /* In effect, the game ends here. As emscripten_set_main_loop() caused
613 * the stack to be unwound, the code after MainLoop() in
614 * openttd_main() is never executed. */
615 if (_game_mode == GM_BOOTSTRAP) {
616 EM_ASM(if (window["openttd_bootstrap_reload"]) openttd_bootstrap_reload());
617 } else {
618 EM_ASM(if (window["openttd_exit"]) openttd_exit());
620 #endif
621 return;
624 this->Tick();
626 /* Emscripten is running an event-based mainloop; there is already some
627 * downtime between each iteration, so no need to sleep. */
628 #ifndef __EMSCRIPTEN__
629 this->SleepTillNextTick();
630 #endif
633 void VideoDriver_SDL_Base::MainLoop()
635 #ifdef __EMSCRIPTEN__
636 /* Run the main loop event-driven, based on RequestAnimationFrame. */
637 emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
638 #else
639 this->StartGameThread();
641 while (!_exit_game) {
642 LoopOnce();
645 this->StopGameThread();
646 #endif
649 bool VideoDriver_SDL_Base::ChangeResolution(int w, int h)
651 return CreateMainSurface(w, h, true);
654 bool VideoDriver_SDL_Base::ToggleFullscreen(bool fullscreen)
656 /* Remember current window size */
657 int w, h;
658 SDL_GetWindowSize(this->sdl_window, &w, &h);
660 if (fullscreen) {
661 /* Find fullscreen window size */
662 SDL_DisplayMode dm;
663 if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(this->sdl_window), &dm) < 0) {
664 Debug(driver, 0, "SDL_GetCurrentDisplayMode() failed: {}", SDL_GetError());
665 } else {
666 SDL_SetWindowSize(this->sdl_window, dm.w, dm.h);
670 Debug(driver, 1, "SDL2: Setting {}", fullscreen ? "fullscreen" : "windowed");
671 int ret = SDL_SetWindowFullscreen(this->sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
672 if (ret == 0) {
673 /* Switching resolution succeeded, set fullscreen value of window. */
674 _fullscreen = fullscreen;
675 if (!fullscreen) SDL_SetWindowSize(this->sdl_window, w, h);
676 } else {
677 Debug(driver, 0, "SDL_SetWindowFullscreen() failed: {}", SDL_GetError());
680 InvalidateWindowClassesData(WC_GAME_OPTIONS, 3);
681 return ret == 0;
684 bool VideoDriver_SDL_Base::AfterBlitterChange()
686 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
687 int w, h;
688 SDL_GetWindowSize(this->sdl_window, &w, &h);
689 return CreateMainSurface(w, h, false);
692 Dimension VideoDriver_SDL_Base::GetScreenSize() const
694 SDL_DisplayMode mode;
695 if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
697 return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
700 bool VideoDriver_SDL_Base::LockVideoBuffer()
702 if (this->buffer_locked) return false;
703 this->buffer_locked = true;
705 _screen.dst_ptr = this->GetVideoPointer();
706 assert(_screen.dst_ptr != nullptr);
708 return true;
711 void VideoDriver_SDL_Base::UnlockVideoBuffer()
713 if (_screen.dst_ptr != nullptr) {
714 /* Hand video buffer back to the drawing backend. */
715 this->ReleaseVideoPointer();
716 _screen.dst_ptr = nullptr;
719 this->buffer_locked = false;