Fix: Data races on cursor state in OpenGL backends
[openttd-github.git] / src / sprite.h
blob44814c09b91bbfaeaf6303cb7a2ccb2493ff2e5e
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 sprite.h Base for drawing complex sprites. */
10 #ifndef SPRITE_H
11 #define SPRITE_H
13 #include "transparency.h"
15 #include "table/sprites.h"
17 #define GENERAL_SPRITE_COLOUR(colour) ((colour) + PALETTE_RECOLOUR_START)
18 #define COMPANY_SPRITE_COLOUR(owner) (GENERAL_SPRITE_COLOUR(_company_colours[owner]))
20 /* The following describes bunch of sprites to be drawn together in a single 3D
21 * bounding box. Used especially for various multi-sprite buildings (like
22 * depots or stations): */
24 /** A tile child sprite and palette to draw for stations etc, with 3D bounding box */
25 struct DrawTileSeqStruct {
26 int8 delta_x; ///< \c 0x80 is sequence terminator
27 int8 delta_y;
28 int8 delta_z; ///< \c 0x80 identifies child sprites
29 byte size_x;
30 byte size_y;
31 byte size_z;
32 PalSpriteID image;
34 /** Make this struct a sequence terminator. */
35 void MakeTerminator()
37 this->delta_x = (int8)0x80;
40 /** Check whether this is a sequence terminator. */
41 bool IsTerminator() const
43 return (byte)this->delta_x == 0x80;
46 /** Check whether this is a parent sprite with a boundingbox. */
47 bool IsParentSprite() const
49 return (byte)this->delta_z != 0x80;
53 /**
54 * Ground palette sprite of a tile, together with its sprite layout.
55 * This struct is used for static sprite layouts in the code.
56 * For allocated ones from NewGRF see #NewGRFSpriteLayout.
58 struct DrawTileSprites {
59 PalSpriteID ground; ///< Palette and sprite for the ground
60 const DrawTileSeqStruct *seq; ///< Array of child sprites. Terminated with a terminator entry
63 /**
64 * This structure is the same for both Industries and Houses.
65 * Buildings here reference a general type of construction
67 struct DrawBuildingsTileStruct {
68 PalSpriteID ground;
69 PalSpriteID building;
70 byte subtile_x;
71 byte subtile_y;
72 byte width;
73 byte height;
74 byte dz;
75 byte draw_proc; // this allows to specify a special drawing procedure.
78 /** Iterate through all DrawTileSeqStructs in DrawTileSprites. */
79 #define foreach_draw_tile_seq(idx, list) for (idx = list; !idx->IsTerminator(); idx++)
81 void DrawCommonTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned);
82 void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned);
84 /**
85 * Draw tile sprite sequence on tile with railroad specifics.
86 * @param total_offset Spriteoffset from normal rail to current railtype.
87 * @param newgrf_offset Startsprite of the Action1 to use.
89 static inline void DrawRailTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
91 DrawCommonTileSeq(ti, dts, to, total_offset, newgrf_offset, default_palette, false);
94 /**
95 * Draw tile sprite sequence in GUI with railroad specifics.
96 * @param total_offset Spriteoffset from normal rail to current railtype.
97 * @param newgrf_offset Startsprite of the Action1 to use.
99 static inline void DrawRailTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
101 DrawCommonTileSeqInGUI(x, y, dts, total_offset, newgrf_offset, default_palette, false);
105 * Draw TTD sprite sequence on tile.
107 static inline void DrawOrigTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, PaletteID default_palette)
109 DrawCommonTileSeq(ti, dts, to, 0, 0, default_palette, false);
113 * Draw TTD sprite sequence in GUI.
115 static inline void DrawOrigTileSeqInGUI(int x, int y, const DrawTileSprites *dts, PaletteID default_palette)
117 DrawCommonTileSeqInGUI(x, y, dts, 0, 0, default_palette, false);
121 * Draw NewGRF industrytile or house sprite layout
122 * @param stage Sprite inside the Action1 spritesets to use, i.e. construction stage.
124 static inline void DrawNewGRFTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, uint32 stage, PaletteID default_palette)
126 DrawCommonTileSeq(ti, dts, to, 0, stage, default_palette, true);
130 * Draw NewGRF object in GUI
131 * @param stage Sprite inside the Action1 spritesets to use, i.e. construction stage.
133 static inline void DrawNewGRFTileSeqInGUI(int x, int y, const DrawTileSprites *dts, uint32 stage, PaletteID default_palette)
135 DrawCommonTileSeqInGUI(x, y, dts, 0, stage, default_palette, true);
139 * Applies PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOUR to a palette entry of a sprite layout entry
140 * @note for ground sprites use #GroundSpritePaletteTransform
141 * @note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set
142 * when to use the default palette.
144 * @param image The sprite to draw
145 * @param pal The palette from the sprite layout
146 * @param default_pal The default recolour sprite to use (typically company colour resp. random industry/house colour)
147 * @return The palette to use
149 static inline PaletteID SpriteLayoutPaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
151 if (HasBit(image, PALETTE_MODIFIER_TRANSPARENT) || HasBit(image, PALETTE_MODIFIER_COLOUR)) {
152 return (pal != 0 ? pal : default_pal);
153 } else {
154 return PAL_NONE;
159 * Applies PALETTE_MODIFIER_COLOUR to a palette entry of a ground sprite
160 * @note Not useable for OTTD internal spritelayouts from table/xxx_land.h as PALETTE_MODIFIER_TRANSPARENT is only set
161 * when to use the default palette.
163 * @param image The sprite to draw
164 * @param pal The palette from the sprite layout
165 * @param default_pal The default recolour sprite to use (typically company colour resp. random industry/house colour)
166 * @return The palette to use
168 static inline PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
170 if (HasBit(image, PALETTE_MODIFIER_COLOUR)) {
171 return (pal != 0 ? pal : default_pal);
172 } else {
173 return PAL_NONE;
177 #endif /* SPRITE_H */