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 SDL_Surface *_sdl_realscreen;
00033 static bool _all_modes;
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 static Palette _local_palette;
00044
00045 #define MAX_DIRTY_RECTS 100
00046 static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS];
00047 static int _num_dirty_rects;
00048 static int _use_hwpalette;
00049 static int _requested_hwpalette;
00050
00051 void VideoDriver_SDL::MakeDirty(int left, int top, int width, int height)
00052 {
00053 if (_num_dirty_rects < MAX_DIRTY_RECTS) {
00054 _dirty_rects[_num_dirty_rects].x = left;
00055 _dirty_rects[_num_dirty_rects].y = top;
00056 _dirty_rects[_num_dirty_rects].w = width;
00057 _dirty_rects[_num_dirty_rects].h = height;
00058 }
00059 _num_dirty_rects++;
00060 }
00061
00062 static void UpdatePalette(bool init = false)
00063 {
00064 SDL_Color pal[256];
00065
00066 for (int i = 0; i != _local_palette.count_dirty; i++) {
00067 pal[i].r = _local_palette.palette[_local_palette.first_dirty + i].r;
00068 pal[i].g = _local_palette.palette[_local_palette.first_dirty + i].g;
00069 pal[i].b = _local_palette.palette[_local_palette.first_dirty + i].b;
00070 pal[i].unused = 0;
00071 }
00072
00073 SDL_CALL SDL_SetColors(_sdl_screen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
00074
00075 if (_sdl_screen != _sdl_realscreen && init) {
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 SDL_CALL SDL_SetColors(_sdl_realscreen, pal, _local_palette.first_dirty, _local_palette.count_dirty);
00097 }
00098
00099 if (_sdl_screen != _sdl_realscreen && !init) {
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 SDL_CALL SDL_BlitSurface(_sdl_screen, NULL, _sdl_realscreen, NULL);
00111 SDL_CALL SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
00112 }
00113 }
00114
00115 static void InitPalette()
00116 {
00117 _local_palette = _cur_palette;
00118 _local_palette.first_dirty = 0;
00119 _local_palette.count_dirty = 256;
00120 UpdatePalette(true);
00121 }
00122
00123 static void CheckPaletteAnim()
00124 {
00125 if (_cur_palette.count_dirty != 0) {
00126 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00127
00128 switch (blitter->UsePaletteAnimation()) {
00129 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00130 UpdatePalette();
00131 break;
00132
00133 case Blitter::PALETTE_ANIMATION_BLITTER:
00134 blitter->PaletteAnimate(_local_palette);
00135 break;
00136
00137 case Blitter::PALETTE_ANIMATION_NONE:
00138 break;
00139
00140 default:
00141 NOT_REACHED();
00142 }
00143 _cur_palette.count_dirty = 0;
00144 }
00145 }
00146
00147 static void DrawSurfaceToScreen()
00148 {
00149 int n = _num_dirty_rects;
00150 if (n == 0) return;
00151
00152 _num_dirty_rects = 0;
00153 if (n > MAX_DIRTY_RECTS) {
00154 if (_sdl_screen != _sdl_realscreen) {
00155 SDL_CALL SDL_BlitSurface(_sdl_screen, NULL, _sdl_realscreen, NULL);
00156 }
00157 SDL_CALL SDL_UpdateRect(_sdl_realscreen, 0, 0, 0, 0);
00158 } else {
00159 if (_sdl_screen != _sdl_realscreen) {
00160 for (int i = 0; i < n; i++) {
00161 SDL_CALL SDL_BlitSurface(_sdl_screen, &_dirty_rects[i], _sdl_realscreen, &_dirty_rects[i]);
00162 }
00163 }
00164 SDL_CALL SDL_UpdateRects(_sdl_realscreen, n, _dirty_rects);
00165 }
00166 }
00167
00168 static void DrawSurfaceToScreenThread(void *)
00169 {
00170
00171 _draw_mutex->BeginCritical();
00172 _draw_mutex->SendSignal();
00173
00174
00175 _draw_mutex->WaitForSignal();
00176
00177 while (_draw_continue) {
00178 CheckPaletteAnim();
00179
00180 DrawSurfaceToScreen();
00181 _draw_mutex->WaitForSignal();
00182 }
00183
00184 _draw_mutex->EndCritical();
00185 _draw_thread->Exit();
00186 }
00187
00188 static const Dimension _default_resolutions[] = {
00189 { 640, 480},
00190 { 800, 600},
00191 {1024, 768},
00192 {1152, 864},
00193 {1280, 800},
00194 {1280, 960},
00195 {1280, 1024},
00196 {1400, 1050},
00197 {1600, 1200},
00198 {1680, 1050},
00199 {1920, 1200}
00200 };
00201
00202 static void GetVideoModes()
00203 {
00204 SDL_Rect **modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | SDL_FULLSCREEN);
00205 if (modes == NULL) usererror("sdl: no modes available");
00206
00207 _all_modes = (SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE | (_fullscreen ? SDL_FULLSCREEN : 0)) == (void*)-1);
00208 if (modes == (void*)-1) {
00209 int n = 0;
00210 for (uint i = 0; i < lengthof(_default_resolutions); i++) {
00211 if (SDL_CALL SDL_VideoModeOK(_default_resolutions[i].width, _default_resolutions[i].height, 8, SDL_FULLSCREEN) != 0) {
00212 _resolutions[n] = _default_resolutions[i];
00213 if (++n == lengthof(_resolutions)) break;
00214 }
00215 }
00216 _num_resolutions = n;
00217 } else {
00218 int n = 0;
00219 for (int i = 0; modes[i]; i++) {
00220 uint w = modes[i]->w;
00221 uint h = modes[i]->h;
00222 int j;
00223 for (j = 0; j < n; j++) {
00224 if (_resolutions[j].width == w && _resolutions[j].height == h) break;
00225 }
00226
00227 if (j == n) {
00228 _resolutions[j].width = w;
00229 _resolutions[j].height = h;
00230 if (++n == lengthof(_resolutions)) break;
00231 }
00232 }
00233 _num_resolutions = n;
00234 SortResolutions(_num_resolutions);
00235 }
00236 }
00237
00238 static void GetAvailableVideoMode(uint *w, uint *h)
00239 {
00240
00241 if (_all_modes || _num_resolutions == 0) return;
00242
00243
00244 for (int i = 0; i != _num_resolutions; i++) {
00245 if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
00246 }
00247
00248
00249 int best = 0;
00250 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
00251 for (int i = 1; i != _num_resolutions; ++i) {
00252 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
00253 if (newdelta < delta) {
00254 best = i;
00255 delta = newdelta;
00256 }
00257 }
00258 *w = _resolutions[best].width;
00259 *h = _resolutions[best].height;
00260 }
00261
00262 #ifdef WIN32
00263
00264
00265 #undef SDL_LoadBMP
00266 #define SDL_LoadBMP(file) SDL_LoadBMP_RW(SDL_CALL SDL_RWFromFile(file, "rb"), 1)
00267 #endif
00268
00269 bool VideoDriver_SDL::CreateMainSurface(uint w, uint h)
00270 {
00271 SDL_Surface *newscreen, *icon;
00272 char caption[50];
00273 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00274 bool want_hwpalette;
00275
00276 GetAvailableVideoMode(&w, &h);
00277
00278 DEBUG(driver, 1, "SDL: using mode %ux%ux%d", w, h, bpp);
00279
00280 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
00281
00282 char icon_path[MAX_PATH];
00283 if (FioFindFullPath(icon_path, lengthof(icon_path), BASESET_DIR, "openttd.32.bmp") != NULL) {
00284
00285 icon = SDL_CALL SDL_LoadBMP(icon_path);
00286 if (icon != NULL) {
00287
00288 uint32 rgbmap = SDL_CALL SDL_MapRGB(icon->format, 255, 0, 255);
00289
00290 SDL_CALL SDL_SetColorKey(icon, SDL_SRCCOLORKEY, rgbmap);
00291 SDL_CALL SDL_WM_SetIcon(icon, NULL);
00292 SDL_CALL SDL_FreeSurface(icon);
00293 }
00294 }
00295
00296 if (_use_hwpalette == 2) {
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 want_hwpalette = (bpp == 8 && _fullscreen);
00319 } else {
00320
00321 want_hwpalette = _use_hwpalette;
00322 }
00323
00324 if (want_hwpalette) DEBUG(driver, 1, "SDL: requesting hardware palete");
00325
00326
00327 if (_sdl_screen != NULL && _sdl_screen != _sdl_realscreen) SDL_CALL SDL_FreeSurface(_sdl_screen);
00328
00329 if (_sdl_realscreen != NULL) {
00330 if (_requested_hwpalette != want_hwpalette) {
00331
00332
00333
00334
00335
00336
00337
00338
00339 DEBUG(driver, 0, "SDL: Restarting SDL video subsystem, to force hwpalette change");
00340 SDL_CALL SDL_QuitSubSystem(SDL_INIT_VIDEO);
00341 SDL_CALL SDL_InitSubSystem(SDL_INIT_VIDEO);
00342 ClaimMousePointer();
00343 SetupKeyboard();
00344 }
00345 }
00346
00347
00348
00349
00350 _requested_hwpalette = want_hwpalette;
00351
00352
00353 newscreen = SDL_CALL SDL_SetVideoMode(w, h, bpp, SDL_SWSURFACE | (want_hwpalette ? SDL_HWPALETTE : 0) | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE));
00354 if (newscreen == NULL) {
00355 DEBUG(driver, 0, "SDL: Couldn't allocate a window to draw on");
00356 return false;
00357 }
00358 _sdl_realscreen = newscreen;
00359
00360 if (bpp == 8 && (_sdl_realscreen->flags & SDL_HWPALETTE) != SDL_HWPALETTE) {
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 DEBUG(driver, 1, "SDL: using shadow surface");
00380 newscreen = SDL_CALL SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, bpp, 0, 0, 0, 0);
00381 if (newscreen == NULL) {
00382 DEBUG(driver, 0, "SDL: Couldn't allocate a shadow surface to draw on");
00383 return false;
00384 }
00385 }
00386
00387
00388 _num_dirty_rects = 0;
00389
00390 _screen.width = newscreen->w;
00391 _screen.height = newscreen->h;
00392 _screen.pitch = newscreen->pitch / (bpp / 8);
00393 _screen.dst_ptr = newscreen->pixels;
00394 _sdl_screen = newscreen;
00395
00396
00397
00398
00399 if (_fullscreen) _cursor.in_window = true;
00400
00401 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00402 blitter->PostResize();
00403
00404 InitPalette();
00405 switch (blitter->UsePaletteAnimation()) {
00406 case Blitter::PALETTE_ANIMATION_NONE:
00407 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00408 UpdatePalette();
00409 break;
00410
00411 case Blitter::PALETTE_ANIMATION_BLITTER:
00412 if (_video_driver != NULL) blitter->PaletteAnimate(_local_palette);
00413 break;
00414
00415 default:
00416 NOT_REACHED();
00417 }
00418
00419 snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
00420 SDL_CALL SDL_WM_SetCaption(caption, caption);
00421
00422 GameSizeChanged();
00423
00424 return true;
00425 }
00426
00427 bool VideoDriver_SDL::ClaimMousePointer()
00428 {
00429 SDL_CALL SDL_ShowCursor(0);
00430 return true;
00431 }
00432
00433 struct VkMapping {
00434 #if SDL_VERSION_ATLEAST(1, 3, 0)
00435 SDL_Keycode vk_from;
00436 #else
00437 uint16 vk_from;
00438 #endif
00439 byte vk_count;
00440 byte map_to;
00441 };
00442
00443 #define AS(x, z) {x, 0, z}
00444 #define AM(x, y, z, w) {x, (byte)(y - x), z}
00445
00446 static const VkMapping _vk_mapping[] = {
00447
00448 AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
00449 AS(SDLK_UP, WKC_UP),
00450 AS(SDLK_DOWN, WKC_DOWN),
00451 AS(SDLK_LEFT, WKC_LEFT),
00452 AS(SDLK_RIGHT, WKC_RIGHT),
00453
00454 AS(SDLK_HOME, WKC_HOME),
00455 AS(SDLK_END, WKC_END),
00456
00457 AS(SDLK_INSERT, WKC_INSERT),
00458 AS(SDLK_DELETE, WKC_DELETE),
00459
00460
00461 AM(SDLK_a, SDLK_z, 'A', 'Z'),
00462 AM(SDLK_0, SDLK_9, '0', '9'),
00463
00464 AS(SDLK_ESCAPE, WKC_ESC),
00465 AS(SDLK_PAUSE, WKC_PAUSE),
00466 AS(SDLK_BACKSPACE, WKC_BACKSPACE),
00467
00468 AS(SDLK_SPACE, WKC_SPACE),
00469 AS(SDLK_RETURN, WKC_RETURN),
00470 AS(SDLK_TAB, WKC_TAB),
00471
00472
00473 AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
00474
00475
00476 AM(SDLK_KP0, SDLK_KP9, '0', '9'),
00477 AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
00478 AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
00479 AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
00480 AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
00481 AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
00482 AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
00483
00484
00485 AS(SDLK_SLASH, WKC_SLASH),
00486 AS(SDLK_SEMICOLON, WKC_SEMICOLON),
00487 AS(SDLK_EQUALS, WKC_EQUALS),
00488 AS(SDLK_LEFTBRACKET, WKC_L_BRACKET),
00489 AS(SDLK_BACKSLASH, WKC_BACKSLASH),
00490 AS(SDLK_RIGHTBRACKET, WKC_R_BRACKET),
00491
00492 AS(SDLK_QUOTE, WKC_SINGLEQUOTE),
00493 AS(SDLK_COMMA, WKC_COMMA),
00494 AS(SDLK_MINUS, WKC_MINUS),
00495 AS(SDLK_PERIOD, WKC_PERIOD)
00496 };
00497
00498 static uint ConvertSdlKeyIntoMy(SDL_keysym *sym, WChar *character)
00499 {
00500 const VkMapping *map;
00501 uint key = 0;
00502
00503 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00504 if ((uint)(sym->sym - map->vk_from) <= map->vk_count) {
00505 key = sym->sym - map->vk_from + map->map_to;
00506 break;
00507 }
00508 }
00509
00510
00511 #if defined(WIN32) || defined(__OS2__)
00512 if (sym->scancode == 41) key = WKC_BACKQUOTE;
00513 #elif defined(__APPLE__)
00514 if (sym->scancode == 10) key = WKC_BACKQUOTE;
00515 #elif defined(__MORPHOS__)
00516 if (sym->scancode == 0) key = WKC_BACKQUOTE;
00517 #elif defined(__BEOS__)
00518 if (sym->scancode == 17) key = WKC_BACKQUOTE;
00519 #elif defined(__SVR4) && defined(__sun)
00520 if (sym->scancode == 60) key = WKC_BACKQUOTE;
00521 if (sym->scancode == 49) key = WKC_BACKSPACE;
00522 #elif defined(__sgi__)
00523 if (sym->scancode == 22) key = WKC_BACKQUOTE;
00524 #else
00525 if (sym->scancode == 49) key = WKC_BACKQUOTE;
00526 #endif
00527
00528
00529 if (sym->mod & KMOD_META) key |= WKC_META;
00530 if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
00531 if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
00532 if (sym->mod & KMOD_ALT) key |= WKC_ALT;
00533
00534 *character = sym->unicode;
00535 return key;
00536 }
00537
00538 int VideoDriver_SDL::PollEvent()
00539 {
00540 SDL_Event ev;
00541
00542 if (!SDL_CALL SDL_PollEvent(&ev)) return -2;
00543
00544 switch (ev.type) {
00545 case SDL_MOUSEMOTION:
00546 if (_cursor.fix_at) {
00547 int dx = ev.motion.x - _cursor.pos.x;
00548 int dy = ev.motion.y - _cursor.pos.y;
00549 if (dx != 0 || dy != 0) {
00550 _cursor.delta.x = dx;
00551 _cursor.delta.y = dy;
00552 SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
00553 }
00554 } else {
00555 _cursor.delta.x = ev.motion.x - _cursor.pos.x;
00556 _cursor.delta.y = ev.motion.y - _cursor.pos.y;
00557 _cursor.pos.x = ev.motion.x;
00558 _cursor.pos.y = ev.motion.y;
00559 _cursor.dirty = true;
00560 }
00561 HandleMouseEvents();
00562 break;
00563
00564 case SDL_MOUSEBUTTONDOWN:
00565 if (_rightclick_emulate && SDL_CALL SDL_GetModState() & KMOD_CTRL) {
00566 ev.button.button = SDL_BUTTON_RIGHT;
00567 }
00568
00569 switch (ev.button.button) {
00570 case SDL_BUTTON_LEFT:
00571 _left_button_down = true;
00572 break;
00573
00574 case SDL_BUTTON_RIGHT:
00575 _right_button_down = true;
00576 _right_button_clicked = true;
00577 break;
00578
00579 case SDL_BUTTON_WHEELUP: _cursor.wheel--; break;
00580 case SDL_BUTTON_WHEELDOWN: _cursor.wheel++; break;
00581
00582 default: break;
00583 }
00584 HandleMouseEvents();
00585 break;
00586
00587 case SDL_MOUSEBUTTONUP:
00588 if (_rightclick_emulate) {
00589 _right_button_down = false;
00590 _left_button_down = false;
00591 _left_button_clicked = false;
00592 } else if (ev.button.button == SDL_BUTTON_LEFT) {
00593 _left_button_down = false;
00594 _left_button_clicked = false;
00595 } else if (ev.button.button == SDL_BUTTON_RIGHT) {
00596 _right_button_down = false;
00597 }
00598 HandleMouseEvents();
00599 break;
00600
00601 case SDL_ACTIVEEVENT:
00602 if (!(ev.active.state & SDL_APPMOUSEFOCUS)) break;
00603
00604 if (ev.active.gain) {
00605 _cursor.in_window = true;
00606 } else {
00607 UndrawMouseCursor();
00608 _cursor.in_window = false;
00609 }
00610 break;
00611
00612 case SDL_QUIT:
00613 HandleExitGameRequest();
00614 break;
00615
00616 case SDL_KEYDOWN:
00617 if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) &&
00618 (ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) {
00619 ToggleFullScreen(!_fullscreen);
00620 } else {
00621 WChar character;
00622 uint keycode = ConvertSdlKeyIntoMy(&ev.key.keysym, &character);
00623 HandleKeypress(keycode, character);
00624 }
00625 break;
00626
00627 case SDL_VIDEORESIZE: {
00628 int w = max(ev.resize.w, 64);
00629 int h = max(ev.resize.h, 64);
00630 CreateMainSurface(w, h);
00631 break;
00632 }
00633 case SDL_VIDEOEXPOSE: {
00634
00635
00636
00637 _num_dirty_rects = MAX_DIRTY_RECTS + 1;
00638 break;
00639 }
00640 }
00641 return -1;
00642 }
00643
00644 const char *VideoDriver_SDL::Start(const char * const *parm)
00645 {
00646 char buf[30];
00647 _use_hwpalette = GetDriverParamInt(parm, "hw_palette", 2);
00648
00649 const char *s = SdlOpen(SDL_INIT_VIDEO);
00650 if (s != NULL) return s;
00651
00652 GetVideoModes();
00653 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00654 return SDL_CALL SDL_GetError();
00655 }
00656
00657 SDL_CALL SDL_VideoDriverName(buf, sizeof buf);
00658 DEBUG(driver, 1, "SDL: using driver '%s'", buf);
00659
00660 MarkWholeScreenDirty();
00661 SetupKeyboard();
00662
00663 _draw_threaded = GetDriverParam(parm, "no_threads") == NULL && GetDriverParam(parm, "no_thread") == NULL;
00664
00665 return NULL;
00666 }
00667
00668 void VideoDriver_SDL::SetupKeyboard()
00669 {
00670 SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
00671 SDL_CALL SDL_EnableUNICODE(1);
00672 }
00673
00674 void VideoDriver_SDL::Stop()
00675 {
00676 SdlClose(SDL_INIT_VIDEO);
00677 }
00678
00679 void VideoDriver_SDL::MainLoop()
00680 {
00681 uint32 cur_ticks = SDL_CALL SDL_GetTicks();
00682 uint32 last_cur_ticks = cur_ticks;
00683 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00684 uint32 mod;
00685 int numkeys;
00686 Uint8 *keys;
00687
00688 CheckPaletteAnim();
00689
00690 if (_draw_threaded) {
00691
00692
00693 _draw_mutex = ThreadMutex::New();
00694 if (_draw_mutex == NULL) {
00695 _draw_threaded = false;
00696 } else {
00697 _draw_mutex->BeginCritical();
00698 _draw_continue = true;
00699
00700 _draw_threaded = ThreadObject::New(&DrawSurfaceToScreenThread, NULL, &_draw_thread);
00701
00702
00703 if (!_draw_threaded) {
00704 _draw_mutex->EndCritical();
00705 delete _draw_mutex;
00706 _draw_mutex = NULL;
00707 } else {
00708
00709 _draw_mutex->WaitForSignal();
00710 }
00711 }
00712 }
00713
00714 DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no ");
00715
00716 for (;;) {
00717 uint32 prev_cur_ticks = cur_ticks;
00718 InteractiveRandom();
00719
00720 while (PollEvent() == -1) {}
00721 if (_exit_game) break;
00722
00723 mod = SDL_CALL SDL_GetModState();
00724 #if SDL_VERSION_ATLEAST(1, 3, 0)
00725 keys = SDL_CALL SDL_GetKeyboardState(&numkeys);
00726 #else
00727 keys = SDL_CALL SDL_GetKeyState(&numkeys);
00728 #endif
00729 #if defined(_DEBUG)
00730 if (_shift_pressed)
00731 #else
00732
00733
00734 #if SDL_VERSION_ATLEAST(1, 3, 0)
00735 if (keys[SDL_SCANCODE_TAB] && (mod & KMOD_ALT) == 0)
00736 #else
00737 if (keys[SDLK_TAB] && (mod & KMOD_ALT) == 0)
00738 #endif
00739 #endif
00740 {
00741 if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
00742 } else if (_fast_forward & 2) {
00743 _fast_forward = 0;
00744 }
00745
00746 cur_ticks = SDL_CALL SDL_GetTicks();
00747 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00748 _realtime_tick += cur_ticks - last_cur_ticks;
00749 last_cur_ticks = cur_ticks;
00750 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
00751
00752 bool old_ctrl_pressed = _ctrl_pressed;
00753
00754 _ctrl_pressed = !!(mod & KMOD_CTRL);
00755 _shift_pressed = !!(mod & KMOD_SHIFT);
00756
00757
00758 _dirkeys =
00759 #if SDL_VERSION_ATLEAST(1, 3, 0)
00760 (keys[SDL_SCANCODE_LEFT] ? 1 : 0) |
00761 (keys[SDL_SCANCODE_UP] ? 2 : 0) |
00762 (keys[SDL_SCANCODE_RIGHT] ? 4 : 0) |
00763 (keys[SDL_SCANCODE_DOWN] ? 8 : 0);
00764 #else
00765 (keys[SDLK_LEFT] ? 1 : 0) |
00766 (keys[SDLK_UP] ? 2 : 0) |
00767 (keys[SDLK_RIGHT] ? 4 : 0) |
00768 (keys[SDLK_DOWN] ? 8 : 0);
00769 #endif
00770 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
00771
00772
00773
00774 if (_draw_mutex != NULL) _draw_mutex->EndCritical();
00775
00776 GameLoop();
00777
00778 if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
00779
00780 UpdateWindows();
00781 _local_palette = _cur_palette;
00782 } else {
00783
00784 if (_draw_mutex != NULL) _draw_mutex->EndCritical();
00785 CSleep(1);
00786 if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
00787
00788 NetworkDrawChatMessage();
00789 DrawMouseCursor();
00790 }
00791
00792
00793 if (_draw_mutex != NULL && !HasModalProgress()) {
00794 _draw_mutex->SendSignal();
00795 } else {
00796
00797 CheckPaletteAnim();
00798 DrawSurfaceToScreen();
00799 }
00800 }
00801
00802 if (_draw_mutex != NULL) {
00803 _draw_continue = false;
00804
00805
00806 _draw_mutex->SendSignal();
00807 _draw_mutex->EndCritical();
00808 _draw_thread->Join();
00809
00810 delete _draw_mutex;
00811 delete _draw_thread;
00812
00813 _draw_mutex = NULL;
00814 _draw_thread = NULL;
00815 }
00816 }
00817
00818 bool VideoDriver_SDL::ChangeResolution(int w, int h)
00819 {
00820 if (_draw_mutex != NULL) _draw_mutex->BeginCritical();
00821 bool ret = CreateMainSurface(w, h);
00822 if (_draw_mutex != NULL) _draw_mutex->EndCritical();
00823 return ret;
00824 }
00825
00826 bool VideoDriver_SDL::ToggleFullscreen(bool fullscreen)
00827 {
00828 _fullscreen = fullscreen;
00829 GetVideoModes();
00830 if (_num_resolutions == 0 || !CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00831
00832 _fullscreen ^= true;
00833 return false;
00834 }
00835 return true;
00836 }
00837
00838 bool VideoDriver_SDL::AfterBlitterChange()
00839 {
00840 return CreateMainSurface(_screen.width, _screen.height);
00841 }
00842
00843 #endif