Update: Translations from eints
[openttd-github.git] / src / gfx_type.h
blob44dc47cf984abf729ab7428f0d9799906a9c11e9
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 */
8 /** @file gfx_type.h Types related to the graphics and/or input devices. */
10 #ifndef GFX_TYPE_H
11 #define GFX_TYPE_H
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 */
23 struct PalSpriteID {
24 SpriteID sprite; ///< The 'real' sprite
25 PaletteID pal; ///< The palette (use \c PAL_NONE) if not needed)
28 enum WindowKeyCodes {
29 WKC_SHIFT = 0x8000,
30 WKC_CTRL = 0x4000,
31 WKC_ALT = 0x2000,
32 WKC_META = 0x1000,
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,
38 /* Special ones */
39 WKC_NONE = 0,
40 WKC_ESC = 1,
41 WKC_BACKSPACE = 2,
42 WKC_INSERT = 3,
43 WKC_DELETE = 4,
45 WKC_PAGEUP = 5,
46 WKC_PAGEDOWN = 6,
47 WKC_END = 7,
48 WKC_HOME = 8,
50 /* Arrow keys */
51 WKC_LEFT = 9,
52 WKC_UP = 10,
53 WKC_RIGHT = 11,
54 WKC_DOWN = 12,
56 /* Return & tab */
57 WKC_RETURN = 13,
58 WKC_TAB = 14,
60 /* Space */
61 WKC_SPACE = 32,
63 /* Function keys */
64 WKC_F1 = 33,
65 WKC_F2 = 34,
66 WKC_F3 = 35,
67 WKC_F4 = 36,
68 WKC_F5 = 37,
69 WKC_F6 = 38,
70 WKC_F7 = 39,
71 WKC_F8 = 40,
72 WKC_F9 = 41,
73 WKC_F10 = 42,
74 WKC_F11 = 43,
75 WKC_F12 = 44,
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 °) */
80 WKC_BACKQUOTE = 45,
81 WKC_PAUSE = 46,
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 */
88 WKC_NUM_DIV = 138,
89 WKC_NUM_MUL = 139,
90 WKC_NUM_MINUS = 140,
91 WKC_NUM_PLUS = 141,
92 WKC_NUM_ENTER = 142,
93 WKC_NUM_DECIMAL = 143,
95 /* Other keys */
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 */
109 struct AnimCursor {
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 */
123 struct CursorVars {
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. */
132 int v_wheel;
133 int h_wheel;
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
149 /* Drag data */
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 {
158 void *dst_ptr;
159 int left, top, width, height;
160 int pitch;
161 ZoomLevel zoom;
164 /** Structure to access the alpha, red, green, and blue channels from a 32 bit number. */
165 union Colour {
166 uint32_t data; ///< Conversion of the channel information to a 32 bit number.
167 struct {
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
172 #else
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)
189 #else
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 */
208 enum FontSize {
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.
213 FS_END,
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" };
222 assert(fs < FS_END);
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.
231 struct SubSprite {
232 int left, top, right, bottom;
235 enum Colours : uint8_t {
236 COLOUR_BEGIN,
237 COLOUR_DARK_BLUE = COLOUR_BEGIN,
238 COLOUR_PALE_GREEN,
239 COLOUR_PINK,
240 COLOUR_YELLOW,
241 COLOUR_RED,
242 COLOUR_LIGHT_BLUE,
243 COLOUR_GREEN,
244 COLOUR_DARK_GREEN,
245 COLOUR_BLUE,
246 COLOUR_CREAM,
247 COLOUR_MAUVE,
248 COLOUR_PURPLE,
249 COLOUR_ORANGE,
250 COLOUR_BROWN,
251 COLOUR_GREY,
252 COLOUR_WHITE,
253 COLOUR_END,
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 */
260 enum TextColour {
261 TC_BEGIN = 0x00,
262 TC_FROMSTRING = 0x00,
263 TC_BLUE = 0x00,
264 TC_SILVER = 0x01,
265 TC_GOLD = 0x02,
266 TC_RED = 0x03,
267 TC_PURPLE = 0x04,
268 TC_LIGHT_BROWN = 0x05,
269 TC_ORANGE = 0x06,
270 TC_GREEN = 0x07,
271 TC_YELLOW = 0x08,
272 TC_DARK_GREEN = 0x09,
273 TC_CREAM = 0x0A,
274 TC_BROWN = 0x0B,
275 TC_WHITE = 0x0C,
276 TC_LIGHT_BLUE = 0x0D,
277 TC_GREY = 0x0E,
278 TC_DARK_BLUE = 0x0F,
279 TC_BLACK = 0x10,
280 TC_END,
281 TC_INVALID = 0xFF,
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 */
297 enum FillRectMode {
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. */
304 enum PaletteType {
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. */
328 struct 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 */
335 enum Support8bpp {
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 */