sdl_v.cpp

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