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