allegro_v.cpp

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

Generated on Wed Dec 23 23:27:56 2009 for OpenTTD by  doxygen 1.5.6