OpenTTD Source: src/video/sdl_v.cpp Source File

OpenTTD Source 14.1

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