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  */
7 
8 /** @file sprite.h Base for drawing complex sprites. */
9 
10 #ifndef SPRITE_H
11 #define SPRITE_H
12 
13 #include "transparency.h"
14 
15 #include "table/sprites.h"
16 
17 #define GENERAL_SPRITE_COLOUR(colour) ((colour) + PALETTE_RECOLOUR_START)
18 #define COMPANY_SPRITE_COLOUR(owner) (GENERAL_SPRITE_COLOUR(_company_colours[owner]))
19 
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): */
23 
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;
33 
34 	/** Make this struct a sequence terminator. */
MakeTerminatorDrawTileSeqStruct35 	void MakeTerminator()
36 	{
37 		this->delta_x = (int8)0x80;
38 	}
39 
40 	/** Check whether this is a sequence terminator. */
IsTerminatorDrawTileSeqStruct41 	bool IsTerminator() const
42 	{
43 		return (byte)this->delta_x == 0x80;
44 	}
45 
46 	/** Check whether this is a parent sprite with a boundingbox. */
IsParentSpriteDrawTileSeqStruct47 	bool IsParentSprite() const
48 	{
49 		return (byte)this->delta_z != 0x80;
50 	}
51 };
52 
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.
57  */
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
61 };
62 
63 /**
64  * This structure is the same for both Industries and Houses.
65  * Buildings here reference a general type of construction
66  */
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.
76 };
77 
78 /** Iterate through all DrawTileSeqStructs in DrawTileSprites. */
79 #define foreach_draw_tile_seq(idx, list) for (idx = list; !idx->IsTerminator(); idx++)
80 
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);
83 
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.
88  */
DrawRailTileSeq(const struct TileInfo * ti,const DrawTileSprites * dts,TransparencyOption to,int32 total_offset,uint32 newgrf_offset,PaletteID default_palette)89 static inline void DrawRailTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
90 {
91 	DrawCommonTileSeq(ti, dts, to, total_offset, newgrf_offset, default_palette, false);
92 }
93 
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.
98  */
DrawRailTileSeqInGUI(int x,int y,const DrawTileSprites * dts,int32 total_offset,uint32 newgrf_offset,PaletteID default_palette)99 static inline void DrawRailTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 total_offset, uint32 newgrf_offset, PaletteID default_palette)
100 {
101 	DrawCommonTileSeqInGUI(x, y, dts, total_offset, newgrf_offset, default_palette, false);
102 }
103 
104 /**
105  * Draw TTD sprite sequence on tile.
106  */
DrawOrigTileSeq(const struct TileInfo * ti,const DrawTileSprites * dts,TransparencyOption to,PaletteID default_palette)107 static inline void DrawOrigTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, PaletteID default_palette)
108 {
109 	DrawCommonTileSeq(ti, dts, to, 0, 0, default_palette, false);
110 }
111 
112 /**
113  * Draw TTD sprite sequence in GUI.
114  */
DrawOrigTileSeqInGUI(int x,int y,const DrawTileSprites * dts,PaletteID default_palette)115 static inline void DrawOrigTileSeqInGUI(int x, int y, const DrawTileSprites *dts, PaletteID default_palette)
116 {
117 	DrawCommonTileSeqInGUI(x, y, dts, 0, 0, default_palette, false);
118 }
119 
120 /**
121  * Draw NewGRF industrytile or house sprite layout
122  * @param stage Sprite inside the Action1 spritesets to use, i.e. construction stage.
123  */
DrawNewGRFTileSeq(const struct TileInfo * ti,const DrawTileSprites * dts,TransparencyOption to,uint32 stage,PaletteID default_palette)124 static inline void DrawNewGRFTileSeq(const struct TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, uint32 stage, PaletteID default_palette)
125 {
126 	DrawCommonTileSeq(ti, dts, to, 0, stage, default_palette, true);
127 }
128 
129 /**
130  * Draw NewGRF object in GUI
131  * @param stage Sprite inside the Action1 spritesets to use, i.e. construction stage.
132  */
DrawNewGRFTileSeqInGUI(int x,int y,const DrawTileSprites * dts,uint32 stage,PaletteID default_palette)133 static inline void DrawNewGRFTileSeqInGUI(int x, int y, const DrawTileSprites *dts, uint32 stage, PaletteID default_palette)
134 {
135 	DrawCommonTileSeqInGUI(x, y, dts, 0, stage, default_palette, true);
136 }
137 
138 /**
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.
143  *
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
148  */
SpriteLayoutPaletteTransform(SpriteID image,PaletteID pal,PaletteID default_pal)149 static inline PaletteID SpriteLayoutPaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
150 {
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;
155 	}
156 }
157 
158 /**
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.
162  *
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
167  */
GroundSpritePaletteTransform(SpriteID image,PaletteID pal,PaletteID default_pal)168 static inline PaletteID GroundSpritePaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
169 {
170 	if (HasBit(image, PALETTE_MODIFIER_COLOUR)) {
171 		return (pal != 0 ? pal : default_pal);
172 	} else {
173 		return PAL_NONE;
174 	}
175 }
176 
177 #endif /* SPRITE_H */
178