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/>.
8 /** @file gfx_type.h Types related to the graphics and/or input devices. */
13 #include "core/endian_type.hpp"
14 #include "core/enum_type.hpp"
15 #include "core/geometry_type.hpp"
16 #include "zoom_type.h"
18 typedef uint32_t SpriteID
; ///< The number of a sprite, without mapping bits and colourtables
19 typedef uint32_t PaletteID
; ///< The number of the palette
20 typedef uint32_t CursorID
; ///< The number of the cursor (sprite)
22 /** Combination of a palette sprite and a 'real' sprite */
24 SpriteID sprite
; ///< The 'real' sprite
25 PaletteID pal
; ///< The palette (use \c PAL_NONE) if not needed)
34 WKC_GLOBAL_HOTKEY
= 0x0800, ///< Fake keycode bit to indicate global hotkeys
36 WKC_SPECIAL_KEYS
= WKC_SHIFT
| WKC_CTRL
| WKC_ALT
| WKC_META
| WKC_GLOBAL_HOTKEY
,
77 /* Backquote is the key left of "1"
78 * we only store this key here, no matter what character is really mapped to it
79 * on a particular keyboard. (US keyboard: ` and ~ ; German keyboard: ^ and °) */
83 /* 0-9 are mapped to 48-57
84 * A-Z are mapped to 65-90
85 * a-z are mapped to 97-122 */
87 /* Numerical keyboard */
93 WKC_NUM_DECIMAL
= 143,
96 WKC_SLASH
= 144, ///< / Forward slash
97 WKC_SEMICOLON
= 145, ///< ; Semicolon
98 WKC_EQUALS
= 146, ///< = Equals
99 WKC_L_BRACKET
= 147, ///< [ Left square bracket
100 WKC_BACKSLASH
= 148, ///< \ Backslash
101 WKC_R_BRACKET
= 149, ///< ] Right square bracket
102 WKC_SINGLEQUOTE
= 150, ///< ' Single quote
103 WKC_COMMA
= 151, ///< , Comma
104 WKC_PERIOD
= 152, ///< . Period
105 WKC_MINUS
= 153, ///< - Minus
108 /** A single sprite of a list of animated cursors */
110 static const CursorID LAST
= MAX_UVALUE(CursorID
);
111 CursorID sprite
; ///< Must be set to LAST_ANIM when it is the last sprite of the loop
112 uint8_t display_time
; ///< Amount of ticks this sprite will be shown
115 struct CursorSprite
{
116 PalSpriteID image
; ///< Image.
117 Point pos
; ///< Relative position.
119 constexpr CursorSprite(SpriteID spr
, PaletteID pal
, int x
, int y
) : image({spr
, pal
}), pos({x
, y
}) {}
122 /** Collection of variables for cursor-display and -animation */
124 /* Logical mouse position */
125 Point pos
; ///< logical mouse position
126 Point delta
; ///< relative mouse movement in this tick
127 int wheel
; ///< mouse wheel movement
128 bool fix_at
; ///< mouse is moving, but cursor is not (used for scrolling)
130 /* We need two different vars to keep track of how far the scrollwheel moved.
131 * OSX uses this for scrolling around the map. */
135 /* Mouse appearance */
136 std::vector
<CursorSprite
> sprites
; ///< Sprites comprising cursor.
137 Point total_offs
, total_size
; ///< union of sprite properties
139 Point draw_pos
, draw_size
; ///< position and size bounding-box for drawing
141 const AnimCursor
*animate_list
; ///< in case of animated cursor, list of frames
142 const AnimCursor
*animate_cur
; ///< in case of animated cursor, current frame
143 uint animate_timeout
; ///< in case of animated cursor, number of ticks to show the current cursor
145 bool visible
; ///< cursor is visible
146 bool dirty
; ///< the rect occupied by the mouse is dirty (redraw)
147 bool in_window
; ///< mouse inside this window, determines drawing logic
150 bool vehchain
; ///< vehicle chain is dragged
152 void UpdateCursorPositionRelative(int delta_x
, int delta_y
);
153 bool UpdateCursorPosition(int x
, int y
);
156 /** Data about how and where to blit pixels. */
157 struct DrawPixelInfo
{
159 int left
, top
, width
, height
;
164 /** Structure to access the alpha, red, green, and blue channels from a 32 bit number. */
166 uint32_t data
; ///< Conversion of the channel information to a 32 bit number.
168 #if defined(__EMSCRIPTEN__)
169 uint8_t r
, g
, b
, a
; ///< colour channels as used in browsers
170 #elif TTD_ENDIAN == TTD_BIG_ENDIAN
171 uint8_t a
, r
, g
, b
; ///< colour channels in BE order
173 uint8_t b
, g
, r
, a
; ///< colour channels in LE order
174 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
178 * Create a new colour.
179 * @param r The channel for the red colour.
180 * @param g The channel for the green colour.
181 * @param b The channel for the blue colour.
182 * @param a The channel for the alpha/transparency.
184 Colour(uint8_t r
, uint8_t g
, uint8_t b
, uint8_t a
= 0xFF) :
185 #if defined(__EMSCRIPTEN__)
186 r(r
), g(g
), b(b
), a(a
)
187 #elif TTD_ENDIAN == TTD_BIG_ENDIAN
188 a(a
), r(r
), g(g
), b(b
)
190 b(b
), g(g
), r(r
), a(a
)
191 #endif /* TTD_ENDIAN == TTD_BIG_ENDIAN */
196 * Create a new colour.
197 * @param data The colour in the correct packed format.
199 Colour(uint data
= 0) : data(data
)
204 static_assert(sizeof(Colour
) == sizeof(uint32_t));
207 /** Available font sizes */
209 FS_NORMAL
, ///< Index of the normal font in the font tables.
210 FS_SMALL
, ///< Index of the small font in the font tables.
211 FS_LARGE
, ///< Index of the large font in the font tables.
212 FS_MONO
, ///< Index of the monospaced font in the font tables.
215 FS_BEGIN
= FS_NORMAL
, ///< First font.
217 DECLARE_POSTFIX_INCREMENT(FontSize
)
219 inline const char *FontSizeToName(FontSize fs
)
221 static const char *SIZE_TO_NAME
[] = { "medium", "small", "large", "mono" };
223 return SIZE_TO_NAME
[fs
];
227 * Used to only draw a part of the sprite.
228 * Draw the subsprite in the rect (sprite_x_offset + left, sprite_y_offset + top) to (sprite_x_offset + right, sprite_y_offset + bottom).
229 * Both corners are included in the drawing area.
232 int left
, top
, right
, bottom
;
235 enum Colours
: uint8_t {
237 COLOUR_DARK_BLUE
= COLOUR_BEGIN
,
254 INVALID_COLOUR
= 0xFF,
256 DECLARE_POSTFIX_INCREMENT(Colours
)
257 DECLARE_ENUM_AS_ADDABLE(Colours
)
259 /** Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palette.png */
262 TC_FROMSTRING
= 0x00,
268 TC_LIGHT_BROWN
= 0x05,
272 TC_DARK_GREEN
= 0x09,
276 TC_LIGHT_BLUE
= 0x0D,
283 TC_IS_PALETTE_COLOUR
= 0x100, ///< Colour value is already a real palette colour index, not an index of a StringColour.
284 TC_NO_SHADE
= 0x200, ///< Do not add shading to this text colour.
285 TC_FORCED
= 0x400, ///< Ignore colour changes from strings.
287 TC_COLOUR_MASK
= 0xFF, ///< Mask to test if TextColour (without flags) is within limits.
288 TC_FLAGS_MASK
= 0x700, ///< Mask to test if TextColour (with flags) is within limits.
290 DECLARE_ENUM_AS_BIT_SET(TextColour
)
292 /* A few values that are related to animations using palette changes */
293 static constexpr uint8_t PALETTE_ANIM_SIZE
= 28; ///< number of animated colours
294 static constexpr uint8_t PALETTE_ANIM_START
= 227; ///< Index in the _palettes array from which all animations are taking places (table/palettes.h)
296 /** Define the operation GfxFillRect performs */
298 FILLRECT_OPAQUE
, ///< Fill rectangle with a single colour
299 FILLRECT_CHECKER
, ///< Draw only every second pixel, used for greying-out
300 FILLRECT_RECOLOUR
, ///< Apply a recolour sprite to the screen content
303 /** Palettes OpenTTD supports. */
305 PAL_DOS
, ///< Use the DOS palette.
306 PAL_WINDOWS
, ///< Use the Windows palette.
307 PAL_AUTODETECT
, ///< Automatically detect the palette based on the graphics pack.
308 MAX_PAL
= 2, ///< The number of palettes.
311 /** Types of sprites that might be loaded */
312 enum class SpriteType
: uint8_t {
313 Normal
= 0, ///< The most basic (normal) sprite
314 MapGen
= 1, ///< Special sprite for the map generator
315 Font
= 2, ///< A sprite used for fonts
316 Recolour
= 3, ///< Recolour sprite
317 Invalid
= 4, ///< Pseudosprite or other unusable sprite, used only internally
321 * The number of milliseconds per game tick.
322 * The value 27 together with a day length of 74 ticks makes one day 1998 milliseconds, almost exactly 2 seconds.
323 * With a 2 second day, one standard month is 1 minute, and one standard year is slightly over 12 minutes.
325 static const uint MILLISECONDS_PER_TICK
= 27;
327 /** Information about the currently used palette. */
329 Colour palette
[256]; ///< Current palette. Entry 0 has to be always fully transparent!
330 int first_dirty
; ///< The first dirty element.
331 int count_dirty
; ///< The number of dirty elements.
334 /** Modes for 8bpp support */
336 S8BPP_NONE
= 0, ///< No support for 8bpp by OS or hardware, force 32bpp blitters.
337 S8BPP_SYSTEM
, ///< No 8bpp support by hardware, do not try to use 8bpp video modes or hardware palettes.
338 S8BPP_HARDWARE
, ///< Full 8bpp support by OS and hardware.
341 /** How to align the to-be drawn text. */
342 enum StringAlignment
{
343 SA_LEFT
= 0 << 0, ///< Left align the text.
344 SA_HOR_CENTER
= 1 << 0, ///< Horizontally center the text.
345 SA_RIGHT
= 2 << 0, ///< Right align the text (must be a single bit).
346 SA_HOR_MASK
= 3 << 0, ///< Mask for horizontal alignment.
348 SA_TOP
= 0 << 2, ///< Top align the text.
349 SA_VERT_CENTER
= 1 << 2, ///< Vertically center the text.
350 SA_BOTTOM
= 2 << 2, ///< Bottom align the text.
351 SA_VERT_MASK
= 3 << 2, ///< Mask for vertical alignment.
353 SA_CENTER
= SA_HOR_CENTER
| SA_VERT_CENTER
, ///< Center both horizontally and vertically.
355 SA_FORCE
= 1 << 4, ///< Force the alignment, i.e. don't swap for RTL languages.
357 DECLARE_ENUM_AS_BIT_SET(StringAlignment
)
359 #endif /* GFX_TYPE_H */