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