OpenTTD
sdl_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_SDL
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 "sdl_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_Surface *_sdl_screen;
37 static SDL_Surface *_sdl_realscreen;
38 static bool _all_modes;
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 
50 #define MAX_DIRTY_RECTS 100
51 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
52 static int _num_dirty_rects;
53 static int _use_hwpalette;
54 static int _requested_hwpalette; /* Did we request a HWPALETTE for the current video mode? */
55 
56 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
57 {
58  if (_num_dirty_rects < MAX_DIRTY_RECTS) {
59  _dirty_rects[_num_dirty_rects].x = left;
60  _dirty_rects[_num_dirty_rects].y = top;
61  _dirty_rects[_num_dirty_rects].w = width;
62  _dirty_rects[_num_dirty_rects].h = height;
63  }
64  _num_dirty_rects++;
65 }
66 
67 static void UpdatePalette(bool init = false)
68 {
69  SDL_Color pal[256];
70 
71  for (int i = 0; i != _local_palette.count_dirty; i++) {
72  pal[i].r = _local_palette.palette[_local_palette.first_dirty + i].r;
73  pal[i].g = _local_palette.palette[_local_palette.first_dirty + i].g;
74  pal[i].b = _local_palette.palette[_local_palette.first_dirty + i].b;
75  pal[i].unused = 0;
76  }
77 
78  SDL_SetColors(_sdl_screen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
79 
80  if (_sdl_screen != _sdl_realscreen && init) {
81  /* When using a shadow surface, also set our palette on the real screen. This lets SDL
82  * allocate as much colors (or approximations) as
83  * possible, instead of using only the default SDL
84  * palette. This allows us to get more colors exactly
85  * right and might allow using better approximations for
86  * other colors.
87  *
88  * Note that colors allocations are tried in-order, so
89  * this favors colors further up into the palette. Also
90  * note that if two colors from the same animation
91  * sequence are approximated using the same color, that
92  * animation will stop working.
93  *
94  * Since changing the system palette causes the colours
95  * to change right away, and allocations might
96  * drastically change, we can't use this for animation,
97  * since that could cause weird coloring between the
98  * palette change and the blitting below, so we only set
99  * the real palette during initialisation.
100  */
101  SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
102  }
103 
104  if (_sdl_screen != _sdl_realscreen && !init) {
105  /* We're not using real hardware palette, but are letting SDL
106  * approximate the palette during shadow -> screen copy. To
107  * change the palette, we need to recopy the entire screen.
108  *
109  * Note that this operation can slow down the rendering
110  * considerably, especially since changing the shadow
111  * palette will need the next blit to re-detect the
112  * best mapping of shadow palette colors to real palette
113  * colors from scratch.
114  */
115  SDL_BlitSurface(_sdl_screen, nullptr, _sdl_realscreen, nullptr);
116  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
117  }
118 }
119 
120 static void InitPalette()
121 {
122  _local_palette = _cur_palette;
123  _local_palette.first_dirty = 0;
124  _local_palette.count_dirty = 256;
125  UpdatePalette(true);
126 }
127 
128 static void CheckPaletteAnim()
129 {
130  if (_cur_palette.count_dirty != 0) {
132 
133  switch (blitter->UsePaletteAnimation()) {
135  UpdatePalette();
136  break;
137 
139  blitter->PaletteAnimate(_local_palette);
140  break;
141 
143  break;
144 
145  default:
146  NOT_REACHED();
147  }
149  }
150 }
151 
152 static void DrawSurfaceToScreen()
153 {
154  PerformanceMeasurer framerate(PFE_VIDEO);
155 
156  int n = _num_dirty_rects;
157  if (n == 0) return;
158 
159  _num_dirty_rects = 0;
160  if (n > MAX_DIRTY_RECTS) {
161  if (_sdl_screen != _sdl_realscreen) {
162  SDL_BlitSurface(_sdl_screen, nullptr, _sdl_realscreen, nullptr);
163  }
164  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
165  } else {
166  if (_sdl_screen != _sdl_realscreen) {
167  for (int i = 0; i < n; i++) {
168  SDL_BlitSurface(_sdl_screen, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
169  }
170  }
171  SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
172  }
173 }
174 
175 static void DrawSurfaceToScreenThread()
176 {
177  /* First tell the main thread we're started */
178  std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
179  _draw_signal->notify_one();
180 
181  /* Now wait for the first thing to draw! */
182  _draw_signal->wait(*_draw_mutex);
183 
184  while (_draw_continue) {
185  CheckPaletteAnim();
186  /* Then just draw and wait till we stop */
187  DrawSurfaceToScreen();
188  _draw_signal->wait(lock);
189  }
190 }
191 
192 static const Dimension _default_resolutions[] = {
193  { 640, 480},
194  { 800, 600},
195  {1024, 768},
196  {1152, 864},
197  {1280, 800},
198  {1280, 960},
199  {1280, 1024},
200  {1400, 1050},
201  {1600, 1200},
202  {1680, 1050},
203  {1920, 1200}
204 };
205 
206 static void GetVideoModes()
207 {
208  SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
209  if (modes == nullptr) usererror("sdl: no modes available");
210 
211  _resolutions.clear();
212 
213  _all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
214  if (modes == (void*)-1) {
215  for (uint i = 0; i < lengthof(_default_resolutions); i++) {
216  if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
217  _resolutions.push_back(_default_resolutions[i]);
218  }
219  }
220  } else {
221  for (int i = 0; modes[i]; i++) {
222  uint w = modes[i]->w;
223  uint h = modes[i]->h;
224  if (w < 640 || h < 480) continue; // reject too small resolutions
225  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
226  _resolutions.emplace_back(w, h);
227  }
228  if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
229  SortResolutions();
230  }
231 }
232 
233 static void GetAvailableVideoMode(uint *w, uint *h)
234 {
235  /* All modes available? */
236  if (_all_modes || _resolutions.empty()) return;
237 
238  /* Is the wanted mode among the available modes? */
239  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
240 
241  /* Use the closest possible resolution */
242  uint best = 0;
243  uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
244  for (uint i = 1; i != _resolutions.size(); ++i) {
245  uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
246  if (newdelta < delta) {
247  best = i;
248  delta = newdelta;
249  }
250  }
251  *w = _resolutions[best].width;
252  *h = _resolutions[best].height;
253 }
254 
255 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
256 {
257  SDL_Surface *newscreen, *icon;
258  char caption[50];
260  bool want_hwpalette;
261 
262  GetAvailableVideoMode(&w, &h);
263 
264  DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
265 
266  if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
267 
268  char icon_path[MAX_PATH];
269  if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != nullptr) {
270  /* Give the application an icon */
271  icon = SDL_LoadBMP(icon_path);
272  if (icon != nullptr) {
273  /* Get the colourkey, which will be magenta */
274  uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
275 
276  SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
277  SDL_WM_SetIcon(icon, nullptr);
278  SDL_FreeSurface(icon);
279  }
280  }
281 
282  if (_use_hwpalette == 2) {
283  /* Default is to autodetect when to use SDL_HWPALETTE.
284  * In this case, SDL_HWPALETTE is only used for 8bpp
285  * blitters in fullscreen.
286  *
287  * When using an 8bpp blitter on a 8bpp system in
288  * windowed mode with SDL_HWPALETTE, OpenTTD will claim
289  * the system palette, making all other applications
290  * get the wrong colours. In this case, we're better of
291  * trying to approximate the colors we need using system
292  * colors, using a shadow surface (see below).
293  *
294  * On a 32bpp system, SDL_HWPALETTE is ignored, so it
295  * doesn't matter what we do.
296  *
297  * When using a 32bpp blitter on a 8bpp system, setting
298  * SDL_HWPALETTE messes up rendering (at least on X11),
299  * so we don't do that. In this case, SDL takes care of
300  * color approximation using its own shadow surface
301  * (which we can't force in 8bpp on 8bpp mode,
302  * unfortunately).
303  */
304  want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
305  } else {
306  /* User specified a value manually */
307  want_hwpalette = _use_hwpalette;
308  }
309 
310  if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palette");
311 
312  /* Free any previously allocated shadow surface */
313  if (_sdl_screen != nullptr && _sdl_screen != _sdl_realscreen) SDL_FreeSurface(_sdl_screen);
314 
315  if (_sdl_realscreen != nullptr) {
316  if (_requested_hwpalette != want_hwpalette) {
317  /* SDL (at least the X11 driver), reuses the
318  * same window and palette settings when the bpp
319  * (and a few flags) are the same. Since we need
320  * to hwpalette value to change (in particular
321  * when switching between fullscreen and
322  * windowed), we restart the entire video
323  * subsystem to force creating a new window.
324  */
325  DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
326  SDL_QuitSubSystem(SDL_INIT_VIDEO);
327  SDL_InitSubSystem(SDL_INIT_VIDEO);
328  ClaimMousePointer();
329  SetupKeyboard();
330  }
331  }
332  /* Remember if we wanted a hwpalette. We can't reliably query
333  * SDL for the SDL_HWPALETTE flag, since it might get set even
334  * though we didn't ask for it (when SDL creates a shadow
335  * surface, for example). */
336  _requested_hwpalette = want_hwpalette;
337 
338  /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
339  newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
340  if (newscreen == nullptr) {
341  DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
342  return false;
343  }
344  _sdl_realscreen = newscreen;
345 
346  if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
347  /* Using an 8bpp blitter, if we didn't get a hardware
348  * palette (most likely because we didn't request one,
349  * see above), we'll have to set up a shadow surface to
350  * render on.
351  *
352  * Our palette will be applied to this shadow surface,
353  * while the real screen surface will use the shared
354  * system palette (which will partly contain our colors,
355  * but most likely will not have enough free color cells
356  * for all of our colors). SDL can use these two
357  * palettes at blit time to approximate colors used in
358  * the shadow surface using system colors automatically.
359  *
360  * Note that when using an 8bpp blitter on a 32bpp
361  * system, SDL will create an internal shadow surface.
362  * This shadow surface will have SDL_HWPALLETE set, so
363  * we won't create a second shadow surface in this case.
364  */
365  DEBUG(driver, 1, "SDL: using shadow surface");
366  newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
367  if (newscreen == nullptr) {
368  DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
369  return false;
370  }
371  }
372 
373  /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
374  _num_dirty_rects = 0;
375 
376  _screen.width = newscreen->w;
377  _screen.height = newscreen->h;
378  _screen.pitch = newscreen->pitch / (bpp / 8);
379  _screen.dst_ptr = newscreen->pixels;
380  _sdl_screen = newscreen;
381 
382  /* When in full screen, we will always have the mouse cursor
383  * within the window, even though SDL does not give us the
384  * appropriate event to know this. */
385  if (_fullscreen) _cursor.in_window = true;
386 
388  blitter->PostResize();
389 
390  InitPalette();
391 
392  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
393  SDL_WM_SetCaption(caption, caption);
394 
395  GameSizeChanged();
396 
397  return true;
398 }
399 
400 bool VideoDriver_SDL::ClaimMousePointer()
401 {
402  SDL_ShowCursor(0);
403  return true;
404 }
405 
406 struct VkMapping {
407 #if SDL_VERSION_ATLEAST(1, 3, 0)
408  SDL_Keycode vk_from;
409 #else
410  uint16 vk_from;
411 #endif
412  byte vk_count;
413  byte map_to;
414 };
415 
416 #define AS(x, z) {x, 0, z}
417 #define AM(x, y, z, w) {x, (byte)(y - x), z}
418 
419 static const VkMapping _vk_mapping[] = {
420  /* Pageup stuff + up/down */
421  AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
422  AS(SDLK_UP, WKC_UP),
423  AS(SDLK_DOWN, WKC_DOWN),
424  AS(SDLK_LEFT, WKC_LEFT),
425  AS(SDLK_RIGHT, WKC_RIGHT),
426 
427  AS(SDLK_HOME, WKC_HOME),
428  AS(SDLK_END, WKC_END),
429 
430  AS(SDLK_INSERT, WKC_INSERT),
431  AS(SDLK_DELETE, WKC_DELETE),
432 
433  /* Map letters & digits */
434  AM(SDLK_a, SDLK_z, 'A', 'Z'),
435  AM(SDLK_0, SDLK_9, '0', '9'),
436 
437  AS(SDLK_ESCAPE, WKC_ESC),
438  AS(SDLK_PAUSE, WKC_PAUSE),
439  AS(SDLK_BACKSPACE, WKC_BACKSPACE),
440 
441  AS(SDLK_SPACE, WKC_SPACE),
442  AS(SDLK_RETURN, WKC_RETURN),
443  AS(SDLK_TAB, WKC_TAB),
444 
445  /* Function keys */
446  AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
447 
448  /* Numeric part. */
449  AM(SDLK_KP0, SDLK_KP9, '0', '9'),
450  AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
451  AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
452  AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
453  AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
454  AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
455  AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
456 
457  /* Other non-letter keys */
458  AS(SDLK_SLASH, WKC_SLASH),
459  AS(SDLK_SEMICOLON, WKC_SEMICOLON),
460  AS(SDLK_EQUALS, WKC_EQUALS),
461  AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
462  AS(SDLK_BACKSLASH, WKC_BACKSLASH),
463  AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
464 
465  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
466  AS(SDLK_COMMA, WKC_COMMA),
467  AS(SDLK_MINUS, WKC_MINUS),
468  AS(SDLK_PERIOD, WKC_PERIOD)
469 };
470 
471 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
472 {
473  const VkMapping *map;
474  uint key = 0;
475 
476  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
477  if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
478  key = sym->sym - map->vk_from + map->map_to;
479  break;
480  }
481  }
482 
483  /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
484 #if defined(_WIN32) || defined(__OS2__)
485  if (sym->scancode == 41) key = WKC_BACKQUOTE;
486 #elif defined(__APPLE__)
487  if (sym->scancode == 10) key = WKC_BACKQUOTE;
488 #elif defined(__SVR4) && defined(__sun)
489  if (sym->scancode == 60) key = WKC_BACKQUOTE;
490  if (sym->scancode == 49) key = WKC_BACKSPACE;
491 #elif defined(__sgi__)
492  if (sym->scancode == 22) key = WKC_BACKQUOTE;
493 #else
494  if (sym->scancode == 49) key = WKC_BACKQUOTE;
495 #endif
496 
497  /* META are the command keys on mac */
498  if (sym->mod & KMOD_META) key |= WKC_META;
499  if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
500  if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
501  if (sym->mod & KMOD_ALT) key |= WKC_ALT;
502 
503  *character = sym->unicode;
504  return key;
505 }
506 
507 int VideoDriver_SDL::PollEvent()
508 {
509  SDL_Event ev;
510 
511  if (!SDL_PollEvent(&ev)) return -2;
512 
513  switch (ev.type) {
514  case SDL_MOUSEMOTION:
515  if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
516  SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
517  }
519  break;
520 
521  case SDL_MOUSEBUTTONDOWN:
522  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
523  ev.button.button = SDL_BUTTON_RIGHT;
524  }
525 
526  switch (ev.button.button) {
527  case SDL_BUTTON_LEFT:
528  _left_button_down = true;
529  break;
530 
531  case SDL_BUTTON_RIGHT:
532  _right_button_down = true;
533  _right_button_clicked = true;
534  break;
535 
536  case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
537  case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
538 
539  default: break;
540  }
542  break;
543 
544  case SDL_MOUSEBUTTONUP:
545  if (_rightclick_emulate) {
546  _right_button_down = false;
547  _left_button_down = false;
548  _left_button_clicked = false;
549  } else if (ev.button.button == SDL_BUTTON_LEFT) {
550  _left_button_down = false;
551  _left_button_clicked = false;
552  } else if (ev.button.button == SDL_BUTTON_RIGHT) {
553  _right_button_down = false;
554  }
556  break;
557 
558  case SDL_ACTIVEEVENT:
559  if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
560 
561  if (ev.active.gain) { // mouse entered the window, enable cursor
562  _cursor.in_window = true;
563  } else {
564  UndrawMouseCursor(); // mouse left the window, undraw cursor
565  _cursor.in_window = false;
566  }
567  break;
568 
569  case SDL_QUIT:
570  HandleExitGameRequest();
571  break;
572 
573  case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
574  if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
575  (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
576  ToggleFullScreen(!_fullscreen);
577  } else {
578  WChar character;
579  uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
580  HandleKeypress(keycode, character);
581  }
582  break;
583 
584  case SDL_VIDEORESIZE: {
585  int w = max(ev.resize.w, 64);
586  int h = max(ev.resize.h, 64);
587  CreateMainSurface(w, h);
588  break;
589  }
590  case SDL_VIDEOEXPOSE: {
591  /* Force a redraw of the entire screen. Note
592  * that SDL 1.2 seems to do this automatically
593  * in most cases, but 1.3 / 2.0 does not. */
594  _num_dirty_rects = MAX_DIRTY_RECTS + 1;
595  break;
596  }
597  }
598  return -1;
599 }
600 
601 const char *VideoDriver_SDL::Start(const char * const *parm)
602 {
603  char buf[30];
604  _use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
605 
606  /* Just on the offchance the audio subsystem started before the video system,
607  * check whether any part of SDL has been initialised before getting here.
608  * Slightly duplicated with sound/sdl_s.cpp */
609  int ret_code = 0;
610  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
611  ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
612  } else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
613  ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
614  }
615  if (ret_code == -1) return SDL_GetError();
616 
617  GetVideoModes();
618  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
619  return SDL_GetError();
620  }
621 
622  SDL_VideoDriverName(buf, sizeof buf);
623  DEBUG(driver, 1, "SDL: using driver '%s'", buf);
624 
626  SetupKeyboard();
627 
628  _draw_threaded = GetDriverParam(parm, "no_threads") == nullptr && GetDriverParam(parm, "no_thread") == nullptr;
629 
630  return nullptr;
631 }
632 
633 void VideoDriver_SDL::SetupKeyboard()
634 {
635  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
636  SDL_EnableUNICODE(1);
637 }
638 
640 {
641  SDL_QuitSubSystem(SDL_INIT_VIDEO);
642  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
643  SDL_Quit(); // If there's nothing left, quit SDL
644  }
645 }
646 
648 {
649  uint32 cur_ticks = SDL_GetTicks();
650  uint32 last_cur_ticks = cur_ticks;
651  uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
652  uint32 mod;
653  int numkeys;
654  Uint8 *keys;
655 
656  CheckPaletteAnim();
657 
658  std::thread draw_thread;
659  std::unique_lock<std::recursive_mutex> draw_lock;
660  if (_draw_threaded) {
661  /* Initialise the mutex first, because that's the thing we *need*
662  * directly in the newly created thread. */
663  _draw_mutex = new std::recursive_mutex();
664  if (_draw_mutex == nullptr) {
665  _draw_threaded = false;
666  } else {
667  draw_lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
668  _draw_signal = new std::condition_variable_any();
669  _draw_continue = true;
670 
671  _draw_threaded = StartNewThread(&draw_thread, "ottd:draw-sdl", &DrawSurfaceToScreenThread);
672 
673  /* Free the mutex if we won't be able to use it. */
674  if (!_draw_threaded) {
675  draw_lock.unlock();
676  draw_lock.release();
677  delete _draw_mutex;
678  delete _draw_signal;
679  _draw_mutex = nullptr;
680  _draw_signal = nullptr;
681  } else {
682  /* Wait till the draw mutex has started itself. */
683  _draw_signal->wait(*_draw_mutex);
684  }
685  }
686  }
687 
688  DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
689 
690  for (;;) {
691  uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
692  InteractiveRandom(); // randomness
693 
694  while (PollEvent() == -1) {}
695  if (_exit_game) break;
696 
697  mod = SDL_GetModState();
698 #if SDL_VERSION_ATLEAST(1, 3, 0)
699  keys = SDL_GetKeyboardState(&numkeys);
700 #else
701  keys = SDL_GetKeyState(&numkeys);
702 #endif
703 #if defined(_DEBUG)
704  if (_shift_pressed)
705 #else
706  /* Speedup when pressing tab, except when using ALT+TAB
707  * to switch to another application */
708 #if SDL_VERSION_ATLEAST(1, 3, 0)
709  if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
710 #else
711  if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
712 #endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
713 #endif /* defined(_DEBUG) */
714  {
715  if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
716  } else if (_fast_forward & 2) {
717  _fast_forward = 0;
718  }
719 
720  cur_ticks = SDL_GetTicks();
721  if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
722  _realtime_tick += cur_ticks - last_cur_ticks;
723  last_cur_ticks = cur_ticks;
724  next_tick = cur_ticks + MILLISECONDS_PER_TICK;
725 
726  bool old_ctrl_pressed = _ctrl_pressed;
727 
728  _ctrl_pressed = !!(mod & KMOD_CTRL);
729  _shift_pressed = !!(mod & KMOD_SHIFT);
730 
731  /* determine which directional keys are down */
732  _dirkeys =
733 #if SDL_VERSION_ATLEAST(1, 3, 0)
734  (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
735  (keys[SDL_SCANCODE_UP] ? 2 : 0) |
736  (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
737  (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
738 #else
739  (keys[SDLK_LEFT] ? 1 : 0) |
740  (keys[SDLK_UP] ? 2 : 0) |
741  (keys[SDLK_RIGHT] ? 4 : 0) |
742  (keys[SDLK_DOWN] ? 8 : 0);
743 #endif
744  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
745 
746  /* The gameloop is the part that can run asynchronously. The rest
747  * except sleeping can't. */
748  if (_draw_mutex != nullptr) draw_lock.unlock();
749 
750  GameLoop();
751 
752  if (_draw_mutex != nullptr) draw_lock.lock();
753 
754  UpdateWindows();
755  _local_palette = _cur_palette;
756  } else {
757  /* Release the thread while sleeping */
758  if (_draw_mutex != nullptr) draw_lock.unlock();
759  CSleep(1);
760  if (_draw_mutex != nullptr) draw_lock.lock();
761 
763  DrawMouseCursor();
764  }
765 
766  /* End of the critical part. */
767  if (_draw_mutex != nullptr && !HasModalProgress()) {
768  _draw_signal->notify_one();
769  } else {
770  /* Oh, we didn't have threads, then just draw unthreaded */
771  CheckPaletteAnim();
772  DrawSurfaceToScreen();
773  }
774  }
775 
776  if (_draw_mutex != nullptr) {
777  _draw_continue = false;
778  /* Sending signal if there is no thread blocked
779  * is very valid and results in noop */
780  _draw_signal->notify_one();
781  if (draw_lock.owns_lock()) draw_lock.unlock();
782  draw_lock.release();
783  draw_thread.join();
784 
785  delete _draw_mutex;
786  delete _draw_signal;
787 
788  _draw_mutex = nullptr;
789  _draw_signal = nullptr;
790  }
791 }
792 
794 {
795  std::unique_lock<std::recursive_mutex> lock;
796  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
797 
798  return CreateMainSurface(w, h);
799 }
800 
802 {
803  std::unique_lock<std::recursive_mutex> lock;
804  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
805 
806  _fullscreen = fullscreen;
807  GetVideoModes(); // get the list of available video modes
808  bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
809 
810  if (!ret) {
811  /* switching resolution failed, put back full_screen to original status */
812  _fullscreen ^= true;
813  }
814 
815  return ret;
816 }
817 
819 {
820  return CreateMainSurface(_screen.width, _screen.height);
821 }
822 
824 {
825  if (_draw_mutex != nullptr) _draw_mutex->lock();
826 }
827 
829 {
830  if (_draw_mutex != nullptr) _draw_mutex->unlock();
831 }
832 
833 #endif /* WITH_SDL */
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
Base of the SDL video driver.
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
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
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 _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
#define lengthof(x)
Return the length of an fixed size array.
Definition: depend.cpp:42
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
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
int GetDriverParamInt(const char *const *parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:75
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.
Full 8bpp support by OS and hardware.
Definition: gfx_type.h:319
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