OpenTTD Source  1.11.2
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 
27 #include "../safeguards.h"
28 
29 static FVideoDriver_SDL iFVideoDriver_SDL;
30 
31 static SDL_Surface *_sdl_surface;
32 static SDL_Surface *_sdl_realscreen;
33 static bool _all_modes;
34 
35 static Palette _local_palette;
36 
37 #define MAX_DIRTY_RECTS 100
38 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
39 static int _num_dirty_rects;
40 static int _use_hwpalette;
41 static int _requested_hwpalette; /* Did we request a HWPALETTE for the current video mode? */
42 
43 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
44 {
45  if (_num_dirty_rects < MAX_DIRTY_RECTS) {
46  _dirty_rects[_num_dirty_rects].x = left;
47  _dirty_rects[_num_dirty_rects].y = top;
48  _dirty_rects[_num_dirty_rects].w = width;
49  _dirty_rects[_num_dirty_rects].h = height;
50  }
51  _num_dirty_rects++;
52 }
53 
54 static void UpdatePalette(bool init = false)
55 {
56  SDL_Color pal[256];
57 
58  for (int i = 0; i != _local_palette.count_dirty; i++) {
62  pal[i].unused = 0;
63  }
64 
65  SDL_SetColors(_sdl_surface, pal, _local_palette.first_dirty, _local_palette.count_dirty);
66 
67  if (_sdl_surface != _sdl_realscreen && init) {
68  /* When using a shadow surface, also set our palette on the real screen. This lets SDL
69  * allocate as many colors (or approximations) as
70  * possible, instead of using only the default SDL
71  * palette. This allows us to get more colors exactly
72  * right and might allow using better approximations for
73  * other colors.
74  *
75  * Note that colors allocations are tried in-order, so
76  * this favors colors further up into the palette. Also
77  * note that if two colors from the same animation
78  * sequence are approximated using the same color, that
79  * animation will stop working.
80  *
81  * Since changing the system palette causes the colours
82  * to change right away, and allocations might
83  * drastically change, we can't use this for animation,
84  * since that could cause weird coloring between the
85  * palette change and the blitting below, so we only set
86  * the real palette during initialisation.
87  */
88  SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
89  }
90 
91  if (_sdl_surface != _sdl_realscreen && !init) {
92  /* We're not using real hardware palette, but are letting SDL
93  * approximate the palette during shadow -> screen copy. To
94  * change the palette, we need to recopy the entire screen.
95  *
96  * Note that this operation can slow down the rendering
97  * considerably, especially since changing the shadow
98  * palette will need the next blit to re-detect the
99  * best mapping of shadow palette colors to real palette
100  * colors from scratch.
101  */
102  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
103  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
104  }
105 }
106 
107 static void InitPalette()
108 {
112  UpdatePalette(true);
113 }
114 
116 {
118 
119  if (_cur_palette.count_dirty != 0) {
121 
122  switch (blitter->UsePaletteAnimation()) {
124  UpdatePalette();
125  break;
126 
128  blitter->PaletteAnimate(_local_palette);
129  break;
130 
132  break;
133 
134  default:
135  NOT_REACHED();
136  }
138  }
139 }
140 
142 {
143  PerformanceMeasurer framerate(PFE_VIDEO);
144 
145  int n = _num_dirty_rects;
146  if (n == 0) return;
147 
148  _num_dirty_rects = 0;
149 
150  if (n > MAX_DIRTY_RECTS) {
151  if (_sdl_surface != _sdl_realscreen) {
152  SDL_BlitSurface(_sdl_surface, nullptr, _sdl_realscreen, nullptr);
153  }
154 
155  SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
156  } else {
157  if (_sdl_surface != _sdl_realscreen) {
158  for (int i = 0; i < n; i++) {
159  SDL_BlitSurface(_sdl_surface, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
160  }
161  }
162 
163  SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
164  }
165 }
166 
167 static const Dimension _default_resolutions[] = {
168  { 640, 480},
169  { 800, 600},
170  {1024, 768},
171  {1152, 864},
172  {1280, 800},
173  {1280, 960},
174  {1280, 1024},
175  {1400, 1050},
176  {1600, 1200},
177  {1680, 1050},
178  {1920, 1200}
179 };
180 
181 static void GetVideoModes()
182 {
183  SDL_Rect **modes = SDL_ListModes(nullptr, SDL_SWSURFACE | SDL_FULLSCREEN);
184  if (modes == nullptr) usererror("sdl: no modes available");
185 
186  _resolutions.clear();
187 
188  _all_modes = (SDL_ListModes(nullptr, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
189  if (modes == (void*)-1) {
190  for (uint i = 0; i < lengthof(_default_resolutions); i++) {
191  if (SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
192  _resolutions.push_back(_default_resolutions[i]);
193  }
194  }
195  } else {
196  for (int i = 0; modes[i]; i++) {
197  uint w = modes[i]->w;
198  uint h = modes[i]->h;
199  if (w < 640 || h < 480) continue; // reject too small resolutions
200  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
201  _resolutions.emplace_back(w, h);
202  }
203  if (_resolutions.empty()) usererror("No usable screen resolutions found!\n");
204  SortResolutions();
205  }
206 }
207 
208 static void GetAvailableVideoMode(uint *w, uint *h)
209 {
210  /* All modes available? */
211  if (_all_modes || _resolutions.empty()) return;
212 
213  /* Is the wanted mode among the available modes? */
214  if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
215 
216  /* Use the closest possible resolution */
217  uint best = 0;
218  uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
219  for (uint i = 1; i != _resolutions.size(); ++i) {
220  uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
221  if (newdelta < delta) {
222  best = i;
223  delta = newdelta;
224  }
225  }
226  *w = _resolutions[best].width;
227  *h = _resolutions[best].height;
228 }
229 
230 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
231 {
232  SDL_Surface *newscreen, *icon;
233  char caption[50];
235  bool want_hwpalette;
236 
237  GetAvailableVideoMode(&w, &h);
238 
239  DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
240 
241  if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
242 
243  std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
244  if (!icon_path.empty()) {
245  /* Give the application an icon */
246  icon = SDL_LoadBMP(icon_path.c_str());
247  if (icon != nullptr) {
248  /* Get the colourkey, which will be magenta */
249  uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
250 
251  SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
252  SDL_WM_SetIcon(icon, nullptr);
253  SDL_FreeSurface(icon);
254  }
255  }
256 
257  if (_use_hwpalette == 2) {
258  /* Default is to autodetect when to use SDL_HWPALETTE.
259  * In this case, SDL_HWPALETTE is only used for 8bpp
260  * blitters in fullscreen.
261  *
262  * When using an 8bpp blitter on a 8bpp system in
263  * windowed mode with SDL_HWPALETTE, OpenTTD will claim
264  * the system palette, making all other applications
265  * get the wrong colours. In this case, we're better of
266  * trying to approximate the colors we need using system
267  * colors, using a shadow surface (see below).
268  *
269  * On a 32bpp system, SDL_HWPALETTE is ignored, so it
270  * doesn't matter what we do.
271  *
272  * When using a 32bpp blitter on a 8bpp system, setting
273  * SDL_HWPALETTE messes up rendering (at least on X11),
274  * so we don't do that. In this case, SDL takes care of
275  * color approximation using its own shadow surface
276  * (which we can't force in 8bpp on 8bpp mode,
277  * unfortunately).
278  */
279  want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
280  } else {
281  /* User specified a value manually */
282  want_hwpalette = _use_hwpalette;
283  }
284 
285  if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palette");
286 
287  /* Free any previously allocated shadow surface */
288  if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);
289 
290  if (_sdl_realscreen != nullptr) {
291  if (_requested_hwpalette != want_hwpalette) {
292  /* SDL (at least the X11 driver), reuses the
293  * same window and palette settings when the bpp
294  * (and a few flags) are the same. Since we need
295  * to hwpalette value to change (in particular
296  * when switching between fullscreen and
297  * windowed), we restart the entire video
298  * subsystem to force creating a new window.
299  */
300  DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
301  SDL_QuitSubSystem(SDL_INIT_VIDEO);
302  SDL_InitSubSystem(SDL_INIT_VIDEO);
303  ClaimMousePointer();
304  SetupKeyboard();
305  }
306  }
307  /* Remember if we wanted a hwpalette. We can't reliably query
308  * SDL for the SDL_HWPALETTE flag, since it might get set even
309  * though we didn't ask for it (when SDL creates a shadow
310  * surface, for example). */
311  _requested_hwpalette = want_hwpalette;
312 
313  /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
314  newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
315  if (newscreen == nullptr) {
316  DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
317  return false;
318  }
319  _sdl_realscreen = newscreen;
320 
321  if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
322  /* Using an 8bpp blitter, if we didn't get a hardware
323  * palette (most likely because we didn't request one,
324  * see above), we'll have to set up a shadow surface to
325  * render on.
326  *
327  * Our palette will be applied to this shadow surface,
328  * while the real screen surface will use the shared
329  * system palette (which will partly contain our colors,
330  * but most likely will not have enough free color cells
331  * for all of our colors). SDL can use these two
332  * palettes at blit time to approximate colors used in
333  * the shadow surface using system colors automatically.
334  *
335  * Note that when using an 8bpp blitter on a 32bpp
336  * system, SDL will create an internal shadow surface.
337  * This shadow surface will have SDL_HWPALLETE set, so
338  * we won't create a second shadow surface in this case.
339  */
340  DEBUG(driver, 1, "SDL: using shadow surface");
341  newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
342  if (newscreen == nullptr) {
343  DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
344  return false;
345  }
346  }
347 
348  /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
349  _num_dirty_rects = 0;
350 
351  _screen.width = newscreen->w;
352  _screen.height = newscreen->h;
353  _screen.pitch = newscreen->pitch / (bpp / 8);
354  _screen.dst_ptr = newscreen->pixels;
355  _sdl_surface = newscreen;
356 
357  /* When in full screen, we will always have the mouse cursor
358  * within the window, even though SDL does not give us the
359  * appropriate event to know this. */
360  if (_fullscreen) _cursor.in_window = true;
361 
363  blitter->PostResize();
364 
365  InitPalette();
366 
367  seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
368  SDL_WM_SetCaption(caption, caption);
369 
370  GameSizeChanged();
371 
372  return true;
373 }
374 
375 bool VideoDriver_SDL::ClaimMousePointer()
376 {
377  SDL_ShowCursor(0);
378  return true;
379 }
380 
381 struct SDLVkMapping {
382  uint16 vk_from;
383  byte vk_count;
384  byte map_to;
385 };
386 
387 #define AS(x, z) {x, 0, z}
388 #define AM(x, y, z, w) {x, (byte)(y - x), z}
389 
390 static const SDLVkMapping _vk_mapping[] = {
391  /* Pageup stuff + up/down */
392  AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
393  AS(SDLK_UP, WKC_UP),
394  AS(SDLK_DOWN, WKC_DOWN),
395  AS(SDLK_LEFT, WKC_LEFT),
396  AS(SDLK_RIGHT, WKC_RIGHT),
397 
398  AS(SDLK_HOME, WKC_HOME),
399  AS(SDLK_END, WKC_END),
400 
401  AS(SDLK_INSERT, WKC_INSERT),
402  AS(SDLK_DELETE, WKC_DELETE),
403 
404  /* Map letters & digits */
405  AM(SDLK_a, SDLK_z, 'A', 'Z'),
406  AM(SDLK_0, SDLK_9, '0', '9'),
407 
408  AS(SDLK_ESCAPE, WKC_ESC),
409  AS(SDLK_PAUSE, WKC_PAUSE),
410  AS(SDLK_BACKSPACE, WKC_BACKSPACE),
411 
412  AS(SDLK_SPACE, WKC_SPACE),
413  AS(SDLK_RETURN, WKC_RETURN),
414  AS(SDLK_TAB, WKC_TAB),
415 
416  /* Function keys */
417  AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
418 
419  /* Numeric part. */
420  AM(SDLK_KP0, SDLK_KP9, '0', '9'),
421  AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
422  AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
423  AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
424  AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
425  AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
426  AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
427 
428  /* Other non-letter keys */
429  AS(SDLK_SLASH, WKC_SLASH),
430  AS(SDLK_SEMICOLON, WKC_SEMICOLON),
431  AS(SDLK_EQUALS, WKC_EQUALS),
432  AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
433  AS(SDLK_BACKSLASH, WKC_BACKSLASH),
434  AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
435 
436  AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
437  AS(SDLK_COMMA, WKC_COMMA),
438  AS(SDLK_MINUS, WKC_MINUS),
439  AS(SDLK_PERIOD, WKC_PERIOD)
440 };
441 
442 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
443 {
444  const SDLVkMapping *map;
445  uint key = 0;
446 
447  for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
448  if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
449  key = sym->sym - map->vk_from + map->map_to;
450  break;
451  }
452  }
453 
454  /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
455 #if defined(_WIN32) || defined(__OS2__)
456  if (sym->scancode == 41) key = WKC_BACKQUOTE;
457 #elif defined(__APPLE__)
458  if (sym->scancode == 10) key = WKC_BACKQUOTE;
459 #elif defined(__SVR4) && defined(__sun)
460  if (sym->scancode == 60) key = WKC_BACKQUOTE;
461  if (sym->scancode == 49) key = WKC_BACKSPACE;
462 #elif defined(__sgi__)
463  if (sym->scancode == 22) key = WKC_BACKQUOTE;
464 #else
465  if (sym->scancode == 49) key = WKC_BACKQUOTE;
466 #endif
467 
468  /* META are the command keys on mac */
469  if (sym->mod & KMOD_META) key |= WKC_META;
470  if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
471  if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
472  if (sym->mod & KMOD_ALT) key |= WKC_ALT;
473 
474  *character = sym->unicode;
475  return key;
476 }
477 
479 {
480  SDL_Event ev;
481 
482  if (!SDL_PollEvent(&ev)) return false;
483 
484  switch (ev.type) {
485  case SDL_MOUSEMOTION:
486  if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
487  SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
488  }
490  break;
491 
492  case SDL_MOUSEBUTTONDOWN:
493  if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
494  ev.button.button = SDL_BUTTON_RIGHT;
495  }
496 
497  switch (ev.button.button) {
498  case SDL_BUTTON_LEFT:
499  _left_button_down = true;
500  break;
501 
502  case SDL_BUTTON_RIGHT:
503  _right_button_down = true;
504  _right_button_clicked = true;
505  break;
506 
507  case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
508  case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
509 
510  default: break;
511  }
513  break;
514 
515  case SDL_MOUSEBUTTONUP:
516  if (_rightclick_emulate) {
517  _right_button_down = false;
518  _left_button_down = false;
519  _left_button_clicked = false;
520  } else if (ev.button.button == SDL_BUTTON_LEFT) {
521  _left_button_down = false;
522  _left_button_clicked = false;
523  } else if (ev.button.button == SDL_BUTTON_RIGHT) {
524  _right_button_down = false;
525  }
527  break;
528 
529  case SDL_ACTIVEEVENT:
530  if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
531 
532  if (ev.active.gain) { // mouse entered the window, enable cursor
533  _cursor.in_window = true;
534  } else {
535  UndrawMouseCursor(); // mouse left the window, undraw cursor
536  _cursor.in_window = false;
537  }
538  break;
539 
540  case SDL_QUIT:
541  HandleExitGameRequest();
542  break;
543 
544  case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
545  if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
546  (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
547  ToggleFullScreen(!_fullscreen);
548  } else {
549  WChar character;
550  uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
551  HandleKeypress(keycode, character);
552  }
553  break;
554 
555  case SDL_VIDEORESIZE: {
556  int w = std::max(ev.resize.w, 64);
557  int h = std::max(ev.resize.h, 64);
558  CreateMainSurface(w, h);
559  break;
560  }
561  case SDL_VIDEOEXPOSE: {
562  /* Force a redraw of the entire screen. Note
563  * that SDL 1.2 seems to do this automatically
564  * in most cases, but 1.3 / 2.0 does not. */
565  _num_dirty_rects = MAX_DIRTY_RECTS + 1;
566  break;
567  }
568  }
569 
570  return true;
571 }
572 
573 const char *VideoDriver_SDL::Start(const StringList &param)
574 {
575  char buf[30];
576  _use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);
577 
578  /* Just on the offchance the audio subsystem started before the video system,
579  * check whether any part of SDL has been initialised before getting here.
580  * Slightly duplicated with sound/sdl_s.cpp */
581  int ret_code = 0;
582  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
583  ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
584  } else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
585  ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
586  }
587  if (ret_code < 0) return SDL_GetError();
588 
589  this->UpdateAutoResolution();
590 
591  GetVideoModes();
592  if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
593  return SDL_GetError();
594  }
595 
596  SDL_VideoDriverName(buf, sizeof buf);
597  DEBUG(driver, 1, "SDL: using driver '%s'", buf);
598 
600  SetupKeyboard();
601 
602  this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
603 
604  return nullptr;
605 }
606 
607 void VideoDriver_SDL::SetupKeyboard()
608 {
609  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
610  SDL_EnableUNICODE(1);
611 }
612 
614 {
615  SDL_QuitSubSystem(SDL_INIT_VIDEO);
616  if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
617  SDL_Quit(); // If there's nothing left, quit SDL
618  }
619 }
620 
622 {
623  uint32 mod = SDL_GetModState();
624  int numkeys;
625  Uint8 *keys = SDL_GetKeyState(&numkeys);
626 
627  bool old_ctrl_pressed = _ctrl_pressed;
628 
629  _ctrl_pressed = !!(mod & KMOD_CTRL);
630  _shift_pressed = !!(mod & KMOD_SHIFT);
631 
632 #if defined(_DEBUG)
634 #else
635  /* Speedup when pressing tab, except when using ALT+TAB
636  * to switch to another application. */
637  this->fast_forward_key_pressed = keys[SDLK_TAB] && (mod & KMOD_ALT) == 0;
638 #endif /* defined(_DEBUG) */
639 
640  /* Determine which directional keys are down. */
641  _dirkeys =
642  (keys[SDLK_LEFT] ? 1 : 0) |
643  (keys[SDLK_UP] ? 2 : 0) |
644  (keys[SDLK_RIGHT] ? 4 : 0) |
645  (keys[SDLK_DOWN] ? 8 : 0);
646 
647  if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
648 }
649 
651 {
652  this->StartGameThread();
653 
654  for (;;) {
655  if (_exit_game) break;
656 
657  this->Tick();
658  this->SleepTillNextTick();
659  }
660 
661  this->StopGameThread();
662 }
663 
665 {
666  return CreateMainSurface(w, h);
667 }
668 
670 {
671  _fullscreen = fullscreen;
672  GetVideoModes(); // get the list of available video modes
673  bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
674 
675  if (!ret) {
676  /* switching resolution failed, put back full_screen to original status */
677  _fullscreen ^= true;
678  }
679 
681  return ret;
682 }
683 
685 {
686  return CreateMainSurface(_screen.width, _screen.height);
687 }
688 
689 #endif /* WITH_SDL */
_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::Tick
void Tick()
Give the video-driver a tick.
Definition: video_driver.cpp:100
Palette::first_dirty
int first_dirty
The first dirty element.
Definition: gfx_type.h:315
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:103
VideoDriver_SDL::Start
const char * Start(const StringList &param) override
Start this driver.
Definition: sdl_v.cpp:573
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
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
VideoDriver::StartGameThread
void StartGameThread()
Start the loop for game-tick.
Definition: video_driver.cpp:84
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:669
VideoDriver_SDL::MainLoop
void MainLoop() override
Perform the actual drawing.
Definition: sdl_v.cpp:650
VideoDriver_SDL::MakeDirty
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
Definition: sdl_v.cpp:43
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:1806
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
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
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:24
_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:664
VideoDriver_SDL::PollEvent
bool PollEvent() override
Process a single system event.
Definition: sdl_v.cpp:478
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:73
FVideoDriver_SDL
Factory for the SDL video driver.
Definition: sdl_v.h:48
VideoDriver::StopGameThread
void StopGameThread()
Stop the loop for the game-tick.
Definition: video_driver.cpp:93
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:26
WKC_PERIOD
@ WKC_PERIOD
. Period
Definition: gfx_type.h:103
WC_GAME_OPTIONS
@ WC_GAME_OPTIONS
Game options window; Window numbers:
Definition: window_type.h:606
VideoDriver_SDL::AfterBlitterChange
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
Definition: sdl_v.cpp:684
VideoDriver::UpdateAutoResolution
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
Definition: video_driver.hpp:239
VideoDriver_SDL::CheckPaletteAnim
void CheckPaletteAnim() override
Process any pending palette animation.
Definition: sdl_v.cpp:115
endof
#define endof(x)
Get the end element of an fixed size array.
Definition: stdafx.h:377
InvalidateWindowClassesData
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
Definition: window.cpp:3339
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:621
seprintf
int CDECL seprintf(char *str, const char *last, const char *format,...)
Safer implementation of snprintf; same as snprintf except:
Definition: string.cpp:460
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:141
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:369
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:1597
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:162
Blitter::PALETTE_ANIMATION_BLITTER
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition: base.hpp:52
SDLVkMapping
Definition: sdl2_v.cpp:252
sdl_v.h
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:325
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:385
VideoDriver_SDL::Stop
void Stop() override
Stop this driver.
Definition: sdl_v.cpp:613
GetDriverParamBool
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition: driver.cpp:61
_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:25
_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