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