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