OpenTTD Source: src/gfx_func.h Source File

OpenTTD Source 15.3

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
40#ifndef GFX_FUNC_H
41#define GFX_FUNC_H
42
43#include "gfx_type.h"
44#include "strings_type.h"
45#include "string_type.h"
46
47void GameLoop();
48
49void CreateConsole();
50
51extern uint8_t _dirkeys;
52extern bool _fullscreen;
53extern uint8_t _support8bpp;
54extern CursorVars _cursor;
55extern bool _ctrl_pressed;
56extern bool _shift_pressed;
57extern uint16_t _game_speed;
58
59extern bool _left_button_down;
60extern bool _left_button_clicked;
61extern bool _right_button_down;
62extern bool _right_button_clicked;
63
64extern DrawPixelInfo _screen;
65extern bool _screen_disable_anim;
66
67extern std::vector<Dimension> _resolutions;
68extern Dimension _cur_resolution;
69extern Palette _cur_palette;
70
71void HandleToolbarHotkey(int hotkey);
72void HandleKeypress(uint keycode, char32_t key);
73void HandleTextInput(std::string_view str, bool marked = false,
74 std::optional<size_t> caret = std::nullopt,
75 std::optional<size_t> insert_location = std::nullopt, std::optional<size_t> replacement_end = std::nullopt);
76void HandleCtrlChanged();
77void HandleMouseEvents();
78void UpdateWindows();
79void ChangeGameSpeed(bool enable_fast_forward);
80
81void DrawMouseCursor();
82void ScreenSizeChanged();
83void GameSizeChanged();
84void UpdateGUIZoom();
85bool AdjustGUIZoom(bool automatic);
86void UndrawMouseCursor();
87
88void RedrawScreenRect(int left, int top, int right, int bottom);
89void GfxScroll(int left, int top, int width, int height, int xo, int yo);
90
91 Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = _gui_zoom);
92 Dimension GetScaledSpriteSize(SpriteID sprid); /* widget.cpp */
93 Dimension GetSquareScaledSpriteSize(SpriteID sprid); /* widget.cpp */
94void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
95void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = _gui_zoom);
96void DrawSpriteIgnorePadding(SpriteID img, PaletteID pal, const Rect &r, StringAlignment align); /* widget.cpp */
97std::unique_ptr<uint32_t[]> DrawSpriteToRgbaBuffer(SpriteID spriteId, ZoomLevel zoom = _gui_zoom);
98
99int DrawString(int left, int right, int top, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
100int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
101int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
102int DrawStringMultiLine(int left, int right, int top, int bottom, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
103bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
104
105void DrawCharCentered(char32_t c, const Rect &r, TextColour colour);
106
107void GfxFillRect(int left, int top, int right, int bottom, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FILLRECT_OPAQUE);
108void GfxFillPolygon(std::span<const Point> shape, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FILLRECT_OPAQUE);
109void GfxDrawLine(int left, int top, int right, int bottom, PixelColour colour, int width = 1, int dash = 0);
110void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3);
111void DrawRectOutline(const Rect &r, PixelColour colour, int width = 1, int dash = 0);
112
113/* Versions of DrawString/DrawStringMultiLine that accept a Rect instead of separate left, right, top and bottom parameters. */
114inline int DrawString(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL)
115{
116 return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
117}
118
119inline int DrawString(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL)
120{
121 return DrawString(r.left, r.right, r.top, str, colour, align, underline, fontsize);
122}
123
124inline int DrawStringMultiLine(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL)
125{
126 return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
127}
128
129inline int DrawStringMultiLine(const Rect &r, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL)
130{
131 return DrawStringMultiLine(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
132}
133
134inline bool DrawStringMultiLineWithClipping(const Rect &r, std::string_view str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL)
135{
136 return DrawStringMultiLineWithClipping(r.left, r.right, r.top, r.bottom, str, colour, align, underline, fontsize);
137}
138
139inline void GfxFillRect(const Rect &r, const std::variant<PixelColour, PaletteID> &colour, FillRectMode mode = FILLRECT_OPAQUE)
140{
141 GfxFillRect(r.left, r.top, r.right, r.bottom, colour, mode);
142}
143
144 Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize = FS_NORMAL);
145 Dimension GetStringBoundingBox(StringID strid, FontSize start_fontsize = FS_NORMAL);
146uint GetStringListWidth(std::span<const StringID> list, FontSize fontsize = FS_NORMAL);
147 Dimension GetStringListBoundingBox(std::span<const StringID> list, FontSize fontsize = FS_NORMAL);
148int GetStringHeight(std::string_view str, int maxw, FontSize fontsize = FS_NORMAL);
149int GetStringHeight(StringID str, int maxw);
150int GetStringLineCount(std::string_view str, int maxw);
151 Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion);
152 Dimension GetStringMultiLineBoundingBox(std::string_view str, const Dimension &suggestion, FontSize fontsize = FS_NORMAL);
153void LoadStringWidthTable(FontSizes fontsizes = FONTSIZES_REQUIRED);
154
155void DrawDirtyBlocks();
156void AddDirtyBlock(int left, int top, int right, int bottom);
157void MarkWholeScreenDirty();
158
159void CheckBlitter();
160
161bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height);
162
163inline bool FillDrawPixelInfo(DrawPixelInfo *n, const Rect &r)
164{
165 return FillDrawPixelInfo(n, r.left, r.top, r.Width(), r.Height());
166}
167
168/* window.cpp */
169void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
170
171void SetMouseCursorBusy(bool busy);
172void SetMouseCursor(CursorID cursor, PaletteID pal);
173void SetAnimatedMouseCursor(std::span<const AnimCursor> table);
174void CursorTick();
175void UpdateCursorSize();
176bool ChangeResInGame(int w, int h);
177void SortResolutions();
178bool ToggleFullScreen(bool fs);
179
180/* gfx.cpp */
181uint8_t GetCharacterWidth(FontSize size, char32_t key);
182uint8_t GetDigitWidth(FontSize size = FS_NORMAL);
183std::pair<uint8_t, uint8_t> GetBroadestDigit(FontSize size);
184
185int GetCharacterHeight(FontSize size);
186
187extern DrawPixelInfo *_cur_dpi;
188
189#endif /* GFX_FUNC_H */
EnumBitSet
Enum-as-bit-set wrapper.
Definition enum_type.hpp:177
_gui_zoom
ZoomLevel _gui_zoom
GUI Zoom level.
Definition gfx.cpp:61
GetStringBoundingBox
Dimension GetStringBoundingBox(std::string_view str, FontSize start_fontsize=FS_NORMAL)
Return the string dimension in pixels.
Definition gfx.cpp:897
UpdateCursorSize
void UpdateCursorSize()
Update cursor dimension.
Definition gfx.cpp:1618
GetBroadestDigit
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:1302
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition gfx.cpp:39
DrawRectOutline
void DrawRectOutline(const Rect &r, PixelColour colour, int width=1, int dash=0)
Draw the outline of a Rect.
Definition gfx.cpp:463
HandleCtrlChanged
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition window.cpp:2709
DrawSprite
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:1034
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset=nullptr, ZoomLevel zoom=_gui_zoom)
Get the size of a sprite.
Definition gfx.cpp:968
GetStringLineCount
int GetStringLineCount(std::string_view str, int maxw)
Calculates number of lines of string.
Definition gfx.cpp:739
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition window.cpp:3127
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition gfx.cpp:41
LoadStringWidthTable
void LoadStringWidthTable(FontSizes fontsizes=FONTSIZES_REQUIRED)
Initialize _stringwidth_table cache for the specified font sizes.
Definition gfx.cpp:1256
DrawOverlappedWindowForAll
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:947
GetStringListBoundingBox
Dimension GetStringListBoundingBox(std::span< const StringID > list, FontSize fontsize=FS_NORMAL)
Get maximum dimension of a list of strings.
Definition gfx.cpp:935
GetSquareScaledSpriteSize
Dimension GetSquareScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI, as a square.
Definition widget.cpp:83
HandleToolbarHotkey
void HandleToolbarHotkey(int hotkey)
Handle Toolbar hotkey events - can come from a source like the MacBook Touch Bar.
Definition window.cpp:2636
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition gfx.cpp:38
GameSizeChanged
void GameSizeChanged()
Size of the application screen changed.
Definition main_gui.cpp:596
_dirkeys
uint8_t _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition gfx.cpp:34
_screen_disable_anim
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition gfx.cpp:46
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition gfx.cpp:42
SetMouseCursor
void SetMouseCursor(CursorID cursor, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition gfx.cpp:1706
_game_speed
uint16_t _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition gfx.cpp:40
GetScaledSpriteSize
Dimension GetScaledSpriteSize(SpriteID sprid)
Scale sprite size for GUI.
Definition widget.cpp:68
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition gfx.cpp:1690
UpdateGUIZoom
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition gfx.cpp:1790
GetDigitWidth
uint8_t GetDigitWidth(FontSize size=FS_NORMAL)
Return the maximum width of single digit.
Definition gfx.cpp:1286
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, std::string_view str, TextColour colour=TC_FROMSTRING, StringAlignment align=(SA_TOP|SA_LEFT), bool underline=false, FontSize fontsize=FS_NORMAL)
Draw string, possibly over multiple lines.
Definition gfx.cpp:785
CheckBlitter
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
Definition gfxinit.cpp:325
GetStringHeight
int GetStringHeight(std::string_view str, int maxw, FontSize fontsize=FS_NORMAL)
Calculates height of string (in pixels).
Definition gfx.cpp:715
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition window.cpp:2966
_cur_palette
Palette _cur_palette
Current palette.
Definition palette.cpp:24
DrawSpriteViewport
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr)
Draw a sprite in a viewport.
Definition gfx.cpp:1006
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition gfx.cpp:44
SetAnimatedMouseCursor
void SetAnimatedMouseCursor(std::span< const AnimCursor > table)
Assign an animation to the cursor.
Definition gfx.cpp:1719
HandleKeypress
void HandleKeypress(uint keycode, char32_t key)
Handle keyboard input.
Definition window.cpp:2653
GetCharacterWidth
uint8_t GetCharacterWidth(FontSize size, char32_t key)
Return width of character glyph.
Definition gfx.cpp:1273
GetStringMultiLineBoundingBox
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition gfx.cpp:751
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition driver.cpp:28
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition fontcache.cpp:87
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition driver.cpp:29
GfxFillPolygon
void GfxFillPolygon(std::span< const Point > shape, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode=FILLRECT_OPAQUE)
Fill a polygon with colour.
Definition gfx.cpp:212
HandleTextInput
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:2739
DrawBox
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:425
GetStringListWidth
uint GetStringListWidth(std::span< const StringID > list, FontSize fontsize=FS_NORMAL)
Get maximum width of a list of strings.
Definition gfx.cpp:920
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, const std::variant< PixelColour, PaletteID > &colour, FillRectMode mode=FILLRECT_OPAQUE)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition gfx.cpp:115
DrawString
int DrawString(int left, int right, int top, std::string_view str, TextColour colour=TC_FROMSTRING, StringAlignment align=SA_LEFT, bool underline=false, FontSize fontsize=FS_NORMAL)
Draw string, possibly truncated to make it fit in its allocated space.
Definition gfx.cpp:668
FillDrawPixelInfo
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:1568
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition gfx.cpp:43
AdjustGUIZoom
bool AdjustGUIZoom(bool automatic)
Resolve GUI zoom level and adjust GUI to new zoom, if auto-suggestion is requested.
Definition gfx.cpp:1818
DrawCharCentered
void DrawCharCentered(char32_t c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition gfx.cpp:951
DrawStringMultiLineWithClipping
bool DrawStringMultiLineWithClipping(int left, int right, int top, int bottom, std::string_view str, TextColour colour=TC_FROMSTRING, StringAlignment align=(SA_TOP|SA_LEFT), bool underline=false, FontSize fontsize=FS_NORMAL)
Draw a multiline string, possibly over multiple lines, if the region is within the current display cl...
Definition gfx.cpp:870
DrawSpriteToRgbaBuffer
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:1190
gfx_type.h
Types related to the graphics and/or input devices.
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
FontSize
FontSize
Available font sizes.
Definition gfx_type.h:248
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition gfx_type.h:249
CursorID
uint32_t CursorID
The number of the cursor (sprite)
Definition gfx_type.h:19
StringAlignment
StringAlignment
How to align the to-be drawn text.
Definition gfx_type.h:387
SA_TOP
@ SA_TOP
Top align the text.
Definition gfx_type.h:393
SA_LEFT
@ SA_LEFT
Left align the text.
Definition gfx_type.h:388
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition gfx_type.h:307
FillRectMode
FillRectMode
Define the operation GfxFillRect performs.
Definition gfx_type.h:344
FILLRECT_OPAQUE
@ FILLRECT_OPAQUE
Fill rectangle with a single colour.
Definition gfx_type.h:345
FONTSIZES_REQUIRED
constexpr FontSizes FONTSIZES_REQUIRED
Mask of font sizes required to be present.
Definition gfx_type.h:264
AddDirtyBlock
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:1516
DrawDirtyBlocks
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition gfx.cpp:1452
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition gfx.cpp:1549
RedrawScreenRect
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition gfx.cpp:1426
string_type.h
Types for strings.
strings_type.h
Types related to strings.
StringID
uint32_t StringID
Numeric value that represents a string, independent of the selected language.
Definition strings_type.h:19
Coord2D< int >
CursorVars
Collection of variables for cursor-display and -animation.
Definition gfx_type.h:123
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition geometry_type.hpp:56
DrawPixelInfo
Data about how and where to blit pixels.
Definition gfx_type.h:157
Palette
Information about the currently used palette.
Definition gfx_type.h:373
PixelColour
Colour for pixel/line drawing.
Definition gfx_type.h:405
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Width
int Width() const
Get width of Rect.
Definition geometry_type.hpp:111
Rect::Height
int Height() const
Get height of Rect.
Definition geometry_type.hpp:117
SubSprite
Used to only draw a part of the sprite.
Definition gfx_type.h:278
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition zoom_type.h:20