OpenTTD Source 20250218-master-g53dd1258a7
sprite.cpp
Go to the documentation of this file.
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
10#include "stdafx.h"
11#include "sprite.h"
12#include "viewport_func.h"
13#include "landscape.h"
14#include "spritecache.h"
15#include "zoom_func.h"
16
17#include "safeguards.h"
18
19
30void DrawCommonTileSeq(const TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32_t orig_offset, uint32_t newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
31{
32 bool parent_sprite_encountered = false;
33 bool skip_childs = false;
34 for (const DrawTileSeqStruct &dtss : dts->GetSequence()) {
35 SpriteID image = dtss.image.sprite;
36 PaletteID pal = dtss.image.pal;
37
38 if (skip_childs) {
39 if (!dtss.IsParentSprite()) continue;
40 skip_childs = false;
41 }
42
43 /* TTD sprite 0 means no sprite */
44 if ((GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) ||
46 skip_childs = dtss.IsParentSprite();
47 continue;
48 }
49
50 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
51 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
52
53 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
54
55 if (dtss.IsParentSprite()) {
56 parent_sprite_encountered = true;
58 image, pal,
59 ti->x + dtss.delta_x, ti->y + dtss.delta_y,
60 dtss.size_x, dtss.size_y,
61 dtss.size_z, ti->z + dtss.delta_z,
63 );
64 } else {
65 int offs_x = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.delta_x) : dtss.delta_x;
66 int offs_y = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.delta_y) : dtss.delta_y;
67 bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
68 if (parent_sprite_encountered) {
69 AddChildSpriteScreen(image, pal, offs_x, offs_y, transparent);
70 } else {
71 if (transparent) {
74 }
75 DrawGroundSprite(image, pal, nullptr, offs_x, offs_y);
76 }
77 }
78 }
79}
80
91void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32_t orig_offset, uint32_t newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
92{
93 Point child_offset = {0, 0};
94
95 bool skip_childs = false;
96 for (const DrawTileSeqStruct &dtss : dts->GetSequence()) {
97 SpriteID image = dtss.image.sprite;
98 PaletteID pal = dtss.image.pal;
99
100 if (skip_childs) {
101 if (!dtss.IsParentSprite()) continue;
102 skip_childs = false;
103 }
104
105 /* TTD sprite 0 means no sprite */
106 if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
107 skip_childs = dtss.IsParentSprite();
108 continue;
109 }
110
111 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
112 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
113
114 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
115
116 if (dtss.IsParentSprite()) {
117 Point pt = RemapCoords(dtss.delta_x, dtss.delta_y, dtss.delta_z);
118 DrawSprite(image, pal, x + UnScaleGUI(pt.x), y + UnScaleGUI(pt.y));
119
120 const Sprite *spr = GetSprite(image & SPRITE_MASK, SpriteType::Normal);
121 child_offset.x = UnScaleGUI(pt.x + spr->x_offs);
122 child_offset.y = UnScaleGUI(pt.y + spr->y_offs);
123 } else {
124 int offs_x = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.delta_x) : dtss.delta_x;
125 int offs_y = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.delta_y) : dtss.delta_y;
126 DrawSprite(image, pal, x + child_offset.x + ScaleSpriteTrad(offs_x), y + child_offset.y + ScaleSpriteTrad(offs_y));
127 }
128 }
129}
debug_inline constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
debug_inline static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
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:988
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
@ Normal
The most basic (normal) sprite.
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
Functions related to OTTD's landscape.
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:79
A number of safeguards to prevent using unsafe methods.
void DrawCommonTileSeq(const TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32_t orig_offset, uint32_t newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
Draws a tile sprite sequence.
Definition sprite.cpp:30
void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32_t orig_offset, uint32_t newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
Draws a tile sprite sequence in the GUI.
Definition sprite.cpp:91
Base for drawing complex sprites.
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:155
Functions to cache sprites in memory.
static constexpr uint8_t SPRITE_MODIFIER_OPAQUE
Set when a sprite must not ever be displayed transparently.
Definition sprites.h:1546
static constexpr uint32_t SPRITE_MASK
The mask to for the main sprite.
Definition sprites.h:1556
static constexpr uint8_t PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition sprites.h:1547
static constexpr uint8_t SPRITE_MODIFIER_CUSTOM_SPRITE
these masks change the colours of the palette for a sprite.
Definition sprites.h:1545
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1535
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1602
Definition of base types and functions in a cross-platform compatible way.
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition sprite.h:25
Ground palette sprite of a tile, together with its sprite layout.
Definition sprite.h:46
Coordinates of a point in 2D.
Data structure describing a sprite.
Definition spritecache.h:17
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache.h:21
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache.h:20
Tile information, used while rendering the tile.
Definition tile_cmd.h:43
int z
Height.
Definition tile_cmd.h:48
int x
X position of the tile in unit coordinates.
Definition tile_cmd.h:44
int y
Y position of the tile in unit coordinates.
Definition tile_cmd.h:45
bool IsTransparencySet(TransparencyOption to)
Check if the transparency option bit is set and if we aren't in the game menu (there's never transpar...
bool IsInvisibilitySet(TransparencyOption to)
Check if the invisibility option bit is set and if we aren't in the game menu (there's never transpar...
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
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:671
void AddChildSpriteScreen(SpriteID image, PaletteID pal, int x, int y, bool transparent, const SubSprite *sub, bool scale, bool relative)
Add a child sprite to a parent sprite.
Definition viewport.cpp:827
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:587
Functions related to (drawing on) viewports.
Functions related to zooming.
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77