allegro_v.cpp

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

Generated on Mon May 9 05:19:03 2011 for OpenTTD by  doxygen 1.6.1