allegro_v.cpp

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

Generated on Sun Sep 13 08:19:21 2009 for OpenTTD by  doxygen 1.5.6