OpenTTD Source  1.11.0-beta1
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 #ifdef WITH_SDL2
11 
12 #include "../stdafx.h"
13 #include "../openttd.h"
14 #include "../gfx_func.h"
15 #include "../rev.h"
16 #include "../blitter/factory.hpp"
17 #include "../network/network.h"
18 #include "../thread.h"
19 #include "../progress.h"
20 #include "../core/random_func.hpp"
21 #include "../core/math_func.hpp"
22 #include "../fileio_func.h"
23 #include "../framerate_type.h"
24 #include "../window_func.h"
25 #include "sdl2_v.h"
26 #include <SDL.h>
27 #include <mutex>
28 #include <condition_variable>
29 #ifdef __EMSCRIPTEN__
30 # include <emscripten.h>
31 # include <emscripten/html5.h>
32 #endif
33 
34 #include "../safeguards.h"
35 
36 static FVideoDriver_SDL iFVideoDriver_SDL;
37 
38 static SDL_Window *_sdl_window;
39 static SDL_Surface *_sdl_surface;
40 static SDL_Surface *_sdl_realscreen;
41 
43 static bool _draw_threaded;
45 static std::recursive_mutex *_draw_mutex = nullptr;
47 static std::condition_variable_any *_draw_signal = nullptr;
49 static volatile bool _draw_continue;
50 static Palette _local_palette;
51 static SDL_Palette *_sdl_palette;
52 
53 #ifdef __EMSCRIPTEN__
54 
55 static bool _cursor_new_in_window = false;
56 #endif
57 
58 #define MAX_DIRTY_RECTS 100
59 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
60 static int _num_dirty_rects;
61 
62 /* Size of window */
63 static int _window_size_w;
64 static int _window_size_h;
65 
66 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
67 {
68  if (_num_dirty_rects < MAX_DIRTY_RECTS) {
69  _dirty_rects[_num_dirty_rects].x = left;
70  _dirty_rects[_num_dirty_rects].y = top;
71  _dirty_rects[_num_dirty_rects].w = width;
72  _dirty_rects[_num_dirty_rects].h = height;
73  }
74  _num_dirty_rects++;
75 }
76 
77 static void UpdatePalette(bool init = false)
78 {
79  SDL_Color pal[256];
80 
81  for (int i = 0; i != _local_palette.count_dirty; i++) {
85  pal[i].a = 0;
86  }
87 
88  SDL_SetPaletteColors(_sdl_palette, pal, _local_palette.first_dirty, _local_palette.count_dirty);
89  SDL_SetSurfacePalette(_sdl_surface, _sdl_palette);
90 
91  if (_sdl_surface != _sdl_realscreen && init) {
92  /* When using a shadow surface, also set our palette on the real screen. This lets SDL
93  * allocate as many colors (or approximations) as
94  * possible, instead of using only the default SDL
95  * palette. This allows us to get more colors exactly
96  * right and might allow using better approximations for
97  * other colors.
98  *
99  * Note that colors allocations are tried in-order, so
100  * this favors colors further up into the palette. Also
101  * note that if two colors from the same animation
102  * sequence are approximated using the same color, that
103  * animation will stop working.
104  *
105  * Since changing the system palette causes the colours
106  * to change right away, and allocations might
107  * drastically change, we can't use this for animation,
108  * since that could cause weird coloring between the
109  * palette change and the blitting below, so we only set
110  * the real palette during initialisation.
111  */
112  SDL_SetSurfacePalette(_sdl_realscreen, _sdl_palette);
113  }
114 
115  if (_sdl_surface != _sdl_realscreen && !init) {
116  /* We're not using real hardware palette, but are letting SDL
117  * approximate the palette during shadow -> screen copy. To
118  * change the palette, we need to recopy the entire screen.
119  *
120  * Note that this operation can slow down the rendering
121  * considerably, especially since changing the shadow
122  * palette will need the next blit to re-detect the
123  * best mapping of shadow palette colors to real palette
124  * colors from scratch.
125  */
126  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
127  SDL_UpdateWindowSurface(_sdl_window);
128  }
129 }
130 
131 static void InitPalette()
132 {
136  UpdatePalette(true);
137 }
138 
139 static void CheckPaletteAnim()
140 {
141  if (_cur_palette.count_dirty != 0) {
143 
144  switch (blitter->UsePaletteAnimation()) {
146  UpdatePalette();
147  break;
148 
150  blitter->PaletteAnimate(_local_palette);
151  break;
152 
154  break;
155 
156  default:
157  NOT_REACHED();
158  }
160  }
161 }
162 
163 static void DrawSurfaceToScreen()
164 {
165  PerformanceMeasurer framerate(PFE_VIDEO);
166 
167  int n = _num_dirty_rects;
168  if (n == 0) return;
169 
170  _num_dirty_rects = 0;
171 
172  if (n > MAX_DIRTY_RECTS) {
173  if (_sdl_surface != _sdl_realscreen) {
174  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
175  }
176 
177  SDL_UpdateWindowSurface(_sdl_window);
178  } else {
179  if (_sdl_surface != _sdl_realscreen) {
180  for (int i = 0; i < n; i++) {
181  SDL_BlitSurface(_sdl_surface, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
182  }
183  }
184 
185  SDL_UpdateWindowSurfaceRects(_sdl_window, _dirty_rects, n);
186  }
187 }
188 
189 static void DrawSurfaceToScreenThread()
190 {
191  /* First tell the main thread we're started */
192  std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
193  _draw_signal->notify_one();
194 
195  /* Now wait for the first thing to draw! */
196  _draw_signal->wait(*_draw_mutex);
197 
198  while (_draw_continue) {
199  CheckPaletteAnim();
200  /* Then just draw and wait till we stop */
201  DrawSurfaceToScreen();
202  _draw_signal->wait(lock);
203  }
204 }
205 
206 static void GetVideoModes()
207 {
208  int modes = SDL_GetNumDisplayModes(0);
209  if (modes == 0) usererror("sdl: no modes available");
210 
211  _resolutions.clear();
212 
213  SDL_DisplayMode mode;
214  for (int i = 0; i < modes; i++) {
215  SDL_GetDisplayMode(0, i, &mode);
216 
217  uint w = mode.w;
218  uint h = mode.h;
219 
220  if (w < 640 || h < 480) continue; // reject too small resolutions
221 
222  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
223  _resolutions.emplace_back(w, h);
224  }
225  if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
226  SortResolutions();
227 }
228 
229 static void GetAvailableVideoMode(uint *w, uint *h)
230 {
231  /* All modes available? */
232  if (!_fullscreen || _resolutions.empty()) return;
233 
234  /* Is the wanted mode among the available modes? */
235  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
236 
237  /* Use the closest possible resolution */
238  uint best = 0;
239  uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
240  for (uint i = 1; i != _resolutions.size(); ++i) {
241  uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
242  if (newdelta < delta) {
243  best = i;
244  delta = newdelta;
245  }
246  }
247  *w = _resolutions[best].width;
248  *h = _resolutions[best].height;
249 }
250 
251 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h, bool resize)
252 {
253  SDL_Surface *newscreen;
254  char caption[50];
256 
257  GetAvailableVideoMode(&w, &h);
258 
259  DEBUG(driver, 1, "SDL2: using mode %ux%ux%d", w, h, bpp);
260 
261  /* Free any previously allocated shadow surface */
262  if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);
263 
264  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
265 
266  if (_sdl_window == nullptr) {
267  Uint32 flags = SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE;
268 
269  if (_fullscreen) {
270  flags |= SDL_WINDOW_FULLSCREEN;
271  }
272 
273  int x = SDL_WINDOWPOS_UNDEFINED, y = SDL_WINDOWPOS_UNDEFINED;
274  SDL_Rect r;
275  if (SDL_GetDisplayBounds(this->startup_display, &r) == 0) {
276  x = r.x + std::max(0, r.w - static_cast<int>(w)) / 2;
277  y = r.y + std::max(0, r.h - static_cast<int>(h)) / 4; // decent desktops have taskbars at the bottom
278  }
279  _sdl_window = SDL_CreateWindow(
280  caption,
281  x, y,
282  w, h,
283  flags);
284 
285  if (_sdl_window == nullptr) {
286  DEBUG(driver, 0, "SDL2: Couldn't allocate a window to draw on");
287  return false;
288  }
289 
290  std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
291  if (!icon_path.empty()) {
292  /* Give the application an icon */
293  SDL_Surface *icon = SDL_LoadBMP(icon_path.c_str());
294  if (icon != nullptr) {
295  /* Get the colourkey, which will be magenta */
296  uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
297 
298  SDL_SetColorKey(icon, SDL_TRUE, rgbmap);
299  SDL_SetWindowIcon(_sdl_window, icon);
300  SDL_FreeSurface(icon);
301  }
302  }
303  }
304 
305  if (resize) SDL_SetWindowSize(_sdl_window, w, h);
306 
307  newscreen = SDL_GetWindowSurface(_sdl_window);
308  if (newscreen == NULL) {
309  DEBUG(driver, 0, "SDL2: Couldn't get window surface: %s", SDL_GetError());
310  return false;
311  }
312 
313  _sdl_realscreen = newscreen;
314 
315  if (bpp == 8) {
316  newscreen = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0);
317 
318  if (newscreen == nullptr) {
319  DEBUG(driver, 0, "SDL2: Couldn't allocate shadow surface: %s", SDL_GetError());
320  return false;
321  }
322  }
323 
324  if (_sdl_palette == nullptr) {
325  _sdl_palette = SDL_AllocPalette(256);
326  }
327 
328  if (_sdl_palette == nullptr) {
329  DEBUG(driver, 0, "SDL_AllocPalette() failed: %s", SDL_GetError());
330  return false;
331  }
332 
333  /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
334  _num_dirty_rects = 0;
335 
336  _screen.width = newscreen->w;
337  _screen.height = newscreen->h;
338  _screen.pitch = newscreen->pitch / (bpp / 8);
339  _screen.dst_ptr = newscreen->pixels;
340  _sdl_surface = newscreen;
341 
342  /* When in full screen, we will always have the mouse cursor
343  * within the window, even though SDL does not give us the
344  * appropriate event to know this. */
345  if (_fullscreen) _cursor.in_window = true;
346 
348  blitter->PostResize();
349 
350  InitPalette();
351 
352  GameSizeChanged();
353 
354  return true;
355 }
356 
357 bool VideoDriver_SDL::ClaimMousePointer()
358 {
359  SDL_ShowCursor(0);
360 #ifdef __EMSCRIPTEN__
361  SDL_SetRelativeMouseMode(SDL_TRUE);
362 #endif
363  return true;
364 }
365 
370 {
371  if (!this->edit_box_focused) {
372  SDL_StartTextInput();
373  this->edit_box_focused = true;
374  }
375 }
376 
381 {
382  if (this->edit_box_focused) {
383  SDL_StopTextInput();
384  this->edit_box_focused = false;
385  }
386 }
387 
388 
389 struct VkMapping {
390  SDL_Keycode vk_from;
391  byte vk_count;
392  byte map_to;
393  bool unprintable;
394 };
395 
396 #define AS(x, z) {x, 0, z, false}
397 #define AM(x, y, z, w) {x, (byte)(y - x), z, false}
398 #define AS_UP(x, z) {x, 0, z, true}
399 #define AM_UP(x, y, z, w) {x, (byte)(y - x), z, true}
400 
401 static const VkMapping _vk_mapping[] = {
402  /* Pageup stuff + up/down */
403  AS_UP(SDLK_PAGEUP, WKC_PAGEUP),
404  AS_UP(SDLK_PAGEDOWN, WKC_PAGEDOWN),
405  AS_UP(SDLK_UP, WKC_UP),
406  AS_UP(SDLK_DOWN, WKC_DOWN),
407  AS_UP(SDLK_LEFT, WKC_LEFT),
408  AS_UP(SDLK_RIGHT, WKC_RIGHT),
409 
410  AS_UP(SDLK_HOME, WKC_HOME),
411  AS_UP(SDLK_END, WKC_END),
412 
413  AS_UP(SDLK_INSERT, WKC_INSERT),
414  AS_UP(SDLK_DELETE, WKC_DELETE),
415 
416  /* Map letters & digits */
417  AM(SDLK_a, SDLK_z, 'A', 'Z'),
418  AM(SDLK_0, SDLK_9, '0', '9'),
419 
420  AS_UP(SDLK_ESCAPE, WKC_ESC),
421  AS_UP(SDLK_PAUSE, WKC_PAUSE),
422  AS_UP(SDLK_BACKSPACE, WKC_BACKSPACE),
423 
424  AS(SDLK_SPACE, WKC_SPACE),
425  AS(SDLK_RETURN, WKC_RETURN),
426  AS(SDLK_TAB, WKC_TAB),
427 
428  /* Function keys */
429  AM_UP(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
430 
431  /* Numeric part. */
432  AM(SDLK_KP_0, SDLK_KP_9, '0', '9'),
433  AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
434  AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
435  AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
436  AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
437  AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
438  AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
439 
440  /* Other non-letter keys */
441  AS(SDLK_SLASH, WKC_SLASH),
442  AS(SDLK_SEMICOLON, WKC_SEMICOLON),
443  AS(SDLK_EQUALS, WKC_EQUALS),
444  AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
445  AS(SDLK_BACKSLASH, WKC_BACKSLASH),
446  AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
447 
448  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
449  AS(SDLK_COMMA, WKC_COMMA),
450  AS(SDLK_MINUS, WKC_MINUS),
451  AS(SDLK_PERIOD, WKC_PERIOD)
452 };
453 
454 static uint ConvertSdlKeyIntoMy(SDL_Keysym *sym, WChar *character)
455 {
456  const VkMapping *map;
457  uint key = 0;
458  bool unprintable = false;
459 
460  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
461  if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
462  key = sym->sym - map->vk_from + map->map_to;
463  unprintable = map->unprintable;
464  break;
465  }
466  }
467 
468  /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
469  if (sym->scancode == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
470 
471  /* META are the command keys on mac */
472  if (sym->mod & KMOD_GUI) key |= WKC_META;
473  if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
474  if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
475  if (sym->mod & KMOD_ALT) key |= WKC_ALT;
476 
477  /* The mod keys have no character. Prevent '?' */
478  if (sym->mod & KMOD_GUI ||
479  sym->mod & KMOD_CTRL ||
480  sym->mod & KMOD_ALT ||
481  unprintable) {
482  *character = WKC_NONE;
483  } else {
484  *character = sym->sym;
485  }
486 
487  return key;
488 }
489 
494 static uint ConvertSdlKeycodeIntoMy(SDL_Keycode kc)
495 {
496  const VkMapping *map;
497  uint key = 0;
498 
499  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
500  if ((uint)(kc - map->vk_from) <= map->vk_count) {
501  key = kc - map->vk_from + map->map_to;
502  break;
503  }
504  }
505 
506  /* check scancode for BACKQUOTE key, because we want the key left
507  of "1", not anything else (on non-US keyboards) */
508  SDL_Scancode sc = SDL_GetScancodeFromKey(kc);
509  if (sc == SDL_SCANCODE_GRAVE) key = WKC_BACKQUOTE;
510 
511  return key;
512 }
513 
514 int VideoDriver_SDL::PollEvent()
515 {
516  SDL_Event ev;
517 
518  if (!SDL_PollEvent(&ev)) return -2;
519 
520  switch (ev.type) {
521  case SDL_MOUSEMOTION:
522 #ifdef __EMSCRIPTEN__
523  if (_cursor_new_in_window) {
524  /* The cursor just moved into the window; this means we don't
525  * know the absolutely position yet to move relative from.
526  * Before this time, SDL didn't know it either, and this is
527  * why we postpone it till now. Update the absolute position
528  * for this once, and work relative after. */
529  _cursor.pos.x = ev.motion.x;
530  _cursor.pos.y = ev.motion.y;
531  _cursor.dirty = true;
532 
533  _cursor_new_in_window = false;
534  SDL_SetRelativeMouseMode(SDL_TRUE);
535  } else {
536  _cursor.UpdateCursorPositionRelative(ev.motion.xrel, ev.motion.yrel);
537  }
538 #else
539  if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
540  SDL_WarpMouseInWindow(_sdl_window, _cursor.pos.x, _cursor.pos.y);
541  }
542 #endif
544  break;
545 
546  case SDL_MOUSEWHEEL:
547  if (ev.wheel.y > 0) {
548  _cursor.wheel--;
549  } else if (ev.wheel.y < 0) {
550  _cursor.wheel++;
551  }
552  break;
553 
554  case SDL_MOUSEBUTTONDOWN:
555  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
556  ev.button.button = SDL_BUTTON_RIGHT;
557  }
558 
559  switch (ev.button.button) {
560  case SDL_BUTTON_LEFT:
561  _left_button_down = true;
562  break;
563 
564  case SDL_BUTTON_RIGHT:
565  _right_button_down = true;
566  _right_button_clicked = true;
567  break;
568 
569  default: break;
570  }
572  break;
573 
574  case SDL_MOUSEBUTTONUP:
575  if (_rightclick_emulate) {
576  _right_button_down = false;
577  _left_button_down = false;
578  _left_button_clicked = false;
579  } else if (ev.button.button == SDL_BUTTON_LEFT) {
580  _left_button_down = false;
581  _left_button_clicked = false;
582  } else if (ev.button.button == SDL_BUTTON_RIGHT) {
583  _right_button_down = false;
584  }
586  break;
587 
588  case SDL_QUIT:
589  HandleExitGameRequest();
590  break;
591 
592  case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
593  if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_GUI)) &&
594  (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
595  if (ev.key.repeat == 0) ToggleFullScreen(!_fullscreen);
596  } else {
597  WChar character;
598 
599  uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
600  // Only handle non-text keys here. Text is handled in
601  // SDL_TEXTINPUT below.
602  if (!this->edit_box_focused ||
603  keycode == WKC_DELETE ||
604  keycode == WKC_NUM_ENTER ||
605  keycode == WKC_LEFT ||
606  keycode == WKC_RIGHT ||
607  keycode == WKC_UP ||
608  keycode == WKC_DOWN ||
609  keycode == WKC_HOME ||
610  keycode == WKC_END ||
611  keycode & WKC_META ||
612  keycode & WKC_CTRL ||
613  keycode & WKC_ALT ||
614  (keycode >= WKC_F1 && keycode <= WKC_F12) ||
615  !IsValidChar(character, CS_ALPHANUMERAL)) {
616  HandleKeypress(keycode, character);
617  }
618  }
619  break;
620 
621  case SDL_TEXTINPUT: {
622  if (!this->edit_box_focused) break;
623  SDL_Keycode kc = SDL_GetKeyFromName(ev.text.text);
624  uint keycode = ConvertSdlKeycodeIntoMy(kc);
625 
626  if (keycode == WKC_BACKQUOTE && FocusedWindowIsConsole()) {
627  WChar character;
628  Utf8Decode(&character, ev.text.text);
629  HandleKeypress(keycode, character);
630  } else {
631  HandleTextInput(ev.text.text);
632  }
633  break;
634  }
635  case SDL_WINDOWEVENT: {
636  if (ev.window.event == SDL_WINDOWEVENT_EXPOSED) {
637  // Force a redraw of the entire screen.
638  _num_dirty_rects = MAX_DIRTY_RECTS + 1;
639  } else if (ev.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
640  int w = std::max(ev.window.data1, 64);
641  int h = std::max(ev.window.data2, 64);
642  CreateMainSurface(w, h, w != ev.window.data1 || h != ev.window.data2);
643  } else if (ev.window.event == SDL_WINDOWEVENT_ENTER) {
644  // mouse entered the window, enable cursor
645  _cursor.in_window = true;
646 #ifdef __EMSCRIPTEN__
647  /* Disable relative mouse mode for the first mouse motion,
648  * so we can pick up the absolutely position again. */
649  _cursor_new_in_window = true;
650  SDL_SetRelativeMouseMode(SDL_FALSE);
651 #endif
652  } else if (ev.window.event == SDL_WINDOWEVENT_LEAVE) {
653  // mouse left the window, undraw cursor
654  UndrawMouseCursor();
655  _cursor.in_window = false;
656  }
657  break;
658  }
659  }
660  return -1;
661 }
662 
663 const char *VideoDriver_SDL::Start(const StringList &parm)
664 {
665  if (BlitterFactory::GetCurrentBlitter()->GetScreenDepth() == 0) return "Only real blitters supported";
666 
667  /* Explicitly disable hardware acceleration. Enabling this causes
668  * UpdateWindowSurface() to update the window's texture instead of
669  * its surface. */
670  SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "0");
671  SDL_SetHint(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "1");
672 
673  /* Just on the offchance the audio subsystem started before the video system,
674  * check whether any part of SDL has been initialised before getting here.
675  * Slightly duplicated with sound/sdl_s.cpp */
676  int ret_code = 0;
677  if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
678  ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
679  }
680  if (ret_code < 0) return SDL_GetError();
681 
682  this->startup_display = GetDriverParamInt(parm, "display", -1);
683  int num_displays = SDL_GetNumVideoDisplays();
684  if (!IsInsideBS(this->startup_display, 0, num_displays)) {
685  /* Mouse position decides which display to use */
686  int mx, my;
687  SDL_GetGlobalMouseState(&mx, &my);
688  this->startup_display = 0; // used when mouse is on no screen...
689  for (int display = 0; display < num_displays; ++display) {
690  SDL_Rect r;
691  if (SDL_GetDisplayBounds(display, &r) == 0 && IsInsideBS(mx, r.x, r.w) && IsInsideBS(my, r.y, r.h)) {
692  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);
693  this->startup_display = display;
694  break;
695  }
696  }
697  }
698 
699  this->UpdateAutoResolution();
700 
701  GetVideoModes();
702  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height, false)) {
703  return SDL_GetError();
704  }
705 
706  const char *dname = SDL_GetCurrentVideoDriver();
707  DEBUG(driver, 1, "SDL2: using driver '%s'", dname);
708 
710 
711  _draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
712 
713  SDL_StopTextInput();
714  this->edit_box_focused = false;
715 
716  return nullptr;
717 }
718 
720 {
721  SDL_QuitSubSystem(SDL_INIT_VIDEO);
722  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
723  SDL_Quit(); // If there's nothing left, quit SDL
724  }
725 }
726 
727 void VideoDriver_SDL::LoopOnce()
728 {
729  uint32 mod;
730  int numkeys;
731  const Uint8 *keys;
732  uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
733  InteractiveRandom(); // randomness
734 
735  while (PollEvent() == -1) {}
736  if (_exit_game) {
737 #ifdef __EMSCRIPTEN__
738  /* Emscripten is event-driven, and as such the main loop is inside
739  * the browser. So if _exit_game goes true, the main loop ends (the
740  * cancel call), but we still have to call the cleanup that is
741  * normally done at the end of the main loop for non-Emscripten.
742  * After that, Emscripten just halts, and the HTML shows a nice
743  * "bye, see you next time" message. */
744  emscripten_cancel_main_loop();
745  MainLoopCleanup();
746 #endif
747  return;
748  }
749 
750  mod = SDL_GetModState();
751  keys = SDL_GetKeyboardState(&numkeys);
752 
753 #if defined(_DEBUG)
754  if (_shift_pressed)
755 #else
756  /* Speedup when pressing tab, except when using ALT+TAB
757  * to switch to another application */
758  if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
759 #endif /* defined(_DEBUG) */
760  {
761  if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
762  } else if (_fast_forward & 2) {
763  _fast_forward = 0;
764  }
765 
766  cur_ticks = SDL_GetTicks();
767  if (SDL_TICKS_PASSED(cur_ticks, next_tick) || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
768  _realtime_tick += cur_ticks - last_cur_ticks;
769  last_cur_ticks = cur_ticks;
770  next_tick = cur_ticks + MILLISECONDS_PER_TICK;
771 
772  bool old_ctrl_pressed = _ctrl_pressed;
773 
774  _ctrl_pressed = !!(mod & KMOD_CTRL);
775  _shift_pressed = !!(mod & KMOD_SHIFT);
776 
777  /* determine which directional keys are down */
778  _dirkeys =
779  (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
780  (keys[SDL_SCANCODE_UP] ? 2 : 0) |
781  (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
782  (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
783  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
784 
785  /* The gameloop is the part that can run asynchronously. The rest
786  * except sleeping can't. */
787  if (_draw_mutex != nullptr) draw_lock.unlock();
788 
789  GameLoop();
790 
791  if (_draw_mutex != nullptr) draw_lock.lock();
792 
793  UpdateWindows();
795  } else {
796  /* Release the thread while sleeping */
797  if (_draw_mutex != nullptr) {
798  draw_lock.unlock();
799  CSleep(1);
800  draw_lock.lock();
801  } else {
802 /* Emscripten is running an event-based mainloop; there is already some
803  * downtime between each iteration, so no need to sleep. */
804 #ifndef __EMSCRIPTEN__
805  CSleep(1);
806 #endif
807  }
808 
810  DrawMouseCursor();
811  }
812 
813  /* End of the critical part. */
814  if (_draw_mutex != nullptr && !HasModalProgress()) {
815  _draw_signal->notify_one();
816  } else {
817  /* Oh, we didn't have threads, then just draw unthreaded */
818  CheckPaletteAnim();
819  DrawSurfaceToScreen();
820  }
821 }
822 
824 {
825  cur_ticks = SDL_GetTicks();
826  last_cur_ticks = cur_ticks;
827  next_tick = cur_ticks + MILLISECONDS_PER_TICK;
828 
829  CheckPaletteAnim();
830 
831  if (_draw_threaded) {
832  /* Initialise the mutex first, because that's the thing we *need*
833  * directly in the newly created thread. */
834  _draw_mutex = new std::recursive_mutex();
835  if (_draw_mutex == nullptr) {
836  _draw_threaded = false;
837  } else {
838  draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
839  _draw_signal = new std::condition_variable_any();
840  _draw_continue = true;
841 
842  _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread);
843 
844  /* Free the mutex if we won't be able to use it. */
845  if (!_draw_threaded) {
846  draw_lock.unlock();
847  draw_lock.release();
848  delete _draw_mutex;
849  delete _draw_signal;
850  _draw_mutex = nullptr;
851  _draw_signal = nullptr;
852  } else {
853  /* Wait till the draw mutex has started itself. */
854  _draw_signal->wait(*_draw_mutex);
855  }
856  }
857  }
858 
859  DEBUG(driver, 1, "SDL2: using %sthreads", _draw_threaded ? "" : "no ");
860 
861 #ifdef __EMSCRIPTEN__
862  /* Run the main loop event-driven, based on RequestAnimationFrame. */
863  emscripten_set_main_loop_arg(&this->EmscriptenLoop, this, 0, 1);
864 #else
865  while (!_exit_game) {
866  LoopOnce();
867  }
868 
869  MainLoopCleanup();
870 #endif
871 }
872 
873 void VideoDriver_SDL::MainLoopCleanup()
874 {
875  if (_draw_mutex != nullptr) {
876  _draw_continue = false;
877  /* Sending signal if there is no thread blocked
878  * is very valid and results in noop */
879  _draw_signal->notify_one();
880  if (draw_lock.owns_lock()) draw_lock.unlock();
881  draw_lock.release();
882  draw_thread.join();
883 
884  delete _draw_mutex;
885  delete _draw_signal;
886 
887  _draw_mutex = nullptr;
888  _draw_signal = nullptr;
889  }
890 
891 #ifdef __EMSCRIPTEN__
892  emscripten_exit_pointerlock();
893  /* In effect, the game ends here. As emscripten_set_main_loop() caused
894  * the stack to be unwound, the code after MainLoop() in
895  * openttd_main() is never executed. */
896  EM_ASM(if (window["openttd_syncfs"]) openttd_syncfs());
897  EM_ASM(if (window["openttd_exit"]) openttd_exit());
898 #endif
899 }
900 
901 bool VideoDriver_SDL::ChangeResolution(int w, int h)
902 {
903  std::unique_lock<std::recursive_mutex> lock;
904  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
905 
906  return CreateMainSurface(w, h, true);
907 }
908 
910 {
911  std::unique_lock<std::recursive_mutex> lock;
912  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
913 
914  /* Remember current window size */
915  if (fullscreen) {
916  SDL_GetWindowSize(_sdl_window, &_window_size_w, &_window_size_h);
917 
918  /* Find fullscreen window size */
919  SDL_DisplayMode dm;
920  if (SDL_GetCurrentDisplayMode(0, &dm) < 0) {
921  DEBUG(driver, 0, "SDL_GetCurrentDisplayMode() failed: %s", SDL_GetError());
922  } else {
923  SDL_SetWindowSize(_sdl_window, dm.w, dm.h);
924  }
925  }
926 
927  DEBUG(driver, 1, "SDL2: Setting %s", fullscreen ? "fullscreen" : "windowed");
928  int ret = SDL_SetWindowFullscreen(_sdl_window, fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
929  if (ret == 0) {
930  /* Switching resolution succeeded, set fullscreen value of window. */
931  _fullscreen = fullscreen;
932  if (!fullscreen) SDL_SetWindowSize(_sdl_window, _window_size_w, _window_size_h);
933  } else {
934  DEBUG(driver, 0, "SDL_SetWindowFullscreen() failed: %s", SDL_GetError());
935  }
936 
937  return ret == 0;
938 }
939 
941 {
942  assert(BlitterFactory::GetCurrentBlitter()->GetScreenDepth() != 0);
943  int w, h;
944  SDL_GetWindowSize(_sdl_window, &w, &h);
945  return CreateMainSurface(w, h, false);
946 }
947 
949 {
950  if (_draw_mutex != nullptr) _draw_mutex->lock();
951 }
952 
954 {
955  if (_draw_mutex != nullptr) _draw_mutex->unlock();
956 }
957 
958 Dimension VideoDriver_SDL::GetScreenSize() const
959 {
960  SDL_DisplayMode mode;
961  if (SDL_GetCurrentDisplayMode(this->startup_display, &mode) != 0) return VideoDriver::GetScreenSize();
962 
963  return { static_cast<uint>(mode.w), static_cast<uint>(mode.h) };
964 }
965 
966 #endif /* WITH_SDL2 */
_draw_threaded
static bool _draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: sdl_v.cpp:38
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
fullscreen
bool fullscreen
Whether to use (true) fullscreen mode.
Definition: win32_v.cpp:52
WKC_SINGLEQUOTE
@ WKC_SINGLEQUOTE
' Single quote
Definition: gfx_type.h:101
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
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:100
VideoDriver_SDL::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl_v.cpp:601
PFE_VIDEO
@ PFE_VIDEO
Speed of painting drawn video buffer.
Definition: framerate_type.h:59
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
CSleep
void CSleep(int milliseconds)
Sleep on the current thread for a defined time.
Definition: thread.h:25
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
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:38
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
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
Blitter
How all blitters should look like.
Definition: base.hpp:28
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
Blitter::GetScreenDepth
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
_local_palette
static Palette _local_palette
Local copy of the palette for use in the drawing thread.
Definition: win32_v.cpp:72
WKC_SLASH
@ WKC_SLASH
/ Forward slash
Definition: gfx_type.h:95
WKC_BACKSLASH
@ WKC_BACKSLASH
\ Backslash
Definition: gfx_type.h:99
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
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
_draw_mutex
static std::recursive_mutex * _draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: sdl_v.cpp:40
VideoDriver_SDL::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: sdl_v.cpp:830
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:1792
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::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: sdl_v.cpp:803
VideoDriver_SDL::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: sdl_v.cpp:649
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3139
height
int height
Height in pixels of our display surface.
Definition: win32_v.cpp:49
VkMapping
Definition: sdl_v.cpp:406
VideoDriver_SDL::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: sdl_v.cpp:825
VideoDriver_SDL::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl_v.cpp:53
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2988
VideoDriver_SDL::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:61
CursorVars::UpdateCursorPosition
bool UpdateCursorPosition(int x, int y, bool queued_warp)
Update cursor position on mouse movement.
Definition: gfx.cpp:1819
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:314
Blitter::PostResize
virtual void PostResize()
Post resize event.
Definition: base.hpp:201
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::EditBoxGainedFocus
void EditBoxGainedFocus() override
An edit box gained the input focus.
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:131
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
_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
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
CursorVars::wheel
int wheel
mouse wheel movement
Definition: gfx_type.h:119
FocusedWindowIsConsole
bool FocusedWindowIsConsole()
Check if a console is focused.
Definition: window.cpp:471
VideoDriver_SDL::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: sdl_v.cpp:795
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
FVideoDriver_SDL
Factory for the SDL video driver.
Definition: sdl2_v.h:73
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_SDL::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl_v.cpp:820
VideoDriver::UpdateAutoResolution
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
Definition: video_driver.hpp:124
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:385
Blitter::PALETTE_ANIMATION_VIDEO_BACKEND
@ PALETTE_ANIMATION_VIDEO_BACKEND
Palette animation should be done by video backend (8bpp only!)
Definition: base.hpp:51
width
int width
Width in pixels of our display surface.
Definition: win32_v.cpp:48
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
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:50
WKC_SEMICOLON
@ WKC_SEMICOLON
; Semicolon
Definition: gfx_type.h:96
_cur_palette
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
GameSizeChanged
void GameSizeChanged()
Size of the application screen changed.
Definition: main_gui.cpp:561
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:310
Blitter::PaletteAnimate
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
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:1610
Blitter::PALETTE_ANIMATION_BLITTER
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition: base.hpp:52
VideoDriver_SDL::EditBoxLostFocus
void EditBoxLostFocus() override
An edit box lost the input focus.
NetworkDrawChatMessage
void NetworkDrawChatMessage()
Draw the chat message-box.
Definition: network_chat_gui.cpp:198
_draw_continue
static volatile bool _draw_continue
Should we keep continue drawing?
Definition: sdl_v.cpp:44
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
WKC_MINUS
@ WKC_MINUS
Definition: gfx_type.h:104
_realtime_tick
uint32 _realtime_tick
The real time in the game.
Definition: debug.cpp:48
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
Palette
Information about the currently used palette.
Definition: gfx_type.h:313
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:393
VideoDriver_SDL::Stop
void Stop() override
Stop this driver.
Definition: sdl_v.cpp:641
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
_draw_signal
static std::condition_variable_any * _draw_signal
Signal to draw the next frame.
Definition: sdl_v.cpp:42
_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