OpenTTD

sprite.cpp
Go to the documentation of this file.
1 /* $Id: sprite.cpp 27134 2015-02-01 20:54:24Z frosch $ */
2
3 /*
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/>.
8 */
9
12 #include "stdafx.h"
13 #include "sprite.h"
14 #include "viewport_func.h"
15 #include "landscape.h"
16 #include "spritecache.h"
17 #include "zoom_func.h"
18
19 #include "safeguards.h"
20
21
32 void DrawCommonTileSeq(const TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
33 {
34 bool parent_sprite_encountered = false;
35 const DrawTileSeqStruct *dtss;
36 bool skip_childs = false;
37 foreach_draw_tile_seq(dtss, dts->seq) {
38 SpriteID image = dtss->image.sprite;
39 PaletteID pal = dtss->image.pal;
40
41 if (skip_childs) {
42 if (!dtss->IsParentSprite()) continue;
43 skip_childs = false;
44 }
45
46 /* TTD sprite 0 means no sprite */
47 if ((GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) ||
48 (IsInvisibilitySet(to) && !HasBit(image, SPRITE_MODIFIER_OPAQUE))) {
49 skip_childs = dtss->IsParentSprite();
50 continue;
51 }
52
53 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
54 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
55
56 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
57
58 if (dtss->IsParentSprite()) {
59 parent_sprite_encountered = true;
60 AddSortableSpriteToDraw(
61 image, pal,
62 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
63 dtss->size_x, dtss->size_y,
64 dtss->size_z, ti->z + dtss->delta_z,
65 !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to)
66 );
67 } else {
68 int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
69 int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
70 bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
71 if (parent_sprite_encountered) {
72 AddChildSpriteScreen(image, pal, offs_x, offs_y, transparent);
73 } else {
74 if (transparent) {
75 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
76 pal = PALETTE_TO_TRANSPARENT;
77 }
78 DrawGroundSprite(image, pal, NULL, offs_x, offs_y);
79 }
80 }
81 }
82 }
83
94 void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
95 {
96 const DrawTileSeqStruct *dtss;
97 Point child_offset = {0, 0};
98
99 bool skip_childs = false;
100 foreach_draw_tile_seq(dtss, dts->seq) {
101 SpriteID image = dtss->image.sprite;
102 PaletteID pal = dtss->image.pal;
103
104 if (skip_childs) {
105 if (!dtss->IsParentSprite()) continue;
106 skip_childs = false;
107 }
108
109 /* TTD sprite 0 means no sprite */
110 if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
111 skip_childs = dtss->IsParentSprite();
112 continue;
113 }
114
115 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
116 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
117
118 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
119
120 if (dtss->IsParentSprite()) {
121 Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
122 DrawSprite(image, pal, x + UnScaleGUI(pt.x), y + UnScaleGUI(pt.y));
123
124 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
125 child_offset.x = UnScaleGUI(pt.x + spr->x_offs);
126 child_offset.y = UnScaleGUI(pt.y + spr->y_offs);
127 } else {
128 int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
129 int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
130 DrawSprite(image, pal, x + child_offset.x + ScaleGUITrad(offs_x), y + child_offset.y + ScaleGUITrad(offs_y));
131 }
132 }
133 }
PaletteID
uint32 PaletteID
The number of the palette.
Definition: gfx_type.h:20
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition: sprites.h:1566
TileInfo
Tile information, used while rendering the tile.
Definition: tile_cmd.h:44
DrawCommonTileSeqInGUI
void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
Draws a tile sprite sequence in the GUI.
Definition: sprite.cpp:94
UnScaleGUI
static int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition: zoom_func.h:72
SPRITE_MASK
The mask to for the main sprite.
Definition: sprites.h:1519
RemapCoords
static Point RemapCoords(int x, int y, int z)
Map 3D world or tile coordinate to equivalent 2D coordinate as used in the viewports and smallmap...
Definition: landscape.h:83
SetBit
static T SetBit(T &x, const uint8 y)
Set a bit in a variable.
Definition: bitmath_func.hpp:123
Sprite
Data structure describing a sprite.
Definition: spritecache.h:18
ScaleGUITrad
static int ScaleGUITrad(int value)
Scale traditional pixel dimensions to GUI zoom level.
Definition: zoom_func.h:82
DrawCommonTileSeq
void DrawCommonTileSeq(const TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
Draws a tile sprite sequence.
Definition: sprite.cpp:32
ST_NORMAL
The most basic (normal) sprite.
Definition: gfx_type.h:298
TileInfo::z
int z
Height.
Definition: tile_cmd.h:49
DrawTileSprites::seq
const DrawTileSeqStruct * seq
Array of child sprites. Terminated with a terminator entry.
Definition: sprite.h:62
viewport_func.h
Functions related to (drawing on) viewports.
TileInfo::x
uint x
X position of the tile in unit coordinates.
Definition: tile_cmd.h:45
Sprite::y_offs
int16 y_offs
Number of pixels to shift the sprite downwards.
Definition: spritecache.h:22
DrawTileSprites
Ground palette sprite of a tile, together with its sprite layout.
Definition: sprite.h:60
foreach_draw_tile_seq
#define foreach_draw_tile_seq(idx, list)
Iterate through all DrawTileSeqStructs in DrawTileSprites.
Definition: sprite.h:81
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
safeguards.h
A number of safeguards to prevent using unsafe methods.
TileInfo::y
uint y
Y position of the tile in unit coordinates.
Definition: tile_cmd.h:46
SPRITE_MODIFIER_CUSTOM_SPRITE
Set when a sprite originates from an Action 1.
Definition: sprites.h:1506
DrawTileSeqStruct::IsParentSprite
bool IsParentSprite() const
Check whether this is a parent sprite with a boundingbox.
Definition: sprite.h:49
SPRITE_WIDTH
number of bits for the sprite number
Definition: sprites.h:1494
spritecache.h
Functions to cache sprites in memory.
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition: viewport.cpp:688
SpriteID
uint32 SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition: gfx_type.h:19
PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition: sprites.h:1508
DrawTileSeqStruct::delta_z
int8 delta_z
0x80 identifies child sprites
Definition: sprite.h:30
SpriteLayoutPaletteTransform
static PaletteID SpriteLayoutPaletteTransform(SpriteID image, PaletteID pal, PaletteID default_pal)
Applies PALETTE_MODIFIER_TRANSPARENT and PALETTE_MODIFIER_COLOUR to a palette entry of a sprite layou...
Definition: sprite.h:151
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:34
zoom_func.h
Functions related to zooming.
Sprite::x_offs
int16 x_offs
Number of pixels to shift the sprite to the right.
Definition: spritecache.h:21
landscape.h
Functions related to OTTD&#39;s landscape.
sprite.h
Base for drawing complex sprites.
Point
Coordinates of a point in 2D.
Definition: geometry_type.hpp:28
DrawGroundSprite
void DrawGroundSprite(SpriteID image, PaletteID pal, const SubSprite *sub, int extra_offs_x, int extra_offs_y)
Draws a ground sprite for the current tile.
Definition: viewport.cpp:604
AddChildSpriteScreen
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale)
Add a child sprite to a parent sprite.
Definition: viewport.cpp:843
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:105
IsInvisibilitySet
static bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:61
DrawTileSeqStruct::delta_x
int8 delta_x
0x80 is sequence terminator
Definition: sprite.h:28
PalSpriteID::sprite
SpriteID sprite
The &#39;real&#39; sprite.
Definition: gfx_type.h:25
DrawTileSeqStruct
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition: sprite.h:27
SPRITE_MODIFIER_OPAQUE
Set when a sprite must not ever be displayed transparently.
Definition: sprites.h:1507
IsTransparencySet
static bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren&#39;t in the game menu (there&#39;s never transpar...
Definition: transparency.h:50
DrawSprite
void DrawSprite(SpriteID img, PaletteID pal, int x, int y, const SubSprite *sub, ZoomLevel zoom)
Draw a sprite, not in a viewport.
Definition: gfx.cpp:833
TransparencyOption
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
Definition: transparency.h:24
PalSpriteID::pal
PaletteID pal
The palette (use PAL_NONE) if not needed)
Definition: gfx_type.h:26