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
8 /** @file sdl_v.cpp Implementation of the SDL video driver. */
9
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
MakeDirty(int left,int top,int width,int height)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
UpdatePalette(bool init=false)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
InitPalette()107 static void InitPalette()
108 {
109 CopyPalette(_local_palette, true);
110 UpdatePalette(true);
111 }
112
CheckPaletteAnim()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
Paint()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
GetVideoModes()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
GetAvailableVideoMode(uint * w,uint * h)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
CreateMainSurface(uint w,uint h)225 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
226 {
227 SDL_Surface *newscreen, *icon;
228 char caption[50];
229 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
230 bool want_hwpalette;
231
232 GetAvailableVideoMode(&w, &h);
233
234 Debug(driver, 1, "SDL: using mode {}x{}x{}", w, h, bpp);
235
236 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
237
238 std::string icon_path = FioFindFullPath(BASESET_DIR, "openttd.32.bmp");
239 if (!icon_path.empty()) {
240 /* Give the application an icon */
241 icon = SDL_LoadBMP(icon_path.c_str());
242 if (icon != nullptr) {
243 /* Get the colourkey, which will be magenta */
244 uint32 rgbmap = SDL_MapRGB(icon->format, 255, 0, 255);
245
246 SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
247 SDL_WM_SetIcon(icon, nullptr);
248 SDL_FreeSurface(icon);
249 }
250 }
251
252 if (_use_hwpalette == 2) {
253 /* Default is to autodetect when to use SDL_HWPALETTE.
254 * In this case, SDL_HWPALETTE is only used for 8bpp
255 * blitters in fullscreen.
256 *
257 * When using an 8bpp blitter on a 8bpp system in
258 * windowed mode with SDL_HWPALETTE, OpenTTD will claim
259 * the system palette, making all other applications
260 * get the wrong colours. In this case, we're better of
261 * trying to approximate the colors we need using system
262 * colors, using a shadow surface (see below).
263 *
264 * On a 32bpp system, SDL_HWPALETTE is ignored, so it
265 * doesn't matter what we do.
266 *
267 * When using a 32bpp blitter on a 8bpp system, setting
268 * SDL_HWPALETTE messes up rendering (at least on X11),
269 * so we don't do that. In this case, SDL takes care of
270 * color approximation using its own shadow surface
271 * (which we can't force in 8bpp on 8bpp mode,
272 * unfortunately).
273 */
274 want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
275 } else {
276 /* User specified a value manually */
277 want_hwpalette = _use_hwpalette;
278 }
279
280 if (want_hwpalette) Debug(driver, 1, "SDL: requesting hardware palette");
281
282 /* Free any previously allocated shadow surface */
283 if (_sdl_surface != nullptr && _sdl_surface != _sdl_realscreen) SDL_FreeSurface(_sdl_surface);
284
285 if (_sdl_realscreen != nullptr) {
286 if (_requested_hwpalette != want_hwpalette) {
287 /* SDL (at least the X11 driver), reuses the
288 * same window and palette settings when the bpp
289 * (and a few flags) are the same. Since we need
290 * to hwpalette value to change (in particular
291 * when switching between fullscreen and
292 * windowed), we restart the entire video
293 * subsystem to force creating a new window.
294 */
295 Debug(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
296 SDL_QuitSubSystem(SDL_INIT_VIDEO);
297 SDL_InitSubSystem(SDL_INIT_VIDEO);
298 ClaimMousePointer();
299 SetupKeyboard();
300 }
301 }
302 /* Remember if we wanted a hwpalette. We can't reliably query
303 * SDL for the SDL_HWPALETTE flag, since it might get set even
304 * though we didn't ask for it (when SDL creates a shadow
305 * surface, for example). */
306 _requested_hwpalette = want_hwpalette;
307
308 /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
309 newscreen = SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
310 if (newscreen == nullptr) {
311 Debug(driver, 0, "SDL: Couldn't allocate a window to draw on");
312 return false;
313 }
314 _sdl_realscreen = newscreen;
315
316 if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
317 /* Using an 8bpp blitter, if we didn't get a hardware
318 * palette (most likely because we didn't request one,
319 * see above), we'll have to set up a shadow surface to
320 * render on.
321 *
322 * Our palette will be applied to this shadow surface,
323 * while the real screen surface will use the shared
324 * system palette (which will partly contain our colors,
325 * but most likely will not have enough free color cells
326 * for all of our colors). SDL can use these two
327 * palettes at blit time to approximate colors used in
328 * the shadow surface using system colors automatically.
329 *
330 * Note that when using an 8bpp blitter on a 32bpp
331 * system, SDL will create an internal shadow surface.
332 * This shadow surface will have SDL_HWPALLETE set, so
333 * we won't create a second shadow surface in this case.
334 */
335 Debug(driver, 1, "SDL: using shadow surface");
336 newscreen = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
337 if (newscreen == nullptr) {
338 Debug(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
339 return false;
340 }
341 }
342
343 /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
344 _num_dirty_rects = 0;
345
346 _screen.width = newscreen->w;
347 _screen.height = newscreen->h;
348 _screen.pitch = newscreen->pitch / (bpp / 8);
349 _screen.dst_ptr = newscreen->pixels;
350 _sdl_surface = newscreen;
351
352 /* When in full screen, we will always have the mouse cursor
353 * within the window, even though SDL does not give us the
354 * appropriate event to know this. */
355 if (_fullscreen) _cursor.in_window = true;
356
357 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
358 blitter->PostResize();
359
360 InitPalette();
361
362 seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
363 SDL_WM_SetCaption(caption, caption);
364
365 GameSizeChanged();
366
367 return true;
368 }
369
ClaimMousePointer()370 bool VideoDriver_SDL::ClaimMousePointer()
371 {
372 SDL_ShowCursor(0);
373 return true;
374 }
375
376 struct SDLVkMapping {
377 uint16 vk_from;
378 byte vk_count;
379 byte map_to;
380 };
381
382 #define AS(x, z) {x, 0, z}
383 #define AM(x, y, z, w) {x, (byte)(y - x), z}
384
385 static const SDLVkMapping _vk_mapping[] = {
386 /* Pageup stuff + up/down */
387 AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
388 AS(SDLK_UP, WKC_UP),
389 AS(SDLK_DOWN, WKC_DOWN),
390 AS(SDLK_LEFT, WKC_LEFT),
391 AS(SDLK_RIGHT, WKC_RIGHT),
392
393 AS(SDLK_HOME, WKC_HOME),
394 AS(SDLK_END, WKC_END),
395
396 AS(SDLK_INSERT, WKC_INSERT),
397 AS(SDLK_DELETE, WKC_DELETE),
398
399 /* Map letters & digits */
400 AM(SDLK_a, SDLK_z, 'A', 'Z'),
401 AM(SDLK_0, SDLK_9, '0', '9'),
402
403 AS(SDLK_ESCAPE, WKC_ESC),
404 AS(SDLK_PAUSE, WKC_PAUSE),
405 AS(SDLK_BACKSPACE, WKC_BACKSPACE),
406
407 AS(SDLK_SPACE, WKC_SPACE),
408 AS(SDLK_RETURN, WKC_RETURN),
409 AS(SDLK_TAB, WKC_TAB),
410
411 /* Function keys */
412 AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
413
414 /* Numeric part. */
415 AM(SDLK_KP0, SDLK_KP9, '0', '9'),
416 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
417 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
418 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
419 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
420 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
421 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
422
423 /* Other non-letter keys */
424 AS(SDLK_SLASH, WKC_SLASH),
425 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
426 AS(SDLK_EQUALS, WKC_EQUALS),
427 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
428 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
429 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
430
431 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
432 AS(SDLK_COMMA, WKC_COMMA),
433 AS(SDLK_MINUS, WKC_MINUS),
434 AS(SDLK_PERIOD, WKC_PERIOD)
435 };
436
ConvertSdlKeyIntoMy(SDL_keysym * sym,WChar * character)437 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
438 {
439 const SDLVkMapping *map;
440 uint key = 0;
441
442 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
443 if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
444 key = sym->sym - map->vk_from + map->map_to;
445 break;
446 }
447 }
448
449 /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
450 #if defined(_WIN32) || defined(__OS2__)
451 if (sym->scancode == 41) key = WKC_BACKQUOTE;
452 #elif defined(__APPLE__)
453 if (sym->scancode == 10) key = WKC_BACKQUOTE;
454 #elif defined(__SVR4) && defined(__sun)
455 if (sym->scancode == 60) key = WKC_BACKQUOTE;
456 if (sym->scancode == 49) key = WKC_BACKSPACE;
457 #elif defined(__sgi__)
458 if (sym->scancode == 22) key = WKC_BACKQUOTE;
459 #else
460 if (sym->scancode == 49) key = WKC_BACKQUOTE;
461 #endif
462
463 /* META are the command keys on mac */
464 if (sym->mod & KMOD_META) key |= WKC_META;
465 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
466 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
467 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
468
469 *character = sym->unicode;
470 return key;
471 }
472
PollEvent()473 bool VideoDriver_SDL::PollEvent()
474 {
475 SDL_Event ev;
476
477 if (!SDL_PollEvent(&ev)) return false;
478
479 switch (ev.type) {
480 case SDL_MOUSEMOTION:
481 if (_cursor.UpdateCursorPosition(ev.motion.x, ev.motion.y, true)) {
482 SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
483 }
484 HandleMouseEvents();
485 break;
486
487 case SDL_MOUSEBUTTONDOWN:
488 if (_rightclick_emulate && SDL_GetModState() & KMOD_CTRL) {
489 ev.button.button = SDL_BUTTON_RIGHT;
490 }
491
492 switch (ev.button.button) {
493 case SDL_BUTTON_LEFT:
494 _left_button_down = true;
495 break;
496
497 case SDL_BUTTON_RIGHT:
498 _right_button_down = true;
499 _right_button_clicked = true;
500 break;
501
502 case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
503 case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
504
505 default: break;
506 }
507 HandleMouseEvents();
508 break;
509
510 case SDL_MOUSEBUTTONUP:
511 if (_rightclick_emulate) {
512 _right_button_down = false;
513 _left_button_down = false;
514 _left_button_clicked = false;
515 } else if (ev.button.button == SDL_BUTTON_LEFT) {
516 _left_button_down = false;
517 _left_button_clicked = false;
518 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
519 _right_button_down = false;
520 }
521 HandleMouseEvents();
522 break;
523
524 case SDL_ACTIVEEVENT:
525 if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
526
527 if (ev.active.gain) { // mouse entered the window, enable cursor
528 _cursor.in_window = true;
529 } else {
530 UndrawMouseCursor(); // mouse left the window, undraw cursor
531 _cursor.in_window = false;
532 }
533 break;
534
535 case SDL_QUIT:
536 HandleExitGameRequest();
537 break;
538
539 case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
540 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
541 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
542 ToggleFullScreen(!_fullscreen);
543 } else {
544 WChar character;
545 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
546 HandleKeypress(keycode, character);
547 }
548 break;
549
550 case SDL_VIDEORESIZE: {
551 int w = std::max(ev.resize.w, 64);
552 int h = std::max(ev.resize.h, 64);
553 CreateMainSurface(w, h);
554 break;
555 }
556 case SDL_VIDEOEXPOSE: {
557 /* Force a redraw of the entire screen. Note
558 * that SDL 1.2 seems to do this automatically
559 * in most cases, but 1.3 / 2.0 does not. */
560 _num_dirty_rects = MAX_DIRTY_RECTS + 1;
561 break;
562 }
563 }
564
565 return true;
566 }
567
Start(const StringList & param)568 const char *VideoDriver_SDL::Start(const StringList ¶m)
569 {
570 char buf[30];
571 _use_hwpalette = GetDriverParamInt(param, "hw_palette", 2);
572
573 /* Just on the offchance the audio subsystem started before the video system,
574 * check whether any part of SDL has been initialised before getting here.
575 * Slightly duplicated with sound/sdl_s.cpp */
576 int ret_code = 0;
577 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
578 ret_code = SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);
579 } else if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
580 ret_code = SDL_InitSubSystem(SDL_INIT_VIDEO);
581 }
582 if (ret_code < 0) return SDL_GetError();
583
584 this->UpdateAutoResolution();
585
586 GetVideoModes();
587 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
588 return SDL_GetError();
589 }
590
591 SDL_VideoDriverName(buf, sizeof buf);
592 Debug(driver, 1, "SDL: using driver '{}'", buf);
593
594 MarkWholeScreenDirty();
595 SetupKeyboard();
596
597 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
598
599 return nullptr;
600 }
601
SetupKeyboard()602 void VideoDriver_SDL::SetupKeyboard()
603 {
604 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
605 SDL_EnableUNICODE(1);
606 }
607
Stop()608 void VideoDriver_SDL::Stop()
609 {
610 SDL_QuitSubSystem(SDL_INIT_VIDEO);
611 if (SDL_WasInit(SDL_INIT_EVERYTHING) == 0) {
612 SDL_Quit(); // If there's nothing left, quit SDL
613 }
614 }
615
InputLoop()616 void VideoDriver_SDL::InputLoop()
617 {
618 uint32 mod = SDL_GetModState();
619 int numkeys;
620 Uint8 *keys = SDL_GetKeyState(&numkeys);
621
622 bool old_ctrl_pressed = _ctrl_pressed;
623
624 _ctrl_pressed = !!(mod & KMOD_CTRL);
625 _shift_pressed = !!(mod & KMOD_SHIFT);
626
627 #if defined(_DEBUG)
628 this->fast_forward_key_pressed = _shift_pressed;
629 #else
630 /* Speedup when pressing tab, except when using ALT+TAB
631 * to switch to another application. */
632 this->fast_forward_key_pressed = keys[SDLK_TAB] && (mod & KMOD_ALT) == 0;
633 #endif /* defined(_DEBUG) */
634
635 /* Determine which directional keys are down. */
636 _dirkeys =
637 (keys[SDLK_LEFT] ? 1 : 0) |
638 (keys[SDLK_UP] ? 2 : 0) |
639 (keys[SDLK_RIGHT] ? 4 : 0) |
640 (keys[SDLK_DOWN] ? 8 : 0);
641
642 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
643 }
644
MainLoop()645 void VideoDriver_SDL::MainLoop()
646 {
647 this->StartGameThread();
648
649 for (;;) {
650 if (_exit_game) break;
651
652 this->Tick();
653 this->SleepTillNextTick();
654 }
655
656 this->StopGameThread();
657 }
658
ChangeResolution(int w,int h)659 bool VideoDriver_SDL::ChangeResolution(int w, int h)
660 {
661 return CreateMainSurface(w, h);
662 }
663
ToggleFullscreen(bool fullscreen)664 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
665 {
666 _fullscreen = fullscreen;
667 GetVideoModes(); // get the list of available video modes
668 bool ret = !_resolutions.empty() && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
669
670 if (!ret) {
671 /* switching resolution failed, put back full_screen to original status */
672 _fullscreen ^= true;
673 }
674
675 InvalidateWindowClassesData(WC_GAME_OPTIONS, 3);
676 return ret;
677 }
678
AfterBlitterChange()679 bool VideoDriver_SDL::AfterBlitterChange()
680 {
681 return CreateMainSurface(_screen.width, _screen.height);
682 }
683
684 #endif /* WITH_SDL */
685