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