# sdl_v.cpp

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