OpenTTD Source  1.11.0-beta2
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 #include <mutex>
27 #ifdef __EMSCRIPTEN__
28 # include <emscripten.h>
29 # include <emscripten/html5.h>
30 #endif
31 
32 #include "../safeguards.h"
33 
34 #ifdef __EMSCRIPTEN__
35 
36 static bool _cursor_new_in_window = false;
37 #endif
38 
39 void VideoDriver_SDL_Base::MakeDirty(int left, int top, int width, int height)
40 {
41  Rect r = {left, top, left + width, top + height};
42  this->dirty_rect = BoundingRect(this->dirty_rect, r);
43 }
44 
46 {
47  if (_cur_palette.count_dirty == 0) return;
48 
50  this->MakeDirty(0, 0, _screen.width, _screen.height);
51 }
52 
53 /* static */ void VideoDriver_SDL_Base::PaintThreadThunk(VideoDriver_SDL_Base *drv)
54 {
55  drv->PaintThread();
56 }
57 
58 static const Dimension default_resolutions[] = {
59  { 640, 480 },
60  { 800, 600 },
61  { 1024, 768 },
62  { 1152, 864 },
63  { 1280, 800 },
64  { 1280, 960 },
65  { 1280, 1024 },
66  { 1400, 1050 },
67  { 1600, 1200 },
68  { 1680, 1050 },
69  { 1920, 1200 }
70 };
71 
72 static void FindResolutions()
73 {
74  _resolutions.clear();
75 
76  for (int i = 0; i < SDL_GetNumDisplayModes(0); i++) {
77  SDL_DisplayMode mode;
78  SDL_GetDisplayMode(0, i, &mode);
79 
80  if (mode.w < 640 || mode.h < 480) continue;
81  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
82  _resolutions.emplace_back(mode.w, mode.h);
83  }
84 
85  /* We have found no resolutions, show the default list */
86  if (_resolutions.empty()) {
87  _resolutions.assign(std::begin(default_resolutions), std::end(default_resolutions));
88  }
89 
90  SortResolutions();
91 }
92 
93 static void GetAvailableVideoMode(uint *w, uint *h)
94 {
95  /* All modes available? */
96  if (!_fullscreen || _resolutions.empty()) return;
97 
98  /* Is the wanted mode among the available modes? */
99  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
100 
101  /* Use the closest possible resolution */
102  uint best = 0;
103  uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
104  for (uint i = 1; i != _resolutions.size(); ++i) {
105  uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
106  if (newdelta < delta) {
107  best = i;
108  delta = newdelta;
109  }
110  }
111  *w = _resolutions[best].width;
112  *h = _resolutions[best].height;
113 }
114 
115 static uint FindStartupDisplay(uint startup_display)
116 {
117  int num_displays = SDL_GetNumVideoDisplays();
118 
119  /* If the user indicated a valid monitor, use that. */
120  if (IsInsideBS(startup_display, 0, num_displays)) return startup_display;
121 
122  /* Mouse position decides which display to use. */
123  int mx, my;
124  SDL_GetGlobalMouseState(&mx, &my);
125  for (int display = 0; display < num_displays; ++display) {
126  SDL_Rect r;
127  if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
128  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);
129  return display;
130  }
131  }
132 
133  return 0;
134 }
135 
136 void VideoDriver_SDL_Base::ClientSizeChanged(int w, int h, bool force)
137 {
138  /* Allocate backing store of the new size. */
139  if (this->AllocateBackingStore(w, h, force)) {
140  /* Mark all palette colours dirty. */
143  this->local_palette = _cur_palette;
144 
146 
147  GameSizeChanged();
148  }
149 }
150 
151 bool VideoDriver_SDL_Base::CreateMainWindow(uint w, uint h, uint flags)
152 {
153  if (this->sdl_window != nullptr) return true;
154 
155  flags |= SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
156 
157  if (_fullscreen) {
158  flags |= SDL_WINDOW_FULLSCREEN;
159  }
160 
161  int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
162  SDL_Rect r;
163  if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
164  x = r.x + std::max(0, r.w - static_cast<int>(w)) / 2;
165  y = r.y + std::max(0, r.h - static_cast<int>(h)) / 4; // decent desktops have taskbars at the bottom
166  }
167 
168  char caption[50];
169  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
170  this->sdl_window = SDL_CreateWindow(
171  caption,
172  x, y,
173  w, h,
174  flags);
175 
176  if (this->sdl_window == nullptr) {
177  DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on: %s", SDL_GetError());
178  return false;
179  }
180 
181  std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
182  if (!icon_path.empty()) {
183  /* Give the application an icon */
184  SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
185  if (icon != nullptr) {
186  /* Get the colourkey, which will be magenta */
187  uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
188 
189  SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
190  SDL_SetWindowIcon(this->sdl_window, icon);
191  SDL_FreeSurface(icon);
192  }
193  }
194 
195  return true;
196 }
197 
198 bool VideoDriver_SDL_Base::CreateMainSurface(uint w, uint h, bool resize)
199 {
200  GetAvailableVideoMode(&w, &h);
201  DEBUG(driver, 1, "SDL2: using mode %ux%u", w, h);
202 
203  if (!this->CreateMainWindow(w, h)) return false;
204  if (resize) SDL_SetWindowSize(this->sdl_window, w, h);
205  this->ClientSizeChanged(w, h, true);
206 
207  /* When in full screen, we will always have the mouse cursor
208  * within the window, even though SDL does not give us the
209  * appropriate event to know this. */
210  if (_fullscreen) _cursor.in_window = true;
211 
212  return true;
213 }
214 
215 bool VideoDriver_SDL_Base::ClaimMousePointer()
216 {
217  SDL_ShowCursor(0);
218 #ifdef __EMSCRIPTEN__
219  SDL_SetRelativeMouseMode(SDL_TRUE);
220 #endif
221  return true;
222 }
223 
228 {
229  if (!this->edit_box_focused) {
230  SDL_StartTextInput();
231  this->edit_box_focused = true;
232  }
233 }
234 
239 {
240  if (this->edit_box_focused) {
241  SDL_StopTextInput();
242  this->edit_box_focused = false;
243  }
244 }
245 
246 
247 struct SDLVkMapping {
248  SDL_Keycode vk_from;
249  byte vk_count;
250  byte map_to;
251  bool unprintable;
252 };
253 
254 #define AS(x, z) {x, 0, z, false}
255 #define AM(x, y, z, w) {x, (byte)(y - x), z, false}
256 #define AS_UP(x, z) {x, 0, z, true}
257 #define AM_UP(x, y, z, w) {x, (byte)(y - x), z, true}
258 
259 static const SDLVkMapping _vk_mapping[] = {
260  /* Pageup stuff + up/down */
261  AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
262  AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
263  AS_UP(SDLK_UP, WKC_UP),
264  AS_UP(SDLK_DOWN, WKC_DOWN),
265  AS_UP(SDLK_LEFT, WKC_LEFT),
266  AS_UP(SDLK_RIGHT, WKC_RIGHT),
267 
268  AS_UP(SDLK_HOME, WKC_HOME),
269  AS_UP(SDLK_END, WKC_END),
270 
271  AS_UP(SDLK_INSERT, WKC_INSERT),
272  AS_UP(SDLK_DELETE, WKC_DELETE),
273 
274  /* Map letters & digits */
275  AM(SDLK_a, SDLK_z, 'A', 'Z'),
276  AM(SDLK_0, SDLK_9, '0', '9'),
277 
278  AS_UP(SDLK_ESCAPE, WKC_ESC),
279  AS_UP(SDLK_PAUSE, WKC_PAUSE),
280  AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
281 
282  AS(SDLK_SPACE, WKC_SPACE),
283  AS(SDLK_RETURN, WKC_RETURN),
284  AS(SDLK_TAB, WKC_TAB),
285 
286  /* Function keys */
287  AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
288 
289  /* Numeric part. */
290  AM(SDLK_KP_0, SDLK_KP_9, '0', '9'),
291  AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
292  AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
293  AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
294  AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
295  AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
296  AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
297 
298  /* Other non-letter keys */
299  AS(SDLK_SLASH, WKC_SLASH),
300  AS(SDLK_SEMICOLON, WKC_SEMICOLON),
301  AS(SDLK_EQUALS, WKC_EQUALS),
302  AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
303  AS(SDLK_BACKSLASH, WKC_BACKSLASH),
304  AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
305 
306  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
307  AS(SDLK_COMMA, WKC_COMMA),
308  AS(SDLK_MINUS, WKC_MINUS),
309  AS(SDLK_PERIOD, WKC_PERIOD)
310 };
311 
312 static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, WChar *character)
313 {
314  const SDLVkMapping *map;
315  uint key = 0;
316  bool unprintable = false;
317 
318  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
319  if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
320  key = sym->sym - map->vk_from + map->map_to;
321  unprintable = map->unprintable;
322  break;
323  }
324  }
325 
326  /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
327  if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
328 
329  /* META are the command keys on mac */
330  if (sym->mod & KMOD_GUI) key |= WKC_META;
331  if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
332  if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
333  if (sym->mod & KMOD_ALT) key |= WKC_ALT;
334 
335  /* The mod keys have no character. Prevent '?' */
336  if (sym->mod & KMOD_GUI ||
337  sym->mod & KMOD_CTRL ||
338  sym->mod & KMOD_ALT ||
339  unprintable) {
340  *character = WKC_NONE;
341  } else {
342  *character = sym->sym;
343  }
344 
345  return key;
346 }
347 
352 static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
353 {
354  const SDLVkMapping *map;
355  uint key = 0;
356 
357  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
358  if ((uint)(kc - map->vk_from) <= map->vk_count) {
359  key = kc - map->vk_from + map->map_to;
360  break;
361  }
362  }
363 
364  /* check scancode for BACKQUOTE key, because we want the key left
365  * of "1", not anything else (on non-US keyboards) */
366  SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
367  if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
368 
369  return key;
370 }
371 
373 {
374  SDL_Event ev;
375 
376  if (!SDL_PollEvent(&ev)) return false;
377 
378  switch (ev.type) {
379  case SDL_MOUSEMOTION:
380 #ifdef __EMSCRIPTEN__
381  if (_cursor_new_in_window) {
382  /* The cursor just moved into the window; this means we don't
383  * know the absolutely position yet to move relative from.
384  * Before this time, SDL didn't know it either, and this is
385  * why we postpone it till now. Update the absolute position
386  * for this once, and work relative after. */
387  _cursor.pos.x = ev.motion.x;
388  _cursor.pos.y = ev.motion.y;
389  _cursor.dirty = true;
390 
391  _cursor_new_in_window = false;
392  SDL_SetRelativeMouseMode(SDL_TRUE);
393  } else {
394  _cursor.UpdateCursorPositionRelative(ev.motion.xrel, ev.motion.yrel);
395  }
396 #else
397  if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
398  SDL_WarpMouseInWindow(this->sdl_window, _cursor.pos.x, _cursor.pos.y);
399  }
400 #endif
402  break;
403 
404  case SDL_MOUSEWHEEL:
405  if (ev.wheel.y > 0) {
406  _cursor.wheel--;
407  } else if (ev.wheel.y < 0) {
408  _cursor.wheel++;
409  }
410  break;
411 
412  case SDL_MOUSEBUTTONDOWN:
413  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
414  ev.button.button = SDL_BUTTON_RIGHT;
415  }
416 
417  switch (ev.button.button) {
418  case SDL_BUTTON_LEFT:
419  _left_button_down = true;
420  break;
421 
422  case SDL_BUTTON_RIGHT:
423  _right_button_down = true;
424  _right_button_clicked = true;
425  break;
426 
427  default: break;
428  }
430  break;
431 
432  case SDL_MOUSEBUTTONUP:
433  if (_rightclick_emulate) {
434  _right_button_down = false;
435  _left_button_down = false;
436  _left_button_clicked = false;
437  } else if (ev.button.button == SDL_BUTTON_LEFT) {
438  _left_button_down = false;
439  _left_button_clicked = false;
440  } else if (ev.button.button == SDL_BUTTON_RIGHT) {
441  _right_button_down = false;
442  }
444  break;
445 
446  case SDL_QUIT:
447  HandleExitGameRequest();
448  break;
449 
450  case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
451  if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
452  (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
453  if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
454  } else {
455  WChar character;
456 
457  uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
458  // Only handle non-text keys here. Text is handled in
459  // SDL_TEXTINPUT below.
460  if (!this->edit_box_focused ||
461  keycode == WKC_DELETE ||
462  keycode == WKC_NUM_ENTER ||
463  keycode == WKC_LEFT ||
464  keycode == WKC_RIGHT ||
465  keycode == WKC_UP ||
466  keycode == WKC_DOWN ||
467  keycode == WKC_HOME ||
468  keycode == WKC_END ||
469  keycode & WKC_META ||
470  keycode & WKC_CTRL ||
471  keycode & WKC_ALT ||
472  (keycode >= WKC_F1 && keycode <= WKC_F12) ||
473  !IsValidChar(character, CS_ALPHANUMERAL)) {
474  HandleKeypress(keycode, character);
475  }
476  }
477  break;
478 
479  case SDL_TEXTINPUT: {
480  if (!this->edit_box_focused) break;
481  SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
482  uint keycode = ConvertSdlKeycodeIntoMy(kc);
483 
484  if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
485  WChar character;
486  Utf8Decode(&character, ev.text.text);
487  HandleKeypress(keycode, character);
488  } else {
489  HandleTextInput(ev.text.text);
490  }
491  break;
492  }
493  case SDL_WINDOWEVENT: {
494  if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
495  // Force a redraw of the entire screen.
496  this->MakeDirty(0, 0, _screen.width, _screen.height);
497  } else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
498  int w = std::max(ev.window.data1, 64);
499  int h = std::max(ev.window.data2, 64);
500  CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
501  } else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
502  // mouse entered the window, enable cursor
503  _cursor.in_window = true;
504 #ifdef __EMSCRIPTEN__
505  /* Disable relative mouse mode for the first mouse motion,
506  * so we can pick up the absolutely position again. */
507  _cursor_new_in_window = true;
508  SDL_SetRelativeMouseMode(SDL_FALSE);
509 #endif
510  } else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
511  // mouse left the window, undraw cursor
512  UndrawMouseCursor();
513  _cursor.in_window = false;
514  }
515  break;
516  }
517  }
518 
519  return true;
520 }
521 
522 static const char *InitializeSDL()
523 {
524  /* Explicitly disable hardware acceleration. Enabling this causes
525  * UpdateWindowSurface() to update the window's texture instead of
526  * its surface. */
527  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
528  SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
529 
530  /* Check if the video-driver is already initialized. */
531  if (SDL_WasInit(SDL_INIT_VIDEO) != 0) return nullptr;
532 
533  if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) return SDL_GetError();
534  return nullptr;
535 }
536 
537 const char *VideoDriver_SDL_Base::Initialize()
538 {
539  this->UpdateAutoResolution();
540 
541  const char *error = InitializeSDL();
542  if (error != nullptr) return error;
543 
544  FindResolutions();
545  DEBUG(driver, 2, "Resolution for display: %ux%u", _cur_resolution.width, _cur_resolution.height);
546 
547  return nullptr;
548 }
549 
550 const char *VideoDriver_SDL_Base::Start(const StringList &parm)
551 {
552  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
553 
554  const char *error = this->Initialize();
555  if (error != nullptr) return error;
556 
557  this->startup_display = FindStartupDisplay(GetDriverParamInt(parm, "display", -1));
558 
559  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
560  return SDL_GetError();
561  }
562 
563  const char *dname = SDL_GetCurrentVideoDriver();
564  DEBUG(driver, 1, "SDL2: using driver '%s'", dname);
565 
567 
568  this->draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
569  /* Wayland SDL video driver uses EGL to render the game. SDL created the
570  * EGL context from the main-thread, and with EGL you are not allowed to
571  * draw in another thread than the context was created. The function of
572  * draw_threaded is to do exactly this: draw in another thread than the
573  * window was created, and as such, this fails on Wayland SDL video
574  * driver. So, we disable threading by default if Wayland SDL video
575  * driver is detected.
576  */
577  if (strcmp(dname, "wayland") == 0) {
578  this->draw_threaded = false;
579  }
580 
581  SDL_StopTextInput();
582  this->edit_box_focused = false;
583 
584  return nullptr;
585 }
586 
588 {
589  SDL_QuitSubSystem(SDL_INIT_VIDEO);
590  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
591  SDL_Quit(); // If there's nothing left, quit SDL
592  }
593 }
594 
596 {
597  uint32 mod = SDL_GetModState();
598  const Uint8 *keys = SDL_GetKeyboardState(NULL);
599 
600  bool old_ctrl_pressed = _ctrl_pressed;
601 
602  _ctrl_pressed = !!(mod & KMOD_CTRL);
603  _shift_pressed = !!(mod & KMOD_SHIFT);
604 
605 #if defined(_DEBUG)
607 #else
608  /* Speedup when pressing tab, except when using ALT+TAB
609  * to switch to another application. */
610  this->fast_forward_key_pressed = keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0;
611 #endif /* defined(_DEBUG) */
612 
613  /* Determine which directional keys are down. */
614  _dirkeys =
615  (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
616  (keys[SDL_SCANCODE_UP] ? 2 : 0) |
617  (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
618  (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
619 
620  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
621 }
622 
623 void VideoDriver_SDL_Base::LoopOnce()
624 {
625  if (_exit_game) {
626 #ifdef __EMSCRIPTEN__
627  /* Emscripten is event-driven, and as such the main loop is inside
628  * the browser. So if _exit_game goes true, the main loop ends (the
629  * cancel call), but we still have to call the cleanup that is
630  * normally done at the end of the main loop for non-Emscripten.
631  * After that, Emscripten just halts, and the HTML shows a nice
632  * "bye, see you next time" message. */
633  emscripten_cancel_main_loop();
634  MainLoopCleanup();
635 #endif
636  return;
637  }
638 
639  if (VideoDriver::Tick()) {
640  if (this->draw_mutex != nullptr && !HasModalProgress()) {
641  this->draw_signal->notify_one();
642  } else {
643  this->Paint();
644  }
645  }
646 
647 /* Emscripten is running an event-based mainloop; there is already some
648  * downtime between each iteration, so no need to sleep. */
649 #ifndef __EMSCRIPTEN__
650  this->SleepTillNextTick();
651 #endif
652 }
653 
655 {
656  if (this->draw_threaded) {
657  /* Initialise the mutex first, because that's the thing we *need*
658  * directly in the newly created thread. */
659  this->draw_mutex = new std::recursive_mutex();
660  if (this->draw_mutex == nullptr) {
661  this->draw_threaded = false;
662  } else {
663  draw_lock = std::unique_lock<std::recursive_mutex>(*this->draw_mutex);
664  this->draw_signal = new std::condition_variable_any();
665  this->draw_continue = true;
666 
667  this->draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &VideoDriver_SDL_Base::PaintThreadThunk, this);
668 
669  /* Free the mutex if we won't be able to use it. */
670  if (!this->draw_threaded) {
671  draw_lock.unlock();
672  draw_lock.release();
673  delete this->draw_mutex;
674  delete this->draw_signal;
675  this->draw_mutex = nullptr;
676  this->draw_signal = nullptr;
677  } else {
678  /* Wait till the draw mutex has started itself. */
679  this->draw_signal->wait(*this->draw_mutex);
680  }
681  }
682  }
683 
684  DEBUG(driver, 1, "SDL2: using %sthreads", this->draw_threaded ? "" : "no ");
685 
686 #ifdef __EMSCRIPTEN__
687  /* Run the main loop event-driven, based on RequestAnimationFrame. */
688  emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
689 #else
690  while (!_exit_game) {
691  LoopOnce();
692  }
693 
694  MainLoopCleanup();
695 #endif
696 }
697 
698 void VideoDriver_SDL_Base::MainLoopCleanup()
699 {
700  if (this->draw_mutex != nullptr) {
701  this->draw_continue = false;
702  /* Sending signal if there is no thread blocked
703  * is very valid and results in noop */
704  this->draw_signal->notify_one();
705  if (draw_lock.owns_lock()) draw_lock.unlock();
706  draw_lock.release();
707  draw_thread.join();
708 
709  delete this->draw_mutex;
710  delete this->draw_signal;
711 
712  this->draw_mutex = nullptr;
713  this->draw_signal = nullptr;
714  }
715 
716 #ifdef __EMSCRIPTEN__
717  emscripten_exit_pointerlock();
718  /* In effect, the game ends here. As emscripten_set_main_loop() caused
719  * the stack to be unwound, the code after MainLoop() in
720  * openttd_main() is never executed. */
721  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
722  EM_ASM(if (window["openttd_exit"]) openttd_exit());
723 #endif
724 }
725 
727 {
728  std::unique_lock<std::recursive_mutex> lock;
729  if (this->draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*this->draw_mutex);
730 
731  return CreateMainSurface(w, h, true);
732 }
733 
735 {
736  std::unique_lock<std::recursive_mutex> lock;
737  if (this->draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*this->draw_mutex);
738 
739  int w, h;
740 
741  /* Remember current window size */
742  if (fullscreen) {
743  SDL_GetWindowSize(this->sdl_window, &w, &h);
744 
745  /* Find fullscreen window size */
746  SDL_DisplayMode dm;
747  if (SDL_GetCurrentDisplayMode(0, &dm) < 0) {
748  DEBUG(driver, 0, "SDL_GetCurrentDisplayMode() failed: %s", SDL_GetError());
749  } else {
750  SDL_SetWindowSize(this->sdl_window, dm.w, dm.h);
751  }
752  }
753 
754  DEBUG(driver, 1, "SDL2: Setting %s", fullscreen ? "fullscreen" : "windowed");
755  int ret = SDL_SetWindowFullscreen(this->sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
756  if (ret == 0) {
757  /* Switching resolution succeeded, set fullscreen value of window. */
758  _fullscreen = fullscreen;
759  if (!fullscreen) SDL_SetWindowSize(this->sdl_window, w, h);
760  } else {
761  DEBUG(driver, 0, "SDL_SetWindowFullscreen() failed: %s", SDL_GetError());
762  }
763 
764  return ret == 0;
765 }
766 
768 {
769  assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
770  int w, h;
771  SDL_GetWindowSize(this->sdl_window, &w, &h);
772  return CreateMainSurface(w, h, false);
773 }
774 
776 {
777  if (this->draw_mutex != nullptr) this->draw_mutex->lock();
778 }
779 
781 {
782  if (this->draw_mutex != nullptr) this->draw_mutex->unlock();
783 }
784 
786 {
787  SDL_DisplayMode mode;
788  if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
789 
790  return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
791 }
792 
794 {
795  if (this->buffer_locked) return false;
796  this->buffer_locked = true;
797 
798  if (this->draw_threaded) this->draw_lock.lock();
799 
800  _screen.dst_ptr = this->GetVideoPointer();
801  assert(_screen.dst_ptr != nullptr);
802 
803  return true;
804 }
805 
807 {
808  if (_screen.dst_ptr != nullptr) {
809  /* Hand video buffer back to the drawing backend. */
810  this->ReleaseVideoPointer();
811  _screen.dst_ptr = nullptr;
812  }
813 
814  if (this->draw_threaded) this->draw_lock.unlock();
815  this->buffer_locked = false;
816 }
_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:806
VideoDriver_SDL_Base::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: sdl2_v.cpp:780
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
VideoDriver::Tick
bool Tick()
Run the game for a single tick, processing boththe game-tick and draw-tick.
Definition: video_driver.cpp:20
VideoDriver_SDL_Base::draw_threaded
bool draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: sdl2_v.h:51
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:39
_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
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
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
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:793
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:227
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:1801
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:654
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:241
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:1828
VideoDriver_SDL_Base::Stop
void Stop() override
Stop this driver.
Definition: sdl2_v.cpp:587
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:595
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::draw_signal
std::condition_variable_any * draw_signal
Signal to draw the next frame.
Definition: sdl2_v.h:53
VideoDriver_SDL_Base::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl2_v.cpp:767
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
VideoDriver_SDL_Base::local_palette
Palette local_palette
Copy of _cur_palette.
Definition: sdl2_v.h:50
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:372
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:734
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:22
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:45
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:151
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:71
VideoDriver::PaintThread
virtual void PaintThread()
Thread function for threaded drawing.
Definition: video_driver.hpp:246
VideoDriver_SDL_Base::draw_mutex
std::recursive_mutex * draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: sdl2_v.h:52
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:24
WKC_PERIOD
@ WKC_PERIOD
. Period
Definition: gfx_type.h:103
VideoDriver::UpdateAutoResolution
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
Definition: video_driver.hpp:208
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:136
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:238
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:375
VideoDriver_SDL_Base::dirty_rect
Rect dirty_rect
Rectangle encompassing the dirty area of the video buffer.
Definition: sdl2_v.h:56
VideoDriver_SDL_Base
The SDL video driver.
Definition: sdl2_v.h:18
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:91
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:352
WKC_SEMICOLON
@ WKC_SEMICOLON
; Semicolon
Definition: gfx_type.h:96
VideoDriver_SDL_Base::draw_continue
volatile bool draw_continue
Should we keep continue drawing?
Definition: sdl2_v.h:54
_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:129
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:1619
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:75
SDLVkMapping
Definition: sdl2_v.cpp:247
VideoDriver_SDL_Base::buffer_locked
bool buffer_locked
Video buffer was locked by the main thread.
Definition: sdl2_v.h:55
VideoDriver_SDL_Base::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: sdl2_v.cpp:775
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:288
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
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:726
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:785
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:59
_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:23
_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:197
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:550