00001
00002
00003
00004
00005
00006
00007
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
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
00117 _draw_mutex->BeginCritical();
00118 _draw_mutex->SendSignal();
00119
00120
00121 _draw_mutex->WaitForSignal();
00122
00123 while (_draw_continue) {
00124
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
00186 if (_all_modes || _num_resolutions == 0) return;
00187
00188
00189 for (int i = 0; i != _num_resolutions; i++) {
00190 if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
00191 }
00192
00193
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 #ifdef WIN32
00208
00209
00210 #undef SDL_LoadBMP
00211 #define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_CALL SDL_RWFromFile(file, "rb"), 1)
00212 #endif
00213
00214 static bool CreateMainSurface(uint w, uint h)
00215 {
00216 SDL_Surface *newscreen, *icon;
00217 char caption[50];
00218 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00219
00220 GetAvailableVideoMode(&w, &h);
00221
00222 DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
00223
00224 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
00225
00226 char icon_path[MAX_PATH];
00227 if (FioFindFullPath(icon_path, lengthof(icon_path), BASESET_DIR, "openttd.32.bmp") != NULL) {
00228
00229 icon = SDL_CALL SDL_LoadBMP(icon_path);
00230 if (icon != NULL) {
00231
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
00240
00241 newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
00242 if (newscreen == NULL) {
00243 DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
00244 return false;
00245 }
00246
00247
00248 _num_dirty_rects = 0;
00249
00250 _screen.width = newscreen->w;
00251 _screen.height = newscreen->h;
00252 _screen.pitch = newscreen->pitch / (bpp / 8);
00253 _screen.dst_ptr = newscreen->pixels;
00254 _sdl_screen = newscreen;
00255
00256 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00257
00258 InitPalette();
00259
00260 snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
00261 SDL_CALL SDL_WM_SetCaption(caption, caption);
00262
00263 GameSizeChanged();
00264
00265 return true;
00266 }
00267
00268 bool VideoDriver_SDL::ClaimMousePointer()
00269 {
00270 SDL_CALL SDL_ShowCursor(0);
00271 return true;
00272 }
00273
00274 struct VkMapping {
00275 uint16 vk_from;
00276 byte vk_count;
00277 byte map_to;
00278 };
00279
00280 #define AS(x, z) {x, 0, z}
00281 #define AM(x, y, z, w) {x, y - x, z}
00282
00283 static const VkMapping _vk_mapping[] = {
00284
00285 AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
00286 AS(SDLK_UP, WKC_UP),
00287 AS(SDLK_DOWN, WKC_DOWN),
00288 AS(SDLK_LEFT, WKC_LEFT),
00289 AS(SDLK_RIGHT, WKC_RIGHT),
00290
00291 AS(SDLK_HOME, WKC_HOME),
00292 AS(SDLK_END, WKC_END),
00293
00294 AS(SDLK_INSERT, WKC_INSERT),
00295 AS(SDLK_DELETE, WKC_DELETE),
00296
00297
00298 AM(SDLK_a, SDLK_z, 'A', 'Z'),
00299 AM(SDLK_0, SDLK_9, '0', '9'),
00300
00301 AS(SDLK_ESCAPE, WKC_ESC),
00302 AS(SDLK_PAUSE, WKC_PAUSE),
00303 AS(SDLK_BACKSPACE, WKC_BACKSPACE),
00304
00305 AS(SDLK_SPACE, WKC_SPACE),
00306 AS(SDLK_RETURN, WKC_RETURN),
00307 AS(SDLK_TAB, WKC_TAB),
00308
00309
00310 AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
00311
00312
00313 AM(SDLK_KP0, SDLK_KP9, '0', '9'),
00314 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
00315 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
00316 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
00317 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
00318 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
00319 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
00320
00321
00322 AS(SDLK_SLASH, WKC_SLASH),
00323 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
00324 AS(SDLK_EQUALS, WKC_EQUALS),
00325 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
00326 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
00327 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
00328
00329 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
00330 AS(SDLK_COMMA, WKC_COMMA),
00331 AS(SDLK_MINUS, WKC_MINUS),
00332 AS(SDLK_PERIOD, WKC_PERIOD)
00333 };
00334
00335 static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
00336 {
00337 const VkMapping *map;
00338 uint key = 0;
00339
00340 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00341 if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
00342 key = sym->sym - map->vk_from + map->map_to;
00343 break;
00344 }
00345 }
00346
00347
00348 #if defined(WIN32) || defined(__OS2__)
00349 if (sym->scancode == 41) key = WKC_BACKQUOTE;
00350 #elif defined(__APPLE__)
00351 if (sym->scancode == 10) key = WKC_BACKQUOTE;
00352 #elif defined(__MORPHOS__)
00353 if (sym->scancode == 0) key = WKC_BACKQUOTE;
00354 #elif defined(__BEOS__)
00355 if (sym->scancode == 17) key = WKC_BACKQUOTE;
00356 #elif defined(__SVR4) && defined(__sun)
00357 if (sym->scancode == 60) key = WKC_BACKQUOTE;
00358 if (sym->scancode == 49) key = WKC_BACKSPACE;
00359 #elif defined(__sgi__)
00360 if (sym->scancode == 22) key = WKC_BACKQUOTE;
00361 #else
00362 if (sym->scancode == 49) key = WKC_BACKQUOTE;
00363 #endif
00364
00365
00366 if (sym->mod & KMOD_META) key |= WKC_META;
00367 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
00368 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
00369 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
00370
00371 return (key << 16) + sym->unicode;
00372 }
00373
00374 static int PollEvent()
00375 {
00376 SDL_Event ev;
00377
00378 if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
00379
00380 switch (ev.type) {
00381 case SDL_MOUSEMOTION:
00382 if (_cursor.fix_at) {
00383 int dx = ev.motion.x - _cursor.pos.x;
00384 int dy = ev.motion.y - _cursor.pos.y;
00385 if (dx != 0 || dy != 0) {
00386 _cursor.delta.x = dx;
00387 _cursor.delta.y = dy;
00388 SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
00389 }
00390 } else {
00391 _cursor.delta.x = ev.motion.x - _cursor.pos.x;
00392 _cursor.delta.y = ev.motion.y - _cursor.pos.y;
00393 _cursor.pos.x = ev.motion.x;
00394 _cursor.pos.y = ev.motion.y;
00395 _cursor.dirty = true;
00396 }
00397 HandleMouseEvents();
00398 break;
00399
00400 case SDL_MOUSEBUTTONDOWN:
00401 if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
00402 ev.button.button = SDL_BUTTON_RIGHT;
00403 }
00404
00405 switch (ev.button.button) {
00406 case SDL_BUTTON_LEFT:
00407 _left_button_down = true;
00408 break;
00409
00410 case SDL_BUTTON_RIGHT:
00411 _right_button_down = true;
00412 _right_button_clicked = true;
00413 break;
00414
00415 case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
00416 case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
00417
00418 default: break;
00419 }
00420 HandleMouseEvents();
00421 break;
00422
00423 case SDL_MOUSEBUTTONUP:
00424 if (_rightclick_emulate) {
00425 _right_button_down = false;
00426 _left_button_down = false;
00427 _left_button_clicked = false;
00428 } else if (ev.button.button == SDL_BUTTON_LEFT) {
00429 _left_button_down = false;
00430 _left_button_clicked = false;
00431 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
00432 _right_button_down = false;
00433 }
00434 HandleMouseEvents();
00435 break;
00436
00437 case SDL_ACTIVEEVENT:
00438 if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
00439
00440 if (ev.active.gain) {
00441 _cursor.in_window = true;
00442 } else {
00443 UndrawMouseCursor();
00444 _cursor.in_window = false;
00445 }
00446 break;
00447
00448 case SDL_QUIT:
00449 HandleExitGameRequest();
00450 break;
00451
00452 case SDL_KEYDOWN:
00453 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
00454 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
00455 ToggleFullScreen(!_fullscreen);
00456 } else {
00457 HandleKeypress(ConvertSdlKeyIntoMy(&ev.key.keysym));
00458 }
00459 break;
00460
00461 case SDL_VIDEORESIZE: {
00462 int w = max(ev.resize.w, 64);
00463 int h = max(ev.resize.h, 64);
00464 CreateMainSurface(w, h);
00465 break;
00466 }
00467 }
00468 return -1;
00469 }
00470
00471 const char *VideoDriver_SDL::Start(const char * const *parm)
00472 {
00473 char buf[30];
00474
00475 const char *s = SdlOpen(SDL_INIT_VIDEO);
00476 if (s != NULL) return s;
00477
00478 GetVideoModes();
00479 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00480 return SDL_CALL SDL_GetError();
00481 }
00482
00483 SDL_CALL SDL_VideoDriverName(buf, sizeof buf);
00484 DEBUG(driver, 1, "SDL: using driver '%s'", buf);
00485
00486 MarkWholeScreenDirty();
00487
00488 SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00489 SDL_CALL SDL_EnableUNICODE(1);
00490
00491 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
00492
00493 return NULL;
00494 }
00495
00496 void VideoDriver_SDL::Stop()
00497 {
00498 SdlClose(SDL_INIT_VIDEO);
00499 }
00500
00501 void VideoDriver_SDL::MainLoop()
00502 {
00503 uint32 cur_ticks = SDL_CALL SDL_GetTicks();
00504 uint32 last_cur_ticks = cur_ticks;
00505 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00506 uint32 pal_tick = 0;
00507 uint32 mod;
00508 int numkeys;
00509 Uint8 *keys;
00510
00511 if (_draw_threaded) {
00512
00513
00514 _draw_mutex = ThreadMutex::New();
00515 if (_draw_mutex == NULL) {
00516 _draw_threaded = false;
00517 } else {
00518 _draw_mutex->BeginCritical();
00519 _draw_continue = true;
00520
00521 _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
00522
00523
00524 if (!_draw_threaded) {
00525 _draw_mutex->EndCritical();
00526 delete _draw_mutex;
00527 } else {
00528
00529 _draw_mutex->WaitForSignal();
00530 }
00531 }
00532 }
00533
00534 DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
00535
00536 for (;;) {
00537 uint32 prev_cur_ticks = cur_ticks;
00538 InteractiveRandom();
00539
00540 while (PollEvent() == -1) {}
00541 if (_exit_game) break;
00542
00543 mod = SDL_CALL SDL_GetModState();
00544 keys = SDL_CALL SDL_GetKeyState(&numkeys);
00545 #if defined(_DEBUG)
00546 if (_shift_pressed)
00547 #else
00548
00549
00550 if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
00551 #endif
00552 {
00553 if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
00554 } else if (_fast_forward & 2) {
00555 _fast_forward = 0;
00556 }
00557
00558 cur_ticks = SDL_CALL SDL_GetTicks();
00559 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00560 _realtime_tick += cur_ticks - last_cur_ticks;
00561 last_cur_ticks = cur_ticks;
00562 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00563
00564 bool old_ctrl_pressed = _ctrl_pressed;
00565
00566 _ctrl_pressed = !!(mod & KMOD_CTRL);
00567 _shift_pressed = !!(mod & KMOD_SHIFT);
00568
00569
00570 _dirkeys =
00571 (keys[SDLK_LEFT] ? 1 : 0) |
00572 (keys[SDLK_UP] ? 2 : 0) |
00573 (keys[SDLK_RIGHT] ? 4 : 0) |
00574 (keys[SDLK_DOWN] ? 8 : 0);
00575
00576 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
00577
00578
00579
00580 if (_draw_threaded) _draw_mutex->EndCritical();
00581
00582 GameLoop();
00583
00584 if (_draw_threaded) _draw_mutex->BeginCritical();
00585
00586 UpdateWindows();
00587 if (++pal_tick > 4) {
00588 CheckPaletteAnim();
00589 pal_tick = 1;
00590 }
00591 } else {
00592
00593 if (_draw_threaded) _draw_mutex->EndCritical();
00594 CSleep(1);
00595 if (_draw_threaded) _draw_mutex->BeginCritical();
00596
00597 NetworkDrawChatMessage();
00598 DrawMouseCursor();
00599 }
00600
00601
00602 if (_draw_threaded && !HasModalProgress()) {
00603 _draw_mutex->SendSignal();
00604 } else {
00605
00606 DrawSurfaceToScreen();
00607 }
00608 }
00609
00610 if (_draw_threaded) {
00611 _draw_continue = false;
00612
00613
00614 _draw_mutex->SendSignal();
00615 _draw_mutex->EndCritical();
00616 _draw_thread->Join();
00617
00618 delete _draw_mutex;
00619 delete _draw_thread;
00620 }
00621 }
00622
00623 bool VideoDriver_SDL::ChangeResolution(int w, int h)
00624 {
00625 if (_draw_threaded) _draw_mutex->BeginCritical();
00626 bool ret = CreateMainSurface(w, h);
00627 if (_draw_threaded) _draw_mutex->EndCritical();
00628 return ret;
00629 }
00630
00631 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
00632 {
00633 _fullscreen = fullscreen;
00634 GetVideoModes();
00635 if (_num_resolutions == 0 || !CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00636
00637 _fullscreen ^= true;
00638 return false;
00639 }
00640 return true;
00641 }
00642
00643 bool VideoDriver_SDL::AfterBlitterChange()
00644 {
00645 return this->ChangeResolution(_screen.width, _screen.height);
00646 }
00647
00648 #endif