OpenTTD Source: src/video/sdl2_v.cpp Source File

OpenTTD Source 1.11.0-RC1

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