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/>.
10 /** @file gfx_func.h Functions related to the gfx engine. */
13 * @defgroup dirty Dirty
15 * Handles the repaint of some part of the screen.
17 * Some places in the code are called functions which makes something "dirty".
18 * This has nothing to do with making a Tile or Window darker or less visible.
19 * This term comes from memory caching and is used to define an object must
20 * be repaint. If some data of an object (like a Tile, Window, Vehicle, whatever)
21 * are changed which are so extensive the object must be repaint its marked
22 * as "dirty". The video driver repaint this object instead of the whole screen
23 * (this is btw. also possible if needed). This is used to avoid a
24 * flickering of the screen by the video driver constantly repainting it.
26 * This whole mechanism is controlled by an rectangle defined in #_invalid_rect. This
27 * rectangle defines the area on the screen which must be repaint. If a new object
28 * needs to be repainted this rectangle is extended to 'catch' the object on the
29 * screen. At some point (which is normally uninteresting for patch writers) this
30 * rectangle is send to the video drivers method
31 * VideoDriver::MakeDirty and it is truncated back to an empty rectangle. At some
32 * later point (which is uninteresting, too) the video driver
33 * repaints all these saved rectangle instead of the whole screen and drop the
34 * rectangle informations. Then a new round begins by marking objects "dirty".
36 * @see VideoDriver::MakeDirty
45 #include "blitter/blitter.h"
46 #include "strings_type.h"
49 /** Data about how and where to blit pixels. */
51 Blitter::Surface
*surface
;
53 int left
, top
, width
, height
;
56 /** Data about how and where to blit pixels. */
57 struct DrawPixelInfo
: BlitArea
{
65 extern byte _dirkeys
; ///< 1 = left, 2 = up, 4 = right, 8 = down
66 extern bool _fullscreen
;
67 extern byte _support8bpp
;
68 extern CursorVars _cursor
;
69 extern bool _ctrl_pressed
; ///< Is Ctrl pressed?
70 extern bool _shift_pressed
; ///< Is Shift pressed?
71 extern byte _fast_forward
;
73 extern bool _left_button_down
;
74 extern bool _left_button_clicked
;
75 extern bool _right_button_down
;
76 extern bool _right_button_clicked
;
78 extern ttd_unique_ptr
<Blitter::Surface
> _screen_surface
;
79 extern int _screen_width
, _screen_height
;
81 extern int _num_resolutions
;
82 extern Dimension _resolutions
[32];
83 extern Dimension _cur_resolution
;
84 extern Palette _cur_palette
; ///< Current palette
85 extern int _cur_palette_first_dirty
; ///< First palette dirty element.
86 extern int _cur_palette_count_dirty
; ///< Number of palette dirty elements.
88 void HandleKeypress(uint keycode
, WChar key
);
89 void HandleTextInput(const char *str
, bool marked
= false, const char *caret
= NULL
, const char *insert_location
= NULL
, const char *replacement_end
= NULL
);
90 void HandleCtrlChanged();
91 void HandleMouseEvents();
92 void CSleep(int milliseconds
);
95 void DrawMouseCursor();
96 void ScreenSizeChanged();
97 void GameSizeChanged();
98 void UndrawMouseCursor();
100 /** Size of the buffer used for drawing strings. */
101 static const int DRAW_STRING_BUFFER
= 2048;
103 void ScrollScreenRect (int left
, int top
, int width
, int height
, int dx
, int dy
);
105 Dimension
GetSpriteSize(SpriteID sprid
, Point
*offset
= NULL
, ZoomLevel zoom
= ZOOM_LVL_GUI
);
106 void DrawSpriteViewport (DrawPixelInfo
*dpi
, SpriteID img
, PaletteID pal
, int x
, int y
, const SubSprite
*sub
= NULL
);
107 void DrawSprite (BlitArea
*dpi
, SpriteID img
, PaletteID pal
, int x
, int y
);
109 /** How to align the to-be drawn text. */
110 enum StringAlignment
{
111 SA_LEFT
= 0 << 0, ///< Left align the text.
112 SA_HOR_CENTER
= 1 << 0, ///< Horizontally center the text.
113 SA_RIGHT
= 2 << 0, ///< Right align the text (must be a single bit).
114 SA_HOR_MASK
= 3 << 0, ///< Mask for horizontal alignment.
116 SA_TOP
= 0 << 2, ///< Top align the text.
117 SA_VERT_CENTER
= 1 << 2, ///< Vertically center the text.
118 SA_BOTTOM
= 2 << 2, ///< Bottom align the text.
119 SA_VERT_MASK
= 3 << 2, ///< Mask for vertical alignment.
121 SA_CENTER
= SA_HOR_CENTER
| SA_VERT_CENTER
, ///< Center both horizontally and vertically.
123 SA_FORCE
= 1 << 4, ///< Force the alignment, i.e. don't swap for RTL languages.
125 DECLARE_ENUM_AS_BIT_SET(StringAlignment
)
127 int DrawString (BlitArea
*dpi
, int left
, int right
, int top
, const char *str
, TextColour colour
= TC_FROMSTRING
, StringAlignment align
= SA_LEFT
, bool underline
= false, FontSize fontsize
= FS_NORMAL
);
128 int DrawString (BlitArea
*dpi
, int left
, int right
, int top
, StringID str
, TextColour colour
= TC_FROMSTRING
, StringAlignment align
= SA_LEFT
, bool underline
= false, FontSize fontsize
= FS_NORMAL
);
129 int DrawStringMultiLine (BlitArea
*dpi
, 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
);
130 int DrawStringMultiLine (BlitArea
*dpi
, 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
);
132 void DrawCharCentered (BlitArea
*dpi
, WChar c
, int x
, int y
, TextColour colour
);
134 void GfxFillRect (BlitArea
*dpi
, int left
, int top
, int right
, int bottom
, int colour
, FillRectMode mode
= FILLRECT_OPAQUE
);
135 void GfxDrawLine (BlitArea
*dpi
, int left
, int top
, int right
, int bottom
, int colour
, int width
= 1, int dash
= 0);
136 void DrawBox (DrawPixelInfo
*dpi
, int x
, int y
, int dx1
, int dy1
, int dx2
, int dy2
, int dx3
, int dy3
);
138 Dimension
GetStringBoundingBox(const char *str
, FontSize start_fontsize
= FS_NORMAL
);
139 Dimension
GetStringBoundingBox(StringID strid
);
140 uint
GetStringHeight (const char *str
, int maxw
, FontSize fontsize
= FS_NORMAL
);
141 uint
GetStringHeight (StringID str
, int maxw
);
142 int GetStringLineCount(StringID str
, int maxw
);
143 void LoadStringWidthTable(bool monospace
= false);
145 void DrawDirtyBlocks();
146 void SetDirtyBlocks(int left
, int top
, int right
, int bottom
);
147 void MarkWholeScreenDirty();
149 void GfxInitPalettes();
152 bool InitBlitArea (const BlitArea
*o
, BlitArea
*n
, int left
, int top
, int width
, int height
);
155 * Determine where to draw a centred object inside a widget.
156 * @param min The top or left coordinate.
157 * @param max The bottom or right coordinate.
158 * @param size The height or width of the object to draw.
159 * @return Offset of where to start drawing the object.
161 static inline int CenterBounds(int min
, int max
, int size
)
163 return min
+ (max
- min
- size
+ 1) / 2;
167 void DrawOverlappedWindowForAll(int left
, int top
, int right
, int bottom
);
169 void SetMouseCursorBusy(bool busy
);
170 void SetMouseCursor (CursorID cursor
);
171 void SetAnimatedMouseCursor(const AnimCursor
*table
);
173 void UpdateCursorSize();
174 bool ChangeResInGame(int w
, int h
);
175 void SortResolutions(int count
);
176 bool ToggleFullScreen(bool fs
);
179 int GetCharacterHeight(FontSize size
);
181 /** Height of characters in the small (#FS_SMALL) font. @note Some characters may be oversized. */
182 #define FONT_HEIGHT_SMALL (GetCharacterHeight(FS_SMALL))
184 /** Height of characters in the normal (#FS_NORMAL) font. @note Some characters may be oversized. */
185 #define FONT_HEIGHT_NORMAL (GetCharacterHeight(FS_NORMAL))
187 /** Height of characters in the large (#FS_LARGE) font. @note Some characters may be oversized. */
188 #define FONT_HEIGHT_LARGE (GetCharacterHeight(FS_LARGE))
190 /** Height of characters in the large (#FS_MONO) font. @note Some characters may be oversized. */
191 #define FONT_HEIGHT_MONO (GetCharacterHeight(FS_MONO))
193 TextColour
GetContrastColour(uint8 background
);
196 * All 16 colour gradients
197 * 8 colours per gradient from darkest (0) to lightest (7)
199 extern byte _colour_gradient
[COLOUR_END
][8];
201 extern bool _palette_remap_grf
[];
204 * Return the colour for a particular greyscale level.
205 * @param level Intensity, 0 = black, 15 = white
208 #define GREY_SCALE(level) (level)
210 static const uint8 PC_BLACK
= GREY_SCALE(1); ///< Black palette colour.
211 static const uint8 PC_DARK_GREY
= GREY_SCALE(6); ///< Dark grey palette colour.
212 static const uint8 PC_GREY
= GREY_SCALE(10); ///< Grey palette colour.
213 static const uint8 PC_WHITE
= GREY_SCALE(15); ///< White palette colour.
215 static const uint8 PC_VERY_DARK_RED
= 0xB2; ///< Almost-black red palette colour.
216 static const uint8 PC_DARK_RED
= 0xB4; ///< Dark red palette colour.
217 static const uint8 PC_RED
= 0xB8; ///< Red palette colour.
219 static const uint8 PC_VERY_DARK_BROWN
= 0x56; ///< Almost-black brown palette colour.
221 static const uint8 PC_ORANGE
= 0xC2; ///< Orange palette colour.
223 static const uint8 PC_YELLOW
= 0xBF; ///< Yellow palette colour.
224 static const uint8 PC_LIGHT_YELLOW
= 0x44; ///< Light yellow palette colour.
225 static const uint8 PC_VERY_LIGHT_YELLOW
= 0x45; ///< Almost-white yellow palette colour.
227 static const uint8 PC_GREEN
= 0xD0; ///< Green palette colour.
229 static const uint8 PC_DARK_BLUE
= 0x9D; ///< Dark blue palette colour.
230 static const uint8 PC_LIGHT_BLUE
= 0x98; ///< Light blue palette colour.
232 #endif /* GFX_FUNC_H */