sdl_v.cpp

Go to the documentation of this file.
00001 /* $Id: sdl_v.cpp 23980 2012-02-23 21:10:08Z 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 "../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   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00262   blitter->PostResize();
00263 
00264   InitPalette();
00265   switch (blitter->UsePaletteAnimation()) {
00266     case Blitter::PALETTE_ANIMATION_NONE:
00267     case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00268       UpdatePalette();
00269       break;
00270 
00271     case Blitter::PALETTE_ANIMATION_BLITTER:
00272       if (_video_driver != NULL) blitter->PaletteAnimate(_local_palette);
00273       break;
00274 
00275     default:
00276       NOT_REACHED();
00277   }
00278 
00279   snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
00280   SDL_CALL SDL_WM_SetCaption(caption, caption);
00281 
00282   GameSizeChanged();
00283 
00284   return true;
00285 }
00286 
00287 bool VideoDriver_SDL::ClaimMousePointer()
00288 {
00289   SDL_CALL SDL_ShowCursor(0);
00290   return true;
00291 }
00292 
00293 struct VkMapping {
00294 #if SDL_VERSION_ATLEAST(1, 3, 0)
00295   SDL_Keycode vk_from;
00296 #else
00297   uint16 vk_from;
00298 #endif
00299   byte vk_count;
00300   byte map_to;
00301 };
00302 
00303 #define AS(x, z) {x, 0, z}
00304 #define AM(x, y, z, w) {x, (byte)(y - x), z}
00305 
00306 static const VkMapping _vk_mapping[] = {
00307   /* Pageup stuff + up/down */
00308   AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
00309   AS(SDLK_UP,     WKC_UP),
00310   AS(SDLK_DOWN,   WKC_DOWN),
00311   AS(SDLK_LEFT,   WKC_LEFT),
00312   AS(SDLK_RIGHT,  WKC_RIGHT),
00313 
00314   AS(SDLK_HOME,   WKC_HOME),
00315   AS(SDLK_END,    WKC_END),
00316 
00317   AS(SDLK_INSERT, WKC_INSERT),
00318   AS(SDLK_DELETE, WKC_DELETE),
00319 
00320   /* Map letters & digits */
00321   AM(SDLK_a, SDLK_z, 'A', 'Z'),
00322   AM(SDLK_0, SDLK_9, '0', '9'),
00323 
00324   AS(SDLK_ESCAPE,    WKC_ESC),
00325   AS(SDLK_PAUSE,     WKC_PAUSE),
00326   AS(SDLK_BACKSPACE, WKC_BACKSPACE),
00327 
00328   AS(SDLK_SPACE,     WKC_SPACE),
00329   AS(SDLK_RETURN,    WKC_RETURN),
00330   AS(SDLK_TAB,       WKC_TAB),
00331 
00332   /* Function keys */
00333   AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
00334 
00335   /* Numeric part. */
00336   AM(SDLK_KP0, SDLK_KP9, '0', '9'),
00337   AS(SDLK_KP_DIVIDE,   WKC_NUM_DIV),
00338   AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
00339   AS(SDLK_KP_MINUS,    WKC_NUM_MINUS),
00340   AS(SDLK_KP_PLUS,     WKC_NUM_PLUS),
00341   AS(SDLK_KP_ENTER,    WKC_NUM_ENTER),
00342   AS(SDLK_KP_PERIOD,   WKC_NUM_DECIMAL),
00343 
00344   /* Other non-letter keys */
00345   AS(SDLK_SLASH,        WKC_SLASH),
00346   AS(SDLK_SEMICOLON,    WKC_SEMICOLON),
00347   AS(SDLK_EQUALS,       WKC_EQUALS),
00348   AS(SDLK_LEFTBRACKET,  WKC_L_BRACKET),
00349   AS(SDLK_BACKSLASH,    WKC_BACKSLASH),
00350   AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
00351 
00352   AS(SDLK_QUOTE,   WKC_SINGLEQUOTE),
00353   AS(SDLK_COMMA,   WKC_COMMA),
00354   AS(SDLK_MINUS,   WKC_MINUS),
00355   AS(SDLK_PERIOD,  WKC_PERIOD)
00356 };
00357 
00358 static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
00359 {
00360   const VkMapping *map;
00361   uint key = 0;
00362 
00363   for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00364     if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
00365       key = sym->sym - map->vk_from + map->map_to;
00366       break;
00367     }
00368   }
00369 
00370   /* check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) */
00371 #if defined(WIN32) || defined(__OS2__)
00372   if (sym->scancode == 41) key = WKC_BACKQUOTE;
00373 #elif defined(__APPLE__)
00374   if (sym->scancode == 10) key = WKC_BACKQUOTE;
00375 #elif defined(__MORPHOS__)
00376   if (sym->scancode == 0)  key = WKC_BACKQUOTE;  // yes, that key is code '0' under MorphOS :)
00377 #elif defined(__BEOS__)
00378   if (sym->scancode == 17) key = WKC_BACKQUOTE;
00379 #elif defined(__SVR4) && defined(__sun)
00380   if (sym->scancode == 60) key = WKC_BACKQUOTE;
00381   if (sym->scancode == 49) key = WKC_BACKSPACE;
00382 #elif defined(__sgi__)
00383   if (sym->scancode == 22) key = WKC_BACKQUOTE;
00384 #else
00385   if (sym->scancode == 49) key = WKC_BACKQUOTE;
00386 #endif
00387 
00388   /* META are the command keys on mac */
00389   if (sym->mod & KMOD_META)  key |= WKC_META;
00390   if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
00391   if (sym->mod & KMOD_CTRL)  key |= WKC_CTRL;
00392   if (sym->mod & KMOD_ALT)   key |= WKC_ALT;
00393 
00394   return (key << 16) + sym->unicode;
00395 }
00396 
00397 static int PollEvent()
00398 {
00399   SDL_Event ev;
00400 
00401   if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
00402 
00403   switch (ev.type) {
00404     case SDL_MOUSEMOTION:
00405       if (_cursor.fix_at) {
00406         int dx = ev.motion.x - _cursor.pos.x;
00407         int dy = ev.motion.y - _cursor.pos.y;
00408         if (dx != 0 || dy != 0) {
00409           _cursor.delta.x = dx;
00410           _cursor.delta.y = dy;
00411           SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
00412         }
00413       } else {
00414         _cursor.delta.x = ev.motion.x - _cursor.pos.x;
00415         _cursor.delta.y = ev.motion.y - _cursor.pos.y;
00416         _cursor.pos.x = ev.motion.x;
00417         _cursor.pos.y = ev.motion.y;
00418         _cursor.dirty = true;
00419       }
00420       HandleMouseEvents();
00421       break;
00422 
00423     case SDL_MOUSEBUTTONDOWN:
00424       if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
00425         ev.button.button = SDL_BUTTON_RIGHT;
00426       }
00427 
00428       switch (ev.button.button) {
00429         case SDL_BUTTON_LEFT:
00430           _left_button_down = true;
00431           break;
00432 
00433         case SDL_BUTTON_RIGHT:
00434           _right_button_down = true;
00435           _right_button_clicked = true;
00436           break;
00437 
00438         case SDL_BUTTON_WHEELUP:   _cursor.wheel--; break;
00439         case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
00440 
00441         default: break;
00442       }
00443       HandleMouseEvents();
00444       break;
00445 
00446     case SDL_MOUSEBUTTONUP:
00447       if (_rightclick_emulate) {
00448         _right_button_down = false;
00449         _left_button_down = false;
00450         _left_button_clicked = false;
00451       } else if (ev.button.button == SDL_BUTTON_LEFT) {
00452         _left_button_down = false;
00453         _left_button_clicked = false;
00454       } else if (ev.button.button == SDL_BUTTON_RIGHT) {
00455         _right_button_down = false;
00456       }
00457       HandleMouseEvents();
00458       break;
00459 
00460     case SDL_ACTIVEEVENT:
00461       if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
00462 
00463       if (ev.active.gain) { // mouse entered the window, enable cursor
00464         _cursor.in_window = true;
00465       } else {
00466         UndrawMouseCursor(); // mouse left the window, undraw cursor
00467         _cursor.in_window = false;
00468       }
00469       break;
00470 
00471     case SDL_QUIT:
00472       HandleExitGameRequest();
00473       break;
00474 
00475     case SDL_KEYDOWN: // Toggle full-screen on ALT + ENTER/F
00476       if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
00477           (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
00478         ToggleFullScreen(!_fullscreen);
00479       } else {
00480         HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym));
00481       }
00482       break;
00483 
00484     case SDL_VIDEORESIZE: {
00485       int w = max(ev.resize.w, 64);
00486       int h = max(ev.resize.h, 64);
00487       CreateMainSurface(w, h);
00488       break;
00489     }
00490     case SDL_VIDEOEXPOSE: {
00491       /* Force a redraw of the entire screen. Note
00492        * that SDL 1.2 seems to do this automatically
00493        * in most cases, but 1.3 / 2.0 does not. */
00494             _num_dirty_rects = MAX_DIRTY_RECTS + 1;
00495       break;
00496     }
00497   }
00498   return -1;
00499 }
00500 
00501 const char *VideoDriver_SDL::Start(const char * const *parm)
00502 {
00503   char buf[30];
00504 
00505   const char *s = SdlOpen(SDL_INIT_VIDEO);
00506   if (s != NULL) return s;
00507 
00508   GetVideoModes();
00509   if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00510     return SDL_CALL SDL_GetError();
00511   }
00512 
00513   SDL_CALL SDL_VideoDriverName(buf, sizeof buf);
00514   DEBUG(driver, 1, "SDL: using driver '%s'", buf);
00515 
00516   MarkWholeScreenDirty();
00517 
00518   SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00519   SDL_CALL SDL_EnableUNICODE(1);
00520 
00521   _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
00522 
00523   return NULL;
00524 }
00525 
00526 void VideoDriver_SDL::Stop()
00527 {
00528   SdlClose(SDL_INIT_VIDEO);
00529 }
00530 
00531 void VideoDriver_SDL::MainLoop()
00532 {
00533   uint32 cur_ticks = SDL_CALL SDL_GetTicks();
00534   uint32 last_cur_ticks = cur_ticks;
00535   uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00536   uint32 mod;
00537   int numkeys;
00538   Uint8 *keys;
00539 
00540   CheckPaletteAnim();
00541 
00542   if (_draw_threaded) {
00543     /* Initialise the mutex first, because that's the thing we *need*
00544      * directly in the newly created thread. */
00545     _draw_mutex = ThreadMutex::New();
00546     if (_draw_mutex == NULL) {
00547       _draw_threaded = false;
00548     } else {
00549       _draw_mutex->BeginCritical();
00550       _draw_continue = true;
00551 
00552       _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
00553 
00554       /* Free the mutex if we won't be able to use it. */
00555       if (!_draw_threaded) {
00556         _draw_mutex->EndCritical();
00557         delete _draw_mutex;
00558       } else {
00559         /* Wait till the draw mutex has started itself. */
00560         _draw_mutex->WaitForSignal();
00561       }
00562     }
00563   }
00564 
00565   DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
00566 
00567   for (;;) {
00568     uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
00569     InteractiveRandom(); // randomness
00570 
00571     while (PollEvent() == -1) {}
00572     if (_exit_game) break;
00573 
00574     mod = SDL_CALL SDL_GetModState();
00575 #if SDL_VERSION_ATLEAST(1, 3, 0)
00576     keys = SDL_CALL SDL_GetKeyboardState(&numkeys);
00577 #else
00578     keys = SDL_CALL SDL_GetKeyState(&numkeys);
00579 #endif
00580 #if defined(_DEBUG)
00581     if (_shift_pressed)
00582 #else
00583     /* Speedup when pressing tab, except when using ALT+TAB
00584      * to switch to another application */
00585 #if SDL_VERSION_ATLEAST(1, 3, 0)
00586     if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
00587 #else
00588     if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
00589 #endif /* SDL_VERSION_ATLEAST(1, 3, 0) */
00590 #endif /* defined(_DEBUG) */
00591     {
00592       if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
00593     } else if (_fast_forward & 2) {
00594       _fast_forward = 0;
00595     }
00596 
00597     cur_ticks = SDL_CALL SDL_GetTicks();
00598     if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00599       _realtime_tick += cur_ticks - last_cur_ticks;
00600       last_cur_ticks = cur_ticks;
00601       next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00602 
00603       bool old_ctrl_pressed = _ctrl_pressed;
00604 
00605       _ctrl_pressed  = !!(mod & KMOD_CTRL);
00606       _shift_pressed = !!(mod & KMOD_SHIFT);
00607 
00608       /* determine which directional keys are down */
00609       _dirkeys =
00610 #if SDL_VERSION_ATLEAST(1, 3, 0)
00611         (keys[SDL_SCANCODE_LEFT]  ? 1 : 0) |
00612         (keys[SDL_SCANCODE_UP]    ? 2 : 0) |
00613         (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
00614         (keys[SDL_SCANCODE_DOWN]  ? 8 : 0);
00615 #else
00616         (keys[SDLK_LEFT]  ? 1 : 0) |
00617         (keys[SDLK_UP]    ? 2 : 0) |
00618         (keys[SDLK_RIGHT] ? 4 : 0) |
00619         (keys[SDLK_DOWN]  ? 8 : 0);
00620 #endif
00621       if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
00622 
00623       /* The gameloop is the part that can run asynchroniously. The rest
00624        * except sleeping can't. */
00625       if (_draw_threaded) _draw_mutex->EndCritical();
00626 
00627       GameLoop();
00628 
00629       if (_draw_threaded) _draw_mutex->BeginCritical();
00630 
00631       UpdateWindows();
00632       _local_palette = _cur_palette;
00633     } else {
00634       /* Release the thread while sleeping */
00635       if (_draw_threaded) _draw_mutex->EndCritical();
00636       CSleep(1);
00637       if (_draw_threaded) _draw_mutex->BeginCritical();
00638 
00639       NetworkDrawChatMessage();
00640       DrawMouseCursor();
00641     }
00642 
00643     /* End of the critical part. */
00644     if (_draw_threaded && !HasModalProgress()) {
00645       _draw_mutex->SendSignal();
00646     } else {
00647       /* Oh, we didn't have threads, then just draw unthreaded */
00648       CheckPaletteAnim();
00649       DrawSurfaceToScreen();
00650     }
00651   }
00652 
00653   if (_draw_threaded) {
00654     _draw_continue = false;
00655     /* Sending signal if there is no thread blocked
00656      * is very valid and results in noop */
00657     _draw_mutex->SendSignal();
00658     _draw_mutex->EndCritical();
00659     _draw_thread->Join();
00660 
00661     delete _draw_mutex;
00662     delete _draw_thread;
00663   }
00664 }
00665 
00666 bool VideoDriver_SDL::ChangeResolution(int w, int h)
00667 {
00668   if (_draw_threaded) _draw_mutex->BeginCritical();
00669   bool ret = CreateMainSurface(w, h);
00670   if (_draw_threaded) _draw_mutex->EndCritical();
00671   return ret;
00672 }
00673 
00674 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
00675 {
00676   _fullscreen = fullscreen;
00677   GetVideoModes(); // get the list of available video modes
00678   if (_num_resolutions == 0 || !CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00679     /* switching resolution failed, put back full_screen to original status */
00680     _fullscreen ^= true;
00681     return false;
00682   }
00683   return true;
00684 }
00685 
00686 bool VideoDriver_SDL::AfterBlitterChange()
00687 {
00688   return this->ChangeResolution(_screen.width, _screen.height);
00689 }
00690 
00691 #endif /* WITH_SDL */