2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
9 * @file allegro_v.cpp Implementation of the Allegro video driver.
10 * @note Implementing threaded pushing of data to the display is
11 * not faster (it's a few percent slower) in contrast to the
12 * results gained with threading it for SDL.
17 #include "../stdafx.h"
18 #include "../openttd.h"
19 #include "../error_func.h"
20 #include "../gfx_func.h"
21 #include "../blitter/factory.hpp"
22 #include "../core/random_func.hpp"
23 #include "../core/math_func.hpp"
24 #include "../framerate_type.h"
25 #include "../progress.h"
26 #include "../thread.h"
27 #include "../window_func.h"
28 #include "allegro_v.h"
31 #include "../safeguards.h"
34 /* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
35 * be triggered, so rereplace the signals and make the debugger useful. */
39 static FVideoDriver_Allegro iFVideoDriver_Allegro
;
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
;
46 static Palette _local_palette
; ///< Current palette to use for drawing.
48 void VideoDriver_Allegro::MakeDirty(int left
, int top
, int width
, int height
)
50 if (_num_dirty_rects
< MAX_DIRTY_RECTS
) {
51 _dirty_rects
[_num_dirty_rects
].x
= left
;
52 _dirty_rects
[_num_dirty_rects
].y
= top
;
53 _dirty_rects
[_num_dirty_rects
].width
= width
;
54 _dirty_rects
[_num_dirty_rects
].height
= height
;
59 void VideoDriver_Allegro::Paint()
61 PerformanceMeasurer
framerate(PFE_VIDEO
);
63 int n
= _num_dirty_rects
;
67 if (n
> MAX_DIRTY_RECTS
) {
68 blit(_allegro_screen
, screen
, 0, 0, 0, 0, _allegro_screen
->w
, _allegro_screen
->h
);
72 for (int i
= 0; i
< n
; i
++) {
73 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
);
78 static void UpdatePalette(uint start
, uint count
)
82 uint end
= start
+ count
;
83 for (uint i
= start
; i
!= end
; i
++) {
84 pal
[i
].r
= _local_palette
.palette
[i
].r
/ 4;
85 pal
[i
].g
= _local_palette
.palette
[i
].g
/ 4;
86 pal
[i
].b
= _local_palette
.palette
[i
].b
/ 4;
90 set_palette_range(pal
, start
, end
- 1, 1);
93 static void InitPalette()
95 UpdatePalette(0, 256);
98 void VideoDriver_Allegro::CheckPaletteAnim()
100 if (!CopyPalette(_local_palette
)) return;
102 Blitter
*blitter
= BlitterFactory::GetCurrentBlitter();
104 switch (blitter
->UsePaletteAnimation()) {
105 case Blitter::PALETTE_ANIMATION_VIDEO_BACKEND
:
106 UpdatePalette(_local_palette
.first_dirty
, _local_palette
.count_dirty
);
109 case Blitter::PALETTE_ANIMATION_BLITTER
:
110 blitter
->PaletteAnimate(_local_palette
);
113 case Blitter::PALETTE_ANIMATION_NONE
:
121 static const Dimension default_resolutions
[] = {
135 static void GetVideoModes()
137 /* Need to set a gfx_mode as there is NO other way to autodetect for
138 * cards ourselves... and we need a card to get the modes. */
139 set_gfx_mode(_fullscreen
? GFX_AUTODETECT_FULLSCREEN
: GFX_AUTODETECT_WINDOWED
, 640, 480, 0, 0);
141 _resolutions
.clear();
143 GFX_MODE_LIST
*mode_list
= get_gfx_mode_list(gfx_driver
->id
);
144 if (mode_list
== nullptr) {
145 _resolutions
.assign(std::begin(default_resolutions
), std::end(default_resolutions
));
149 GFX_MODE
*modes
= mode_list
->mode
;
151 for (int i
= 0; modes
[i
].bpp
!= 0; i
++) {
152 uint w
= modes
[i
].width
;
153 uint h
= modes
[i
].height
;
154 if (w
< 640 || h
< 480) continue;
155 if (std::find(_resolutions
.begin(), _resolutions
.end(), Dimension(w
, h
)) != _resolutions
.end()) continue;
156 _resolutions
.emplace_back(w
, h
);
161 destroy_gfx_mode_list(mode_list
);
164 static void GetAvailableVideoMode(uint
*w
, uint
*h
)
166 /* No video modes, so just try it and see where it ends */
167 if (_resolutions
.empty()) return;
169 /* is the wanted mode among the available modes? */
170 if (std::find(_resolutions
.begin(), _resolutions
.end(), Dimension(*w
, *h
)) != _resolutions
.end()) return;
172 /* use the closest possible resolution */
174 uint delta
= Delta(_resolutions
[0].width
, *w
) * Delta(_resolutions
[0].height
, *h
);
175 for (uint i
= 1; i
!= _resolutions
.size(); ++i
) {
176 uint newdelta
= Delta(_resolutions
[i
].width
, *w
) * Delta(_resolutions
[i
].height
, *h
);
177 if (newdelta
< delta
) {
182 *w
= _resolutions
[best
].width
;
183 *h
= _resolutions
[best
].height
;
186 static bool CreateMainSurface(uint w
, uint h
)
188 int bpp
= BlitterFactory::GetCurrentBlitter()->GetScreenDepth();
189 if (bpp
== 0) UserError("Can't use a blitter that blits 0 bpp for normal visuals");
190 set_color_depth(bpp
);
192 GetAvailableVideoMode(&w
, &h
);
193 if (set_gfx_mode(_fullscreen
? GFX_AUTODETECT_FULLSCREEN
: GFX_AUTODETECT_WINDOWED
, w
, h
, 0, 0) != 0) {
194 Debug(driver
, 0, "Allegro: Couldn't allocate a window to draw on '{}'", allegro_error
);
198 /* The size of the screen might be bigger than the part we can actually draw on!
199 * So calculate the size based on the top, bottom, left and right */
200 _allegro_screen
= create_bitmap_ex(bpp
, screen
->cr
- screen
->cl
, screen
->cb
- screen
->ct
);
201 _screen
.width
= _allegro_screen
->w
;
202 _screen
.height
= _allegro_screen
->h
;
203 _screen
.pitch
= ((uint8_t*)screen
->line
[1] - (uint8_t*)screen
->line
[0]) / (bpp
/ 8);
204 _screen
.dst_ptr
= _allegro_screen
->line
[0];
206 /* Initialise the screen so we don't blit garbage to the screen */
207 memset(_screen
.dst_ptr
, 0, static_cast<size_t>(_screen
.height
) * _screen
.pitch
);
209 /* Set the mouse at the place where we expect it */
211 _cursor
.pos
.x
= mouse_x
;
212 _cursor
.pos
.y
= mouse_y
;
214 BlitterFactory::GetCurrentBlitter()->PostResize();
218 std::string caption
= VideoDriver::GetCaption();
219 set_window_title(caption
.c_str());
221 enable_hardware_cursor();
222 select_mouse_cursor(MOUSE_CURSOR_ARROW
);
223 show_mouse(_allegro_screen
);
230 bool VideoDriver_Allegro::ClaimMousePointer()
232 select_mouse_cursor(MOUSE_CURSOR_NONE
);
234 disable_hardware_cursor();
238 std::vector
<int> VideoDriver_Allegro::GetListOfMonitorRefreshRates()
240 std::vector
<int> rates
= {};
242 int refresh_rate
= get_refresh_rate();
243 if (refresh_rate
!= 0) rates
.push_back(refresh_rate
);
248 struct AllegroVkMapping
{
254 #define AS(x, z) {x, 1, z}
255 #define AM(x, y, z, w) {x, y - x + 1, z}
257 static const AllegroVkMapping _vk_mapping
[] = {
258 /* Pageup stuff + up/down */
259 AM(KEY_PGUP
, KEY_PGDN
, WKC_PAGEUP
, WKC_PAGEDOWN
),
261 AS(KEY_DOWN
, WKC_DOWN
),
262 AS(KEY_LEFT
, WKC_LEFT
),
263 AS(KEY_RIGHT
, WKC_RIGHT
),
265 AS(KEY_HOME
, WKC_HOME
),
266 AS(KEY_END
, WKC_END
),
268 AS(KEY_INSERT
, WKC_INSERT
),
269 AS(KEY_DEL
, WKC_DELETE
),
271 /* Map letters & digits */
272 AM(KEY_A
, KEY_Z
, 'A', 'Z'),
273 AM(KEY_0
, KEY_9
, '0', '9'),
275 AS(KEY_ESC
, WKC_ESC
),
276 AS(KEY_PAUSE
, WKC_PAUSE
),
277 AS(KEY_BACKSPACE
, WKC_BACKSPACE
),
279 AS(KEY_SPACE
, WKC_SPACE
),
280 AS(KEY_ENTER
, WKC_RETURN
),
281 AS(KEY_TAB
, WKC_TAB
),
284 AM(KEY_F1
, KEY_F12
, WKC_F1
, WKC_F12
),
287 AM(KEY_0_PAD
, KEY_9_PAD
, '0', '9'),
288 AS(KEY_SLASH_PAD
, WKC_NUM_DIV
),
289 AS(KEY_ASTERISK
, WKC_NUM_MUL
),
290 AS(KEY_MINUS_PAD
, WKC_NUM_MINUS
),
291 AS(KEY_PLUS_PAD
, WKC_NUM_PLUS
),
292 AS(KEY_ENTER_PAD
, WKC_NUM_ENTER
),
293 AS(KEY_DEL_PAD
, WKC_DELETE
),
295 /* Other non-letter keys */
296 AS(KEY_SLASH
, WKC_SLASH
),
297 AS(KEY_SEMICOLON
, WKC_SEMICOLON
),
298 AS(KEY_EQUALS
, WKC_EQUALS
),
299 AS(KEY_OPENBRACE
, WKC_L_BRACKET
),
300 AS(KEY_BACKSLASH
, WKC_BACKSLASH
),
301 AS(KEY_CLOSEBRACE
, WKC_R_BRACKET
),
303 AS(KEY_QUOTE
, WKC_SINGLEQUOTE
),
304 AS(KEY_COMMA
, WKC_COMMA
),
305 AS(KEY_MINUS
, WKC_MINUS
),
306 AS(KEY_STOP
, WKC_PERIOD
),
307 AS(KEY_TILDE
, WKC_BACKQUOTE
),
310 static uint32_t ConvertAllegroKeyIntoMy(char32_t
*character
)
313 int unicode
= ureadkey(&scancode
);
317 for (const auto &map
: _vk_mapping
) {
318 if (IsInsideBS(scancode
, map
.vk_from
, map
.vk_count
)) {
319 key
= scancode
- map
.vk_from
+ map
.map_to
;
324 if (key_shifts
& KB_SHIFT_FLAG
) key
|= WKC_SHIFT
;
325 if (key_shifts
& KB_CTRL_FLAG
) key
|= WKC_CTRL
;
326 if (key_shifts
& KB_ALT_FLAG
) key
|= WKC_ALT
;
328 Debug(driver
, 0, "Scancode character pressed {}", scancode
);
329 Debug(driver
, 0, "Unicode character pressed {}", unicode
);
332 *character
= unicode
;
336 static const uint LEFT_BUTTON
= 0;
337 static const uint RIGHT_BUTTON
= 1;
339 bool VideoDriver_Allegro::PollEvent()
343 bool mouse_action
= false;
346 static int prev_button_state
;
347 if (prev_button_state
!= mouse_b
) {
348 uint diff
= prev_button_state
^ mouse_b
;
350 uint button
= FindFirstBit(diff
);
351 ClrBit(diff
, button
);
352 if (HasBit(mouse_b
, button
)) {
353 /* Pressed mouse button */
354 if (_rightclick_emulate
&& (key_shifts
& KB_CTRL_FLAG
)) {
355 button
= RIGHT_BUTTON
;
356 ClrBit(diff
, RIGHT_BUTTON
);
360 _left_button_down
= true;
364 _right_button_down
= true;
365 _right_button_clicked
= true;
373 /* Released mouse button */
374 if (_rightclick_emulate
) {
375 _right_button_down
= false;
376 _left_button_down
= false;
377 _left_button_clicked
= false;
378 } else if (button
== LEFT_BUTTON
) {
379 _left_button_down
= false;
380 _left_button_clicked
= false;
381 } else if (button
== RIGHT_BUTTON
) {
382 _right_button_down
= false;
386 prev_button_state
= mouse_b
;
391 if (_cursor
.UpdateCursorPosition(mouse_x
, mouse_y
)) {
392 position_mouse(_cursor
.pos
.x
, _cursor
.pos
.y
);
394 if (_cursor
.delta
.x
!= 0 || _cursor
.delta
.y
) mouse_action
= true;
396 static int prev_mouse_z
= 0;
397 if (prev_mouse_z
!= mouse_z
) {
398 _cursor
.wheel
= (prev_mouse_z
- mouse_z
) < 0 ? -1 : 1;
399 prev_mouse_z
= mouse_z
;
403 if (mouse_action
) HandleMouseEvents();
406 if ((key_shifts
& KB_ALT_FLAG
) && (key
[KEY_ENTER
] || key
[KEY_F
])) {
407 ToggleFullScreen(!_fullscreen
);
408 } else if (keypressed()) {
410 uint keycode
= ConvertAllegroKeyIntoMy(&character
);
411 HandleKeypress(keycode
, character
);
418 * There are multiple modules that might be using Allegro and
419 * Allegro can only be initiated once.
421 int _allegro_instance_count
= 0;
423 std::optional
<std::string_view
> VideoDriver_Allegro::Start(const StringList
¶m
)
425 if (_allegro_instance_count
== 0 && install_allegro(SYSTEM_AUTODETECT
, &errno
, nullptr)) {
426 Debug(driver
, 0, "allegro: install_allegro failed '{}'", allegro_error
);
427 return "Failed to set up Allegro";
429 _allegro_instance_count
++;
431 this->UpdateAutoResolution();
438 /* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
439 * be triggered, so rereplace the signals and make the debugger useful. */
440 signal(SIGABRT
, nullptr);
441 signal(SIGSEGV
, nullptr);
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 this->is_game_threaded
= !GetDriverParamBool(param
, "no_threads") && !GetDriverParamBool(param
, "no_thread");
456 void VideoDriver_Allegro::Stop()
458 if (--_allegro_instance_count
== 0) allegro_exit();
461 void VideoDriver_Allegro::InputLoop()
463 bool old_ctrl_pressed
= _ctrl_pressed
;
465 _ctrl_pressed
= !!(key_shifts
& KB_CTRL_FLAG
);
466 _shift_pressed
= !!(key_shifts
& KB_SHIFT_FLAG
);
468 /* Speedup when pressing tab, except when using ALT+TAB
469 * to switch to another application. */
470 this->fast_forward_key_pressed
= key
[KEY_TAB
] && (key_shifts
& KB_ALT_FLAG
) == 0;
472 /* Determine which directional keys are down. */
474 (key
[KEY_LEFT
] ? 1 : 0) |
475 (key
[KEY_UP
] ? 2 : 0) |
476 (key
[KEY_RIGHT
] ? 4 : 0) |
477 (key
[KEY_DOWN
] ? 8 : 0);
479 if (old_ctrl_pressed
!= _ctrl_pressed
) HandleCtrlChanged();
482 void VideoDriver_Allegro::MainLoop()
484 this->StartGameThread();
487 if (_exit_game
) break;
490 this->SleepTillNextTick();
493 this->StopGameThread();
496 bool VideoDriver_Allegro::ChangeResolution(int w
, int h
)
498 return CreateMainSurface(w
, h
);
501 bool VideoDriver_Allegro::ToggleFullscreen(bool fullscreen
)
503 _fullscreen
= fullscreen
;
504 GetVideoModes(); // get the list of available video modes
505 if (_resolutions
.empty() || !this->ChangeResolution(_cur_resolution
.width
, _cur_resolution
.height
)) {
506 /* switching resolution failed, put back full_screen to original status */
513 bool VideoDriver_Allegro::AfterBlitterChange()
515 return CreateMainSurface(_screen
.width
, _screen
.height
);
518 #endif /* WITH_ALLEGRO */