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