OpenTTD Source 20261123-master-g1c55c72fd0
gfx_func.h
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
38
39
40#ifndef GFX_FUNC_H
41#define GFX_FUNC_H
42
43#include "gfx_type.h"
44#include "strings_type.h"
45
46void GameLoop();
47
48void CreateConsole();
49
51extern bool _fullscreen;
53extern CursorVars _cursor;
54extern uint16_t _game_speed;
55
56extern DrawPixelInfo _screen;
57extern bool _screen_disable_anim;
58
59extern std::vector<Dimension> _resolutions;
61extern Palette _cur_palette;
62
65
70inline bool IsLeftButtonDown() { return _input_pressed.Test(ControlInput::Left); }
71
77
82inline bool IsCtrlPressed() { return _input_pressed.Test(ControlInput::Ctrl); }
83
88inline bool IsShiftPressed() { return _input_pressed.Test(ControlInput::Shift); }
89
90void HandleToolbarHotkey(int hotkey);
91void HandleKeypress(uint keycode, char32_t key);
92void HandleTextInput(std::string_view str, bool marked = false,
93 std::optional<size_t> caret = std::nullopt,
94 std::optional<size_t> insert_location = std::nullopt, std::optional<size_t> replacement_end = std::nullopt);
97void UpdateWindows();
98void ChangeGameSpeed(bool enable_fast_forward);
99
100void DrawMouseCursor();
101void ScreenSizeChanged();
102void GameSizeChanged();
103void UpdateGUIZoom();
104bool AdjustGUIZoom(bool automatic);
105void UndrawMouseCursor();
107int GetMaximumScale();
108
109void RedrawScreenRect(int left, int top, int right, int bottom);
110void GfxScroll(int left, int top, int width, int height, int xo, int yo);
111
112Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = _gui_zoom);
113Dimension GetScaledSpriteSize(SpriteID sprid); /* widget.cpp */
114Dimension GetSquareScaledSpriteSize(SpriteID sprid); /* widget.cpp */
115void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
116void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = _gui_zoom);
117void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, Alignment align); /* widget.cpp */
118std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom = _gui_zoom);
119
120int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = AlignmentH::Start, bool underline = false, FontSize fontsize = FontSize::Normal);
121int DrawString(int left, int right, int top, StringID str, ExtendedTextColour colour = TextColour::FromString, Alignment align = AlignmentH::Start, bool underline = false, FontSize fontsize = FontSize::Normal);
122int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = {AlignmentH::Start, AlignmentV::Top}, bool underline = false, FontSize fontsize = FontSize::Normal);
123int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, ExtendedTextColour colour = TextColour::FromString, Alignment align = {AlignmentH::Start, AlignmentV::Top}, bool underline = false, FontSize fontsize = FontSize::Normal);
124bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = {AlignmentH::Start, AlignmentV::Top}, bool underline = false, FontSize fontsize = FontSize::Normal);
125
126void DrawCharCentered(char32_t c, const Rect &r, TextColour colour);
127
128void GfxFillRect(int left, int top, int right, int bottom, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FillRectMode::Opaque);
129void GfxFillPolygon(std::span<const Point> shape, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FillRectMode::Opaque);
130void GfxDrawLine(int left, int top, int right, int bottom, PixelColour colour, int width = 1, int dash = 0);
131void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3);
132void DrawRectOutline(const Rect &r, PixelColour colour, int width = 1, int dash = 0);
133
145inline int DrawString(const Rect &r, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = AlignmentH::Start, bool underline = false, FontSize fontsize = FontSize::Normal)
146{
147 return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
148}
149
151inline int DrawString(const Rect &r, StringID str, ExtendedTextColour colour = TextColour::FromString, Alignment align = AlignmentH::Start, bool underline = false, FontSize fontsize = FontSize::Normal)
152{
153 return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
154}
155
168inline int DrawStringMultiLine(const Rect &r, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = {AlignmentH::Start, AlignmentV::Top}, bool underline = false, FontSize fontsize = FontSize::Normal)
169{
170 return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
171}
172
175{
176 return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
177}
178
193inline bool DrawStringMultiLineWithClipping(const Rect &r, std::string_view str, ExtendedTextColour colour = TextColour::FromString, Alignment align = {AlignmentH::Start, AlignmentV::Top}, bool underline = false, FontSize fontsize = FontSize::Normal)
194{
195 return DrawStringMultiLineWithClipping(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
196}
197
209inline void GfxFillRect(const Rect &r, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FillRectMode::Opaque)
210{
211 GfxFillRect(r.left, r.top, r.right, r.bottom, colour, mode);
212}
213
214Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize = FontSize::Normal);
216uint GetStringListWidth(std::span<const StringID> list, FontSize fontsize = FontSize::Normal);
217Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize fontsize = FontSize::Normal);
218int GetStringHeight(std::string_view str, int maxw, FontSize fontsize = FontSize::Normal);
219int GetStringHeight(StringID str, int maxw);
220int GetStringLineCount(std::string_view str, int maxw);
222Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FontSize::Normal);
224
225void DrawDirtyBlocks();
226void AddDirtyBlock(int left, int top, int right, int bottom);
228
229void CheckBlitter();
230
231bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height);
232
233inline bool FillDrawPixelInfo(DrawPixelInfo *n, const Rect &r)
234{
235 return FillDrawPixelInfo(n, r.left, r.top, r.Width(), r.Height());
236}
237
238/* window.cpp */
239void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
240
241void SetMouseCursorBusy(bool busy);
242void SetMouseCursor(CursorID cursor, PaletteID pal);
243void SetAnimatedMouseCursor(std::span<const AnimCursor> table);
244void SetCursor(CursorID cursor, PaletteID pal);
245void CursorTick();
246void UpdateCursorSize();
247bool ChangeResInGame(int w, int h);
248void SortResolutions();
249bool ToggleFullScreen(bool fs);
250
251/* gfx.cpp */
252uint8_t GetCharacterWidth(FontSize size, char32_t key);
254std::pair<uint8_t, uint8_t> GetBroadestDigit(FontSize size);
255
257
258extern DrawPixelInfo *_cur_dpi;
259
260#endif /* GFX_FUNC_H */
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:29
@ Start
Align to the start, LTR/RTL aware.
@ Top
Align to the top.
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot).
Definition gfx.cpp:42
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition gfx.cpp:40
ControlInputs _input_pressed
Control inputs that are currently pressed.
Definition gfx.cpp:34
ControlInputs _input_clicked
Control inputs that have been actioned.
Definition gfx.cpp:35
Support8bpp _support8bpp
State of the support for 8bpp graphics.
Definition gfx.cpp:38
DirectionKeys _dirkeys
Pressed direction keys.
Definition gfx.cpp:36
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:57
void UpdateCursorSize()
Update cursor dimension.
Definition gfx.cpp:1617
std::pair< uint8_t, uint8_t > GetBroadestDigit(FontSize size)
Determine the broadest digits for guessing the maximum width of a n-digit number.
Definition gfx.cpp:1301
int DrawString(int left, int right, int top, std::string_view str, ExtendedTextColour colour=TextColour::FromString, Alignment align=AlignmentH::Start, bool underline=false, FontSize fontsize=FontSize::Normal)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:663
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize=FontSize::Normal)
Calculates height of string (in pixels).
Definition gfx.cpp:711
void DrawRectOutline(const Rect &r, PixelColour colour, int width=1, int dash=0)
Draw the outline of a Rect.
Definition gfx.cpp:459
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2771
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr, ZoomLevel zoom=_gui_zoom)
Draw a sprite, not in a viewport.
Definition gfx.cpp:1032
bool IsLeftButtonDown()
Test if the left mouse button is pressed.
Definition gfx_func.h:70
Dimension GetSpriteSize(SpriteID sprid, Point *offset=nullptr, ZoomLevel zoom=_gui_zoom)
Get the size of a sprite.
Definition gfx.cpp:966
int GetStringLineCount(std::string_view str, int maxw)
Calculates number of lines of string.
Definition gfx.cpp:735
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize=FontSize::Normal)
Get maximum dimension of a list of strings.
Definition gfx.cpp:933
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition window.cpp:3203
void LoadStringWidthTable(FontSizes fontsizes=FONTSIZES_REQUIRED)
Initialize _stringwidth_table cache for the specified font sizes.
Definition gfx.cpp:1255
void DrawOverlappedWindowForAll(int left, int top, int right, int bottom)
From a rectangle that needs redrawing, find the windows that intersect with the rectangle.
Definition window.cpp:992
Dimension GetSquareScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI, as a square.
Definition widget.cpp:85
bool IsShiftPressed()
Test if the shift key is pressed.
Definition gfx_func.h:88
void HandleToolbarHotkey(int hotkey)
Handle Toolbar hotkey events - can come from a source like the MacBook Touch Bar.
Definition window.cpp:2698
uint GetStringListWidth(std::span< const StringID > list, FontSize fontsize=FontSize::Normal)
Get maximum width of a list of strings.
Definition gfx.cpp:918
void GameSizeChanged()
Size of the application screen changed.
Definition main_gui.cpp:592
void SetMouseCursor(CursorID cursor, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition gfx.cpp:1705
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour=TextColour::FromString, Alignment align={AlignmentH::Start, AlignmentV::Top}, bool underline=false, FontSize fontsize=FontSize::Normal)
Draw string, possibly over multiple lines.
Definition gfx.cpp:782
Dimension GetScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI.
Definition widget.cpp:70
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1689
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition gfx.cpp:1836
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
Definition gfxinit.cpp:325
int GetAutomaticScale()
Get the interface scale for automatic scaling.
Definition gfx.cpp:1819
bool IsRightButtonDown()
Test if the right mouse button is pressed.
Definition gfx_func.h:76
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:3032
Palette _cur_palette
Current palette.
Definition palette.cpp:24
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr)
Draw a sprite in a viewport.
Definition gfx.cpp:1004
void SetAnimatedMouseCursor(std::span< const AnimCursor > table)
Assign an animation to the cursor.
Definition gfx.cpp:1718
int GetMaximumScale()
Get the maximum interface scale.
Definition gfx.cpp:1828
bool IsCtrlPressed()
Test if the ctrl key is pressed.
Definition gfx_func.h:82
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2715
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1272
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize=FontSize::Normal)
Return the string dimension in pixels.
Definition gfx.cpp:894
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode=FillRectMode::Opaque)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:111
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition gfx.cpp:747
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:88
bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, ExtendedTextColour colour=TextColour::FromString, Alignment align={AlignmentH::Start, AlignmentV::Top}, bool underline=false, FontSize fontsize=FontSize::Normal)
Draw a multiline string, possibly over multiple lines, if the region is within the current display cl...
Definition gfx.cpp:867
uint8_t GetDigitWidth(FontSize size=FontSize::Normal)
Return the maximum width of single digit.
Definition gfx.cpp:1285
void HandleTextInput(std::string_view str, bool marked=false, std::optional< size_t > caret=std::nullopt, std::optional< size_t > insert_location=std::nullopt, std::optional< size_t > replacement_end=std::nullopt)
Handle text input.
Definition window.cpp:2807
void GfxFillPolygon(std::span< const Point > shape, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode=FillRectMode::Opaque)
Fill a polygon with colour.
Definition gfx.cpp:208
void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3)
Draws the projection of a parallelepiped.
Definition gfx.cpp:421
void SetCursor(CursorID cursor, PaletteID pal)
Assign an animation or a non-animated sprite to the cursor.
Definition gfx.cpp:1732
bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height)
Set up a clipping area for only drawing into a certain area.
Definition gfx.cpp:1567
void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, Alignment align)
Draw a sprite within a Rect, ignoring the sprite's padding.
Definition widget.cpp:350
bool AdjustGUIZoom(bool automatic)
Resolve GUI zoom level and adjust GUI to new zoom, if auto-suggestion is requested.
Definition gfx.cpp:1859
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y).
Definition gfx.cpp:949
std::unique_ptr< uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom=_gui_zoom)
Draws a sprite to a new RGBA buffer (see Colour union) instead of drawing to the screen.
Definition gfx.cpp:1189
Types related to the graphics and/or input devices.
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
Support8bpp
Modes for 8bpp support.
Definition gfx_type.h:427
FontSize
Available font sizes.
Definition gfx_type.h:248
@ Normal
Index of the normal font in the font tables.
Definition gfx_type.h:249
uint32_t CursorID
The number of the cursor (sprite).
Definition gfx_type.h:19
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
EnumBitSet< ControlInput, uint8_t > ControlInputs
Bitset of ControlInput elements.
Definition gfx_type.h:454
EnumBitSet< DirectionKey, uint8_t > DirectionKeys
Bitset of the direction keys.
Definition gfx_type.h:440
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:315
@ FromString
Marker for telling to use the colour from the string.
Definition gfx_type.h:317
FillRectMode
Define the operation GfxFillRect performs.
Definition gfx_type.h:391
@ Opaque
Fill rectangle with a single colour.
Definition gfx_type.h:392
EnumBitSet< FontSize, uint8_t > FontSizes
Bitset of FontSize elements.
Definition gfx_type.h:259
@ Ctrl
Ctrl modifier is pressed.
Definition gfx_type.h:449
@ Shift
Shift modifier is pressed.
Definition gfx_type.h:450
@ Right
Right mouse button is pressed.
Definition gfx_type.h:445
@ Left
Left mouse button is pressed.
Definition gfx_type.h:444
constexpr FontSizes FONTSIZES_REQUIRED
Mask of font sizes required to be present.
Definition gfx_type.h:264
void AddDirtyBlock(int left, int top, int right, int bottom)
Extend the internal _invalid_rect rectangle to contain the rectangle defined by the given parameters.
Definition gfx.cpp:1515
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition gfx.cpp:1451
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1548
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition gfx.cpp:1425
#define Rect
Macro that prevents name conflicts between included headers.
#define Point
Macro that prevents name conflicts between included headers.
Types related to strings.
StrongType::Typedef< uint32_t, struct StringIDTag, StrongType::Compare, StrongType::Integer > StringID
Numeric value that represents a string, independent of the selected language.
Horizontal and vertical alignment.
Collection of variables for cursor-display and -animation.
Definition gfx_type.h:123
Dimensions (a width and height) of a rectangle in 2D.
Data about how and where to blit pixels.
Definition gfx_type.h:157
Container for the text colour and some text colour related flags for drawing.
Definition gfx_type.h:349
Colour for pixel/line drawing.
Definition gfx_type.h:307
int Width() const
Get width of Rect.
int Height() const
Get height of Rect.
Used to only draw a part of the sprite.
Definition gfx_type.h:278
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20