Fix old map array tunnel head conversion
[openttd/fttd.git] / src / video / allegro_v.cpp
blob9a44d95c6b598c1aa3ec3c32bef2537c754de588
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /**
11 * @file allegro_v.cpp Implementation of the Allegro video driver.
12 * @note Implementing threaded pushing of data to the display is
13 * not faster (it's a few percent slower) in contrast to the
14 * results gained with threading it for SDL.
17 #ifdef WITH_ALLEGRO
19 #include "../stdafx.h"
20 #include "../debug.h"
21 #include "../openttd.h"
22 #include "../gfx_func.h"
23 #include "../rev.h"
24 #include "../blitter/blitter.h"
25 #include "../network/network.h"
26 #include "../core/random_func.hpp"
27 #include "../core/math_func.hpp"
28 #include "allegro_v.h"
29 #include <allegro.h>
31 #ifdef _DEBUG
32 /* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
33 * be triggered, so rereplace the signals and make the debugger useful. */
34 #include <signal.h>
35 #endif
37 /** Factory for the allegro video driver. */
38 static VideoDriverFactory <VideoDriver_Allegro>
39 iFVideoDriver_Allegro (4, "allegro", "Allegro Video Driver");
41 static BITMAP *_allegro_screen;
43 #define MAX_DIRTY_RECTS 100
44 static PointDimension _dirty_rects[MAX_DIRTY_RECTS];
45 static int _num_dirty_rects;
47 void VideoDriver_Allegro::MakeDirty(int left, int top, int width, int height)
49 if (_num_dirty_rects < MAX_DIRTY_RECTS) {
50 _dirty_rects[_num_dirty_rects].x = left;
51 _dirty_rects[_num_dirty_rects].y = top;
52 _dirty_rects[_num_dirty_rects].width = width;
53 _dirty_rects[_num_dirty_rects].height = height;
55 _num_dirty_rects++;
58 static void DrawSurfaceToScreen()
60 int n = _num_dirty_rects;
61 if (n == 0) return;
63 _num_dirty_rects = 0;
64 if (n > MAX_DIRTY_RECTS) {
65 blit(_allegro_screen, screen, 0, 0, 0, 0, _allegro_screen->w, _allegro_screen->h);
66 return;
69 for (int i = 0; i < n; i++) {
70 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);
75 static void UpdatePalette(uint start, uint count)
77 static PALETTE pal;
79 uint end = start + count;
80 for (uint i = start; i != end; i++) {
81 pal[i].r = _cur_palette[i].r / 4;
82 pal[i].g = _cur_palette[i].g / 4;
83 pal[i].b = _cur_palette[i].b / 4;
84 pal[i].filler = 0;
87 set_palette_range(pal, start, end - 1, 1);
90 static void InitPalette()
92 UpdatePalette(0, 256);
95 static void CheckPaletteAnim()
97 if (_cur_palette_count_dirty != 0) {
98 switch (Blitter::get()->palette_animation) {
99 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND:
100 UpdatePalette (_cur_palette_first_dirty, _cur_palette_count_dirty);
101 break;
103 case Blitter::PALETTE_ANIMATION_BLITTER:
104 VideoDriver::PaletteAnimate (_cur_palette);
105 break;
107 case Blitter::PALETTE_ANIMATION_NONE:
108 break;
110 default:
111 NOT_REACHED();
113 _cur_palette_count_dirty = 0;
117 static const Dimension default_resolutions[] = {
118 { 640, 480},
119 { 800, 600},
120 {1024, 768},
121 {1152, 864},
122 {1280, 800},
123 {1280, 960},
124 {1280, 1024},
125 {1400, 1050},
126 {1600, 1200},
127 {1680, 1050},
128 {1920, 1200}
131 static void GetVideoModes()
133 /* Need to set a gfx_mode as there is NO other way to autodetect for
134 * cards ourselves... and we need a card to get the modes. */
135 set_gfx_mode(_fullscreen ? GFX_AUTODETECT_FULLSCREEN : GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
137 GFX_MODE_LIST *mode_list = get_gfx_mode_list(gfx_driver->id);
138 if (mode_list == NULL) {
139 memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
140 _num_resolutions = lengthof(default_resolutions);
141 return;
144 GFX_MODE *modes = mode_list->mode;
146 int n = 0;
147 for (int i = 0; modes[i].bpp != 0; i++) {
148 uint w = modes[i].width;
149 uint h = modes[i].height;
150 if (w >= 640 && h >= 480) {
151 int j;
152 for (j = 0; j < n; j++) {
153 if (_resolutions[j].width == w && _resolutions[j].height == h) break;
156 if (j == n) {
157 _resolutions[j].width = w;
158 _resolutions[j].height = h;
159 if (++n == lengthof(_resolutions)) break;
163 _num_resolutions = n;
164 SortResolutions(_num_resolutions);
166 destroy_gfx_mode_list(mode_list);
169 static void GetAvailableVideoMode(uint *w, uint *h)
171 /* No video modes, so just try it and see where it ends */
172 if (_num_resolutions == 0) return;
174 /* is the wanted mode among the available modes? */
175 for (int i = 0; i != _num_resolutions; i++) {
176 if (*w == _resolutions[i].width && *h == _resolutions[i].height) return;
179 /* use the closest possible resolution */
180 int best = 0;
181 uint delta = Delta(_resolutions[0].width, *w) * Delta(_resolutions[0].height, *h);
182 for (int i = 1; i != _num_resolutions; ++i) {
183 uint newdelta = Delta(_resolutions[i].width, *w) * Delta(_resolutions[i].height, *h);
184 if (newdelta < delta) {
185 best = i;
186 delta = newdelta;
189 *w = _resolutions[best].width;
190 *h = _resolutions[best].height;
193 static bool CreateMainSurface(uint w, uint h)
195 int bpp = Blitter::get()->screen_depth;
196 if (bpp == 0) usererror("Can't use a blitter that blits 0 bpp for normal visuals");
197 set_color_depth(bpp);
199 GetAvailableVideoMode(&w, &h);
200 if (set_gfx_mode(_fullscreen ? GFX_AUTODETECT_FULLSCREEN : GFX_AUTODETECT_WINDOWED, w, h, 0, 0) != 0) {
201 DEBUG(driver, 0, "Allegro: Couldn't allocate a window to draw on '%s'", allegro_error);
202 return false;
205 /* The size of the screen might be bigger than the part we can actually draw on!
206 * So calculate the size based on the top, bottom, left and right */
207 _allegro_screen = create_bitmap_ex(bpp, screen->cr - screen->cl, screen->cb - screen->ct);
208 uint pitch = ((byte*)screen->line[1] - (byte*)screen->line[0]) / (bpp / 8);
209 _screen_surface.reset (Blitter::get()->create (_allegro_screen->line[0],
210 _allegro_screen->w, _allegro_screen->h, pitch, true));
211 _screen_width = _allegro_screen->w;
212 _screen_height = _allegro_screen->h;
214 /* Initialise the screen so we don't blit garbage to the screen */
215 memset (_allegro_screen->line[0], 0, _screen_height * pitch);
217 /* Set the mouse at the place where we expect it */
218 poll_mouse();
219 _cursor.pos.x = mouse_x;
220 _cursor.pos.y = mouse_y;
222 InitPalette();
224 char caption[32];
225 snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
226 set_window_title(caption);
228 enable_hardware_cursor();
229 select_mouse_cursor(MOUSE_CURSOR_ARROW);
230 show_mouse(_allegro_screen);
232 GameSizeChanged();
234 return true;
237 bool VideoDriver_Allegro::ClaimMousePointer()
239 select_mouse_cursor(MOUSE_CURSOR_NONE);
240 show_mouse(NULL);
241 disable_hardware_cursor();
242 return true;
245 struct VkMapping {
246 uint16 vk_from;
247 byte vk_count;
248 byte map_to;
251 #define AS(x, z) {x, 0, z}
252 #define AM(x, y, z, w) {x, y - x, z}
254 static const VkMapping _vk_mapping[] = {
255 /* Pageup stuff + up/down */
256 AM(KEY_PGUP, KEY_PGDN, WKC_PAGEUP, WKC_PAGEDOWN),
257 AS(KEY_UP, WKC_UP),
258 AS(KEY_DOWN, WKC_DOWN),
259 AS(KEY_LEFT, WKC_LEFT),
260 AS(KEY_RIGHT, WKC_RIGHT),
262 AS(KEY_HOME, WKC_HOME),
263 AS(KEY_END, WKC_END),
265 AS(KEY_INSERT, WKC_INSERT),
266 AS(KEY_DEL, WKC_DELETE),
268 /* Map letters & digits */
269 AM(KEY_A, KEY_Z, 'A', 'Z'),
270 AM(KEY_0, KEY_9, '0', '9'),
272 AS(KEY_ESC, WKC_ESC),
273 AS(KEY_PAUSE, WKC_PAUSE),
274 AS(KEY_BACKSPACE, WKC_BACKSPACE),
276 AS(KEY_SPACE, WKC_SPACE),
277 AS(KEY_ENTER, WKC_RETURN),
278 AS(KEY_TAB, WKC_TAB),
280 /* Function keys */
281 AM(KEY_F1, KEY_F12, WKC_F1, WKC_F12),
283 /* Numeric part. */
284 AM(KEY_0_PAD, KEY_9_PAD, '0', '9'),
285 AS(KEY_SLASH_PAD, WKC_NUM_DIV),
286 AS(KEY_ASTERISK, WKC_NUM_MUL),
287 AS(KEY_MINUS_PAD, WKC_NUM_MINUS),
288 AS(KEY_PLUS_PAD, WKC_NUM_PLUS),
289 AS(KEY_ENTER_PAD, WKC_NUM_ENTER),
290 AS(KEY_DEL_PAD, WKC_DELETE),
292 /* Other non-letter keys */
293 AS(KEY_SLASH, WKC_SLASH),
294 AS(KEY_SEMICOLON, WKC_SEMICOLON),
295 AS(KEY_EQUALS, WKC_EQUALS),
296 AS(KEY_OPENBRACE, WKC_L_BRACKET),
297 AS(KEY_BACKSLASH, WKC_BACKSLASH),
298 AS(KEY_CLOSEBRACE, WKC_R_BRACKET),
300 AS(KEY_QUOTE, WKC_SINGLEQUOTE),
301 AS(KEY_COMMA, WKC_COMMA),
302 AS(KEY_MINUS, WKC_MINUS),
303 AS(KEY_STOP, WKC_PERIOD),
304 AS(KEY_TILDE, WKC_BACKQUOTE),
307 static uint32 ConvertAllegroKeyIntoMy(WChar *character)
309 int scancode;
310 int unicode = ureadkey(&scancode);
312 const VkMapping *map;
313 uint key = 0;
315 for (map = _vk_mapping; map != endof(_vk_mapping); ++map) {
316 if ((uint)(scancode - map->vk_from) <= map->vk_count) {
317 key = scancode - map->vk_from + map->map_to;
318 break;
322 if (key_shifts & KB_SHIFT_FLAG) key |= WKC_SHIFT;
323 if (key_shifts & KB_CTRL_FLAG) key |= WKC_CTRL;
324 if (key_shifts & KB_ALT_FLAG) key |= WKC_ALT;
325 #if 0
326 DEBUG(driver, 0, "Scancode character pressed %u", scancode);
327 DEBUG(driver, 0, "Unicode character pressed %u", unicode);
328 #endif
330 *character = unicode;
331 return key;
334 static const uint LEFT_BUTTON = 0;
335 static const uint RIGHT_BUTTON = 1;
337 static void PollEvent()
339 poll_mouse();
341 bool mouse_action = false;
343 /* Mouse buttons */
344 static int prev_button_state;
345 if (prev_button_state != mouse_b) {
346 uint diff = prev_button_state ^ mouse_b;
347 while (diff != 0) {
348 uint button = FindFirstBit(diff);
349 ClrBit(diff, button);
350 if (HasBit(mouse_b, button)) {
351 /* Pressed mouse button */
352 if (_rightclick_emulate && (key_shifts & KB_CTRL_FLAG)) {
353 button = RIGHT_BUTTON;
354 ClrBit(diff, RIGHT_BUTTON);
356 switch (button) {
357 case LEFT_BUTTON:
358 _left_button_down = true;
359 break;
361 case RIGHT_BUTTON:
362 _right_button_down = true;
363 _right_button_clicked = true;
364 break;
366 default:
367 /* ignore rest */
368 break;
370 } else {
371 /* Released mouse button */
372 if (_rightclick_emulate) {
373 _right_button_down = false;
374 _left_button_down = false;
375 _left_button_clicked = false;
376 } else if (button == LEFT_BUTTON) {
377 _left_button_down = false;
378 _left_button_clicked = false;
379 } else if (button == RIGHT_BUTTON) {
380 _right_button_down = false;
384 prev_button_state = mouse_b;
385 mouse_action = true;
388 /* Mouse movement */
389 if (_cursor.UpdateCursorPosition(mouse_x, mouse_y, false)) {
390 position_mouse(_cursor.pos.x, _cursor.pos.y);
392 if (_cursor.delta.x != 0 || _cursor.delta.y) mouse_action = true;
394 static int prev_mouse_z = 0;
395 if (prev_mouse_z != mouse_z) {
396 _cursor.wheel = (prev_mouse_z - mouse_z) < 0 ? -1 : 1;
397 prev_mouse_z = mouse_z;
398 mouse_action = true;
401 if (mouse_action) HandleMouseEvents();
403 poll_keyboard();
404 if ((key_shifts & KB_ALT_FLAG) && (key[KEY_ENTER] || key[KEY_F])) {
405 ToggleFullScreen(!_fullscreen);
406 } else if (keypressed()) {
407 WChar character;
408 uint keycode = ConvertAllegroKeyIntoMy(&character);
409 HandleKeypress(keycode, character);
414 * There are multiple modules that might be using Allegro and
415 * Allegro can only be initiated once.
417 int _allegro_instance_count = 0;
419 const char *VideoDriver_Allegro::Start(const char * const *parm)
421 if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
422 DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
423 return "Failed to set up Allegro";
425 _allegro_instance_count++;
427 install_timer();
428 install_mouse();
429 install_keyboard();
431 #if defined _DEBUG
432 /* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
433 * be triggered, so rereplace the signals and make the debugger useful. */
434 signal(SIGABRT, NULL);
435 signal(SIGSEGV, NULL);
436 #endif
438 #if defined(DOS)
439 /* Force DOS builds to ALWAYS use full screen as
440 * it can't do windowed. */
441 _fullscreen = true;
442 #endif
444 GetVideoModes();
445 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
446 return "Failed to set up Allegro video";
448 MarkWholeScreenDirty();
449 set_close_button_callback(HandleExitGameRequest);
451 return NULL;
454 void VideoDriver_Allegro::Stop()
456 if (--_allegro_instance_count == 0) allegro_exit();
459 #if defined(UNIX) || defined(__OS2__) || defined(PSP) || defined(DOS)
460 # include <sys/time.h> /* gettimeofday */
462 static uint32 GetTime()
464 struct timeval tim;
466 gettimeofday(&tim, NULL);
467 return tim.tv_usec / 1000 + tim.tv_sec * 1000;
469 #else
470 static uint32 GetTime()
472 return GetTickCount();
474 #endif
477 void VideoDriver_Allegro::MainLoop()
479 uint32 cur_ticks = GetTime();
480 uint32 last_cur_ticks = cur_ticks;
481 uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
483 CheckPaletteAnim();
485 for (;;) {
486 uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
487 InteractiveRandom(); // randomness
489 PollEvent();
490 if (_exit_game) return;
492 #if defined(_DEBUG)
493 if (_shift_pressed)
494 #else
495 /* Speedup when pressing tab, except when using ALT+TAB
496 * to switch to another application */
497 if (key[KEY_TAB] && (key_shifts & KB_ALT_FLAG) == 0)
498 #endif
500 if (!_networking && _game_mode != GM_MENU) _fast_forward |= 2;
501 } else if (_fast_forward & 2) {
502 _fast_forward = 0;
505 cur_ticks = GetTime();
506 if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
507 _realtime_tick += cur_ticks - last_cur_ticks;
508 last_cur_ticks = cur_ticks;
509 next_tick = cur_ticks + MILLISECONDS_PER_TICK;
511 bool old_ctrl_pressed = _ctrl_pressed;
513 _ctrl_pressed = !!(key_shifts & KB_CTRL_FLAG);
514 _shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
516 /* determine which directional keys are down */
517 _dirkeys =
518 (key[KEY_LEFT] ? 1 : 0) |
519 (key[KEY_UP] ? 2 : 0) |
520 (key[KEY_RIGHT] ? 4 : 0) |
521 (key[KEY_DOWN] ? 8 : 0);
523 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
525 GameLoop();
527 UpdateWindows();
528 CheckPaletteAnim();
529 DrawSurfaceToScreen();
530 } else {
531 CSleep(1);
532 NetworkDrawChatMessage();
533 DrawMouseCursor();
534 DrawSurfaceToScreen();
539 bool VideoDriver_Allegro::ChangeResolution(int w, int h)
541 return CreateMainSurface(w, h);
544 bool VideoDriver_Allegro::ToggleFullscreen(bool fullscreen)
546 #ifdef DOS
547 return false;
548 #else
549 _fullscreen = fullscreen;
550 GetVideoModes(); // get the list of available video modes
551 if (_num_resolutions == 0 || !this->ChangeResolution(_cur_resolution.width, _cur_resolution.height)) {
552 /* switching resolution failed, put back full_screen to original status */
553 _fullscreen ^= true;
554 return false;
556 return true;
557 #endif
561 * Switch to a new blitter.
562 * @param blitter The blitter to switch to.
563 * @return False if switching failed and the old blitter could not be restored.
565 bool VideoDriver_Allegro::SwitchBlitter (const Blitter::Info *blitter)
567 const Blitter::Info *old = Blitter::get();
569 Blitter::select (blitter);
571 if (CreateMainSurface (_screen_width, _screen_height)) return true;
573 /* Failed to switch blitter, let's hope we can return to the old one. */
574 Blitter::select (old);
575 return CreateMainSurface (_screen_width, _screen_height);
578 #endif /* WITH_ALLEGRO */