OpenTTD Source 20260208-master-g43af8e94d0
sdl2_v.cpp
Go to the documentation of this file.
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11#include "../openttd.h"
12#include "../gfx_func.h"
14#include "../thread.h"
15#include "../progress.h"
16#include "../core/math_func.hpp"
18#include "../core/utf8.hpp"
19#include "../fileio_func.h"
20#include "../framerate_type.h"
21#include "../window_func.h"
22#include "sdl2_v.h"
23#include <SDL.h>
24#ifdef __EMSCRIPTEN__
25# include <emscripten.h>
26# include <emscripten/html5.h>
27#endif
28
29#include "../safeguards.h"
30
31void VideoDriver_SDL_Base::MakeDirty(int left, int top, int width, int height)
32{
33 Rect r = {left, top, left + width, top + height};
34 this->dirty_rect = BoundingRect(this->dirty_rect, r);
35}
36
38{
39 if (!CopyPalette(this->local_palette)) return;
40 this->MakeDirty(0, 0, _screen.width, _screen.height);
41}
42
43static const Dimension default_resolutions[] = {
44 { 640, 480 },
45 { 800, 600 },
46 { 1024, 768 },
47 { 1152, 864 },
48 { 1280, 800 },
49 { 1280, 960 },
50 { 1280, 1024 },
51 { 1400, 1050 },
52 { 1600, 1200 },
53 { 1680, 1050 },
54 { 1920, 1200 }
55};
56
57static void FindResolutions()
58{
59 _resolutions.clear();
60
61 for (int display = 0; display < SDL_GetNumVideoDisplays(); display++) {
62 for (int i = 0; i < SDL_GetNumDisplayModes(display); i++) {
63 SDL_DisplayMode mode;
64 SDL_GetDisplayMode(display, i, &mode);
65
66 if (mode.w < 640 || mode.h < 480) continue;
67 if (std::ranges::find(_resolutions, Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
68 _resolutions.emplace_back(mode.w, mode.h);
69 }
70 }
71
72 /* We have found no resolutions, show the default list */
73 if (_resolutions.empty()) {
74 _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
75 }
76
77 SortResolutions();
78}
79
80static void GetAvailableVideoMode(uint *w, uint *h)
81{
82 /* All modes available? */
83 if (!_fullscreen || _resolutions.empty()) return;
84
85 /* Is the wanted mode among the available modes? */
86 if (std::ranges::find(_resolutions, Dimension(*w, *h)) != _resolutions.end()) return;
87
88 /* Use the closest possible resolution */
89 uint best = 0;
90 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
91 for (uint i = 1; i != _resolutions.size(); ++i) {
92 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
93 if (newdelta < delta) {
94 best = i;
95 delta = newdelta;
96 }
97 }
98 *w = _resolutions[best].width;
99 *h = _resolutions[best].height;
100}
101
102static uint FindStartupDisplay(uint startup_display)
103{
104 int num_displays = SDL_GetNumVideoDisplays();
105
106 /* If the user indicated a valid monitor, use that. */
107 if (IsInsideBS(startup_display, 0, num_displays)) return startup_display;
108
109 /* Mouse position decides which display to use. */
110 int mx, my;
111 SDL_GetGlobalMouseState(&mx, &my);
112 for (int display = 0; display < num_displays; ++display) {
113 SDL_Rect r;
114 if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
115 Debug(driver, 1, "SDL2: Mouse is at ({}, {}), use display {} ({}, {}, {}, {})", mx, my, display, r.x, r.y, r.w, r.h);
116 return display;
117 }
118 }
119
120 return 0;
121}
122
123void VideoDriver_SDL_Base::ClientSizeChanged(int w, int h, bool force)
124{
125 /* Allocate backing store of the new size. */
126 if (this->AllocateBackingStore(w, h, force)) {
127 CopyPalette(this->local_palette, true);
128
130
132 }
133}
134
135bool VideoDriver_SDL_Base::CreateMainWindow(uint w, uint h, uint flags)
136{
137 if (this->sdl_window != nullptr) return true;
138
139 flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
140
141 if (_fullscreen) {
142 flags |= SDL_WINDOW_FULLSCREEN;
143 }
144
145 int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
146 SDL_Rect r;
147 if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
148 x = r.x + std::max(0, r.w - static_cast<int>(w)) / 2;
149 y = r.y + std::max(0, r.h - static_cast<int>(h)) / 4; // decent desktops have taskbars at the bottom
150 }
151
152 std::string caption = VideoDriver::GetCaption();
153 this->sdl_window = SDL_CreateWindow(
154 caption.c_str(),
155 x, y,
156 w, h,
157 flags);
158
159 if (this->sdl_window == nullptr) {
160 Debug(driver, 0, "SDL2: Couldn't allocate a window to draw on: {}", SDL_GetError());
161 return false;
162 }
163
164 std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
165 if (!icon_path.empty()) {
166 /* Give the application an icon */
167 SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
168 if (icon != nullptr) {
169 /* Get the colourkey, which will be magenta */
170 uint32_t rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
171
172 SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
173 SDL_SetWindowIcon(this->sdl_window, icon);
174 SDL_FreeSurface(icon);
175 }
176 }
177
178 return true;
179}
180
181bool VideoDriver_SDL_Base::CreateMainSurface(uint w, uint h, bool resize)
182{
183 GetAvailableVideoMode(&w, &h);
184 Debug(driver, 1, "SDL2: using mode {}x{}", w, h);
185
186 if (!this->CreateMainWindow(w, h)) return false;
187 if (resize) SDL_SetWindowSize(this->sdl_window, w, h);
188 this->ClientSizeChanged(w, h, true);
189
190 /* When in full screen, we will always have the mouse cursor
191 * within the window, even though SDL does not give us the
192 * appropriate event to know this. */
193 if (_fullscreen) _cursor.in_window = true;
194
195 return true;
196}
197
199{
200 /* Emscripten never claims the pointer, so we do not need to change the cursor visibility. */
201#ifndef __EMSCRIPTEN__
202 SDL_ShowCursor(0);
203#endif
204}
205
210{
211 if (!this->edit_box_focused) {
212 SDL_StartTextInput();
213 this->edit_box_focused = true;
214 }
215}
216
221{
222 if (this->edit_box_focused) {
223 SDL_StopTextInput();
224 this->edit_box_focused = false;
225 }
226}
227
229{
230 std::vector<int> rates = {};
231 for (int i = 0; i < SDL_GetNumVideoDisplays(); i++) {
232 SDL_DisplayMode mode = {};
233 if (SDL_GetDisplayMode(i, 0, &mode) != 0) continue;
234 if (mode.refresh_rate != 0) rates.push_back(mode.refresh_rate);
235 }
236 return rates;
237}
238
239
240struct SDLVkMapping {
241 const SDL_Keycode vk_from;
242 const uint8_t vk_count;
243 const uint8_t map_to;
244 const bool unprintable;
245
246 constexpr SDLVkMapping(SDL_Keycode vk_first, SDL_Keycode vk_last, uint8_t map_first, [[maybe_unused]] uint8_t map_last, bool unprintable)
247 : vk_from(vk_first), vk_count(vk_last - vk_first + 1), map_to(map_first), unprintable(unprintable)
248 {
249 assert((vk_last - vk_first) == (map_last - map_first));
250 }
251};
252
253#define AS(x, z) {x, x, z, z, false}
254#define AM(x, y, z, w) {x, y, z, w, false}
255#define AS_UP(x, z) {x, x, z, z, true}
256#define AM_UP(x, y, z, w) {x, y, z, w, true}
257
258static constexpr SDLVkMapping _vk_mapping[] = {
259 /* Pageup stuff + up/down */
260 AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
261 AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
262 AS_UP(SDLK_UP, WKC_UP),
263 AS_UP(SDLK_DOWN, WKC_DOWN),
264 AS_UP(SDLK_LEFT, WKC_LEFT),
265 AS_UP(SDLK_RIGHT, WKC_RIGHT),
266
267 AS_UP(SDLK_HOME, WKC_HOME),
268 AS_UP(SDLK_END, WKC_END),
269
270 AS_UP(SDLK_INSERT, WKC_INSERT),
271 AS_UP(SDLK_DELETE, WKC_DELETE),
272
273 /* Map letters & digits */
274 AM(SDLK_a, SDLK_z, 'A', 'Z'),
275 AM(SDLK_0, SDLK_9, '0', '9'),
276
277 AS_UP(SDLK_ESCAPE, WKC_ESC),
278 AS_UP(SDLK_PAUSE, WKC_PAUSE),
279 AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
280
281 AS(SDLK_SPACE, WKC_SPACE),
282 AS(SDLK_RETURN, WKC_RETURN),
283 AS(SDLK_TAB, WKC_TAB),
284
285 /* Function keys */
286 AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
287
288 /* Numeric part. */
289 AS(SDLK_KP_1, '1'),
290 AS(SDLK_KP_2, '2'),
291 AS(SDLK_KP_3, '3'),
292 AS(SDLK_KP_4, '4'),
293 AS(SDLK_KP_5, '5'),
294 AS(SDLK_KP_6, '6'),
295 AS(SDLK_KP_7, '7'),
296 AS(SDLK_KP_8, '8'),
297 AS(SDLK_KP_9, '9'),
298 AS(SDLK_KP_0, '0'),
299 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
300 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
301 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
302 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
303 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
304 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
305
306 /* Other non-letter keys */
307 AS(SDLK_SLASH, WKC_SLASH),
308 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
309 AS(SDLK_EQUALS, WKC_EQUALS),
310 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
311 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
312 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
313
314 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
315 AS(SDLK_COMMA, WKC_COMMA),
316 AS(SDLK_MINUS, WKC_MINUS),
317 AS(SDLK_PERIOD, WKC_PERIOD)
318};
319
320static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, char32_t *character)
321{
322 uint key = 0;
323 bool unprintable = false;
324
325 for (const auto &map : _vk_mapping) {
326 if (IsInsideBS(sym->sym, map.vk_from, map.vk_count)) {
327 key = sym->sym - map.vk_from + map.map_to;
328 unprintable = map.unprintable;
329 break;
330 }
331 }
332
333 /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
334 if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
335
336 /* META are the command keys on mac */
337 if (sym->mod & KMOD_GUI) key |= WKC_META;
338 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
339 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
340 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
341
342 /* The mod keys have no character. Prevent '?' */
343 if (sym->mod & KMOD_GUI ||
344 sym->mod & KMOD_CTRL ||
345 sym->mod & KMOD_ALT ||
346 unprintable) {
347 *character = WKC_NONE;
348 } else {
349 *character = sym->sym;
350 }
351
352 return key;
353}
354
359static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
360{
361 uint key = 0;
362
363 for (const auto &map : _vk_mapping) {
364 if (IsInsideBS(kc, map.vk_from, map.vk_count)) {
365 key = kc - map.vk_from + map.map_to;
366 break;
367 }
368 }
369
370 /* check scancode for BACKQUOTE key, because we want the key left
371 * of "1", not anything else (on non-US keyboards) */
372 SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
373 if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
374
375 return key;
376}
377
379{
380 SDL_Event ev;
381
382 if (!SDL_PollEvent(&ev)) return false;
383
384 switch (ev.type) {
385 case SDL_MOUSEMOTION: {
386 int32_t x = ev.motion.x;
387 int32_t y = ev.motion.y;
388
389 if (_cursor.fix_at) {
390 /* Get all queued mouse events now in case we have to warp the cursor. In the
391 * end, we only care about the current mouse position and not bygone events. */
392 while (SDL_PeepEvents(&ev, 1, SDL_GETEVENT, SDL_MOUSEMOTION, SDL_MOUSEMOTION)) {
393 x = ev.motion.x;
394 y = ev.motion.y;
395 }
396 }
397
398 if (_cursor.UpdateCursorPosition(x, y)) {
399 SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y);
400 }
402 break;
403 }
404
405 case SDL_MOUSEWHEEL: {
406 if (ev.wheel.y > 0) {
407 _cursor.wheel--;
408 } else if (ev.wheel.y < 0) {
409 _cursor.wheel++;
410 }
411
412 /* Handle 2D scrolling. */
413 const float SCROLL_BUILTIN_MULTIPLIER = 14.0f;
414#if SDL_VERSION_ATLEAST(2, 18, 0)
415 _cursor.v_wheel -= ev.wheel.preciseY * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
416 _cursor.h_wheel += ev.wheel.preciseX * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier;
417#else
418 _cursor.v_wheel -= static_cast<float>(ev.wheel.y * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier);
419 _cursor.h_wheel += static_cast<float>(ev.wheel.x * SCROLL_BUILTIN_MULTIPLIER * _settings_client.gui.scrollwheel_multiplier);
420#endif
421 _cursor.wheel_moved = true;
422 break;
423 }
424
425 case SDL_MOUSEBUTTONDOWN:
426 if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
427 ev.button.button = SDL_BUTTON_RIGHT;
428 }
429
430 switch (ev.button.button) {
431 case SDL_BUTTON_LEFT:
432 _left_button_down = true;
433 break;
434
435 case SDL_BUTTON_RIGHT:
436 _right_button_down = true;
438 break;
439
440 default: break;
441 }
443 break;
444
445 case SDL_MOUSEBUTTONUP:
447 _right_button_down = false;
448 _left_button_down = false;
449 _left_button_clicked = false;
450 } else if (ev.button.button == SDL_BUTTON_LEFT) {
451 _left_button_down = false;
452 _left_button_clicked = false;
453 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
454 _right_button_down = false;
455 }
457 break;
458
459 case SDL_QUIT:
460 HandleExitGameRequest();
461 break;
462
463 case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
464 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
465 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
466 if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
467 } else {
468 char32_t character;
469
470 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
471 /* Only handle non-text keys here. Text is handled in
472 * SDL_TEXTINPUT below. */
473 if (!this->edit_box_focused ||
474 keycode == WKC_DELETE ||
475 keycode == WKC_NUM_ENTER ||
476 keycode == WKC_LEFT ||
477 keycode == WKC_RIGHT ||
478 keycode == WKC_UP ||
479 keycode == WKC_DOWN ||
480 keycode == WKC_HOME ||
481 keycode == WKC_END ||
482 keycode & WKC_META ||
483 keycode & WKC_CTRL ||
484 keycode & WKC_ALT ||
485 (keycode >= WKC_F1 && keycode <= WKC_F12) ||
486 !IsValidChar(character, CS_ALPHANUMERAL)) {
487 HandleKeypress(keycode, character);
488 }
489 }
490 break;
491
492 case SDL_TEXTINPUT: {
493 if (!this->edit_box_focused) break;
494 SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
495 uint keycode = ConvertSdlKeycodeIntoMy(kc);
496
497 if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
498 auto [len, c] = DecodeUtf8(ev.text.text);
499 if (len > 0) HandleKeypress(keycode, c);
500 } else {
501 HandleTextInput(ev.text.text);
502 }
503 break;
504 }
505 case SDL_WINDOWEVENT: {
506 if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
507 /* Force a redraw of the entire screen. */
508 this->MakeDirty(0, 0, _screen.width, _screen.height);
509 } else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
510 int w = std::max(ev.window.data1, 64);
511 int h = std::max(ev.window.data2, 64);
512 CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
513 } else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
514 /* mouse entered the window, enable cursor */
515 _cursor.in_window = true;
516 /* Ensure pointer lock will not occur. */
517 SDL_SetRelativeMouseMode(SDL_FALSE);
518 } else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
519 /* mouse left the window, undraw cursor */
520 UndrawMouseCursor();
521 _cursor.in_window = false;
522 }
523 break;
524 }
525 }
526
527 return true;
528}
529
530static std::optional<std::string_view> InitializeSDL()
531{
532 /* Check if the video-driver is already initialized. */
533 if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return std::nullopt;
534
535#ifdef SDL_HINT_APP_NAME
536 SDL_SetHint(SDL_HINT_APP_NAME, "OpenTTD");
537#endif
538
539 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) return SDL_GetError();
540 return std::nullopt;
541}
542
543std::optional<std::string_view> VideoDriver_SDL_Base::Initialize()
544{
545 this->UpdateAutoResolution();
546
547 auto error = InitializeSDL();
548 if (error) return error;
549
550 FindResolutions();
551 Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
552
553 return std::nullopt;
554}
555
556std::optional<std::string_view> VideoDriver_SDL_Base::Start(const StringList &param)
557{
558 if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
559
560 auto error = this->Initialize();
561 if (error) return error;
562
563#ifdef SDL_HINT_MOUSE_AUTO_CAPTURE
564 if (GetDriverParamBool(param, "no_mouse_capture")) {
565 /* By default SDL captures the mouse, while a button is pressed.
566 * This is annoying during debugging, when OpenTTD is suspended while the button was pressed.
567 */
568 if (!SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0")) return SDL_GetError();
569 }
570#endif
571
572 this->startup_display = FindStartupDisplay(GetDriverParamInt(param, "display", -1));
573
574 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
575 return SDL_GetError();
576 }
577
578 const char *dname = SDL_GetCurrentVideoDriver();
579 Debug(driver, 1, "SDL2: using driver '{}'", dname);
580
581 this->driver_info = this->GetName();
582 this->driver_info += " (";
583 this->driver_info += dname;
584 this->driver_info += ")";
585
587
588 SDL_StopTextInput();
589 this->edit_box_focused = false;
590
591#ifdef __EMSCRIPTEN__
592 this->is_game_threaded = false;
593#else
594 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
595#endif
596
597 return std::nullopt;
598}
599
601{
602 SDL_QuitSubSystem(SDL_INIT_VIDEO);
603 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
604 SDL_Quit(); // If there's nothing left, quit SDL
605 }
606}
607
609{
610 uint32_t mod = SDL_GetModState();
611 const Uint8 *keys = SDL_GetKeyboardState(nullptr);
612
613 bool old_ctrl_pressed = _ctrl_pressed;
614
615 _ctrl_pressed = !!(mod & KMOD_CTRL);
616 _shift_pressed = !!(mod & KMOD_SHIFT);
617
618 /* Speedup when pressing tab, except when using ALT+TAB
619 * to switch to another application. */
620 this->fast_forward_key_pressed = keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0;
621
622 /* Determine which directional keys are down. */
623 _dirkeys =
624 (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
625 (keys[SDL_SCANCODE_UP] ? 2 : 0) |
626 (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
627 (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
628
629 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
630}
631
632void VideoDriver_SDL_Base::LoopOnce()
633{
634 if (_exit_game) {
635#ifdef __EMSCRIPTEN__
636 /* Emscripten is event-driven, and as such the main loop is inside
637 * the browser. So if _exit_game goes true, the main loop ends (the
638 * cancel call), but we still have to call the cleanup that is
639 * normally done at the end of the main loop for non-Emscripten.
640 * After that, Emscripten just halts, and the HTML shows a nice
641 * "bye, see you next time" message. */
642 extern void PostMainLoop();
643 PostMainLoop();
644
645 emscripten_cancel_main_loop();
646 emscripten_exit_pointerlock();
647 /* In effect, the game ends here. As emscripten_set_main_loop() caused
648 * the stack to be unwound, the code after MainLoop() in
649 * openttd_main() is never executed. */
650 if (_game_mode == GM_BOOTSTRAP) {
651 EM_ASM(if (window["openttd_bootstrap_reload"]) openttd_bootstrap_reload());
652 } else {
653 EM_ASM(if (window["openttd_exit"]) openttd_exit());
654 }
655#endif
656 return;
657 }
658
659 this->Tick();
660
661/* Emscripten is running an event-based mainloop; there is already some
662 * downtime between each iteration, so no need to sleep. */
663#ifndef __EMSCRIPTEN__
664 this->SleepTillNextTick();
665#endif
666}
667
669{
670#ifdef __EMSCRIPTEN__
671 /* Run the main loop event-driven, based on RequestAnimationFrame. */
672 emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
673#else
674 this->StartGameThread();
675
676 while (!_exit_game) {
677 LoopOnce();
678 }
679
680 this->StopGameThread();
681#endif
682}
683
685{
686 return CreateMainSurface(w, h, true);
687}
688
690{
691 /* Remember current window size */
692 int w, h;
693 SDL_GetWindowSize(this->sdl_window, &w, &h);
694
695 if (fullscreen) {
696 /* Find fullscreen window size */
697 SDL_DisplayMode dm;
698 if (SDL_GetCurrentDisplayMode(SDL_GetWindowDisplayIndex(this->sdl_window), &dm) < 0) {
699 Debug(driver, 0, "SDL_GetCurrentDisplayMode() failed: {}", SDL_GetError());
700 } else {
701 SDL_SetWindowSize(this->sdl_window, dm.w, dm.h);
702 }
703 }
704
705 Debug(driver, 1, "SDL2: Setting {}", fullscreen ? "fullscreen" : "windowed");
706 int ret = SDL_SetWindowFullscreen(this->sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
707 if (ret == 0) {
708 /* Switching resolution succeeded, set fullscreen value of window. */
709 _fullscreen = fullscreen;
710 if (!fullscreen) SDL_SetWindowSize(this->sdl_window, w, h);
711 } else {
712 Debug(driver, 0, "SDL_SetWindowFullscreen() failed: {}", SDL_GetError());
713 }
714
716 return ret == 0;
717}
718
720{
721 assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
722 int w, h;
723 SDL_GetWindowSize(this->sdl_window, &w, &h);
724 return CreateMainSurface(w, h, false);
725}
726
728{
729 SDL_DisplayMode mode;
730 if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
731
732 return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
733}
734
736{
737 if (this->buffer_locked) return false;
738 this->buffer_locked = true;
739
740 _screen.dst_ptr = this->GetVideoPointer();
741 assert(_screen.dst_ptr != nullptr);
742
743 return true;
744}
745
747{
748 if (_screen.dst_ptr != nullptr) {
749 /* Hand video buffer back to the drawing backend. */
750 this->ReleaseVideoPointer();
751 _screen.dst_ptr = nullptr;
752 }
753
754 this->buffer_locked = false;
755}
756
758{
759 if (inhibited) {
760 SDL_DisableScreenSaver();
761 } else {
762 SDL_EnableScreenSaver();
763 }
764}
#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, maint_cost, ttdpatch_type, class_id, name, preview)
AirportSpec definition for airports with at least one depot.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:136
virtual void PostResize()
Post resize event.
Definition base.hpp:205
virtual std::string_view GetName() const =0
Get the name of this driver.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
Definition sdl2_v.cpp:556
bool buffer_locked
Video buffer was locked by the main thread.
Definition sdl2_v.h:51
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition sdl2_v.cpp:727
bool PollEvent() override
Process a single system event.
Definition sdl2_v.cpp:378
void EditBoxLostFocus() override
This is called to indicate that an edit box has lost focus, text input mode should be disabled.
Definition sdl2_v.cpp:220
virtual void ReleaseVideoPointer()=0
Hand video buffer back to the painting backend.
void ClaimMousePointer() override
Claim the exclusive rights for the mouse pointer.
Definition sdl2_v.cpp:198
virtual void * GetVideoPointer()=0
Get a pointer to the video buffer.
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition sdl2_v.cpp:719
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition sdl2_v.cpp:608
Palette local_palette
Current palette to use for drawing.
Definition sdl2_v.h:50
void MainLoop() override
Perform the actual drawing.
Definition sdl2_v.cpp:668
std::string driver_info
Information string about selected driver.
Definition sdl2_v.h:53
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition sdl2_v.cpp:746
void ClientSizeChanged(int w, int h, bool force)
Indicate to the driver the client-side might have changed.
Definition sdl2_v.cpp:123
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition sdl2_v.cpp:31
virtual bool CreateMainWindow(uint w, uint h, uint flags=0)
Create the main window.
Definition sdl2_v.cpp:135
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
Definition sdl2_v.cpp:228
void SetScreensaverInhibited(bool inhibited) override
Prevents the system from going to sleep.
Definition sdl2_v.cpp:757
bool edit_box_focused
This is true to indicate that keyboard input is in text input mode, and SDL_TEXTINPUT events are enab...
Definition sdl2_v.h:88
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition sdl2_v.cpp:684
void Stop() override
Stop this driver.
Definition sdl2_v.cpp:600
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition sdl2_v.cpp:735
void EditBoxGainedFocus() override
This is called to indicate that an edit box has gained focus, text input mode should be enabled.
Definition sdl2_v.cpp:209
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition sdl2_v.h:52
void CheckPaletteAnim() override
Process any pending palette animation.
Definition sdl2_v.cpp:37
struct SDL_Window * sdl_window
Main SDL window.
Definition sdl2_v.h:49
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition sdl2_v.cpp:689
virtual Dimension GetScreenSize() const
Get the resolution of the main screen.
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
static std::string GetCaption()
Get the caption to use for the game's title bar.
void StopGameThread()
Stop the loop for the game-tick.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
std::pair< size_t, char32_t > DecodeUtf8(std::string_view buf)
Decode a character from UTF-8.
Definition utf8.cpp:46
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
int GetDriverParamInt(const StringList &parm, std::string_view name, int def)
Get an integer parameter the list of parameters.
Definition driver.cpp:79
bool GetDriverParamBool(const StringList &parm, std::string_view name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:67
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:29
bool _rightclick_emulate
Whether right clicking is emulated.
Definition driver.cpp:30
Factory to 'query' all available blitters.
std::string FioFindFullPath(Subdirectory subdir, std::string_view filename)
Find a path to the filename in one of the search directories.
Definition fileio.cpp:144
Functions for standard in/out file operations.
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game).
Definition fileio_type.h:96
Types for recording game performance data.
Rect BoundingRect(const Rect &r1, const Rect &r2)
Compute the bounding rectangle around two rectangles.
Geometry functions.
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:40
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:42
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:39
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:35
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:43
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:45
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:44
Functions related to the gfx engine.
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2712
void GameSizeChanged()
Size of the application screen changed.
Definition main_gui.cpp:596
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:2975
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2656
void HandleTextInput(std::string_view str, bool marked=false, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Handle text input.
Definition window.cpp:2748
@ WKC_BACKSLASH
\ Backslash
Definition gfx_type.h:101
@ WKC_MINUS
Definition gfx_type.h:106
@ WKC_COMMA
, Comma
Definition gfx_type.h:104
@ WKC_PERIOD
. Period
Definition gfx_type.h:105
@ WKC_EQUALS
= Equals
Definition gfx_type.h:99
@ WKC_SLASH
/ Forward slash
Definition gfx_type.h:97
@ WKC_SINGLEQUOTE
' Single quote
Definition gfx_type.h:103
@ WKC_R_BRACKET
] Right square bracket
Definition gfx_type.h:102
@ WKC_L_BRACKET
[ Left square bracket
Definition gfx_type.h:100
@ WKC_SEMICOLON
; Semicolon
Definition gfx_type.h:98
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1554
Integer math functions.
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Some generic types.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:225
Functions related to modal progress.
A number of safeguards to prevent using unsafe methods.
static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
Like ConvertSdlKeyIntoMy(), but takes an SDL_Keycode as input instead of an SDL_Keysym.
Definition sdl2_v.cpp:359
Base of the SDL2 video driver.
ClientSettings _settings_client
The current settings for this game.
Definition settings.cpp:60
Definition of base types and functions in a cross-platform compatible way.
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
Definition string.cpp:373
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
Definition string_type.h:25
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
Dimensions (a width and height) of a rectangle in 2D.
Specification of a rectangle with absolute coordinates of all edges.
Base of all threads.
Handling of UTF-8 encoded data.
bool FocusedWindowIsConsole()
Check if a console is focused.
Definition window.cpp:464
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition window.cpp:3328
Window functions not directly related to making/drawing windows.
@ WC_GAME_OPTIONS
Game options window; Window numbers: