OpenTTD Source 20241224-master-gee860a5c8e
allegro_v.cpp
Go to the documentation of this file.
1/*
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/>.
6 */
7
15#ifdef WITH_ALLEGRO
16
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"
29#include <allegro.h>
30
31#include "../safeguards.h"
32
33#ifdef _DEBUG
34/* Allegro replaces SEGV/ABRT signals meaning that the debugger will never
35 * be triggered, so rereplace the signals and make the debugger useful. */
36#include <signal.h>
37#endif
38
39static FVideoDriver_Allegro iFVideoDriver_Allegro;
40
41static BITMAP *_allegro_screen;
42
43#define MAX_DIRTY_RECTS 100
44static PointDimension _dirty_rects[MAX_DIRTY_RECTS];
45static int _num_dirty_rects;
46static Palette _local_palette;
47
48void VideoDriver_Allegro::MakeDirty(int left, int top, int width, int height)
49{
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;
55 }
56 _num_dirty_rects++;
57}
58
60{
62
63 int n = _num_dirty_rects;
64 if (n == 0) return;
65
66 _num_dirty_rects = 0;
67 if (n > MAX_DIRTY_RECTS) {
68 blit(_allegro_screen, screen, 0, 0, 0, 0, _allegro_screen->w, _allegro_screen->h);
69 return;
70 }
71
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);
74 }
75}
76
77
78static void UpdatePalette(uint start, uint count)
79{
80 static PALETTE pal;
81
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;
87 pal[i].filler = 0;
88 }
89
90 set_palette_range(pal, start, end - 1, 1);
91}
92
93static void InitPalette()
94{
95 UpdatePalette(0, 256);
96}
97
99{
100 if (!CopyPalette(_local_palette)) return;
101
103
104 switch (blitter->UsePaletteAnimation()) {
106 UpdatePalette(_local_palette.first_dirty, _local_palette.count_dirty);
107 break;
108
110 blitter->PaletteAnimate(_local_palette);
111 break;
112
114 break;
115
116 default:
117 NOT_REACHED();
118 }
119}
120
121static const Dimension default_resolutions[] = {
122 { 640, 480},
123 { 800, 600},
124 {1024, 768},
125 {1152, 864},
126 {1280, 800},
127 {1280, 960},
128 {1280, 1024},
129 {1400, 1050},
130 {1600, 1200},
131 {1680, 1050},
132 {1920, 1200}
133};
134
135static void GetVideoModes()
136{
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);
140
141 _resolutions.clear();
142
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));
146 return;
147 }
148
149 GFX_MODE *modes = mode_list->mode;
150
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::ranges::find(_resolutions, Dimension(w, h)) != _resolutions.end()) continue;
156 _resolutions.emplace_back(w, h);
157 }
158
159 SortResolutions();
160
161 destroy_gfx_mode_list(mode_list);
162}
163
164static void GetAvailableVideoMode(uint *w, uint *h)
165{
166 /* No video modes, so just try it and see where it ends */
167 if (_resolutions.empty()) return;
168
169 /* is the wanted mode among the available modes? */
170 if (std::ranges::find(_resolutions, Dimension(*w, *h)) != _resolutions.end()) return;
171
172 /* use the closest possible resolution */
173 uint best = 0;
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) {
178 best = i;
179 delta = newdelta;
180 }
181 }
182 *w = _resolutions[best].width;
183 *h = _resolutions[best].height;
184}
185
186static bool CreateMainSurface(uint w, uint h)
187{
189 if (bpp == 0) UserError("Can't use a blitter that blits 0 bpp for normal visuals");
190 set_color_depth(bpp);
191
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);
195 return false;
196 }
197
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];
205
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);
208
209 /* Set the mouse at the place where we expect it */
210 poll_mouse();
211 _cursor.pos.x = mouse_x;
212 _cursor.pos.y = mouse_y;
213
215
216 InitPalette();
217
218 std::string caption = VideoDriver::GetCaption();
219 set_window_title(caption.c_str());
220
221 enable_hardware_cursor();
222 select_mouse_cursor(MOUSE_CURSOR_ARROW);
223 show_mouse(_allegro_screen);
224
226
227 return true;
228}
229
230bool VideoDriver_Allegro::ClaimMousePointer()
231{
232 select_mouse_cursor(MOUSE_CURSOR_NONE);
233 show_mouse(nullptr);
234 disable_hardware_cursor();
235 return true;
236}
237
239{
240 std::vector<int> rates = {};
241
242 int refresh_rate = get_refresh_rate();
243 if (refresh_rate != 0) rates.push_back(refresh_rate);
244
245 return rates;
246}
247
248struct AllegroVkMapping {
249 uint16_t vk_from;
250 uint8_t vk_count;
251 uint8_t map_to;
252};
253
254#define AS(x, z) {x, 1, z}
255#define AM(x, y, z, w) {x, y - x + 1, z}
256
257static const AllegroVkMapping _vk_mapping[] = {
258 /* Pageup stuff + up/down */
259 AM(KEY_PGUP, KEY_PGDN, WKC_PAGEUP, WKC_PAGEDOWN),
260 AS(KEY_UP, WKC_UP),
261 AS(KEY_DOWN, WKC_DOWN),
262 AS(KEY_LEFT, WKC_LEFT),
263 AS(KEY_RIGHT, WKC_RIGHT),
264
265 AS(KEY_HOME, WKC_HOME),
266 AS(KEY_END, WKC_END),
267
268 AS(KEY_INSERT, WKC_INSERT),
269 AS(KEY_DEL, WKC_DELETE),
270
271 /* Map letters & digits */
272 AM(KEY_A, KEY_Z, 'A', 'Z'),
273 AM(KEY_0, KEY_9, '0', '9'),
274
275 AS(KEY_ESC, WKC_ESC),
276 AS(KEY_PAUSE, WKC_PAUSE),
277 AS(KEY_BACKSPACE, WKC_BACKSPACE),
278
279 AS(KEY_SPACE, WKC_SPACE),
280 AS(KEY_ENTER, WKC_RETURN),
281 AS(KEY_TAB, WKC_TAB),
282
283 /* Function keys */
284 AM(KEY_F1, KEY_F12, WKC_F1, WKC_F12),
285
286 /* Numeric part. */
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),
294
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),
302
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),
308};
309
310static uint32_t ConvertAllegroKeyIntoMy(char32_t *character)
311{
312 int scancode;
313 int unicode = ureadkey(&scancode);
314
315 uint key = 0;
316
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;
320 break;
321 }
322 }
323
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;
327#if 0
328 Debug(driver, 0, "Scancode character pressed {}", scancode);
329 Debug(driver, 0, "Unicode character pressed {}", unicode);
330#endif
331
332 *character = unicode;
333 return key;
334}
335
336static const uint LEFT_BUTTON = 0;
337static const uint RIGHT_BUTTON = 1;
338
340{
341 poll_mouse();
342
343 bool mouse_action = false;
344
345 /* Mouse buttons */
346 static int prev_button_state;
347 if (prev_button_state != mouse_b) {
348 uint diff = prev_button_state ^ mouse_b;
349 while (diff != 0) {
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);
357 }
358 switch (button) {
359 case LEFT_BUTTON:
360 _left_button_down = true;
361 break;
362
363 case RIGHT_BUTTON:
364 _right_button_down = true;
366 break;
367
368 default:
369 /* ignore rest */
370 break;
371 }
372 } else {
373 /* Released mouse button */
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;
383 }
384 }
385 }
386 prev_button_state = mouse_b;
387 mouse_action = true;
388 }
389
390 /* Mouse movement */
391 if (_cursor.UpdateCursorPosition(mouse_x, mouse_y)) {
392 position_mouse(_cursor.pos.x, _cursor.pos.y);
393 }
394 if (_cursor.delta.x != 0 || _cursor.delta.y) mouse_action = true;
395
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;
400 mouse_action = true;
401 }
402
403 if (mouse_action) HandleMouseEvents();
404
405 poll_keyboard();
406 if ((key_shifts & KB_ALT_FLAG) && (key[KEY_ENTER] || key[KEY_F])) {
407 ToggleFullScreen(!_fullscreen);
408 } else if (keypressed()) {
409 char32_t character;
410 uint keycode = ConvertAllegroKeyIntoMy(&character);
411 HandleKeypress(keycode, character);
412 }
413
414 return false;
415}
416
421int _allegro_instance_count = 0;
422
423std::optional<std::string_view> VideoDriver_Allegro::Start(const StringList &param)
424{
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";
428 }
429 _allegro_instance_count++;
430
431 this->UpdateAutoResolution();
432
433 install_timer();
434 install_mouse();
435 install_keyboard();
436
437#if defined _DEBUG
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);
442#endif
443
444 GetVideoModes();
445 if (!CreateMainSurface(_cur_resolution.width, _cur_resolution.height)) {
446 return "Failed to set up Allegro video";
447 }
449 set_close_button_callback(HandleExitGameRequest);
450
451 this->is_game_threaded = !GetDriverParamBool(param, "no_threads") && !GetDriverParamBool(param, "no_thread");
452
453 return std::nullopt;
454}
455
457{
458 if (--_allegro_instance_count == 0) allegro_exit();
459}
460
462{
463 bool old_ctrl_pressed = _ctrl_pressed;
464
465 _ctrl_pressed = !!(key_shifts & KB_CTRL_FLAG);
466 _shift_pressed = !!(key_shifts & KB_SHIFT_FLAG);
467
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;
471
472 /* Determine which directional keys are down. */
473 _dirkeys =
474 (key[KEY_LEFT] ? 1 : 0) |
475 (key[KEY_UP] ? 2 : 0) |
476 (key[KEY_RIGHT] ? 4 : 0) |
477 (key[KEY_DOWN] ? 8 : 0);
478
479 if (old_ctrl_pressed != _ctrl_pressed) HandleCtrlChanged();
480}
481
483{
484 this->StartGameThread();
485
486 for (;;) {
487 if (_exit_game) break;
488
489 this->Tick();
490 this->SleepTillNextTick();
491 }
492
493 this->StopGameThread();
494}
495
497{
498 return CreateMainSurface(w, h);
499}
500
501bool VideoDriver_Allegro::ToggleFullscreen(bool fullscreen)
502{
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 */
507 _fullscreen ^= true;
508 return false;
509 }
510 return true;
511}
512
514{
515 return CreateMainSurface(_screen.width, _screen.height);
516}
517
518#endif /* WITH_ALLEGRO */
#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, maint_cost, ttdpatch_type, class_id, name, preview)
AirportSpec definition for airports with at least one depot.
Base of the Allegro video driver.
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr uint8_t FindFirstBit(T x)
Search the first set bit in a value.
constexpr T ClrBit(T &x, const uint8_t y)
Clears a bit in a variable.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition factory.hpp:138
How all blitters should look like.
Definition base.hpp:29
virtual uint8_t GetScreenDepth()=0
Get the screen depth this blitter works for.
virtual Blitter::PaletteAnimation UsePaletteAnimation()=0
Check if the blitter uses palette animation at all.
@ PALETTE_ANIMATION_NONE
No palette animation.
Definition base.hpp:51
@ PALETTE_ANIMATION_VIDEO_BACKEND
Palette animation should be done by video backend (8bpp only!)
Definition base.hpp:52
@ PALETTE_ANIMATION_BLITTER
The blitter takes care of the palette animation.
Definition base.hpp:53
virtual void PaletteAnimate(const Palette &palette)=0
Called when the 8bpp palette is changed; you should redraw all pixels on the screen that are equal to...
virtual void PostResize()
Post resize event.
Definition base.hpp:205
Factory for the allegro video driver.
Definition allegro_v.h:46
RAII class for measuring simple elements of performance.
void MainLoop() override
Perform the actual drawing.
bool ToggleFullscreen(bool fullscreen) override
Change the full screen setting.
bool PollEvent() override
Process a single system event.
bool ChangeResolution(int w, int h) override
Change the resolution of the window.
void InputLoop() override
Handle input logic, is CTRL pressed, should we fast-forward, etc.
void MakeDirty(int left, int top, int width, int height) override
Mark a particular area dirty.
void Paint() override
Paint the window.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
void Stop() override
Stop this driver.
void CheckPaletteAnim() override
Process any pending palette animation.
std::vector< int > GetListOfMonitorRefreshRates() override
Get a list of refresh rates of each available monitor.
bool AfterBlitterChange() override
Callback invoked after the blitter was changed.
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
static std::string GetCaption()
Get the caption to use for the game's title bar.
void StopGameThread()
Stop the loop for the game-tick.
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:25
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:26
bool _rightclick_emulate
Whether right clicking is emulated.
Definition driver.cpp:27
bool GetDriverParamBool(const StringList &parm, const char *name)
Get a boolean parameter the list of parameters.
Definition driver.cpp:64
@ PFE_VIDEO
Speed of painting drawn video buffer.
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:39
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:41
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:34
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:42
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:44
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:43
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2625
void GameSizeChanged()
Size of the application screen changed.
Definition main_gui.cpp:589
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:2885
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2569
@ WKC_BACKSLASH
\ Backslash
Definition gfx_type.h:100
@ WKC_MINUS
Definition gfx_type.h:105
@ WKC_COMMA
, Comma
Definition gfx_type.h:103
@ WKC_PERIOD
. Period
Definition gfx_type.h:104
@ WKC_EQUALS
= Equals
Definition gfx_type.h:98
@ WKC_SLASH
/ Forward slash
Definition gfx_type.h:96
@ WKC_SINGLEQUOTE
' Single quote
Definition gfx_type.h:102
@ WKC_R_BRACKET
] Right square bracket
Definition gfx_type.h:101
@ WKC_L_BRACKET
[ Left square bracket
Definition gfx_type.h:99
@ WKC_SEMICOLON
; Semicolon
Definition gfx_type.h:97
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1529
constexpr bool IsInsideBS(const T x, const size_t base, const size_t size)
Checks if a value is between a window started at some base point.
constexpr T Delta(const T a, const T b)
Returns the (absolute) difference between two (scalar) variables.
bool CopyPalette(Palette &local_palette, bool force_copy)
Copy the current palette if the palette was updated.
Definition palette.cpp:152
std::vector< std::string > StringList
Type for a list of strings.
Definition string_type.h:60
bool UpdateCursorPosition(int x, int y)
Update cursor position on mouse movement.
Definition gfx.cpp:1728
Point pos
logical mouse position
Definition gfx_type.h:125
int wheel
mouse wheel movement
Definition gfx_type.h:127
Point delta
relative mouse movement in this tick
Definition gfx_type.h:126
Dimensions (a width and height) of a rectangle in 2D.
Information about the currently used palette.
Definition gfx_type.h:328
int first_dirty
The first dirty element.
Definition gfx_type.h:330
int count_dirty
The number of dirty elements.
Definition gfx_type.h:331
Colour palette[256]
Current palette. Entry 0 has to be always fully transparent!
Definition gfx_type.h:329
Specification of a rectangle with an absolute top-left coordinate and a (relative) width/height.
uint8_t b
colour channels in BE order
Definition gfx_type.h:171