OpenTTD Source  1.11.0-beta2
sdl_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_SDL
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 "../thread.h"
18 #include "../progress.h"
19 #include "../core/random_func.hpp"
20 #include "../core/math_func.hpp"
21 #include "../fileio_func.h"
22 #include "../framerate_type.h"
23 #include "../window_func.h"
24 #include "sdl_v.h"
25 #include <SDL.h>
26 #include <mutex>
27 #include <condition_variable>
28 
29 #include "../safeguards.h"
30 
31 static FVideoDriver_SDL iFVideoDriver_SDL;
32 
33 static SDL_Surface *_sdl_surface;
34 static SDL_Surface *_sdl_realscreen;
35 static bool _all_modes;
36 
38 static bool _draw_threaded;
40 static std::recursive_mutex *_draw_mutex = nullptr;
42 static std::condition_variable_any *_draw_signal = nullptr;
44 static volatile bool _draw_continue;
45 static Palette _local_palette;
46 
47 #define MAX_DIRTY_RECTS 100
48 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
49 static int _num_dirty_rects;
50 static int _use_hwpalette;
51 static int _requested_hwpalette; /* Did we request a HWPALETTE for the current video mode? */
52 
53 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
54 {
55  if (_num_dirty_rects < MAX_DIRTY_RECTS) {
56  _dirty_rects[_num_dirty_rects].x = left;
57  _dirty_rects[_num_dirty_rects].y = top;
58  _dirty_rects[_num_dirty_rects].w = width;
59  _dirty_rects[_num_dirty_rects].h = height;
60  }
61  _num_dirty_rects++;
62 }
63 
64 static void UpdatePalette(bool init = false)
65 {
66  SDL_Color pal[256];
67 
68  for (int i = 0; i != _local_palette.count_dirty; i++) {
72  pal[i].unused = 0;
73  }
74 
75  SDL_SetColors(_sdl_surface, pal, _local_palette.first_dirty, _local_palette.count_dirty);
76 
77  if (_sdl_surface != _sdl_realscreen && init) {
78  /* When using a shadow surface, also set our palette on the real screen. This lets SDL
79  * allocate as many colors (or approximations) as
80  * possible, instead of using only the default SDL
81  * palette. This allows us to get more colors exactly
82  * right and might allow using better approximations for
83  * other colors.
84  *
85  * Note that colors allocations are tried in-order, so
86  * this favors colors further up into the palette. Also
87  * note that if two colors from the same animation
88  * sequence are approximated using the same color, that
89  * animation will stop working.
90  *
91  * Since changing the system palette causes the colours
92  * to change right away, and allocations might
93  * drastically change, we can't use this for animation,
94  * since that could cause weird coloring between the
95  * palette change and the blitting below, so we only set
96  * the real palette during initialisation.
97  */
98  SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
99  }
100 
101  if (_sdl_surface != _sdl_realscreen && !init) {
102  /* We're not using real hardware palette, but are letting SDL
103  * approximate the palette during shadow -> screen copy. To
104  * change the palette, we need to recopy the entire screen.
105  *
106  * Note that this operation can slow down the rendering
107  * considerably, especially since changing the shadow
108  * palette will need the next blit to re-detect the
109  * best mapping of shadow palette colors to real palette
110  * colors from scratch.
111  */
112  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
113  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
114  }
115 }
116 
117 static void InitPalette()
118 {
122  UpdatePalette(true);
123 }
124 
126 {
128 
129  if (_cur_palette.count_dirty != 0) {
131 
132  switch (blitter->UsePaletteAnimation()) {
134  UpdatePalette();
135  break;
136 
138  blitter->PaletteAnimate(_local_palette);
139  break;
140 
142  break;
143 
144  default:
145  NOT_REACHED();
146  }
148  }
149 }
150 
152 {
153  PerformanceMeasurer framerate(PFE_VIDEO);
154 
155  int n = _num_dirty_rects;
156  if (n == 0) return;
157 
158  _num_dirty_rects = 0;
159 
160  if (n > MAX_DIRTY_RECTS) {
161  if (_sdl_surface != _sdl_realscreen) {
162  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
163  }
164 
165  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
166  } else {
167  if (_sdl_surface != _sdl_realscreen) {
168  for (int i = 0; i < n; i++) {
169  SDL_BlitSurface(_sdl_surface, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
170  }
171  }
172 
173  SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
174  }
175 }
176 
178 {
179  /* First tell the main thread we're started */
180  std::unique_lock<std::recursive_mutex> lock(*_draw_mutex);
181  _draw_signal->notify_one();
182 
183  /* Now wait for the first thing to draw! */
184  _draw_signal->wait(*_draw_mutex);
185 
186  while (_draw_continue) {
187  /* Then just draw and wait till we stop */
188  this->Paint();
189  _draw_signal->wait(lock);
190  }
191 }
192 
193 /* static */ void VideoDriver_SDL::PaintThreadThunk(VideoDriver_SDL *drv)
194 {
195  drv->PaintThread();
196 }
197 
198 static const Dimension _default_resolutions[] = {
199  { 640, 480},
200  { 800, 600},
201  {1024, 768},
202  {1152, 864},
203  {1280, 800},
204  {1280, 960},
205  {1280, 1024},
206  {1400, 1050},
207  {1600, 1200},
208  {1680, 1050},
209  {1920, 1200}
210 };
211 
212 static void GetVideoModes()
213 {
214  SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
215  if (modes == nullptr) usererror("sdl: no modes available");
216 
217  _resolutions.clear();
218 
219  _all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
220  if (modes == (void*)-1) {
221  for (uint i = 0; i < lengthof(_default_resolutions); i++) {
222  if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
223  _resolutions.push_back(_default_resolutions[i]);
224  }
225  }
226  } else {
227  for (int i = 0; modes[i]; i++) {
228  uint w = modes[i]->w;
229  uint h = modes[i]->h;
230  if (w < 640 || h < 480) continue; // reject too small resolutions
231  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
232  _resolutions.emplace_back(w, h);
233  }
234  if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
235  SortResolutions();
236  }
237 }
238 
239 static void GetAvailableVideoMode(uint *w, uint *h)
240 {
241  /* All modes available? */
242  if (_all_modes || _resolutions.empty()) return;
243 
244  /* Is the wanted mode among the available modes? */
245  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
246 
247  /* Use the closest possible resolution */
248  uint best = 0;
249  uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
250  for (uint i = 1; i != _resolutions.size(); ++i) {
251  uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
252  if (newdelta < delta) {
253  best = i;
254  delta = newdelta;
255  }
256  }
257  *w = _resolutions[best].width;
258  *h = _resolutions[best].height;
259 }
260 
261 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
262 {
263  SDL_Surface *newscreen, *icon;
264  char caption[50];
266  bool want_hwpalette;
267 
268  GetAvailableVideoMode(&w, &h);
269 
270  DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
271 
272  if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
273 
274  std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
275  if (!icon_path.empty()) {
276  /* Give the application an icon */
277  icon = SDL_LoadBMP(icon_path.c_str());
278  if (icon != nullptr) {
279  /* Get the colourkey, which will be magenta */
280  uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
281 
282  SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
283  SDL_WM_SetIcon(icon, nullptr);
284  SDL_FreeSurface(icon);
285  }
286  }
287 
288  if (_use_hwpalette == 2) {
289  /* Default is to autodetect when to use SDL_HWPALETTE.
290  * In this case, SDL_HWPALETTE is only used for 8bpp
291  * blitters in fullscreen.
292  *
293  * When using an 8bpp blitter on a 8bpp system in
294  * windowed mode with SDL_HWPALETTE, OpenTTD will claim
295  * the system palette, making all other applications
296  * get the wrong colours. In this case, we're better of
297  * trying to approximate the colors we need using system
298  * colors, using a shadow surface (see below).
299  *
300  * On a 32bpp system, SDL_HWPALETTE is ignored, so it
301  * doesn't matter what we do.
302  *
303  * When using a 32bpp blitter on a 8bpp system, setting
304  * SDL_HWPALETTE messes up rendering (at least on X11),
305  * so we don't do that. In this case, SDL takes care of
306  * color approximation using its own shadow surface
307  * (which we can't force in 8bpp on 8bpp mode,
308  * unfortunately).
309  */
310  want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
311  } else {
312  /* User specified a value manually */
313  want_hwpalette = _use_hwpalette;
314  }
315 
316  if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palette");
317 
318  /* Free any previously allocated shadow surface */
319  if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);
320 
321  if (_sdl_realscreen != nullptr) {
322  if (_requested_hwpalette != want_hwpalette) {
323  /* SDL (at least the X11 driver), reuses the
324  * same window and palette settings when the bpp
325  * (and a few flags) are the same. Since we need
326  * to hwpalette value to change (in particular
327  * when switching between fullscreen and
328  * windowed), we restart the entire video
329  * subsystem to force creating a new window.
330  */
331  DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
332  SDL_QuitSubSystem(SDL_INIT_VIDEO);
333  SDL_InitSubSystem(SDL_INIT_VIDEO);
334  ClaimMousePointer();
335  SetupKeyboard();
336  }
337  }
338  /* Remember if we wanted a hwpalette. We can't reliably query
339  * SDL for the SDL_HWPALETTE flag, since it might get set even
340  * though we didn't ask for it (when SDL creates a shadow
341  * surface, for example). */
342  _requested_hwpalette = want_hwpalette;
343 
344  /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
345  newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
346  if (newscreen == nullptr) {
347  DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
348  return false;
349  }
350  _sdl_realscreen = newscreen;
351 
352  if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
353  /* Using an 8bpp blitter, if we didn't get a hardware
354  * palette (most likely because we didn't request one,
355  * see above), we'll have to set up a shadow surface to
356  * render on.
357  *
358  * Our palette will be applied to this shadow surface,
359  * while the real screen surface will use the shared
360  * system palette (which will partly contain our colors,
361  * but most likely will not have enough free color cells
362  * for all of our colors). SDL can use these two
363  * palettes at blit time to approximate colors used in
364  * the shadow surface using system colors automatically.
365  *
366  * Note that when using an 8bpp blitter on a 32bpp
367  * system, SDL will create an internal shadow surface.
368  * This shadow surface will have SDL_HWPALLETE set, so
369  * we won't create a second shadow surface in this case.
370  */
371  DEBUG(driver, 1, "SDL: using shadow surface");
372  newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
373  if (newscreen == nullptr) {
374  DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
375  return false;
376  }
377  }
378 
379  /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
380  _num_dirty_rects = 0;
381 
382  _screen.width = newscreen->w;
383  _screen.height = newscreen->h;
384  _screen.pitch = newscreen->pitch / (bpp / 8);
385  _screen.dst_ptr = newscreen->pixels;
386  _sdl_surface = newscreen;
387 
388  /* When in full screen, we will always have the mouse cursor
389  * within the window, even though SDL does not give us the
390  * appropriate event to know this. */
391  if (_fullscreen) _cursor.in_window = true;
392 
394  blitter->PostResize();
395 
396  InitPalette();
397 
398  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
399  SDL_WM_SetCaption(caption, caption);
400 
401  GameSizeChanged();
402 
403  return true;
404 }
405 
406 bool VideoDriver_SDL::ClaimMousePointer()
407 {
408  SDL_ShowCursor(0);
409  return true;
410 }
411 
412 struct SDLVkMapping {
413  uint16 vk_from;
414  byte vk_count;
415  byte map_to;
416 };
417 
418 #define AS(x, z) {x, 0, z}
419 #define AM(x, y, z, w) {x, (byte)(y - x), z}
420 
421 static const SDLVkMapping _vk_mapping[] = {
422  /* Pageup stuff + up/down */
423  AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
424  AS(SDLK_UP, WKC_UP),
425  AS(SDLK_DOWN, WKC_DOWN),
426  AS(SDLK_LEFT, WKC_LEFT),
427  AS(SDLK_RIGHT, WKC_RIGHT),
428 
429  AS(SDLK_HOME, WKC_HOME),
430  AS(SDLK_END, WKC_END),
431 
432  AS(SDLK_INSERT, WKC_INSERT),
433  AS(SDLK_DELETE, WKC_DELETE),
434 
435  /* Map letters & digits */
436  AM(SDLK_a, SDLK_z, 'A', 'Z'),
437  AM(SDLK_0, SDLK_9, '0', '9'),
438 
439  AS(SDLK_ESCAPE, WKC_ESC),
440  AS(SDLK_PAUSE, WKC_PAUSE),
441  AS(SDLK_BACKSPACE, WKC_BACKSPACE),
442 
443  AS(SDLK_SPACE, WKC_SPACE),
444  AS(SDLK_RETURN, WKC_RETURN),
445  AS(SDLK_TAB, WKC_TAB),
446 
447  /* Function keys */
448  AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
449 
450  /* Numeric part. */
451  AM(SDLK_KP0, SDLK_KP9, '0', '9'),
452  AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
453  AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
454  AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
455  AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
456  AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
457  AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
458 
459  /* Other non-letter keys */
460  AS(SDLK_SLASH, WKC_SLASH),
461  AS(SDLK_SEMICOLON, WKC_SEMICOLON),
462  AS(SDLK_EQUALS, WKC_EQUALS),
463  AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
464  AS(SDLK_BACKSLASH, WKC_BACKSLASH),
465  AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
466 
467  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
468  AS(SDLK_COMMA, WKC_COMMA),
469  AS(SDLK_MINUS, WKC_MINUS),
470  AS(SDLK_PERIOD, WKC_PERIOD)
471 };
472 
473 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
474 {
475  const SDLVkMapping *map;
476  uint key = 0;
477 
478  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
479  if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
480  key = sym->sym - map->vk_from + map->map_to;
481  break;
482  }
483  }
484 
485  /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
486 #if defined(_WIN32) || defined(__OS2__)
487  if (sym->scancode == 41) key = WKC_BACKQUOTE;
488 #elif defined(__APPLE__)
489  if (sym->scancode == 10) key = WKC_BACKQUOTE;
490 #elif defined(__SVR4) && defined(__sun)
491  if (sym->scancode == 60) key = WKC_BACKQUOTE;
492  if (sym->scancode == 49) key = WKC_BACKSPACE;
493 #elif defined(__sgi__)
494  if (sym->scancode == 22) key = WKC_BACKQUOTE;
495 #else
496  if (sym->scancode == 49) key = WKC_BACKQUOTE;
497 #endif
498 
499  /* META are the command keys on mac */
500  if (sym->mod & KMOD_META) key |= WKC_META;
501  if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
502  if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
503  if (sym->mod & KMOD_ALT) key |= WKC_ALT;
504 
505  *character = sym->unicode;
506  return key;
507 }
508 
510 {
511  SDL_Event ev;
512 
513  if (!SDL_PollEvent(&ev)) return false;
514 
515  switch (ev.type) {
516  case SDL_MOUSEMOTION:
517  if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
518  SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
519  }
521  break;
522 
523  case SDL_MOUSEBUTTONDOWN:
524  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
525  ev.button.button = SDL_BUTTON_RIGHT;
526  }
527 
528  switch (ev.button.button) {
529  case SDL_BUTTON_LEFT:
530  _left_button_down = true;
531  break;
532 
533  case SDL_BUTTON_RIGHT:
534  _right_button_down = true;
535  _right_button_clicked = true;
536  break;
537 
538  case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
539  case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; 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_ACTIVEEVENT:
561  if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
562 
563  if (ev.active.gain) { // mouse entered the window, enable cursor
564  _cursor.in_window = true;
565  } else {
566  UndrawMouseCursor(); // mouse left the window, undraw cursor
567  _cursor.in_window = false;
568  }
569  break;
570 
571  case SDL_QUIT:
572  HandleExitGameRequest();
573  break;
574 
575  case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
576  if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
577  (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
578  ToggleFullScreen(!_fullscreen);
579  } else {
580  WChar character;
581  uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
582  HandleKeypress(keycode, character);
583  }
584  break;
585 
586  case SDL_VIDEORESIZE: {
587  int w = std::max(ev.resize.w, 64);
588  int h = std::max(ev.resize.h, 64);
589  CreateMainSurface(w, h);
590  break;
591  }
592  case SDL_VIDEOEXPOSE: {
593  /* Force a redraw of the entire screen. Note
594  * that SDL 1.2 seems to do this automatically
595  * in most cases, but 1.3 / 2.0 does not. */
596  _num_dirty_rects = MAX_DIRTY_RECTS + 1;
597  break;
598  }
599  }
600 
601  return true;
602 }
603 
604 const char *VideoDriver_SDL::Start(const StringList &parm)
605 {
606  char buf[30];
607  _use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
608 
609  /* Just on the offchance the audio subsystem started before the video system,
610  * check whether any part of SDL has been initialised before getting here.
611  * Slightly duplicated with sound/sdl_s.cpp */
612  int ret_code = 0;
613  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
614  ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
615  } else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
616  ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
617  }
618  if (ret_code < 0) return SDL_GetError();
619 
620  this->UpdateAutoResolution();
621 
622  GetVideoModes();
623  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
624  return SDL_GetError();
625  }
626 
627  SDL_VideoDriverName(buf, sizeof buf);
628  DEBUG(driver, 1, "SDL: using driver '%s'", buf);
629 
631  SetupKeyboard();
632 
633  _draw_threaded = !GetDriverParamBool(parm, "no_threads") && !GetDriverParamBool(parm, "no_thread");
634 
635  return nullptr;
636 }
637 
638 void VideoDriver_SDL::SetupKeyboard()
639 {
640  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
641  SDL_EnableUNICODE(1);
642 }
643 
645 {
646  SDL_QuitSubSystem(SDL_INIT_VIDEO);
647  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
648  SDL_Quit(); // If there's nothing left, quit SDL
649  }
650 }
651 
653 {
654  uint32 mod = SDL_GetModState();
655  int numkeys;
656  Uint8 *keys = SDL_GetKeyState(&numkeys);
657 
658  bool old_ctrl_pressed = _ctrl_pressed;
659 
660  _ctrl_pressed = !!(mod & KMOD_CTRL);
661  _shift_pressed = !!(mod & KMOD_SHIFT);
662 
663 #if defined(_DEBUG)
665 #else
666  /* Speedup when pressing tab, except when using ALT+TAB
667  * to switch to another application. */
668  this->fast_forward_key_pressed = keys[SDLK_TAB] && (mod & KMOD_ALT) == 0;
669 #endif /* defined(_DEBUG) */
670 
671  /* Determine which directional keys are down. */
672  _dirkeys =
673  (keys[SDLK_LEFT] ? 1 : 0) |
674  (keys[SDLK_UP] ? 2 : 0) |
675  (keys[SDLK_RIGHT] ? 4 : 0) |
676  (keys[SDLK_DOWN] ? 8 : 0);
677 
678  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
679 }
680 
682 {
683  std::thread draw_thread;
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  this->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", &VideoDriver_SDL::PaintThreadThunk, this);
696 
697  /* Free the mutex if we won't be able to use it. */
698  if (!_draw_threaded) {
699  this->draw_lock.unlock();
700  this->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, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
713 
714  for (;;) {
715  if (_exit_game) break;
716 
717  if (this->Tick()) {
718  if (_draw_mutex != nullptr && !HasModalProgress()) {
719  _draw_signal->notify_one();
720  } else {
721  this->Paint();
722  }
723  }
724  this->SleepTillNextTick();
725  }
726 
727  if (_draw_mutex != nullptr) {
728  _draw_continue = false;
729  /* Sending signal if there is no thread blocked
730  * is very valid and results in noop */
731  _draw_signal->notify_one();
732  if (this->draw_lock.owns_lock()) this->draw_lock.unlock();
733  this->draw_lock.release();
734  draw_thread.join();
735 
736  delete _draw_mutex;
737  delete _draw_signal;
738 
739  _draw_mutex = nullptr;
740  _draw_signal = nullptr;
741  }
742 }
743 
745 {
746  std::unique_lock<std::recursive_mutex> lock;
747  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
748 
749  return CreateMainSurface(w, h);
750 }
751 
753 {
754  std::unique_lock<std::recursive_mutex> lock;
755  if (_draw_mutex != nullptr) lock = std::unique_lock<std::recursive_mutex>(*_draw_mutex);
756 
757  _fullscreen = fullscreen;
758  GetVideoModes(); // get the list of available video modes
759  bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
760 
761  if (!ret) {
762  /* switching resolution failed, put back full_screen to original status */
763  _fullscreen ^= true;
764  }
765 
766  return ret;
767 }
768 
770 {
771  return CreateMainSurface(_screen.width, _screen.height);
772 }
773 
775 {
776  if (_draw_mutex != nullptr) _draw_mutex->lock();
777 }
778 
780 {
781  if (_draw_mutex != nullptr) _draw_mutex->unlock();
782 }
783 
785 {
786  if (_draw_threaded) this->draw_lock.lock();
787  return true;
788 }
789 
791 {
792  if (_draw_threaded) this->draw_lock.unlock();
793 }
794 
795 #endif /* WITH_SDL */
_draw_threaded
static bool _draw_threaded
Whether the drawing is/may be done in a separate thread.
Definition: sdl_v.cpp:38
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
WKC_SINGLEQUOTE
@ WKC_SINGLEQUOTE
' Single quote
Definition: gfx_type.h:101
VideoDriver_SDL::UnlockVideoBuffer
void UnlockVideoBuffer() override
Unlock a previously locked video buffer.
Definition: sdl_v.cpp:790
VideoDriver_SDL::LockVideoBuffer
bool LockVideoBuffer() override
Make sure the video buffer is ready for drawing.
Definition: sdl_v.cpp:784
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
VideoDriver::Tick
bool Tick()
Run the game for a single tick, processing boththe game-tick and draw-tick.
Definition: video_driver.cpp:20
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
VideoDriver_SDL
The SDL video driver.
Definition: sdl_v.h:16
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:100
VideoDriver_SDL::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl_v.cpp:604
PFE_VIDEO
@ PFE_VIDEO
Speed of painting drawn video buffer.
Definition: framerate_type.h:59
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:38
Blitter::UsePaletteAnimation
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
HandleKeypress
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2681
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
Blitter
How all blitters should look like.
Definition: base.hpp:28
BASESET_DIR
@ BASESET_DIR
Subdirectory for all base data (base sets, intro game)
Definition: fileio_type.h:116
Blitter::GetScreenDepth
virtual uint8 GetScreenDepth()=0
Get the screen depth this blitter works for.
_local_palette
static Palette _local_palette
Local copy of the palette for use in the drawing thread.
Definition: win32_v.cpp:45
WKC_SLASH
@ WKC_SLASH
/ Forward slash
Definition: gfx_type.h:95
WKC_BACKSLASH
@ WKC_BACKSLASH
\ Backslash
Definition: gfx_type.h:99
PerformanceMeasurer
RAII class for measuring simple elements of performance.
Definition: framerate_type.h:92
WKC_L_BRACKET
@ WKC_L_BRACKET
[ Left square bracket
Definition: gfx_type.h:98
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
_draw_mutex
static std::recursive_mutex * _draw_mutex
Mutex to keep the access to the shared memory controlled.
Definition: sdl_v.cpp:40
VideoDriver_SDL::ReleaseBlitterLock
void ReleaseBlitterLock() override
Release any lock(s) required to be held when changing blitters.
Definition: sdl_v.cpp:779
AS
#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, maint_cost, ttdpatch_type, class_id, name, preview)
AirportSpec definition for airports with at least one depot.
Definition: airport_defaults.h:391
VideoDriver_SDL::ToggleFullscreen
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
Definition: sdl_v.cpp:752
VideoDriver_SDL::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: sdl_v.cpp:681
VideoDriver_SDL::AcquireBlitterLock
void AcquireBlitterLock() override
Acquire any lock(s) required to be held when changing blitters.
Definition: sdl_v.cpp:774
VideoDriver_SDL::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl_v.cpp:53
WKC_EQUALS
@ WKC_EQUALS
= Equals
Definition: gfx_type.h:97
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2989
CursorVars::UpdateCursorPosition
bool UpdateCursorPosition(int x, int y, bool queued_warp)
Update cursor position on mouse movement.
Definition: gfx.cpp:1828
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
Palette::palette
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition: gfx_type.h:314
Blitter::PostResize
virtual void PostResize()
Post resize event.
Definition: base.hpp:209
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:48
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:140
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:22
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
CursorVars::wheel
int wheel
mouse wheel movement
Definition: gfx_type.h:119
VideoDriver_SDL::ChangeResolution
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
Definition: sdl_v.cpp:744
VideoDriver_SDL::PollEvent
bool PollEvent() override
Process a single system event.
Definition: sdl_v.cpp:509
Palette::count_dirty
int count_dirty
The number of dirty elements.
Definition: gfx_type.h:316
GetDriverParamInt
int GetDriverParamInt(const StringList &parm, const char *name, int def)
Get an integer parameter the list of parameters.
Definition: driver.cpp:71
FVideoDriver_SDL
Factory for the SDL video driver.
Definition: sdl_v.h:59
WKC_R_BRACKET
@ WKC_R_BRACKET
] Right square bracket
Definition: gfx_type.h:100
S8BPP_HARDWARE
@ S8BPP_HARDWARE
Full 8bpp support by OS and hardware.
Definition: gfx_type.h:323
_rightclick_emulate
bool _rightclick_emulate
Whether right clicking is emulated.
Definition: driver.cpp:24
WKC_PERIOD
@ WKC_PERIOD
. Period
Definition: gfx_type.h:103
VideoDriver_SDL::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl_v.cpp:769
VideoDriver::UpdateAutoResolution
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
Definition: video_driver.hpp:208
VideoDriver_SDL::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: sdl_v.cpp:125
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:375
Blitter::PALETTE_ANIMATION_VIDEO_BACKEND
@ PALETTE_ANIMATION_VIDEO_BACKEND
Palette animation should be done by video backend (8bpp only!)
Definition: base.hpp:51
VideoDriver_SDL::InputLoop
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: sdl_v.cpp:652
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:442
WKC_COMMA
@ WKC_COMMA
, Comma
Definition: gfx_type.h:102
Blitter::PALETTE_ANIMATION_NONE
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition: base.hpp:50
WKC_SEMICOLON
@ WKC_SEMICOLON
; Semicolon
Definition: gfx_type.h:96
_cur_palette
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
VideoDriver_SDL::Paint
void Paint() override
Paint the window.
Definition: sdl_v.cpp:151
GameSizeChanged
void GameSizeChanged()
Size of the application screen changed.
Definition: main_gui.cpp:561
lengthof
#define lengthof(x)
Return the length of an fixed size array.
Definition: stdafx.h:367
Blitter::PaletteAnimate
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
HandleCtrlChanged
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2738
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1619
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:75
Blitter::PALETTE_ANIMATION_BLITTER
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition: base.hpp:52
SDLVkMapping
Definition: sdl2_v.cpp:247
sdl_v.h
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:288
VideoDriver_SDL::PaintThread
void PaintThread() override
Thread function for threaded drawing.
Definition: sdl_v.cpp:177
_draw_continue
static volatile bool _draw_continue
Should we keep continue drawing?
Definition: sdl_v.cpp:44
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
FioFindFullPath
std::string FioFindFullPath(Subdirectory subdir, const char *filename)
Find a path to the filename in one of the search directories.
Definition: fileio.cpp:299
WKC_MINUS
@ WKC_MINUS
Definition: gfx_type.h:104
CursorVars::pos
Point pos
logical mouse position
Definition: gfx_type.h:117
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:41
Palette
Information about the currently used palette.
Definition: gfx_type.h:313
CursorVars::in_window
bool in_window
mouse inside this window, determines drawing logic
Definition: gfx_type.h:141
lastof
#define lastof(x)
Get the last element of an fixed size array.
Definition: stdafx.h:383
VideoDriver_SDL::Stop
void Stop() override
Stop this driver.
Definition: sdl_v.cpp:644
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:59
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:39
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:23
_draw_signal
static std::condition_variable_any * _draw_signal
Signal to draw the next frame.
Definition: sdl_v.cpp:42
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:40
Delta
static T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
Definition: math_func.hpp:170