# sdl_v.cpp

Go to the documentation of this file.

    00001 /* $Id: sdl_v.cpp 18809 2010-01-15 16:41:15Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifdef WITH_SDL
    00013
    00014 #include "../stdafx.h"
    00015 #include "../openttd.h"
    00016 #include "../gfx_func.h"
    00017 #include "../sdl.h"
    00018 #include "../variables.h"
    00019 #include "../rev.h"
    00020 #include "../blitter/factory.hpp"
    00021 #include "../network/network.h"
    00022 #include "../functions.h"
    00023 #include "../thread/thread.h"
    00024 #include "../genworld.h"
    00025 #include "../core/random_func.hpp"
    00026 #include "sdl_v.h"
    00027 #include <SDL.h>
    00028
    00029 static FVideoDriver_SDL iFVideoDriver_SDL;
    00030
    00031 static SDL_Surface *_sdl_screen;
    00032 static bool _all_modes;
    00033
    00035 static bool _draw_threaded;
    00037 static ThreadObject *_draw_thread = NULL;
    00039 static ThreadMutex *_draw_mutex = NULL;
    00041 static volatile bool _draw_continue;
    00042
    00043 #define MAX_DIRTY_RECTS 100
    00044 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
    00045 static int _num_dirty_rects;
    00046
    00047 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
    00048 {
    00049   if (_num_dirty_rects < MAX_DIRTY_RECTS) {
    00050     _dirty_rects[_num_dirty_rects].x = left;
    00051     _dirty_rects[_num_dirty_rects].y = top;
    00052     _dirty_rects[_num_dirty_rects].w = width;
    00053     _dirty_rects[_num_dirty_rects].h = height;
    00054   }
    00055   _num_dirty_rects++;
    00056 }
    00057
    00058 static void UpdatePalette(uint start, uint count)
    00059 {
    00060   SDL_Color pal[256];
    00061
    00062   for (uint i = 0; i != count; i++) {
    00063     pal[i].r = _cur_palette[start + i].r;
    00064     pal[i].g = _cur_palette[start + i].g;
    00065     pal[i].b = _cur_palette[start + i].b;
    00066     pal[i].unused = 0;
    00067   }
    00068
    00069   SDL_CALL SDL_SetColors(_sdl_screen, pal, start, count);
    00070 }
    00071
    00072 static void InitPalette()
    00073 {
    00074   UpdatePalette(0, 256);
    00075 }
    00076
    00077 static void CheckPaletteAnim()
    00078 {
    00079   if (_pal_count_dirty != 0) {
    00080     Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
    00081
    00082     switch (blitter->UsePaletteAnimation()) {
    00083       case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
    00084         UpdatePalette(_pal_first_dirty, _pal_count_dirty);
    00085         break;
    00086
    00087       case Blitter::PALETTE_ANIMATION_BLITTER:
    00088         blitter->PaletteAnimate(_pal_first_dirty, _pal_count_dirty);
    00089         break;
    00090
    00091       case Blitter::PALETTE_ANIMATION_NONE:
    00092         break;
    00093
    00094       default:
    00095         NOT_REACHED();
    00096     }
    00097     _pal_count_dirty = 0;
    00098   }
    00099 }
    00100
    00101 static void DrawSurfaceToScreen()
    00102 {
    00103   int n = _num_dirty_rects;
    00104   if (n == 0) return;
    00105
    00106   _num_dirty_rects = 0;
    00107   if (n > MAX_DIRTY_RECTS) {
    00108     SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0);
    00109   } else {
    00110     SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects);
    00111   }
    00112 }
    00113
    00114 static void DrawSurfaceToScreenThread(void *)
    00115 {
    00116   /* First tell the main thread we're started */
    00117   _draw_mutex->BeginCritical();
    00118   _draw_mutex->SendSignal();
    00119
    00120   /* Now wait for the first thing to draw! */
    00121   _draw_mutex->WaitForSignal();
    00122
    00123   while (_draw_continue) {
    00124     /* Then just draw and wait till we stop */
    00125     DrawSurfaceToScreen();
    00126     _draw_mutex->WaitForSignal();
    00127   }
    00128
    00129   _draw_mutex->EndCritical();
    00130   _draw_thread->Exit();
    00131 }
    00132
    00133 static const Dimension _default_resolutions[] = {
    00134   { 640,  480},
    00135   { 800,  600},
    00136   {1024,  768},
    00137   {1152,  864},
    00138   {1280,  800},
    00139   {1280,  960},
    00140   {1280, 1024},
    00141   {1400, 1050},
    00142   {1600, 1200},
    00143   {1680, 1050},
    00144   {1920, 1200}
    00145 };
    00146
    00147 static void GetVideoModes()
    00148 {
    00149   SDL_Rect **modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | SDL_FULLSCREEN);
    00150   if (modes == NULL) usererror("sdl: no modes available");
    00151
    00152   _all_modes = (SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
    00153   if (modes == (void*)-1) {
    00154     int n = 0;
    00155     for (uint i = 0; i < lengthof(_default_resolutions); i++) {
    00156       if (SDL_CALL SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
    00157         _resolutions[n] = _default_resolutions[i];
    00158         if (++n == lengthof(_resolutions)) break;
    00159       }
    00160     }
    00161     _num_resolutions = n;
    00162   } else {
    00163     int n = 0;
    00164     for (int i = 0; modes[i]; i++) {
    00165       uint w = modes[i]->w;
    00166       uint h = modes[i]->h;
    00167       int j;
    00168       for (j = 0; j < n; j++) {
    00169         if (_resolutions[j].width == w && _resolutions[j].height == h) break;
    00170       }
    00171
    00172       if (j == n) {
    00173         _resolutions[j].width  = w;
    00174         _resolutions[j].height = h;
    00175         if (++n == lengthof(_resolutions)) break;
    00176       }
    00177     }
    00178     _num_resolutions = n;
    00179     SortResolutions(_num_resolutions);
    00180   }
    00181 }
    00182
    00183 static void GetAvailableVideoMode(uint *w, uint *h)
    00184 {
    00185   /* All modes available? */
    00186   if (_all_modes || _num_resolutions == 0) return;
    00187
    00188   /* Is the wanted mode among the available modes? */
    00189   for (int i = 0; i != _num_resolutions; i++) {
    00190     if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
    00191   }
    00192
    00193   /* Use the closest possible resolution */
    00194   int best = 0;
    00195   uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
    00196   for (int i = 1; i != _num_resolutions; ++i) {
    00197     uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
    00198     if (newdelta < delta) {
    00199       best = i;
    00200       delta = newdelta;
    00201     }
    00202   }
    00203   *w = _resolutions[best].width;
    00204   *h = _resolutions[best].height;
    00205 }
    00206
    00207 #ifndef ICON_DIR
    00208 #define ICON_DIR "media"
    00209 #endif
    00210
    00211 #ifdef WIN32
    00212 /* Let's redefine the LoadBMP macro with because we are dynamically
    00213  * loading SDL and need to 'SDL_CALL' all functions */
    00214 #undef SDL_LoadBMP
    00215 #define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_CALL SDL_RWFromFile(file, "rb"), 1)
    00216 #endif
    00217
    00218 static bool CreateMainSurface(uint w, uint h)
    00219 {
    00220   SDL_Surface *newscreen, *icon;
    00221   char caption[50];
    00222   int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
    00223
    00224   GetAvailableVideoMode(&w, &h);
    00225
    00226   DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
    00227
    00228   if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
    00229
    00230   /* Give the application an icon */
    00231   icon = SDL_CALL SDL_LoadBMP(ICON_DIR PATHSEP "openttd.32.bmp");
    00232   if (icon != NULL) {
    00233     /* Get the colourkey, which will be magenta */
    00234     uint32 rgbmap = SDL_CALL SDL_MapRGB(icon->format, 255, 0, 255);
    00235
    00236     SDL_CALL SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
    00237     SDL_CALL SDL_WM_SetIcon(icon, NULL);
    00238     SDL_CALL SDL_FreeSurface(icon);
    00239   }
    00240
    00241   /* DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK */
    00242   newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
    00243   if (newscreen == NULL) {
    00244     DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
    00245     return false;
    00246   }
    00247
    00248   /* Delay drawing for this cycle; the next cycle will redraw the whole screen */
    00249   _num_dirty_rects = 0;
    00250
    00251   _screen.width = newscreen->w;
    00252   _screen.height = newscreen->h;
    00253   _screen.pitch = newscreen->pitch / (bpp / 8);
    00254   _screen.dst_ptr = newscreen->pixels;
    00255   _sdl_screen = newscreen;
    00256
    00257   BlitterFactoryBase::GetCurrentBlitter()->PostResize();
    00258
    00259   InitPalette();
    00260
    00261   snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
    00262   SDL_CALL SDL_WM_SetCaption(caption, caption);
    00263   SDL_CALL SDL_ShowCursor(0);
    00264
    00265   GameSizeChanged();
    00266
    00267   return true;
    00268 }
    00269
    00270 struct VkMapping {
    00271   uint16 vk_from;
    00272   byte vk_count;
    00273   byte map_to;
    00274 };
    00275
    00276 #define AS(x, z) {x, 0, z}
    00277 #define AM(x, y, z, w) {x, y - x, z}
    00278
    00279 static const VkMapping _vk_mapping[] = {
    00280   /* Pageup stuff + up/down */
    00281   AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
    00282   AS(SDLK_UP,     WKC_UP),
    00283   AS(SDLK_DOWN,   WKC_DOWN),
    00284   AS(SDLK_LEFT,   WKC_LEFT),
    00285   AS(SDLK_RIGHT,  WKC_RIGHT),
    00286
    00287   AS(SDLK_HOME,   WKC_HOME),
    00288   AS(SDLK_END,    WKC_END),
    00289
    00290   AS(SDLK_INSERT, WKC_INSERT),
    00291   AS(SDLK_DELETE, WKC_DELETE),
    00292
    00293   /* Map letters & digits */
    00294   AM(SDLK_a, SDLK_z, 'A', 'Z'),
    00295   AM(SDLK_0, SDLK_9, '0', '9'),
    00296
    00297   AS(SDLK_ESCAPE,    WKC_ESC),
    00298   AS(SDLK_PAUSE,     WKC_PAUSE),
    00299   AS(SDLK_BACKSPACE, WKC_BACKSPACE),
    00300
    00301   AS(SDLK_SPACE,     WKC_SPACE),
    00302   AS(SDLK_RETURN,    WKC_RETURN),
    00303   AS(SDLK_TAB,       WKC_TAB),
    00304
    00305   /* Function keys */
    00306   AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
    00307
    00308   /* Numeric part. */
    00309   AM(SDLK_KP0, SDLK_KP9, '0', '9'),
    00310   AS(SDLK_KP_DIVIDE,   WKC_NUM_DIV),
    00311   AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
    00312   AS(SDLK_KP_MINUS,    WKC_NUM_MINUS),
    00313   AS(SDLK_KP_PLUS,     WKC_NUM_PLUS),
    00314   AS(SDLK_KP_ENTER,    WKC_NUM_ENTER),
    00315   AS(SDLK_KP_PERIOD,   WKC_NUM_DECIMAL),
    00316
    00317   /* Other non-letter keys */
    00318   AS(SDLK_SLASH,        WKC_SLASH),
    00319   AS(SDLK_SEMICOLON,    WKC_SEMICOLON),
    00320   AS(SDLK_EQUALS,       WKC_EQUALS),
    00321   AS(SDLK_LEFTBRACKET,  WKC_L_BRACKET),
    00322   AS(SDLK_BACKSLASH,    WKC_BACKSLASH),
    00323   AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
    00324
    00325   AS(SDLK_QUOTE,   WKC_SINGLEQUOTE),
    00326   AS(SDLK_COMMA,   WKC_COMMA),
    00327   AS(SDLK_MINUS,   WKC_MINUS),
    00328   AS(SDLK_PERIOD,  WKC_PERIOD)
    00329 };
    00330
    00331 static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
    00332 {
    00333   const VkMapping *map;
    00334   uint key = 0;
    00335
    00336   for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
    00337     if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
    00338       key = sym->sym - map->vk_from + map->map_to;
    00339       break;
    00340     }
    00341   }
    00342
    00343   /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
    00344 #if defined(WIN32) || defined(__OS2__)
    00345   if (sym->scancode == 41) key = WKC_BACKQUOTE;
    00346 #elif defined(__APPLE__)
    00347   if (sym->scancode == 10) key = WKC_BACKQUOTE;
    00348 #elif defined(__MORPHOS__)
    00349   if (sym->scancode == 0)  key = WKC_BACKQUOTE;  // yes, that key is code '0' under MorphOS :)
    00350 #elif defined(__BEOS__)
    00351   if (sym->scancode == 17) key = WKC_BACKQUOTE;
    00352 #elif defined(__SVR4) && defined(__sun)
    00353   if (sym->scancode == 60) key = WKC_BACKQUOTE;
    00354   if (sym->scancode == 49) key = WKC_BACKSPACE;
    00355 #elif defined(__sgi__)
    00356   if (sym->scancode == 22) key = WKC_BACKQUOTE;
    00357 #else
    00358   if (sym->scancode == 49) key = WKC_BACKQUOTE;
    00359 #endif
    00360
    00361   /* META are the command keys on mac */
    00362   if (sym->mod & KMOD_META)  key |= WKC_META;
    00363   if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
    00364   if (sym->mod & KMOD_CTRL)  key |= WKC_CTRL;
    00365   if (sym->mod & KMOD_ALT)   key |= WKC_ALT;
    00366
    00367   return (key << 16) + sym->unicode;
    00368 }
    00369
    00370 static int PollEvent()
    00371 {
    00372   SDL_Event ev;
    00373
    00374   if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
    00375
    00376   switch (ev.type) {
    00377     case SDL_MOUSEMOTION:
    00378       if (_cursor.fix_at) {
    00379         int dx = ev.motion.x - _cursor.pos.x;
    00380         int dy = ev.motion.y - _cursor.pos.y;
    00381         if (dx != 0 || dy != 0) {
    00382           _cursor.delta.x = dx;
    00383           _cursor.delta.y = dy;
    00384           SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
    00385         }
    00386       } else {
    00387         _cursor.delta.x = ev.motion.x - _cursor.pos.x;
    00388         _cursor.delta.y = ev.motion.y - _cursor.pos.y;
    00389         _cursor.pos.x = ev.motion.x;
    00390         _cursor.pos.y = ev.motion.y;
    00391         _cursor.dirty = true;
    00392       }
    00393       HandleMouseEvents();
    00394       break;
    00395
    00396     case SDL_MOUSEBUTTONDOWN:
    00397       if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
    00398         ev.button.button = SDL_BUTTON_RIGHT;
    00399       }
    00400
    00401       switch (ev.button.button) {
    00402         case SDL_BUTTON_LEFT:
    00403           _left_button_down = true;
    00404           break;
    00405
    00406         case SDL_BUTTON_RIGHT:
    00407           _right_button_down = true;
    00408           _right_button_clicked = true;
    00409           break;
    00410
    00411         case SDL_BUTTON_WHEELUP:   _cursor.wheel--; break;
    00412         case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
    00413
    00414         default: break;
    00415       }
    00416       HandleMouseEvents();
    00417       break;
    00418
    00419     case SDL_MOUSEBUTTONUP:
    00420       if (_rightclick_emulate) {
    00421         _right_button_down = false;
    00422         _left_button_down = false;
    00423         _left_button_clicked = false;
    00424       } else if (ev.button.button == SDL_BUTTON_LEFT) {
    00425         _left_button_down = false;
    00426         _left_button_clicked = false;
    00427       } else if (ev.button.button == SDL_BUTTON_RIGHT) {
    00428         _right_button_down = false;
    00429       }
    00430       HandleMouseEvents();
    00431       break;
    00432
    00433     case SDL_ACTIVEEVENT:
    00434       if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
    00435
    00436       if (ev.active.gain) { // mouse entered the window, enable cursor
    00437         _cursor.in_window = true;
    00438       } else {
    00439         UndrawMouseCursor(); // mouse left the window, undraw cursor
    00440         _cursor.in_window = false;
    00441       }
    00442       break;
    00443
    00444     case SDL_QUIT:
    00445       HandleExitGameRequest();
    00446       break;
    00447
    00448     case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
    00449       if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
    00450           (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
    00451         ToggleFullScreen(!_fullscreen);
    00452       } else {
    00453         HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym));
    00454       }
    00455       break;
    00456
    00457     case SDL_VIDEORESIZE: {
    00458       int w = max(ev.resize.w, 64);
    00459       int h = max(ev.resize.h, 64);
    00460       CreateMainSurface(w, h);
    00461       break;
    00462     }
    00463   }
    00464   return -1;
    00465 }
    00466
    00467 const char *VideoDriver_SDL::Start(const char * const *parm)
    00468 {
    00469   char buf[30];
    00470
    00471   const char *s = SdlOpen(SDL_INIT_VIDEO);
    00472   if (s != NULL) return s;
    00473
    00474   GetVideoModes();
    00475   if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
    00476     return SDL_CALL SDL_GetError();
    00477   }
    00478
    00479   SDL_CALL SDL_VideoDriverName(buf, 30);
    00480   DEBUG(driver, 1, "SDL: using driver '%s'", buf);
    00481
    00482   MarkWholeScreenDirty();
    00483
    00484   SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
    00485   SDL_CALL SDL_EnableUNICODE(1);
    00486
    00487   _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
    00488
    00489   return NULL;
    00490 }
    00491
    00492 void VideoDriver_SDL::Stop()
    00493 {
    00494   SdlClose(SDL_INIT_VIDEO);
    00495 }
    00496
    00497 void VideoDriver_SDL::MainLoop()
    00498 {
    00499   uint32 cur_ticks = SDL_CALL SDL_GetTicks();
    00500   uint32 last_cur_ticks = cur_ticks;
    00501   uint32 next_tick = cur_ticks + 30;
    00502   uint32 pal_tick = 0;
    00503   uint32 mod;
    00504   int numkeys;
    00505   Uint8 *keys;
    00506
    00507   if (_draw_threaded) {
    00508     /* Initialise the mutex first, because that's the thing we *need*
    00509      * directly in the newly created thread. */
    00510     _draw_mutex = ThreadMutex::New();
    00511     if (_draw_mutex == NULL) {
    00512       _draw_threaded = false;
    00513     } else {
    00514       _draw_mutex->BeginCritical();
    00515       _draw_continue = true;
    00516
    00517       _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
    00518
    00519       /* Free the mutex if we won't be able to use it. */
    00520       if (!_draw_threaded) {
    00521         _draw_mutex->EndCritical();
    00522         delete _draw_mutex;
    00523       } else {
    00524         /* Wait till the draw mutex has started itself. */
    00525         _draw_mutex->WaitForSignal();
    00526       }
    00527     }
    00528   }
    00529
    00530   DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
    00531
    00532   for (;;) {
    00533     uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
    00534     InteractiveRandom(); // randomness
    00535
    00536     while (PollEvent() == -1) {}
    00537     if (_exit_game) break;
    00538
    00539     mod = SDL_CALL SDL_GetModState();
    00540     keys = SDL_CALL SDL_GetKeyState(&numkeys);
    00541 #if defined(_DEBUG)
    00542     if (_shift_pressed)
    00543 #else
    00544     /* Speedup when pressing tab, except when using ALT+TAB
    00545      * to switch to another application */
    00546     if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
    00547 #endif
    00548     {
    00549       if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
    00550     } else if (_fast_forward & 2) {
    00551       _fast_forward = 0;
    00552     }
    00553
    00554     cur_ticks = SDL_CALL SDL_GetTicks();
    00555     if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
    00556       _realtime_tick += cur_ticks - last_cur_ticks;
    00557       last_cur_ticks = cur_ticks;
    00558       next_tick = cur_ticks + 30;
    00559
    00560       bool old_ctrl_pressed = _ctrl_pressed;
    00561
    00562       _ctrl_pressed  = !!(mod & KMOD_CTRL);
    00563       _shift_pressed = !!(mod & KMOD_SHIFT);
    00564
    00565       /* determine which directional keys are down */
    00566       _dirkeys =
    00567         (keys[SDLK_LEFT]  ? 1 : 0) |
    00568         (keys[SDLK_UP]    ? 2 : 0) |
    00569         (keys[SDLK_RIGHT] ? 4 : 0) |
    00570         (keys[SDLK_DOWN]  ? 8 : 0);
    00571
    00572       if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
    00573
    00574       /* The gameloop is the part that can run asynchroniously. The rest
    00575        * except sleeping can't. */
    00576       if (_draw_threaded) _draw_mutex->EndCritical();
    00577
    00578       GameLoop();
    00579
    00580       if (_draw_threaded) _draw_mutex->BeginCritical();
    00581
    00582       UpdateWindows();
    00583       if (++pal_tick > 4) {
    00584         CheckPaletteAnim();
    00585         pal_tick = 1;
    00586       }
    00587     } else {
    00588       /* Release the thread while sleeping */
    00589       if (_draw_threaded) _draw_mutex->EndCritical();
    00590       CSleep(1);
    00591       if (_draw_threaded) _draw_mutex->BeginCritical();
    00592
    00593       NetworkDrawChatMessage();
    00594       DrawMouseCursor();
    00595     }
    00596
    00597     /* End of the critical part. */
    00598     if (_draw_threaded && !IsGeneratingWorld()) {
    00599       _draw_mutex->SendSignal();
    00600     } else {
    00601       /* Oh, we didn't have threads, then just draw unthreaded */
    00602       DrawSurfaceToScreen();
    00603     }
    00604   }
    00605
    00606   if (_draw_threaded) {
    00607     _draw_continue = false;
    00608     /* Sending signal if there is no thread blocked
    00609      * is very valid and results in noop */
    00610     _draw_mutex->SendSignal();
    00611     _draw_mutex->EndCritical();
    00612     _draw_thread->Join();
    00613
    00614     delete _draw_mutex;
    00615     delete _draw_thread;
    00616   }
    00617 }
    00618
    00619 bool VideoDriver_SDL::ChangeResolution(int w, int h)
    00620 {
    00621   if (_draw_threaded) _draw_mutex->BeginCritical();
    00622   bool ret = CreateMainSurface(w, h);
    00623   if (_draw_threaded) _draw_mutex->EndCritical();
    00624   return ret;
    00625 }
    00626
    00627 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
    00628 {
    00629   _fullscreen = fullscreen;
    00630   GetVideoModes(); // get the list of available video modes
    00631   if (_num_resolutions == 0 || !CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
    00632     /* switching resolution failed, put back full_screen to original status */
    00633     _fullscreen ^= true;
    00634     return false;
    00635   }
    00636   return true;
    00637 }
    00638
    00639 #endif /* WITH_SDL */