OpenTTD Source  13.2.1
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 void VideoDriver_SDL_Base::MakeDirty(int left, int top, int width, int height)
34 {
35  Rect r = {left, top, left + width, top + height};
36  this->dirty_rect = BoundingRect(this->dirty_rect, r);
37 }
38 
40 {
41  if (!CopyPalette(this->local_palette)) return;
42  this->MakeDirty(0, 0, _screen.width, _screen.height);
43 }
44 
45 static const Dimension default_resolutions[] = {
46  { 640, 480 },
47  { 800, 600 },
48  { 1024, 768 },
49  { 1152, 864 },
50  { 1280, 800 },
51  { 1280, 960 },
52  { 1280, 1024 },
53  { 1400, 1050 },
54  { 1600, 1200 },
55  { 1680, 1050 },
56  { 1920, 1200 }
57 };
58 
59 static void FindResolutions()
60 {
61  _resolutions.clear();
62 
63  for (int i = 0; i < SDL_GetNumDisplayModes(0); i++) {
64  SDL_DisplayMode mode;
65  SDL_GetDisplayMode(0, i, &mode);
66 
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);
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 
80 static 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::find(_resolutions.begin(), _resolutions.end(), 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 
102 static 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 
123 void 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 
131  GameSizeChanged();
132  }
133 }
134 
135 bool 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  char caption[50];
153  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
154  this->sdl_window = SDL_CreateWindow(
155  caption,
156  x, y,
157  w, h,
158  flags);
159 
160  if (this->sdl_window == nullptr) {
161  Debug(driver, 0, "SDL2: Couldn't allocate a window to draw on: {}", SDL_GetError());
162  return false;
163  }
164 
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 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
172 
173  SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
174  SDL_SetWindowIcon(this->sdl_window, icon);
175  SDL_FreeSurface(icon);
176  }
177  }
178 
179  return true;
180 }
181 
182 bool VideoDriver_SDL_Base::CreateMainSurface(uint w, uint h, bool resize)
183 {
184  GetAvailableVideoMode(&w, &h);
185  Debug(driver, 1, "SDL2: using mode {}x{}", w, h);
186 
187  if (!this->CreateMainWindow(w, h)) return false;
188  if (resize) SDL_SetWindowSize(this->sdl_window, w, h);
189  this->ClientSizeChanged(w, h, true);
190 
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;
195 
196  return true;
197 }
198 
199 bool VideoDriver_SDL_Base::ClaimMousePointer()
200 {
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;
206 }
207 
212 {
213  if (!this->edit_box_focused) {
214  SDL_StartTextInput();
215  this->edit_box_focused = true;
216  }
217 }
218 
223 {
224  if (this->edit_box_focused) {
225  SDL_StopTextInput();
226  this->edit_box_focused = false;
227  }
228 }
229 
231 {
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);
237  }
238  return rates;
239 }
240 
241 
242 struct SDLVkMapping {
243  SDL_Keycode vk_from;
244  byte vk_count;
245  byte map_to;
246  bool unprintable;
247 };
248 
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}
253 
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),
262 
263  AS_UP(SDLK_HOME, WKC_HOME),
264  AS_UP(SDLK_END, WKC_END),
265 
266  AS_UP(SDLK_INSERT, WKC_INSERT),
267  AS_UP(SDLK_DELETE, WKC_DELETE),
268 
269  /* Map letters & digits */
270  AM(SDLK_a, SDLK_z, 'A', 'Z'),
271  AM(SDLK_0, SDLK_9, '0', '9'),
272 
273  AS_UP(SDLK_ESCAPE, WKC_ESC),
274  AS_UP(SDLK_PAUSE, WKC_PAUSE),
275  AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
276 
277  AS(SDLK_SPACE, WKC_SPACE),
278  AS(SDLK_RETURN, WKC_RETURN),
279  AS(SDLK_TAB, WKC_TAB),
280 
281  /* Function keys */
282  AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
283 
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),
292 
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),
300 
301  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
302  AS(SDLK_COMMA, WKC_COMMA),
303  AS(SDLK_MINUS, WKC_MINUS),
304  AS(SDLK_PERIOD, WKC_PERIOD)
305 };
306 
307 static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, WChar *character)
308 {
309  const SDLVkMapping *map;
310  uint key = 0;
311  bool unprintable = false;
312 
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;
318  }
319  }
320 
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;
323 
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;
329 
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;
338  }
339 
340  return key;
341 }
342 
347 static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
348 {
349  const SDLVkMapping *map;
350  uint key = 0;
351 
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;
356  }
357  }
358 
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;
363 
364  return key;
365 }
366 
368 {
369  SDL_Event ev;
370 
371  if (!SDL_PollEvent(&ev)) return false;
372 
373  switch (ev.type) {
374  case SDL_MOUSEMOTION: {
375  int32_t x = ev.motion.x;
376  int32_t y = ev.motion.y;
377 
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;
384  }
385  }
386 
387  if (_cursor.UpdateCursorPosition(x, y)) {
388  SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y);
389  }
391  break;
392  }
393 
394  case SDL_MOUSEWHEEL:
395  if (ev.wheel.y > 0) {
396  _cursor.wheel--;
397  } else if (ev.wheel.y < 0) {
398  _cursor.wheel++;
399  }
400  break;
401 
402  case SDL_MOUSEBUTTONDOWN:
403  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
404  ev.button.button = SDL_BUTTON_RIGHT;
405  }
406 
407  switch (ev.button.button) {
408  case SDL_BUTTON_LEFT:
409  _left_button_down = true;
410  break;
411 
412  case SDL_BUTTON_RIGHT:
413  _right_button_down = true;
414  _right_button_clicked = true;
415  break;
416 
417  default: break;
418  }
420  break;
421 
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;
432  }
434  break;
435 
436  case SDL_QUIT:
437  HandleExitGameRequest();
438  break;
439 
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  WChar character;
446 
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);
465  }
466  }
467  break;
468 
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);
473 
474  if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
475  WChar character;
476  Utf8Decode(&character, ev.text.text);
477  HandleKeypress(keycode, character);
478  } else {
479  HandleTextInput(ev.text.text);
480  }
481  break;
482  }
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;
500  }
501  break;
502  }
503  }
504 
505  return true;
506 }
507 
508 static const char *InitializeSDL()
509 {
510  /* Explicitly disable hardware acceleration. Enabling this causes
511  * UpdateWindowSurface() to update the window's texture instead of
512  * its surface. */
513  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
514 
515  /* Check if the video-driver is already initialized. */
516  if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return nullptr;
517 
518  if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) return SDL_GetError();
519  return nullptr;
520 }
521 
522 const char *VideoDriver_SDL_Base::Initialize()
523 {
524  this->UpdateAutoResolution();
525 
526  const char *error = InitializeSDL();
527  if (error != nullptr) return error;
528 
529  FindResolutions();
530  Debug(driver, 2, "Resolution for display: {}x{}", _cur_resolution.width, _cur_resolution.height);
531 
532  return nullptr;
533 }
534 
535 const char *VideoDriver_SDL_Base::Start(const StringList &param)
536 {
537  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
538 
539  const char *error = this->Initialize();
540  if (error != nullptr) return error;
541 
542  this->startup_display = FindStartupDisplay(GetDriverParamInt(param, "display", -1));
543 
544  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
545  return SDL_GetError();
546  }
547 
548  const char *dname = SDL_GetCurrentVideoDriver();
549  Debug(driver, 1, "SDL2: using driver '{}'", dname);
550 
551  this->driver_info = this->GetName();
552  this->driver_info += " (";
553  this->driver_info += dname;
554  this->driver_info += ")";
555 
557 
558  SDL_StopTextInput();
559  this->edit_box_focused = false;
560 
561 #ifdef __EMSCRIPTEN__
562  this->is_game_threaded = false;
563 #else
564  this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
565 #endif
566 
567  return nullptr;
568 }
569 
571 {
572  SDL_QuitSubSystem(SDL_INIT_VIDEO);
573  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
574  SDL_Quit(); // If there's nothing left, quit SDL
575  }
576 }
577 
579 {
580  uint32 mod = SDL_GetModState();
581  const Uint8 *keys = SDL_GetKeyboardState(nullptr);
582 
583  bool old_ctrl_pressed = _ctrl_pressed;
584 
585  _ctrl_pressed = !!(mod & KMOD_CTRL);
586  _shift_pressed = !!(mod & KMOD_SHIFT);
587 
588 #if defined(_DEBUG)
590 #else
591  /* Speedup when pressing tab, except when using ALT+TAB
592  * to switch to another application. */
593  this->fast_forward_key_pressed = keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0;
594 #endif /* defined(_DEBUG) */
595 
596  /* Determine which directional keys are down. */
597  _dirkeys =
598  (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
599  (keys[SDL_SCANCODE_UP] ? 2 : 0) |
600  (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
601  (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
602 
603  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
604 }
605 
606 void VideoDriver_SDL_Base::LoopOnce()
607 {
608  if (_exit_game) {
609 #ifdef __EMSCRIPTEN__
610  /* Emscripten is event-driven, and as such the main loop is inside
611  * the browser. So if _exit_game goes true, the main loop ends (the
612  * cancel call), but we still have to call the cleanup that is
613  * normally done at the end of the main loop for non-Emscripten.
614  * After that, Emscripten just halts, and the HTML shows a nice
615  * "bye, see you next time" message. */
616  emscripten_cancel_main_loop();
617  emscripten_exit_pointerlock();
618  /* In effect, the game ends here. As emscripten_set_main_loop() caused
619  * the stack to be unwound, the code after MainLoop() in
620  * openttd_main() is never executed. */
621  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
622  EM_ASM(if (window["openttd_exit"]) openttd_exit());
623 #endif
624  return;
625  }
626 
627  this->Tick();
628 
629 /* Emscripten is running an event-based mainloop; there is already some
630  * downtime between each iteration, so no need to sleep. */
631 #ifndef __EMSCRIPTEN__
632  this->SleepTillNextTick();
633 #endif
634 }
635 
637 {
638 #ifdef __EMSCRIPTEN__
639  /* Run the main loop event-driven, based on RequestAnimationFrame. */
640  emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
641 #else
642  this->StartGameThread();
643 
644  while (!_exit_game) {
645  LoopOnce();
646  }
647 
648  this->StopGameThread();
649 #endif
650 }
651 
653 {
654  return CreateMainSurface(w, h, true);
655 }
656 
658 {
659  /* Remember current window size */
660  int w, h;
661  SDL_GetWindowSize(this->sdl_window, &w, &h);
662 
663  if (fullscreen) {
664  /* Find fullscreen window size */
665  SDL_DisplayMode dm;
666  if (SDL_GetCurrentDisplayMode(0, &dm) < 0) {
667  Debug(driver, 0, "SDL_GetCurrentDisplayMode() failed: {}", SDL_GetError());
668  } else {
669  SDL_SetWindowSize(this->sdl_window, dm.w, dm.h);
670  }
671  }
672 
673  Debug(driver, 1, "SDL2: Setting {}", fullscreen ? "fullscreen" : "windowed");
674  int ret = SDL_SetWindowFullscreen(this->sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
675  if (ret == 0) {
676  /* Switching resolution succeeded, set fullscreen value of window. */
677  _fullscreen = fullscreen;
678  if (!fullscreen) SDL_SetWindowSize(this->sdl_window, w, h);
679  } else {
680  Debug(driver, 0, "SDL_SetWindowFullscreen() failed: {}", SDL_GetError());
681  }
682 
684  return ret == 0;
685 }
686 
688 {
689  assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
690  int w, h;
691  SDL_GetWindowSize(this->sdl_window, &w, &h);
692  return CreateMainSurface(w, h, false);
693 }
694 
696 {
697  SDL_DisplayMode mode;
698  if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
699 
700  return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
701 }
702 
704 {
705  if (this->buffer_locked) return false;
706  this->buffer_locked = true;
707 
708  _screen.dst_ptr = this->GetVideoPointer();
709  assert(_screen.dst_ptr != nullptr);
710 
711  return true;
712 }
713 
715 {
716  if (_screen.dst_ptr != nullptr) {
717  /* Hand video buffer back to the drawing backend. */
718  this->ReleaseVideoPointer();
719  _screen.dst_ptr = nullptr;
720  }
721 
722  this->buffer_locked = false;
723 }
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:34
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:714
CursorVars::UpdateCursorPosition
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
Definition: gfx.cpp:1964
VideoDriver::Tick
void Tick()
Give the video-driver a tick.
Definition: video_driver.cpp:101
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:36
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:2713
VideoDriver_SDL_Base::driver_info
std::string driver_info
Information string about selected driver.
Definition: sdl2_v.h:53
VideoDriver_SDL_Base::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl2_v.cpp:33
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:41
HandleKeypress
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2627
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:230
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:38
VideoDriver_SDL_Base::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: sdl2_v.cpp:703
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:211
VideoDriver_SDL_Base::AllocateBackingStore
virtual bool AllocateBackingStore(int w, int h, bool force=false)=0
(Re-)create the backing store.
sdl2_v.h
VideoDriver::StartGameThread
void StartGameThread()
Start the loop for game-tick.
Definition: video_driver.cpp:85
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:636
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2932
VideoDriver_SDL_Base::GetName
const char * GetName() const override
Get the name of this driver.
Definition: sdl2_v.h:44
VideoDriver_SDL_Base::Stop
void Stop() override
Stop this driver.
Definition: sdl2_v.cpp:570
VideoDriver_SDL_Base::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: sdl2_v.cpp:578
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:214
VideoDriver_SDL_Base::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl2_v.cpp:687
VideoDriver_SDL_Base::local_palette
Palette local_palette
Current palette to use for drawing.
Definition: sdl2_v.h:50
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
VideoDriver_SDL_Base::PollEvent
bool PollEvent() override
Process a single system event.
Definition: sdl2_v.cpp:367
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:59
VideoDriver_SDL_Base::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: sdl2_v.cpp:657
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:31
CursorVars::fix_at
bool fix_at
mouse is moving, but cursor is not (used for scrolling)
Definition: gfx_type.h:120
IsValidChar
bool IsValidChar(WChar key, CharSetFilter afilter)
Only allow certain keys.
Definition: string.cpp:494
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:39
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:39
FocusedWindowIsConsole
bool FocusedWindowIsConsole()
Check if a console is focused.
Definition: window.cpp:495
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:135
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:593
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:82
VideoDriver::StopGameThread
void StopGameThread()
Stop the loop for the game-tick.
Definition: video_driver.cpp:94
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:33
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:242
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:123
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:222
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:394
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:3271
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition: gfx.cpp:1294
VideoDriver_SDL_Base::dirty_rect
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition: sdl2_v.h:52
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:88
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:554
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:347
WKC_SEMICOLON
@ WKC_SEMICOLON
; Semicolon
Definition: gfx_type.h:96
error
void CDECL error(const char *s,...)
Error handling for fatal non-user errors.
Definition: openttd.cpp:134
GameSizeChanged
void GameSizeChanged()
Size of the application screen changed.
Definition: main_gui.cpp:580
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
HandleCtrlChanged
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2683
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1767
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:170
SDLVkMapping
Definition: sdl2_v.cpp:242
VideoDriver_SDL_Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: sdl2_v.h:51
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:348
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:141
VideoDriver_SDL_Base::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: sdl2_v.cpp:652
WKC_MINUS
@ WKC_MINUS
Definition: gfx_type.h:104
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
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:44
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:402
VideoDriver_SDL_Base::GetScreenSize
Dimension GetScreenSize() const override
Get the resolution of the main screen.
Definition: sdl2_v.cpp:695
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:70
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:42
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:32
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:43
Delta
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:196
VideoDriver::GetScreenSize
virtual Dimension GetScreenSize() const
Get the resolution of the main screen.
Definition: video_driver.hpp:231
VideoDriver_SDL_Base::sdl_window
struct SDL_Window * sdl_window
Main SDL window.
Definition: sdl2_v.h:49
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:535