OpenTTD Source  12.0-beta2
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 <http://www.gnu.org/licenses/>.
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 
47 void GameLoop();
48 
49 void CreateConsole();
50 
51 extern byte _dirkeys;
52 extern bool _fullscreen;
53 extern byte _support8bpp;
54 extern CursorVars _cursor;
55 extern bool _ctrl_pressed;
56 extern bool _shift_pressed;
57 extern uint16 _game_speed;
58 
59 extern bool _left_button_down;
60 extern bool _left_button_clicked;
61 extern bool _right_button_down;
62 extern bool _right_button_clicked;
63 
64 extern DrawPixelInfo _screen;
65 extern bool _screen_disable_anim;
66 
67 extern std::vector<Dimension> _resolutions;
69 extern Palette _cur_palette;
70 
71 void HandleKeypress(uint keycode, WChar key);
72 void HandleTextInput(const char *str, bool marked = false, const char *caret = nullptr, const char *insert_location = nullptr, const char *replacement_end = nullptr);
73 void HandleCtrlChanged();
74 void HandleMouseEvents();
75 void UpdateWindows();
76 void ChangeGameSpeed(bool enable_fast_forward);
77 
78 void DrawMouseCursor();
79 void ScreenSizeChanged();
80 void GameSizeChanged();
81 void UpdateGUIZoom();
82 void UndrawMouseCursor();
83 
85 static const int DRAW_STRING_BUFFER = 2048;
86 
87 void RedrawScreenRect(int left, int top, int right, int bottom);
88 void GfxScroll(int left, int top, int width, int height, int xo, int yo);
89 
90 Dimension GetSpriteSize(SpriteID sprid, Point *offset = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
91 void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr);
92 void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub = nullptr, ZoomLevel zoom = ZOOM_LVL_GUI);
93 
94 int DrawString(int left, int right, int top, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
95 int DrawString(int left, int right, int top, const std::string &str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
96 int DrawString(int left, int right, int top, StringID str, TextColour colour = TC_FROMSTRING, StringAlignment align = SA_LEFT, bool underline = false, FontSize fontsize = FS_NORMAL);
97 int DrawStringMultiLine(int left, int right, int top, int bottom, const char *str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
98 int DrawStringMultiLine(int left, int right, int top, int bottom, const std::string &str, TextColour colour = TC_FROMSTRING, StringAlignment align = (SA_TOP | SA_LEFT), bool underline = false, FontSize fontsize = FS_NORMAL);
99 int 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);
100 
101 void DrawCharCentered(WChar c, const Rect &r, TextColour colour);
102 
103 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode = FILLRECT_OPAQUE);
104 void GfxFillPolygon(const std::vector<Point> &shape, int colour, FillRectMode mode = FILLRECT_OPAQUE);
105 void GfxDrawLine(int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0);
106 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3);
107 
108 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize = FS_NORMAL);
109 Dimension GetStringBoundingBox(const std::string &str, FontSize start_fontsize = FS_NORMAL);
111 int GetStringHeight(const char *str, int maxw, FontSize fontsize = FS_NORMAL);
112 int GetStringHeight(StringID str, int maxw);
113 int GetStringLineCount(StringID str, int maxw);
115 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion);
116 void LoadStringWidthTable(bool monospace = false);
117 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize = FS_NORMAL);
118 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize = FS_NORMAL);
119 
120 void DrawDirtyBlocks();
121 void AddDirtyBlock(int left, int top, int right, int bottom);
122 void MarkWholeScreenDirty();
123 
124 bool CopyPalette(Palette &local_palette, bool force_copy = false);
125 void GfxInitPalettes();
126 void CheckBlitter();
127 
128 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height);
129 
137 static inline int CenterBounds(int min, int max, int size)
138 {
139  return min + (max - min - size + 1) / 2;
140 }
141 
142 /* window.cpp */
143 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
144 
145 void SetMouseCursorBusy(bool busy);
146 void SetMouseCursor(CursorID cursor, PaletteID pal);
147 void SetAnimatedMouseCursor(const AnimCursor *table);
148 void CursorTick();
149 void UpdateCursorSize();
150 bool ChangeResInGame(int w, int h);
151 void SortResolutions();
152 bool ToggleFullScreen(bool fs);
153 
154 /* gfx.cpp */
155 byte GetCharacterWidth(FontSize size, WChar key);
156 byte GetDigitWidth(FontSize size = FS_NORMAL);
157 void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL);
158 
159 int GetCharacterHeight(FontSize size);
160 
162 #define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))
163 
165 #define FONT_HEIGHT_NORMAL (GetCharacterHeight(FS_NORMAL))
166 
168 #define FONT_HEIGHT_LARGE (GetCharacterHeight(FS_LARGE))
169 
171 #define FONT_HEIGHT_MONO (GetCharacterHeight(FS_MONO))
172 
173 extern DrawPixelInfo *_cur_dpi;
174 
175 TextColour GetContrastColour(uint8 background, uint8 threshold = 128);
176 
181 extern byte _colour_gradient[COLOUR_END][8];
182 
188 #define GREY_SCALE(level) (level)
189 
190 static const uint8 PC_BLACK = GREY_SCALE(1);
191 static const uint8 PC_DARK_GREY = GREY_SCALE(6);
192 static const uint8 PC_GREY = GREY_SCALE(10);
193 static const uint8 PC_WHITE = GREY_SCALE(15);
194 
195 static const uint8 PC_VERY_DARK_RED = 0xB2;
196 static const uint8 PC_DARK_RED = 0xB4;
197 static const uint8 PC_RED = 0xB8;
198 
199 static const uint8 PC_VERY_DARK_BROWN = 0x56;
200 
201 static const uint8 PC_ORANGE = 0xC2;
202 
203 static const uint8 PC_YELLOW = 0xBF;
204 static const uint8 PC_LIGHT_YELLOW = 0x44;
205 static const uint8 PC_VERY_LIGHT_YELLOW = 0x45;
206 
207 static const uint8 PC_GREEN = 0xD0;
208 
209 static const uint8 PC_VERY_DARK_BLUE = 0x9A;
210 static const uint8 PC_DARK_BLUE = 0x9D;
211 static const uint8 PC_LIGHT_BLUE = 0x98;
212 
213 static const uint8 PC_ROUGH_LAND = 0x52;
214 static const uint8 PC_GRASS_LAND = 0x54;
215 static const uint8 PC_BARE_LAND = 0x37;
216 static const uint8 PC_RAINFOREST = 0x5C;
217 static const uint8 PC_FIELDS = 0x25;
218 static const uint8 PC_TREES = 0x57;
219 static const uint8 PC_WATER = 0xC9;
220 #endif /* GFX_FUNC_H */
_cur_palette
Palette _cur_palette
Current palette.
Definition: gfx.cpp:48
SetMouseCursorBusy
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1830
PC_WHITE
static const uint8 PC_WHITE
White palette colour.
Definition: gfx_func.h:193
SetMouseCursor
void SetMouseCursor(CursorID cursor, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1845
_left_button_clicked
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:39
_dirkeys
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:31
CursorVars
Collection of variables for cursor-display and -animation.
Definition: gfx_type.h:115
_left_button_down
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:38
WChar
char32_t WChar
Type for wide characters, i.e.
Definition: string_type.h:35
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:1646
_right_button_down
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:40
UpdateGUIZoom
void UpdateGUIZoom()
Resolve GUI zoom level, if auto-suggestion is requested.
Definition: gfx.cpp:1962
DrawSpriteViewport
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr)
Draw a sprite in a viewport.
Definition: gfx.cpp:1014
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
HandleTextInput
void HandleTextInput(const char *str, bool marked=false, const char *caret=nullptr, const char *insert_location=nullptr, const char *replacement_end=nullptr)
Handle text input.
Definition: window.cpp:2680
PC_YELLOW
static const uint8 PC_YELLOW
Yellow palette colour.
Definition: gfx_func.h:203
PC_DARK_RED
static const uint8 PC_DARK_RED
Dark red palette colour.
Definition: gfx_func.h:196
HandleKeypress
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2594
PC_ORANGE
static const uint8 PC_ORANGE
Orange palette colour.
Definition: gfx_func.h:201
PC_RED
static const uint8 PC_RED
Red palette colour.
Definition: gfx_func.h:197
GfxFillPolygon
void GfxFillPolygon(const std::vector< Point > &shape, int colour, FillRectMode mode=FILLRECT_OPAQUE)
Fill a polygon with colour.
Definition: gfx.cpp:212
PC_RAINFOREST
static const uint8 PC_RAINFOREST
Pale green palette colour for rainforest.
Definition: gfx_func.h:216
GetStringHeight
int GetStringHeight(const char *str, int maxw, FontSize fontsize=FS_NORMAL)
Calculates height of string (in pixels).
Definition: gfx.cpp:713
TextColour
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:250
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
StringAlignment
StringAlignment
How to align the to-be drawn text.
Definition: gfx_type.h:327
FillRectMode
FillRectMode
Define the operation GfxFillRect performs.
Definition: gfx_type.h:286
GetSpriteSize
Dimension GetSpriteSize(SpriteID sprid, Point *offset=nullptr, ZoomLevel zoom=ZOOM_LVL_GUI)
Get the size of a sprite.
Definition: gfx.cpp:976
_screen_disable_anim
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot)
Definition: gfx.cpp:43
PC_GRASS_LAND
static const uint8 PC_GRASS_LAND
Dark green palette colour for grass land.
Definition: gfx_func.h:214
strings_type.h
GfxFillRect
void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode=FILLRECT_OPAQUE)
Applies a certain FillRectMode-operation to a rectangle [left, right] x [top, bottom] on the screen.
Definition: gfx.cpp:116
SubSprite
Used to only draw a part of the sprite.
Definition: gfx_type.h:222
DrawCharCentered
void DrawCharCentered(WChar c, const Rect &r, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition: gfx.cpp:959
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:25
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:17
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3042
PC_VERY_LIGHT_YELLOW
static const uint8 PC_VERY_LIGHT_YELLOW
Almost-white yellow palette colour.
Definition: gfx_func.h:205
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:970
SetAnimatedMouseCursor
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition: gfx.cpp:1858
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:1708
CopyPalette
bool CopyPalette(Palette &local_palette, bool force_copy=false)
Copy the current palette if the palette was updated.
Definition: gfx.cpp:1216
DRAW_STRING_BUFFER
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
CursorID
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:19
FS_NORMAL
@ FS_NORMAL
Index of the normal font in the font tables.
Definition: gfx_type.h:207
DrawString
int DrawString(int left, int right, int top, const char *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:642
SA_TOP
@ SA_TOP
Top align the text.
Definition: gfx_type.h:333
HandleMouseEvents
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2900
DrawStringMultiLine
int DrawStringMultiLine(int left, int right, int top, int bottom, const char *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:787
GetCharPosInString
Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize=FS_NORMAL)
Get the leading corner of a character in a single-line string relative to the start of the string.
Definition: gfx.cpp:931
_right_button_clicked
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:41
GetCharacterWidth
byte GetCharacterWidth(FontSize size, WChar key)
Return width of character glyph.
Definition: gfx.cpp:1387
UpdateCursorSize
void UpdateCursorSize()
Update cursor dimension.
Definition: gfx.cpp:1758
DrawSprite
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr, ZoomLevel zoom=ZOOM_LVL_GUI)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:1041
RedrawScreenRect
void RedrawScreenRect(int left, int top, int right, int bottom)
Repaints a specific rectangle of the screen.
Definition: gfx.cpp:1531
DrawDirtyBlocks
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as 'dirty'.
Definition: gfx.cpp:1557
GetStringLineCount
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
Definition: gfx.cpp:738
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:21
CheckBlitter
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
Definition: gfxinit.cpp:334
GetStringMultiLineBoundingBox
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition: gfx.cpp:753
string_type.h
PC_BLACK
static const uint8 PC_BLACK
Black palette colour.
Definition: gfx_func.h:190
GetContrastColour
TextColour GetContrastColour(uint8 background, uint8 threshold=128)
Determine a contrasty text colour for a coloured background.
Definition: gfx.cpp:1354
PC_TREES
static const uint8 PC_TREES
Green palette colour for trees.
Definition: gfx_func.h:218
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:24
PC_GREY
static const uint8 PC_GREY
Grey palette colour.
Definition: gfx_func.h:192
PC_FIELDS
static const uint8 PC_FIELDS
Light brown palette colour for fields.
Definition: gfx_func.h:217
FILLRECT_OPAQUE
@ FILLRECT_OPAQUE
Fill rectangle with a single colour.
Definition: gfx_type.h:287
PC_VERY_DARK_BROWN
static const uint8 PC_VERY_DARK_BROWN
Almost-black brown palette colour.
Definition: gfx_func.h:199
StringID
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:16
GetBroadestDigit
void GetBroadestDigit(uint *front, uint *next, FontSize size=FS_NORMAL)
Determine the broadest digits for guessing the maximum width of a n-digit number.
Definition: gfx.cpp:1415
PC_VERY_DARK_RED
static const uint8 PC_VERY_DARK_RED
Almost-black red palette colour.
Definition: gfx_func.h:195
PC_LIGHT_YELLOW
static const uint8 PC_LIGHT_YELLOW
Light yellow palette colour.
Definition: gfx_func.h:204
_ctrl_pressed
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:35
PC_ROUGH_LAND
static const uint8 PC_ROUGH_LAND
Dark green palette colour for rough land.
Definition: gfx_func.h:213
PC_DARK_BLUE
static const uint8 PC_DARK_BLUE
Dark blue palette colour.
Definition: gfx_func.h:210
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:18
GetDigitWidth
byte GetDigitWidth(FontSize size=FS_NORMAL)
Return the maximum width of single digit.
Definition: gfx.cpp:1400
LoadStringWidthTable
void LoadStringWidthTable(bool monospace=false)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1368
GetCharAtPosition
const char * GetCharAtPosition(const char *str, int x, FontSize start_fontsize=FS_NORMAL)
Get the character from a string that is drawn at a specific position.
Definition: gfx.cpp:944
PC_VERY_DARK_BLUE
static const uint8 PC_VERY_DARK_BLUE
Almost-black blue palette colour.
Definition: gfx_func.h:209
AnimCursor
A single sprite of a list of animated cursors.
Definition: gfx_type.h:108
PC_WATER
static const uint8 PC_WATER
Dark blue palette colour for water.
Definition: gfx_func.h:219
SA_LEFT
@ SA_LEFT
Left align the text.
Definition: gfx_type.h:328
GameSizeChanged
void GameSizeChanged()
Size of the application screen changed.
Definition: main_gui.cpp:561
GetCharacterHeight
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:69
GetStringBoundingBox
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize=FS_NORMAL)
Return the string dimension in pixels.
Definition: gfx.cpp:888
CenterBounds
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition: gfx_func.h:137
HandleCtrlChanged
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2650
MarkWholeScreenDirty
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1689
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:420
_shift_pressed
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:36
FontSize
FontSize
Available font sizes.
Definition: gfx_type.h:206
_colour_gradient
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:52
GREY_SCALE
#define GREY_SCALE(level)
Return the colour for a particular greyscale level.
Definition: gfx_func.h:188
PC_DARK_GREY
static const uint8 PC_DARK_GREY
Dark grey palette colour.
Definition: gfx_func.h:191
PC_LIGHT_BLUE
static const uint8 PC_LIGHT_BLUE
Light blue palette colour.
Definition: gfx_func.h:211
gfx_type.h
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:47
Palette
Information about the currently used palette.
Definition: gfx_type.h:313
_game_speed
uint16 _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:37
PC_GREEN
static const uint8 PC_GREEN
Green palette colour.
Definition: gfx_func.h:207
PC_BARE_LAND
static const uint8 PC_BARE_LAND
Brown palette colour for bare land.
Definition: gfx_func.h:215
DrawPixelInfo
Data about how and where to blit pixels.
Definition: gfx_type.h:155