OpenTTD: src/video/sdl_v.cpp Source File

OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

sdl_v.cpp
Go to the documentation of this file.
1 /* $Id: sdl_v.cpp 26538 2014-04-28 21:06:51Z rubidium $ */
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 "../sdl.h"
18 #include "../rev.h"
19 #include "../blitter/factory.hpp"
20 #include "../network/network.h"
21 #include "../thread/thread.h"
22 #include "../progress.h"
23 #include "../core/random_func.hpp"
24 #include "../core/math_func.hpp"
25 #include "../fileio_func.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_CALL 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_CALL 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_CALL SDL_BlitSurface(_sdl_screen, NULL, _sdl_realscreen, NULL);
113 SDL_CALL 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) {
128 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
129
130 switch (blitter->UsePaletteAnimation()) {
131 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
132 UpdatePalette();
133 break;
134
135 case Blitter::PALETTE_ANIMATION_BLITTER:
136 blitter->PaletteAnimate(_local_palette);
137 break;
138
139 case Blitter::PALETTE_ANIMATION_NONE:
140 break;
141
142 default:
143 NOT_REACHED();
144 }
145 _cur_palette.count_dirty = 0;
146 }
147 }
148
149 static void DrawSurfaceToScreen()
150 {
151 int n = _num_dirty_rects;
152 if (n == 0) return;
153
154 _num_dirty_rects = 0;
155 if (n > MAX_DIRTY_RECTS) {
156 if (_sdl_screen != _sdl_realscreen) {
157 SDL_CALL SDL_BlitSurface(_sdl_screen, NULL, _sdl_realscreen, NULL);
158 }
159 SDL_CALL SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
160 } else {
161 if (_sdl_screen != _sdl_realscreen) {
162 for (int i = 0; i < n; i++) {
163 SDL_CALL SDL_BlitSurface(_sdl_screen, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
164 }
165 }
166 SDL_CALL SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
167 }
168 }
169
170 static void DrawSurfaceToScreenThread(void *)
171 {
172 /* First tell the main thread we're started */
173 _draw_mutex->BeginCritical();
174 _draw_mutex->SendSignal();
175
176 /* Now wait for the first thing to draw! */
177 _draw_mutex->WaitForSignal();
178
179 while (_draw_continue) {
180 CheckPaletteAnim();
181 /* Then just draw and wait till we stop */
182 DrawSurfaceToScreen();
183 _draw_mutex->WaitForSignal();
184 }
185
186 _draw_mutex->EndCritical();
187 _draw_thread->Exit();
188 }
189
190 static const Dimension _default_resolutions[] = {
191 { 640, 480},
192 { 800, 600},
193 {1024, 768},
194 {1152, 864},
195 {1280, 800},
196 {1280, 960},
197 {1280, 1024},
198 {1400, 1050},
199 {1600, 1200},
200 {1680, 1050},
201 {1920, 1200}
202 };
203
204 static void GetVideoModes()
205 {
206 SDL_Rect **modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | SDL_FULLSCREEN);
207 if (modes == NULL) usererror("sdl: no modes available");
208
209 _all_modes = (SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
210 if (modes == (void*)-1) {
211 int n = 0;
212 for (uint i = 0; i < lengthof(_default_resolutions); i++) {
213 if (SDL_CALL SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
214 _resolutions[n] = _default_resolutions[i];
215 if (++n == lengthof(_resolutions)) break;
216 }
217 }
218 _num_resolutions = n;
219 } else {
220 int n = 0;
221 for (int i = 0; modes[i]; i++) {
222 uint w = modes[i]->w;
223 uint h = modes[i]->h;
224 int j;
225 for (j = 0; j < n; j++) {
226 if (_resolutions[j].width == w && _resolutions[j].height == h) break;
227 }
228
229 if (j == n) {
230 _resolutions[j].width = w;
231 _resolutions[j].height = h;
232 if (++n == lengthof(_resolutions)) break;
233 }
234 }
235 _num_resolutions = n;
236 SortResolutions(_num_resolutions);
237 }
238 }
239
240 static void GetAvailableVideoMode(uint *w, uint *h)
241 {
242 /* All modes available? */
243 if (_all_modes || _num_resolutions == 0) return;
244
245 /* Is the wanted mode among the available modes? */
246 for (int i = 0; i != _num_resolutions; i++) {
247 if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
248 }
249
250 /* Use the closest possible resolution */
251 int best = 0;
252 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
253 for (int i = 1; i != _num_resolutions; ++i) {
254 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
255 if (newdelta < delta) {
256 best = i;
257 delta = newdelta;
258 }
259 }
260 *w = _resolutions[best].width;
261 *h = _resolutions[best].height;
262 }
263
264 #ifdef WIN32
265 /* Let's redefine the LoadBMP macro with because we are dynamically
266 * loading SDL and need to 'SDL_CALL' all functions */
267 #undef SDL_LoadBMP
268 #define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_CALL SDL_RWFromFile(file, "rb"), 1)
269 #endif
270
271 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
272 {
273 SDL_Surface *newscreen, *icon;
274 char caption[50];
275 int bpp = BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
276 bool want_hwpalette;
277
278 GetAvailableVideoMode(&w, &h);
279
280 DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
281
282 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
283
284 char icon_path[MAX_PATH];
285 if (FioFindFullPath(icon_path, lastof(icon_path), BASESET_DIR, "openttd.32.bmp") != NULL) {
286 /* Give the application an icon */
287 icon = SDL_CALL SDL_LoadBMP(icon_path);
288 if (icon != NULL) {
289 /* Get the colourkey, which will be magenta */
290 uint32 rgbmap = SDL_CALL SDL_MapRGB(icon->format, 255, 0, 255);
291
292 SDL_CALL SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
293 SDL_CALL SDL_WM_SetIcon(icon, NULL);
294 SDL_CALL SDL_FreeSurface(icon);
295 }
296 }
297
298 if (_use_hwpalette == 2) {
299 /* Default is to autodetect when to use SDL_HWPALETTE.
300 * In this case, SDL_HWPALETTE is only used for 8bpp
301 * blitters in fullscreen.
302 *
303 * When using an 8bpp blitter on a 8bpp system in
304 * windowed mode with SDL_HWPALETTE, OpenTTD will claim
305 * the system palette, making all other applications
306 * get the wrong colours. In this case, we're better of
307 * trying to approximate the colors we need using system
308 * colors, using a shadow surface (see below).
309 *
310 * On a 32bpp system, SDL_HWPALETTE is ignored, so it
311 * doesn't matter what we do.
312 *
313 * When using a 32bpp blitter on a 8bpp system, setting
314 * SDL_HWPALETTE messes up rendering (at least on X11),
315 * so we don't do that. In this case, SDL takes care of
316 * color approximation using its own shadow surface
317 * (which we can't force in 8bpp on 8bpp mode,
318 * unfortunately).
319 */
320 want_hwpalette = bpp == 8 && _fullscreen && _support8bpp == S8BPP_HARDWARE;
321 } else {
322 /* User specified a value manually */
323 want_hwpalette = _use_hwpalette;
324 }
325
326 if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palete");
327
328 /* Free any previously allocated shadow surface */
329 if (_sdl_screen != NULL && _sdl_screen != _sdl_realscreen) SDL_CALL SDL_FreeSurface(_sdl_screen);
330
331 if (_sdl_realscreen != NULL) {
332 if (_requested_hwpalette != want_hwpalette) {
333 /* SDL (at least the X11 driver), reuses the
334 * same window and palette settings when the bpp
335 * (and a few flags) are the same. Since we need
336 * to hwpalette value to change (in particular
337 * when switching between fullscreen and
338 * windowed), we restart the entire video
339 * subsystem to force creating a new window.
340 */
341 DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
342 SDL_CALL SDL_QuitSubSystem(SDL_INIT_VIDEO);
343 SDL_CALL SDL_InitSubSystem(SDL_INIT_VIDEO);
344 ClaimMousePointer();
345 SetupKeyboard();
346 }
347 }
348 /* Remember if we wanted a hwpalette. We can't reliably query
349 * SDL for the SDL_HWPALETTE flag, since it might get set even
350 * though we didn't ask for it (when SDL creates a shadow
351 * surface, for example). */
352 _requested_hwpalette = want_hwpalette;
353
354 /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
355 newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
356 if (newscreen == NULL) {
357 DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
358 return false;
359 }
360 _sdl_realscreen = newscreen;
361
362 if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
363 /* Using an 8bpp blitter, if we didn't get a hardware
364 * palette (most likely because we didn't request one,
365 * see above), we'll have to set up a shadow surface to
366 * render on.
367 *
368 * Our palette will be applied to this shadow surface,
369 * while the real screen surface will use the shared
370 * system palette (which will partly contain our colors,
371 * but most likely will not have enough free color cells
372 * for all of our colors). SDL can use these two
373 * palettes at blit time to approximate colors used in
374 * the shadow surface using system colors automatically.
375 *
376 * Note that when using an 8bpp blitter on a 32bpp
377 * system, SDL will create an internal shadow surface.
378 * This shadow surface will have SDL_HWPALLETE set, so
379 * we won't create a second shadow surface in this case.
380 */
381 DEBUG(driver, 1, "SDL: using shadow surface");
382 newscreen = SDL_CALL SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
383 if (newscreen == NULL) {
384 DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
385 return false;
386 }
387 }
388
389 /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
390 _num_dirty_rects = 0;
391
392 _screen.width = newscreen->w;
393 _screen.height = newscreen->h;
394 _screen.pitch = newscreen->pitch / (bpp / 8);
395 _screen.dst_ptr = newscreen->pixels;
396 _sdl_screen = newscreen;
397
398 /* When in full screen, we will always have the mouse cursor
399 * within the window, even though SDL does not give us the
400 * appropriate event to know this. */
401 if (_fullscreen) _cursor.in_window = true;
402
403 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
404 blitter->PostResize();
405
406 InitPalette();
407 switch (blitter->UsePaletteAnimation()) {
408 case Blitter::PALETTE_ANIMATION_NONE:
409 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
410 UpdatePalette();
411 break;
412
413 case Blitter::PALETTE_ANIMATION_BLITTER:
414 if (VideoDriver::GetInstance() != NULL) blitter->PaletteAnimate(_local_palette);
415 break;
416
417 default:
418 NOT_REACHED();
419 }
420
421 seprintf(caption, lastof(caption), "OpenTTD %s", _openttd_revision);
422 SDL_CALL SDL_WM_SetCaption(caption, caption);
423
424 GameSizeChanged();
425
426 return true;
427 }
428
429 bool VideoDriver_SDL::ClaimMousePointer()
430 {
431 SDL_CALL SDL_ShowCursor(0);
432 return true;
433 }
434
435 struct VkMapping {
436 #if SDL_VERSION_ATLEAST(1, 3, 0)
437 SDL_Keycode vk_from;
438 #else
439 uint16 vk_from;
440 #endif
441 byte vk_count;
442 byte map_to;
443 };
444
445 #define AS(x, z) {x, 0, z}
446 #define AM(x, y, z, w) {x, (byte)(y - x), z}
447
448 static const VkMapping _vk_mapping[] = {
449 /* Pageup stuff + up/down */
450 AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
451 AS(SDLK_UP, WKC_UP),
452 AS(SDLK_DOWN, WKC_DOWN),
453 AS(SDLK_LEFT, WKC_LEFT),
454 AS(SDLK_RIGHT, WKC_RIGHT),
455
456 AS(SDLK_HOME, WKC_HOME),
457 AS(SDLK_END, WKC_END),
458
459 AS(SDLK_INSERT, WKC_INSERT),
460 AS(SDLK_DELETE, WKC_DELETE),
461
462 /* Map letters & digits */
463 AM(SDLK_a, SDLK_z, 'A', 'Z'),
464 AM(SDLK_0, SDLK_9, '0', '9'),
465
466 AS(SDLK_ESCAPE, WKC_ESC),
467 AS(SDLK_PAUSE, WKC_PAUSE),
468 AS(SDLK_BACKSPACE, WKC_BACKSPACE),
469
470 AS(SDLK_SPACE, WKC_SPACE),
471 AS(SDLK_RETURN, WKC_RETURN),
472 AS(SDLK_TAB, WKC_TAB),
473
474 /* Function keys */
475 AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
476
477 /* Numeric part. */
478 AM(SDLK_KP0, SDLK_KP9, '0', '9'),
479 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
480 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
481 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
482 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
483 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
484 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
485
486 /* Other non-letter keys */
487 AS(SDLK_SLASH, WKC_SLASH),
488 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
489 AS(SDLK_EQUALS, WKC_EQUALS),
490 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
491 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
492 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
493
494 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
495 AS(SDLK_COMMA, WKC_COMMA),
496 AS(SDLK_MINUS, WKC_MINUS),
497 AS(SDLK_PERIOD, WKC_PERIOD)
498 };
499
500 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
501 {
502 const VkMapping *map;
503 uint key = 0;
504
505 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
506 if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
507 key = sym->sym - map->vk_from + map->map_to;
508 break;
509 }
510 }
511
512 /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
513 #if defined(WIN32) || defined(__OS2__)
514 if (sym->scancode == 41) key = WKC_BACKQUOTE;
515 #elif defined(__APPLE__)
516 if (sym->scancode == 10) key = WKC_BACKQUOTE;
517 #elif defined(__MORPHOS__)
518 if (sym->scancode == 0) key = WKC_BACKQUOTE; // yes, that key is code '0' under MorphOS :)
519 #elif defined(__BEOS__)
520 if (sym->scancode == 17) key = WKC_BACKQUOTE;
521 #elif defined(__SVR4) && defined(__sun)
522 if (sym->scancode == 60) key = WKC_BACKQUOTE;
523 if (sym->scancode == 49) key = WKC_BACKSPACE;
524 #elif defined(__sgi__)
525 if (sym->scancode == 22) key = WKC_BACKQUOTE;
526 #else
527 if (sym->scancode == 49) key = WKC_BACKQUOTE;
528 #endif
529
530 /* META are the command keys on mac */
531 if (sym->mod & KMOD_META) key |= WKC_META;
532 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
533 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
534 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
535
536 *character = sym->unicode;
537 return key;
538 }
539
540 int VideoDriver_SDL::PollEvent()
541 {
542 SDL_Event ev;
543
544 if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
545
546 switch (ev.type) {
547 case SDL_MOUSEMOTION:
548 if (_cursor.fix_at) {
549 int dx = ev.motion.x - _cursor.pos.x;
550 int dy = ev.motion.y - _cursor.pos.y;
551 if (dx != 0 || dy != 0) {
552 _cursor.delta.x = dx;
553 _cursor.delta.y = dy;
554 SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
555 }
556 } else {
557 _cursor.delta.x = ev.motion.x - _cursor.pos.x;
558 _cursor.delta.y = ev.motion.y - _cursor.pos.y;
559 _cursor.pos.x = ev.motion.x;
560 _cursor.pos.y = ev.motion.y;
561 _cursor.dirty = true;
562 }
563 HandleMouseEvents();
564 break;
565
566 case SDL_MOUSEBUTTONDOWN:
567 if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
568 ev.button.button = SDL_BUTTON_RIGHT;
569 }
570
571 switch (ev.button.button) {
572 case SDL_BUTTON_LEFT:
573 _left_button_down = true;
574 break;
575
576 case SDL_BUTTON_RIGHT:
577 _right_button_down = true;
578 _right_button_clicked = true;
579 break;
580
581 case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
582 case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
583
584 default: break;
585 }
586 HandleMouseEvents();
587 break;
588
589 case SDL_MOUSEBUTTONUP:
590 if (_rightclick_emulate) {
591 _right_button_down = false;
592 _left_button_down = false;
593 _left_button_clicked = false;
594 } else if (ev.button.button == SDL_BUTTON_LEFT) {
595 _left_button_down = false;
596 _left_button_clicked = false;
597 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
598 _right_button_down = false;
599 }
600 HandleMouseEvents();
601 break;
602
603 case SDL_ACTIVEEVENT:
604 if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
605
606 if (ev.active.gain) { // mouse entered the window, enable cursor
607 _cursor.in_window = true;
608 } else {
609 UndrawMouseCursor(); // mouse left the window, undraw cursor
610 _cursor.in_window = false;
611 }
612 break;
613
614 case SDL_QUIT:
615 HandleExitGameRequest();
616 break;
617
618 case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
619 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
620 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
621 ToggleFullScreen(!_fullscreen);
622 } else {
623 WChar character;
624 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
625 HandleKeypress(keycode, character);
626 }
627 break;
628
629 case SDL_VIDEORESIZE: {
630 int w = max(ev.resize.w, 64);
631 int h = max(ev.resize.h, 64);
632 CreateMainSurface(w, h);
633 break;
634 }
635 case SDL_VIDEOEXPOSE: {
636 /* Force a redraw of the entire screen. Note
637 * that SDL 1.2 seems to do this automatically
638 * in most cases, but 1.3 / 2.0 does not. */
639 _num_dirty_rects = MAX_DIRTY_RECTS + 1;
640 break;
641 }
642 }
643 return -1;
644 }
645
646 const char *VideoDriver_SDL::Start(const char * const *parm)
647 {
648 char buf[30];
649 _use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
650
651 const char *s = SdlOpen(SDL_INIT_VIDEO);
652 if (s != NULL) return s;
653
654 GetVideoModes();
655 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
656 return SDL_CALL SDL_GetError();
657 }
658
659 SDL_CALL SDL_VideoDriverName(buf, sizeof buf);
660 DEBUG(driver, 1, "SDL: using driver '%s'", buf);
661
662 MarkWholeScreenDirty();
663 SetupKeyboard();
664
665 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
666
667 return NULL;
668 }
669
670 void VideoDriver_SDL::SetupKeyboard()
671 {
672 SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
673 SDL_CALL SDL_EnableUNICODE(1);
674 }
675
676 void VideoDriver_SDL::Stop()
677 {
678 SdlClose(SDL_INIT_VIDEO);
679 }
680
681 void VideoDriver_SDL::MainLoop()
682 {
683 uint32 cur_ticks = SDL_CALL SDL_GetTicks();
684 uint32 last_cur_ticks = cur_ticks;
685 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
686 uint32 mod;
687 int numkeys;
688 Uint8 *keys;
689
690 CheckPaletteAnim();
691
692 if (_draw_threaded) {
693 /* Initialise the mutex first, because that's the thing we *need*
694 * directly in the newly created thread. */
695 _draw_mutex = ThreadMutex::New();
696 if (_draw_mutex == NULL) {
697 _draw_threaded = false;
698 } else {
699 _draw_mutex->BeginCritical();
700 _draw_continue = true;
701
702 _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
703
704 /* Free the mutex if we won't be able to use it. */
705 if (!_draw_threaded) {
706 _draw_mutex->EndCritical();
707 delete _draw_mutex;
708 _draw_mutex = NULL;
709 } else {
710 /* Wait till the draw mutex has started itself. */
711 _draw_mutex->WaitForSignal();
712 }
713 }
714 }
715
716 DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
717
718 for (;;) {
719 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
720 InteractiveRandom(); // randomness
721
722 while (PollEvent() == -1) {}
723 if (_exit_game) break;
724
725 mod = SDL_CALL SDL_GetModState();
726 #if SDL_VERSION_ATLEAST(1, 3, 0)
727 keys = SDL_CALL SDL_GetKeyboardState(&numkeys);
728 #else
729 keys = SDL_CALL SDL_GetKeyState(&numkeys);
730 #endif
731 #if defined(_DEBUG)
732 if (_shift_pressed)
733 #else
734 /* Speedup when pressing tab, except when using ALT+TAB
735 * to switch to another application */
736 #if SDL_VERSION_ATLEAST(1, 3, 0)
737 if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
738 #else
739 if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
740 #endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
741 #endif /* defined(_DEBUG) */
742 {
743 if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
744 } else if (_fast_forward & 2) {
745 _fast_forward = 0;
746 }
747
748 cur_ticks = SDL_CALL SDL_GetTicks();
749 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
750 _realtime_tick += cur_ticks - last_cur_ticks;
751 last_cur_ticks = cur_ticks;
752 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
753
754 bool old_ctrl_pressed = _ctrl_pressed;
755
756 _ctrl_pressed = !!(mod & KMOD_CTRL);
757 _shift_pressed = !!(mod & KMOD_SHIFT);
758
759 /* determine which directional keys are down */
760 _dirkeys =
761 #if SDL_VERSION_ATLEAST(1, 3, 0)
762 (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
763 (keys[SDL_SCANCODE_UP] ? 2 : 0) |
764 (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
765 (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
766 #else
767 (keys[SDLK_LEFT] ? 1 : 0) |
768 (keys[SDLK_UP] ? 2 : 0) |
769 (keys[SDLK_RIGHT] ? 4 : 0) |
770 (keys[SDLK_DOWN] ? 8 : 0);
771 #endif
772 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
773
774 /* The gameloop is the part that can run asynchronously. The rest
775 * except sleeping can't. */
776 if (_draw_mutex != NULL) _draw_mutex->EndCritical();
777
778 GameLoop();
779
780 if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
781
782 UpdateWindows();
783 _local_palette = _cur_palette;
784 } else {
785 /* Release the thread while sleeping */
786 if (_draw_mutex != NULL) _draw_mutex->EndCritical();
787 CSleep(1);
788 if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
789
790 NetworkDrawChatMessage();
791 DrawMouseCursor();
792 }
793
794 /* End of the critical part. */
795 if (_draw_mutex != NULL && !HasModalProgress()) {
796 _draw_mutex->SendSignal();
797 } else {
798 /* Oh, we didn't have threads, then just draw unthreaded */
799 CheckPaletteAnim();
800 DrawSurfaceToScreen();
801 }
802 }
803
804 if (_draw_mutex != NULL) {
805 _draw_continue = false;
806 /* Sending signal if there is no thread blocked
807 * is very valid and results in noop */
808 _draw_mutex->SendSignal();
809 _draw_mutex->EndCritical();
810 _draw_thread->Join();
811
812 delete _draw_mutex;
813 delete _draw_thread;
814
815 _draw_mutex = NULL;
816 _draw_thread = NULL;
817 }
818 }
819
820 bool VideoDriver_SDL::ChangeResolution(int w, int h)
821 {
822 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
823 bool ret = CreateMainSurface(w, h);
824 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
825 return ret;
826 }
827
828 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
829 {
830 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
831 _fullscreen = fullscreen;
832 GetVideoModes(); // get the list of available video modes
833 bool ret = _num_resolutions != 0 && CreateMainSurface(_cur_resolution.width, _cur_resolution.height);
834
835 if (!ret) {
836 /* switching resolution failed, put back full_screen to original status */
837 _fullscreen ^= true;
838 }
839
840 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
841 return ret;
842 }
843
844 bool VideoDriver_SDL::AfterBlitterChange()
845 {
846 if (_draw_mutex != NULL) _draw_mutex->BeginCritical(true);
847 bool ret = CreateMainSurface(_screen.width, _screen.height);
848 if (_draw_mutex != NULL) _draw_mutex->EndCritical(true);
849 return ret;
850 }
851
852 #endif /* WITH_SDL */