OpenTTD
gfx_func.h
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
42 #ifndef GFX_FUNC_H
43 #define GFX_FUNC_H
44 
45 #include "gfx_type.h"
46 #include "strings_type.h"
47 #include "string_type.h"
48 
49 void GameLoop();
50 
51 void CreateConsole();
52 
53 extern byte _dirkeys;
54 extern bool _fullscreen;
55 extern byte _support8bpp;
56 extern CursorVars _cursor;
57 extern bool _ctrl_pressed;
58 extern bool _shift_pressed;
59 extern byte _fast_forward;
60 
61 extern bool _left_button_down;
62 extern bool _left_button_clicked;
63 extern bool _right_button_down;
64 extern bool _right_button_clicked;
65 
66 extern DrawPixelInfo _screen;
67 extern bool _screen_disable_anim;
68 
69 extern std::vector<Dimension> _resolutions;
71 extern Palette _cur_palette;
72 
73 void HandleKeypress(uint keycode, WChar key);
74 void HandleTextInput(const char *str, bool marked = false, const char *caret = nullptr, const char *insert_location = nullptr, const char *replacement_end = nullptr);
75 void HandleCtrlChanged();
76 void HandleMouseEvents();
77 void UpdateWindows();
78 
79 void DrawMouseCursor();
80 void ScreenSizeChanged();
81 void GameSizeChanged();
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 
96  SA_LEFT = 0 << 0,
97  SA_HOR_CENTER = 1 << 0,
98  SA_RIGHT = 2 << 0,
99  SA_HOR_MASK = 3 << 0,
100 
101  SA_TOP = 0 << 2,
102  SA_VERT_CENTER = 1 << 2,
103  SA_BOTTOM = 2 << 2,
104  SA_VERT_MASK = 3 << 2,
105 
107 
108  SA_FORCE = 1 << 4,
109 };
111 
112 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);
113 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);
114 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);
115 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);
116 
117 void DrawCharCentered(uint32 c, int x, int y, TextColour colour);
118 
119 void GfxFillRect(int left, int top, int right, int bottom, int colour, FillRectMode mode = FILLRECT_OPAQUE);
120 void GfxDrawLine(int left, int top, int right, int bottom, int colour, int width = 1, int dash = 0);
121 void DrawBox(int x, int y, int dx1, int dy1, int dx2, int dy2, int dx3, int dy3);
122 
123 Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize = FS_NORMAL);
125 int GetStringHeight(const char *str, int maxw, FontSize fontsize = FS_NORMAL);
126 int GetStringHeight(StringID str, int maxw);
127 int GetStringLineCount(StringID str, int maxw);
129 Dimension GetStringMultiLineBoundingBox(const char *str, const Dimension &suggestion);
130 void LoadStringWidthTable(bool monospace = false);
131 Point GetCharPosInString(const char *str, const char *ch, FontSize start_fontsize = FS_NORMAL);
132 const char *GetCharAtPosition(const char *str, int x, FontSize start_fontsize = FS_NORMAL);
133 
134 void DrawDirtyBlocks();
135 void SetDirtyBlocks(int left, int top, int right, int bottom);
136 void MarkWholeScreenDirty();
137 
138 void GfxInitPalettes();
139 void CheckBlitter();
140 
141 bool FillDrawPixelInfo(DrawPixelInfo *n, int left, int top, int width, int height);
142 
150 static inline int CenterBounds(int min, int max, int size)
151 {
152  return min + (max - min - size + 1) / 2;
153 }
154 
155 /* window.cpp */
156 void DrawOverlappedWindowForAll(int left, int top, int right, int bottom);
157 
158 void SetMouseCursorBusy(bool busy);
159 void SetMouseCursor(CursorID cursor, PaletteID pal);
160 void SetAnimatedMouseCursor(const AnimCursor *table);
161 void CursorTick();
162 void UpdateCursorSize();
163 bool ChangeResInGame(int w, int h);
164 void SortResolutions();
165 bool ToggleFullScreen(bool fs);
166 
167 /* gfx.cpp */
168 byte GetCharacterWidth(FontSize size, uint32 key);
169 byte GetDigitWidth(FontSize size = FS_NORMAL);
170 void GetBroadestDigit(uint *front, uint *next, FontSize size = FS_NORMAL);
171 
172 int GetCharacterHeight(FontSize size);
173 
175 #define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))
176 
178 #define FONT_HEIGHT_NORMAL (GetCharacterHeight(FS_NORMAL))
179 
181 #define FONT_HEIGHT_LARGE (GetCharacterHeight(FS_LARGE))
182 
184 #define FONT_HEIGHT_MONO (GetCharacterHeight(FS_MONO))
185 
186 extern DrawPixelInfo *_cur_dpi;
187 
188 TextColour GetContrastColour(uint8 background, uint8 threshold = 128);
189 
194 extern byte _colour_gradient[COLOUR_END][8];
195 
196 extern bool _palette_remap_grf[];
197 
203 #define GREY_SCALE(level) (level)
204 
205 static const uint8 PC_BLACK = GREY_SCALE(1);
206 static const uint8 PC_DARK_GREY = GREY_SCALE(6);
207 static const uint8 PC_GREY = GREY_SCALE(10);
208 static const uint8 PC_WHITE = GREY_SCALE(15);
209 
210 static const uint8 PC_VERY_DARK_RED = 0xB2;
211 static const uint8 PC_DARK_RED = 0xB4;
212 static const uint8 PC_RED = 0xB8;
213 
214 static const uint8 PC_VERY_DARK_BROWN = 0x56;
215 
216 static const uint8 PC_ORANGE = 0xC2;
217 
218 static const uint8 PC_YELLOW = 0xBF;
219 static const uint8 PC_LIGHT_YELLOW = 0x44;
220 static const uint8 PC_VERY_LIGHT_YELLOW = 0x45;
221 
222 static const uint8 PC_GREEN = 0xD0;
223 
224 static const uint8 PC_DARK_BLUE = 0x9D;
225 static const uint8 PC_LIGHT_BLUE = 0x98;
226 
227 #endif /* GFX_FUNC_H */
void DrawSpriteViewport(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub=nullptr)
Draw a sprite in a viewport.
Definition: gfx.cpp:805
bool _ctrl_pressed
Is Ctrl pressed?
Definition: gfx.cpp:37
bool _right_button_clicked
Is right mouse button clicked?
Definition: gfx.cpp:43
Dimension GetStringBoundingBox(const char *str, FontSize start_fontsize=FS_NORMAL)
Return the string dimension in pixels.
Definition: gfx.cpp:698
Dimension GetSpriteSize(SpriteID sprid, Point *offset=nullptr, ZoomLevel zoom=ZOOM_LVL_GUI)
Get the size of a sprite.
Definition: gfx.cpp:767
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
DECLARE_ENUM_AS_BIT_SET(GenderEthnicity) enum CompanyManagerFaceVariable
Bitgroups of the CompanyManagerFace variable.
Data about how and where to blit pixels.
Definition: gfx_type.h:156
static const uint8 PC_WHITE
White palette colour.
Definition: gfx_func.h:208
Horizontally center the text.
Definition: gfx_func.h:97
Information about the currently used palette.
Definition: gfx_type.h:309
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:726
bool _right_button_down
Is right mouse button pressed?
Definition: gfx.cpp:42
byte GetCharacterWidth(FontSize size, uint32 key)
Return width of character glyph.
Definition: gfx.cpp:1149
StringAlignment
How to align the to-be drawn text.
Definition: gfx_func.h:95
Mask for horizontal alignment.
Definition: gfx_func.h:99
void SetMouseCursorBusy(bool busy)
Set or unset the ZZZ cursor.
Definition: gfx.cpp:1600
static const uint8 PC_LIGHT_BLUE
Light blue palette colour.
Definition: gfx_func.h:225
static const int DRAW_STRING_BUFFER
Size of the buffer used for drawing strings.
Definition: gfx_func.h:85
void DrawDirtyBlocks()
Repaints the rectangle blocks which are marked as &#39;dirty&#39;.
Definition: gfx.cpp:1301
void SetMouseCursor(CursorID cursor, PaletteID pal)
Assign a single non-animated sprite to the cursor.
Definition: gfx.cpp:1615
void UpdateCursorSize()
Update cursor dimension.
Definition: gfx.cpp:1528
static const uint8 PC_LIGHT_YELLOW
Light yellow palette colour.
Definition: gfx_func.h:219
Dimension GetStringMultiLineBoundingBox(StringID str, const Dimension &suggestion)
Calculate string bounding box for multi-line strings.
Definition: gfx.cpp:587
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
#define GREY_SCALE(level)
Return the colour for a particular greyscale level.
Definition: gfx_func.h:203
FillRectMode
Define the operation GfxFillRect performs.
Definition: gfx_type.h:282
void SetAnimatedMouseCursor(const AnimCursor *table)
Assign an animation to the cursor.
Definition: gfx.cpp:1628
Force the alignment, i.e. don&#39;t swap for RTL languages.
Definition: gfx_func.h:108
static const uint8 PC_GREEN
Green palette colour.
Definition: gfx_func.h:222
Collection of variables for cursor-display and -animation.
Definition: gfx_type.h:117
static const uint8 PC_DARK_GREY
Dark grey palette colour.
Definition: gfx_func.h:206
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:22
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:112
int GetStringHeight(const char *str, int maxw, FontSize fontsize=FS_NORMAL)
Calculates height of string (in pixels).
Definition: gfx.cpp:547
Fill rectangle with a single colour.
Definition: gfx_type.h:283
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:620
Center both horizontally and vertically.
Definition: gfx_func.h:106
Bottom align the text.
Definition: gfx_func.h:103
Top align the text.
Definition: gfx_func.h:101
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
Definition: gfx_type.h:247
Palette _cur_palette
Current palette.
Definition: gfx.cpp:50
int GetStringLineCount(StringID str, int maxw)
Calculates number of lines of string.
Definition: gfx.cpp:572
void HandleKeypress(uint keycode, WChar key)
Handle keyboard input.
Definition: window.cpp:2654
bool _palette_remap_grf[]
Whether the given NewGRFs must get a palette remap from windows to DOS or not.
Definition: gfxinit.cpp:32
void LoadStringWidthTable(bool monospace=false)
Initialize _stringwidth_table cache.
Definition: gfx.cpp:1130
A single sprite of a list of animated cursors.
Definition: gfx_type.h:110
void HandleMouseEvents()
Handle a mouse event from the video driver.
Definition: window.cpp:2961
Mask for vertical alignment.
Definition: gfx_func.h:104
bool _screen_disable_anim
Disable palette animation (important for 32bpp-anim blitter during giant screenshot) ...
Definition: gfx.cpp:45
static T min(const T a, const T b)
Returns the minimum of two values.
Definition: math_func.hpp:42
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:1478
uint32 StringID
Numeric value that represents a string, independent of the selected language.
Definition: strings_type.h:18
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:278
static const uint8 PC_BLACK
Black palette colour.
Definition: gfx_func.h:205
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:943
byte _dirkeys
1 = left, 2 = up, 4 = right, 8 = down
Definition: gfx.cpp:33
TextColour GetContrastColour(uint8 background, uint8 threshold=128)
Determine a contrasty text colour for a coloured background.
Definition: gfx.cpp:1116
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:499
bool _shift_pressed
Is Shift pressed?
Definition: gfx.cpp:38
void DrawCharCentered(uint32 c, int x, int y, TextColour colour)
Draw single character horizontally centered around (x,y)
Definition: gfx.cpp:754
static const uint8 PC_VERY_DARK_RED
Almost-black red palette colour.
Definition: gfx_func.h:210
static int CenterBounds(int min, int max, int size)
Determine where to draw a centred object inside a widget.
Definition: gfx_func.h:150
static const uint8 PC_GREY
Grey palette colour.
Definition: gfx_func.h:207
static const uint8 PC_DARK_BLUE
Dark blue palette colour.
Definition: gfx_func.h:224
static const uint8 PC_YELLOW
Yellow palette colour.
Definition: gfx_func.h:218
void HandleCtrlChanged()
State of CONTROL key has changed.
Definition: window.cpp:2711
static const uint8 PC_DARK_RED
Dark red palette colour.
Definition: gfx_func.h:211
byte GetDigitWidth(FontSize size=FS_NORMAL)
Return the maximum width of single digit.
Definition: gfx.cpp:1162
bool _left_button_down
Is left mouse button pressed?
Definition: gfx.cpp:40
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
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:832
static const uint8 PC_ORANGE
Orange palette colour.
Definition: gfx_func.h:216
static const uint8 PC_VERY_DARK_BROWN
Almost-black brown palette colour.
Definition: gfx_func.h:214
static const uint8 PC_RED
Red palette colour.
Definition: gfx_func.h:212
FontSize
Available font sizes.
Definition: gfx_type.h:203
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:739
Index of the normal font in the font tables.
Definition: gfx_type.h:204
Used to only draw a part of the sprite.
Definition: gfx_type.h:219
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:1177
byte _colour_gradient[COLOUR_END][8]
All 16 colour gradients 8 colours per gradient from darkest (0) to lightest (7)
Definition: gfx.cpp:54
uint32 CursorID
The number of the cursor (sprite)
Definition: gfx_type.h:21
Coordinates of a point in 2D.
Types related to strings.
void GameSizeChanged()
Size of the application screen changed.
Definition: main_gui.cpp:596
Types for strings.
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:2742
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
bool _left_button_clicked
Is left mouse button clicked?
Definition: gfx.cpp:41
Right align the text (must be a single bit).
Definition: gfx_func.h:98
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:23
Left align the text.
Definition: gfx_func.h:96
Vertically center the text.
Definition: gfx_func.h:102
int GetCharacterHeight(FontSize size)
Get height of a character for a given font size.
Definition: fontcache.cpp:64
void SetDirtyBlocks(int left, int top, int right, int bottom)
This function extends the internal _invalid_rect rectangle as it now contains the rectangle defined b...
Definition: gfx.cpp:1416
uint32 WChar
Type for wide characters, i.e.
Definition: string_type.h:37
static const uint8 PC_VERY_LIGHT_YELLOW
Almost-white yellow palette colour.
Definition: gfx_func.h:220
Dimensions (a width and height) of a rectangle in 2D.
void CheckBlitter()
Check whether we still use the right blitter, or use another (better) one.
Definition: gfxinit.cpp:330
Types related to the graphics and/or input devices.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
Definition: gfx.cpp:1459
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3112