allegro_v.cpp

Go to the documentation of this file.
00001 /* $Id: allegro_v.cpp 18709 2010-01-04 02:32:36Z peter1138 $ */
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   BlitterFactoryBase::GetCurrentBlitter()->PostResize();
00225 
00226   InitPalette();
00227 
00228   char caption[32];
00229   snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
00230   set_window_title(caption);
00231 
00232   GameSizeChanged();
00233 
00234   return true;
00235 }
00236 
00237 struct VkMapping {
00238   uint16 vk_from;
00239   byte vk_count;
00240   byte map_to;
00241 };
00242 
00243 #define AS(x, z) {x, 0, z}
00244 #define AM(x, y, z, w) {x, y - x, z}
00245 
00246 static const VkMapping _vk_mapping[] = {
00247   /* Pageup stuff + up/down */
00248   AM(KEY_PGUP, KEY_PGDN, WKC_PAGEUP, WKC_PAGEDOWN),
00249   AS(KEY_UP,     WKC_UP),
00250   AS(KEY_DOWN,   WKC_DOWN),
00251   AS(KEY_LEFT,   WKC_LEFT),
00252   AS(KEY_RIGHT,  WKC_RIGHT),
00253 
00254   AS(KEY_HOME,   WKC_HOME),
00255   AS(KEY_END,    WKC_END),
00256 
00257   AS(KEY_INSERT, WKC_INSERT),
00258   AS(KEY_DEL,    WKC_DELETE),
00259 
00260   /* Map letters & digits */
00261   AM(KEY_A, KEY_Z, 'A', 'Z'),
00262   AM(KEY_0, KEY_9, '0', '9'),
00263 
00264   AS(KEY_ESC,       WKC_ESC),
00265   AS(KEY_PAUSE,     WKC_PAUSE),
00266   AS(KEY_BACKSPACE, WKC_BACKSPACE),
00267 
00268   AS(KEY_SPACE,     WKC_SPACE),
00269   AS(KEY_ENTER,     WKC_RETURN),
00270   AS(KEY_TAB,       WKC_TAB),
00271 
00272   /* Function keys */
00273   AM(KEY_F1, KEY_F12, WKC_F1, WKC_F12),
00274 
00275   /* Numeric part. */
00276   AM(KEY_0_PAD, KEY_9_PAD, '0', '9'),
00277   AS(KEY_SLASH_PAD,   WKC_NUM_DIV),
00278   AS(KEY_ASTERISK,    WKC_NUM_MUL),
00279   AS(KEY_MINUS_PAD,   WKC_NUM_MINUS),
00280   AS(KEY_PLUS_PAD,    WKC_NUM_PLUS),
00281   AS(KEY_ENTER_PAD,   WKC_NUM_ENTER),
00282   AS(KEY_DEL_PAD,     WKC_DELETE),
00283 
00284   /* Other non-letter keys */
00285   AS(KEY_SLASH,        WKC_SLASH),
00286   AS(KEY_SEMICOLON,    WKC_SEMICOLON),
00287   AS(KEY_EQUALS,       WKC_EQUALS),
00288   AS(KEY_OPENBRACE,    WKC_L_BRACKET),
00289   AS(KEY_BACKSLASH,    WKC_BACKSLASH),
00290   AS(KEY_CLOSEBRACE,   WKC_R_BRACKET),
00291 
00292   AS(KEY_QUOTE,   WKC_SINGLEQUOTE),
00293   AS(KEY_COMMA,   WKC_COMMA),
00294   AS(KEY_MINUS,   WKC_MINUS),
00295   AS(KEY_STOP,    WKC_PERIOD),
00296   AS(KEY_TILDE,   WKC_BACKQUOTE),
00297 };
00298 
00299 static uint32 ConvertAllegroKeyIntoMy()
00300 {
00301   int scancode;
00302   int unicode = ureadkey(&scancode);
00303 
00304   const VkMapping *map;
00305   uint key = 0;
00306 
00307   for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
00308     if ((uint)(scancode - map->vk_from) <= map->vk_count) {
00309       key = scancode - map->vk_from + map->map_to;
00310       break;
00311     }
00312   }
00313 
00314   if (key_shifts & KB_SHIFT_FLAG) key |= WKC_SHIFT;
00315   if (key_shifts & KB_CTRL_FLAG)  key |= WKC_CTRL;
00316   if (key_shifts & KB_ALT_FLAG)   key |= WKC_ALT;
00317 #if 0
00318   DEBUG(driver, 0, "Scancode character pressed %u", scancode);
00319   DEBUG(driver, 0, "Unicode character pressed %u", unicode);
00320 #endif
00321   return (key << 16) + unicode;
00322 }
00323 
00324 enum {
00325   LEFT_BUTTON,
00326   RIGHT_BUTTON,
00327 };
00328 
00329 static void PollEvent()
00330 {
00331   poll_mouse();
00332 
00333   bool mouse_action = false;
00334 
00335   /* Mouse buttons */
00336   static int prev_button_state;
00337   if (prev_button_state != mouse_b) {
00338     uint diff = prev_button_state ^ mouse_b;
00339     while (diff != 0) {
00340       int button = FindFirstBit(diff);
00341       ClrBit(diff, button);
00342       if (HasBit(mouse_b, button)) {
00343         /* Pressed mouse button */
00344         if (_rightclick_emulate && (key_shifts & KB_CTRL_FLAG)) {
00345           button = RIGHT_BUTTON;
00346           ClrBit(diff, RIGHT_BUTTON);
00347         }
00348         switch (button) {
00349           case LEFT_BUTTON:
00350             _left_button_down = true;
00351             break;
00352 
00353           case RIGHT_BUTTON:
00354             _right_button_down = true;
00355             _right_button_clicked = true;
00356             break;
00357 
00358           default:
00359             /* ignore rest */
00360             break;
00361         }
00362       } else {
00363         /* Released mouse button */
00364         if (_rightclick_emulate) {
00365           _right_button_down = false;
00366           _left_button_down = false;
00367           _left_button_clicked = false;
00368         } else if (button == LEFT_BUTTON) {
00369           _left_button_down = false;
00370           _left_button_clicked = false;
00371         } else if (button == RIGHT_BUTTON) {
00372           _right_button_down = false;
00373         }
00374       }
00375     }
00376     prev_button_state = mouse_b;
00377     mouse_action = true;
00378   }
00379 
00380   /* Mouse movement */
00381   int dx = mouse_x - _cursor.pos.x;
00382   int dy = mouse_y - _cursor.pos.y;
00383   if (dx != 0 || dy != 0) {
00384     if (_cursor.fix_at) {
00385       _cursor.delta.x = dx;
00386       _cursor.delta.y = dy;
00387       position_mouse(_cursor.pos.x, _cursor.pos.y);
00388     } else {
00389       _cursor.delta.x = dx;
00390       _cursor.delta.y = dy;
00391       _cursor.pos.x = mouse_x;
00392       _cursor.pos.y = mouse_y;
00393       _cursor.dirty = true;
00394     }
00395     mouse_action = true;
00396   }
00397 
00398   static int prev_mouse_z = 0;
00399   if (prev_mouse_z != mouse_z) {
00400     _cursor.wheel = (prev_mouse_z - mouse_z) < 0 ? -1 : 1;
00401     prev_mouse_z = mouse_z;
00402     mouse_action = true;
00403   }
00404 
00405   if (mouse_action) HandleMouseEvents();
00406 
00407   poll_keyboard();
00408   if ((key_shifts & KB_ALT_FLAG) && (key[KEY_ENTER] || key[KEY_F])) {
00409     ToggleFullScreen(!_fullscreen);
00410   } else if (keypressed()) {
00411     HandleKeypress(ConvertAllegroKeyIntoMy());
00412   }
00413 }
00414 
00417 int _allegro_instance_count = 0;
00418 
00419 const char *VideoDriver_Allegro::Start(const char * const *parm)
00420 {
00421   if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
00422     DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
00423     return "Failed to set up Allegro";
00424   }
00425   _allegro_instance_count++;
00426 
00427   install_timer();
00428   install_mouse();
00429   install_keyboard();
00430 
00431 #if defined _DEBUG
00432 /* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
00433  * be triggered, so rereplace the signals and make the debugger userful. */
00434   signal(SIGABRT, NULL);
00435   signal(SIGSEGV, NULL);
00436 #endif
00437 
00438 #if defined(DOS)
00439   /* Force DOS builds to ALWAYS use full screen as
00440    * it can't do windowed. */
00441   _fullscreen = true;
00442 #endif
00443 
00444   GetVideoModes();
00445   if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
00446     return "Failed to set up Allegro video";
00447   }
00448   MarkWholeScreenDirty();
00449   set_close_button_callback(HandleExitGameRequest);
00450 
00451   return NULL;
00452 }
00453 
00454 void VideoDriver_Allegro::Stop()
00455 {
00456   if (--_allegro_instance_count == 0) allegro_exit();
00457 }
00458 
00459 #if defined(UNIX) || defined(__OS2__) || defined(PSP) || defined(DOS)
00460 # include <sys/time.h> /* gettimeofday */
00461 
00462 static uint32 GetTime()
00463 {
00464   struct timeval tim;
00465 
00466   gettimeofday(&tim, NULL);
00467   return tim.tv_usec / 1000 + tim.tv_sec * 1000;
00468 }
00469 #else
00470 static uint32 GetTime()
00471 {
00472   return GetTickCount();
00473 }
00474 #endif
00475 
00476 
00477 void VideoDriver_Allegro::MainLoop()
00478 {
00479   uint32 cur_ticks = GetTime();
00480   uint32 last_cur_ticks = cur_ticks;
00481   uint32 next_tick = cur_ticks + 30;
00482   uint32 pal_tick = 0;
00483 
00484   for (;;) {
00485     uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
00486     InteractiveRandom(); // randomness
00487 
00488     PollEvent();
00489     if (_exit_game) return;
00490 
00491 #if defined(_DEBUG)
00492     if (_shift_pressed)
00493 #else
00494     /* Speedup when pressing tab, except when using ALT+TAB
00495      * to switch to another application */
00496     if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
00497 #endif
00498     {
00499       if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
00500     } else if (_fast_forward & 2) {
00501       _fast_forward = 0;
00502     }
00503 
00504     cur_ticks = GetTime();
00505     if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
00506       _realtime_tick += cur_ticks - last_cur_ticks;
00507       last_cur_ticks = cur_ticks;
00508       next_tick = cur_ticks + 30;
00509 
00510       bool old_ctrl_pressed = _ctrl_pressed;
00511 
00512       _ctrl_pressed  = !!(key_shifts & KB_CTRL_FLAG);
00513       _shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
00514 
00515       /* determine which directional keys are down */
00516       _dirkeys =
00517         (key[KEY_LEFT]  ? 1 : 0) |
00518         (key[KEY_UP]    ? 2 : 0) |
00519         (key[KEY_RIGHT] ? 4 : 0) |
00520         (key[KEY_DOWN]  ? 8 : 0);
00521 
00522       if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
00523 
00524       GameLoop();
00525 
00526       UpdateWindows();
00527       if (++pal_tick > 4) {
00528         CheckPaletteAnim();
00529         pal_tick = 1;
00530       }
00531       DrawSurfaceToScreen();
00532     } else {
00533       CSleep(1);
00534       NetworkDrawChatMessage();
00535       DrawMouseCursor();
00536       DrawSurfaceToScreen();
00537     }
00538   }
00539 }
00540 
00541 bool VideoDriver_Allegro::ChangeResolution(int w, int h)
00542 {
00543   return CreateMainSurface(w, h);
00544 }
00545 
00546 bool VideoDriver_Allegro::ToggleFullscreen(bool fullscreen)
00547 {
00548 #ifdef DOS
00549   return false;
00550 #else
00551   _fullscreen = fullscreen;
00552   GetVideoModes(); // get the list of available video modes
00553   if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
00554     /* switching resolution failed, put back full_screen to original status */
00555     _fullscreen ^= true;
00556     return false;
00557   }
00558   return true;
00559 #endif
00560 }
00561 
00562 #endif /* WITH_ALLEGRO */

Generated on Tue Jan 5 21:03:00 2010 for OpenTTD by  doxygen 1.5.6