00001
00002
00003
00004
00005
00006
00007
00008
00009
00017 #ifdef WITH_ALLEGRO
00018
00019 #include "../stdafx.h"
00020 #include "../openttd.h"
00021 #include "../gfx_func.h"
00022 #include "../variables.h"
00023 #include "../rev.h"
00024 #include "../blitter/factory.hpp"
00025 #include "../network/network.h"
00026 #include "../core/random_func.hpp"
00027 #include "../functions.h"
00028 #include "allegro_v.h"
00029 #include <allegro.h>
00030
00031 #ifdef _DEBUG
00032
00033
00034 #include <signal.h>
00035 #endif
00036
00037 static FVideoDriver_Allegro iFVideoDriver_Allegro;
00038
00039 static BITMAP *_allegro_screen;
00040
00041 #define MAX_DIRTY_RECTS 100
00042 static PointDimension _dirty_rects[MAX_DIRTY_RECTS];
00043 static int _num_dirty_rects;
00044
00045 void VideoDriver_Allegro::MakeDirty(int left, int top, int width, int height)
00046 {
00047 if (_num_dirty_rects < MAX_DIRTY_RECTS) {
00048 _dirty_rects[_num_dirty_rects].x = left;
00049 _dirty_rects[_num_dirty_rects].y = top;
00050 _dirty_rects[_num_dirty_rects].width = width;
00051 _dirty_rects[_num_dirty_rects].height = height;
00052 }
00053 _num_dirty_rects++;
00054 }
00055
00056 static void DrawSurfaceToScreen()
00057 {
00058 int n = _num_dirty_rects;
00059 if (n == 0) return;
00060
00061 _num_dirty_rects = 0;
00062 if (n > MAX_DIRTY_RECTS) {
00063 blit(_allegro_screen, screen, 0, 0, 0, 0, _allegro_screen->w, _allegro_screen->h);
00064 return;
00065 }
00066
00067 for (int i = 0; i < n; i++) {
00068 blit(_allegro_screen, screen, _dirty_rects[i].x, _dirty_rects[i].y, _dirty_rects[i].x, _dirty_rects[i].y, _dirty_rects[i].width, _dirty_rects[i].height);
00069 }
00070 }
00071
00072
00073 static void UpdatePalette(uint start, uint count)
00074 {
00075 static PALETTE pal;
00076
00077 uint end = start + count;
00078 for (uint i = start; i != end; i++) {
00079 pal[i].r = _cur_palette[i].r / 4;
00080 pal[i].g = _cur_palette[i].g / 4;
00081 pal[i].b = _cur_palette[i].b / 4;
00082 pal[i].filler = 0;
00083 }
00084
00085 set_palette_range(pal, start, end - 1, 1);
00086 }
00087
00088 static void InitPalette()
00089 {
00090 UpdatePalette(0, 256);
00091 }
00092
00093 static void CheckPaletteAnim()
00094 {
00095 if (_pal_count_dirty != 0) {
00096 Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00097
00098 switch (blitter->UsePaletteAnimation()) {
00099 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
00100 UpdatePalette(_pal_first_dirty, _pal_count_dirty);
00101 break;
00102
00103 case Blitter::PALETTE_ANIMATION_BLITTER:
00104 blitter->PaletteAnimate(_pal_first_dirty, _pal_count_dirty);
00105 break;
00106
00107 case Blitter::PALETTE_ANIMATION_NONE:
00108 break;
00109
00110 default:
00111 NOT_REACHED();
00112 }
00113 _pal_count_dirty = 0;
00114 }
00115 }
00116
00117 static const Dimension default_resolutions[] = {
00118 { 640, 480},
00119 { 800, 600},
00120 {1024, 768},
00121 {1152, 864},
00122 {1280, 800},
00123 {1280, 960},
00124 {1280, 1024},
00125 {1400, 1050},
00126 {1600, 1200},
00127 {1680, 1050},
00128 {1920, 1200}
00129 };
00130
00131 static void GetVideoModes()
00132 {
00133
00134
00135 set_gfx_mode(_fullscreen ? GFX_AUTODETECT_FULLSCREEN : GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
00136
00137 GFX_MODE_LIST *mode_list = get_gfx_mode_list(gfx_driver->id);
00138 if (mode_list == NULL) {
00139 memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
00140 _num_resolutions = lengthof(default_resolutions);
00141 return;
00142 }
00143
00144 GFX_MODE *modes = mode_list->mode;
00145
00146 int n = 0;
00147 for (int i = 0; modes[i].bpp != 0; i++) {
00148 uint w = modes[i].width;
00149 uint h = modes[i].height;
00150 if (w >= 640 && h >= 480) {
00151 int j;
00152 for (j = 0; j < n; j++) {
00153 if (_resolutions[j].width == w && _resolutions[j].height == h) break;
00154 }
00155
00156 if (j == n) {
00157 _resolutions[j].width = w;
00158 _resolutions[j].height = h;
00159 if (++n == lengthof(_resolutions)) break;
00160 }
00161 }
00162 }
00163 _num_resolutions = n;
00164 SortResolutions(_num_resolutions);
00165
00166 destroy_gfx_mode_list(mode_list);
00167 }
00168
00169 static void GetAvailableVideoMode(uint *w, uint *h)
00170 {
00171
00172 if (_num_resolutions == 0) return;
00173
00174
00175 for (int i = 0; i != _num_resolutions; i++) {
00176 if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
00177 }
00178
00179
00180 int best = 0;
00181 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
00182 for (int i = 1; i != _num_resolutions; ++i) {
00183 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
00184 if (newdelta < delta) {
00185 best = i;
00186 delta = newdelta;
00187 }
00188 }
00189 *w = _resolutions[best].width;
00190 *h = _resolutions[best].height;
00191 }
00192
00193 static bool CreateMainSurface(uint w, uint h)
00194 {
00195 int bpp = BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth();
00196 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
00197 set_color_depth(bpp);
00198
00199 GetAvailableVideoMode(&w, &h);
00200 if (set_gfx_mode(_fullscreen ? GFX_AUTODETECT_FULLSCREEN : GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0) {
00201 DEBUG(driver, 0, "Allegro: Couldn't allocate a window to draw on '%s'", allegro_error);
00202 return false;
00203 }
00204
00205
00206
00207 _allegro_screen = create_bitmap_ex(bpp, screen->cr - screen->cl, screen->cb - screen->ct);
00208 _screen.width = _allegro_screen->w;
00209 _screen.height = _allegro_screen->h;
00210 _screen.pitch = ((byte*)screen->line[1] - (byte*)screen->line[0]) / (bpp / 8);
00211 _screen.dst_ptr = _allegro_screen->line[0];
00212
00213
00214 memset(_screen.dst_ptr, 0, _screen.height * _screen.pitch);
00215
00216
00217 poll_mouse();
00218 _cursor.pos.x = mouse_x;
00219 _cursor.pos.y = mouse_y;
00220
00221 BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00222
00223 InitPalette();
00224
00225 char caption[32];
00226 snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
00227 set_window_title(caption);
00228
00229 GameSizeChanged();
00230
00231 return true;
00232 }
00233
00234 struct VkMapping {
00235 uint16 vk_from;
00236 byte vk_count;
00237 byte map_to;
00238 };
00239
00240 #define AS(x, z) {x, 0, z}
00241 #define AM(x, y, z, w) {x, y - x, z}
00242
00243 static const VkMapping _vk_mapping[] = {
00244
00245 AM(KEY_PGUP, KEY_PGDN, WKC_PAGEUP, WKC_PAGEDOWN),
00246 AS(KEY_UP, WKC_UP),
00247 AS(KEY_DOWN, WKC_DOWN),
00248 AS(KEY_LEFT, WKC_LEFT),
00249 AS(KEY_RIGHT, WKC_RIGHT),
00250
00251 AS(KEY_HOME, WKC_HOME),
00252 AS(KEY_END, WKC_END),
00253
00254 AS(KEY_INSERT, WKC_INSERT),
00255 AS(KEY_DEL, WKC_DELETE),
00256
00257
00258 AM(KEY_A, KEY_Z, 'A', 'Z'),
00259 AM(KEY_0, KEY_9, '0', '9'),
00260
00261 AS(KEY_ESC, WKC_ESC),
00262 AS(KEY_PAUSE, WKC_PAUSE),
00263 AS(KEY_BACKSPACE, WKC_BACKSPACE),
00264
00265 AS(KEY_SPACE, WKC_SPACE),
00266 AS(KEY_ENTER, WKC_RETURN),
00267 AS(KEY_TAB, WKC_TAB),
00268
00269
00270 AM(KEY_F1, KEY_F12, WKC_F1, WKC_F12),
00271
00272
00273 AM(KEY_0_PAD, KEY_9_PAD, '0', '9'),
00274 AS(KEY_SLASH_PAD, WKC_NUM_DIV),
00275 AS(KEY_ASTERISK, WKC_NUM_MUL),
00276 AS(KEY_MINUS_PAD, WKC_NUM_MINUS),
00277 AS(KEY_PLUS_PAD, WKC_NUM_PLUS),
00278 AS(KEY_ENTER_PAD, WKC_NUM_ENTER),
00279 AS(KEY_DEL_PAD, WKC_DELETE),
00280
00281
00282 AS(KEY_SLASH, WKC_SLASH),
00283 AS(KEY_SEMICOLON, WKC_SEMICOLON),
00284 AS(KEY_EQUALS, WKC_EQUALS),
00285 AS(KEY_OPENBRACE, WKC_L_BRACKET),
00286 AS(KEY_BACKSLASH, WKC_BACKSLASH),
00287 AS(KEY_CLOSEBRACE, WKC_R_BRACKET),
00288
00289 AS(KEY_QUOTE, WKC_SINGLEQUOTE),
00290 AS(KEY_COMMA, WKC_COMMA),
00291 AS(KEY_MINUS, WKC_MINUS),
00292 AS(KEY_STOP, WKC_PERIOD),
00293 AS(KEY_TILDE, WKC_BACKQUOTE),
00294 };
00295
00296 static uint32 ConvertAllegroKeyIntoMy()
00297 {
00298 int scancode;
00299 int unicode = ureadkey(&scancode);
00300
00301 const VkMapping *map;
00302 uint key = 0;
00303
00304 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00305 if ((uint)(scancode - map->vk_from) <= map->vk_count) {
00306 key = scancode - map->vk_from + map->map_to;
00307 break;
00308 }
00309 }
00310
00311 if (key_shifts & KB_SHIFT_FLAG) key |= WKC_SHIFT;
00312 if (key_shifts & KB_CTRL_FLAG) key |= WKC_CTRL;
00313 if (key_shifts & KB_ALT_FLAG) key |= WKC_ALT;
00314 #if 0
00315 DEBUG(driver, 0, "Scancode character pressed %u", scancode);
00316 DEBUG(driver, 0, "Unicode character pressed %u", unicode);
00317 #endif
00318 return (key << 16) + unicode;
00319 }
00320
00321 enum {
00322 LEFT_BUTTON,
00323 RIGHT_BUTTON,
00324 };
00325
00326 static void PollEvent()
00327 {
00328 poll_mouse();
00329
00330 bool mouse_action = false;
00331
00332
00333 static int prev_button_state;
00334 if (prev_button_state != mouse_b) {
00335 uint diff = prev_button_state ^ mouse_b;
00336 while (diff != 0) {
00337 int button = FindFirstBit(diff);
00338 ClrBit(diff, button);
00339 if (HasBit(mouse_b, button)) {
00340
00341 if (_rightclick_emulate && (key_shifts & KB_CTRL_FLAG)) {
00342 button = RIGHT_BUTTON;
00343 ClrBit(diff, RIGHT_BUTTON);
00344 }
00345 switch (button) {
00346 case LEFT_BUTTON:
00347 _left_button_down = true;
00348 break;
00349
00350 case RIGHT_BUTTON:
00351 _right_button_down = true;
00352 _right_button_clicked = true;
00353 break;
00354
00355 default:
00356
00357 break;
00358 }
00359 } else {
00360
00361 if (_rightclick_emulate) {
00362 _right_button_down = false;
00363 _left_button_down = false;
00364 _left_button_clicked = false;
00365 } else if (button == LEFT_BUTTON) {
00366 _left_button_down = false;
00367 _left_button_clicked = false;
00368 } else if (button == RIGHT_BUTTON) {
00369 _right_button_down = false;
00370 }
00371 }
00372 }
00373 prev_button_state = mouse_b;
00374 mouse_action = true;
00375 }
00376
00377
00378 int dx = mouse_x - _cursor.pos.x;
00379 int dy = mouse_y - _cursor.pos.y;
00380 if (dx != 0 || dy != 0) {
00381 if (_cursor.fix_at) {
00382 _cursor.delta.x = dx;
00383 _cursor.delta.y = dy;
00384 position_mouse(_cursor.pos.x, _cursor.pos.y);
00385 } else {
00386 _cursor.delta.x = dx;
00387 _cursor.delta.y = dy;
00388 _cursor.pos.x = mouse_x;
00389 _cursor.pos.y = mouse_y;
00390 _cursor.dirty = true;
00391 }
00392 mouse_action = true;
00393 }
00394
00395 static int prev_mouse_z = 0;
00396 if (prev_mouse_z != mouse_z) {
00397 _cursor.wheel = (prev_mouse_z - mouse_z) < 0 ? -1 : 1;
00398 prev_mouse_z = mouse_z;
00399 mouse_action = true;
00400 }
00401
00402 if (mouse_action) HandleMouseEvents();
00403
00404 poll_keyboard();
00405 if ((key_shifts & KB_ALT_FLAG) && (key[KEY_ENTER] || key[KEY_F])) {
00406 ToggleFullScreen(!_fullscreen);
00407 } else if (keypressed()) {
00408 HandleKeypress(ConvertAllegroKeyIntoMy());
00409 }
00410 }
00411
00414 int _allegro_instance_count = 0;
00415
00416 const char *VideoDriver_Allegro::Start(const char * const *parm)
00417 {
00418 if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
00419 DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
00420 return "Failed to set up Allegro";
00421 }
00422 _allegro_instance_count++;
00423
00424 install_timer();
00425 install_mouse();
00426 install_keyboard();
00427
00428 #if defined _DEBUG
00429
00430
00431 signal(SIGABRT, NULL);
00432 signal(SIGSEGV, NULL);
00433 #endif
00434
00435 #if defined(DOS)
00436
00437
00438 _fullscreen = true;
00439 #endif
00440
00441 GetVideoModes();
00442 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00443 return "Failed to set up Allegro video";
00444 }
00445 MarkWholeScreenDirty();
00446 set_close_button_callback(HandleExitGameRequest);
00447
00448 return NULL;
00449 }
00450
00451 void VideoDriver_Allegro::Stop()
00452 {
00453 if (--_allegro_instance_count == 0) allegro_exit();
00454 }
00455
00456 #if defined(UNIX) || defined(__OS2__) || defined(PSP) || defined(DOS)
00457 # include <sys/time.h>
00458
00459 static uint32 GetTime()
00460 {
00461 struct timeval tim;
00462
00463 gettimeofday(&tim, NULL);
00464 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
00465 }
00466 #else
00467 static uint32 GetTime()
00468 {
00469 return GetTickCount();
00470 }
00471 #endif
00472
00473
00474 void VideoDriver_Allegro::MainLoop()
00475 {
00476 uint32 cur_ticks = GetTime();
00477 uint32 last_cur_ticks = cur_ticks;
00478 uint32 next_tick = cur_ticks + 30;
00479 uint32 pal_tick = 0;
00480
00481 for (;;) {
00482 uint32 prev_cur_ticks = cur_ticks;
00483 InteractiveRandom();
00484
00485 PollEvent();
00486 if (_exit_game) return;
00487
00488 #if defined(_DEBUG)
00489 if (_shift_pressed)
00490 #else
00491
00492
00493 if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
00494 #endif
00495 {
00496 if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
00497 } else if (_fast_forward & 2) {
00498 _fast_forward = 0;
00499 }
00500
00501 cur_ticks = GetTime();
00502 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00503 _realtime_tick += cur_ticks - last_cur_ticks;
00504 last_cur_ticks = cur_ticks;
00505 next_tick = cur_ticks + 30;
00506
00507 bool old_ctrl_pressed = _ctrl_pressed;
00508
00509 _ctrl_pressed = !!(key_shifts & KB_CTRL_FLAG);
00510 _shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
00511
00512
00513 _dirkeys =
00514 (key[KEY_LEFT] ? 1 : 0) |
00515 (key[KEY_UP] ? 2 : 0) |
00516 (key[KEY_RIGHT] ? 4 : 0) |
00517 (key[KEY_DOWN] ? 8 : 0);
00518
00519 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
00520
00521 GameLoop();
00522
00523 UpdateWindows();
00524 if (++pal_tick > 4) {
00525 CheckPaletteAnim();
00526 pal_tick = 1;
00527 }
00528 DrawSurfaceToScreen();
00529 } else {
00530 CSleep(1);
00531 NetworkDrawChatMessage();
00532 DrawMouseCursor();
00533 DrawSurfaceToScreen();
00534 }
00535 }
00536 }
00537
00538 bool VideoDriver_Allegro::ChangeResolution(int w, int h)
00539 {
00540 return CreateMainSurface(w, h);
00541 }
00542
00543 bool VideoDriver_Allegro::ToggleFullscreen(bool fullscreen)
00544 {
00545 #ifdef DOS
00546 return false;
00547 #else
00548 _fullscreen = fullscreen;
00549 GetVideoModes();
00550 if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
00551
00552 _fullscreen ^= true;
00553 return false;
00554 }
00555 return true;
00556 #endif
00557 }
00558
00559 #endif