OpenTTD Source: src/sprite.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
30 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)
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)) ||
45 (IsInvisibilitySet(to) && !HasBit(image, SPRITE_MODIFIER_OPAQUE))) {
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;
57 AddSortableSpriteToDraw(image, pal, *ti, dtss, !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to)
58 );
59 } else {
60 int offs_x = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.origin.x) : dtss.origin.x;
61 int offs_y = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.origin.y) : dtss.origin.y;
62 bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
63 if (parent_sprite_encountered) {
64 AddChildSpriteScreen(image, pal, offs_x, offs_y, transparent);
65 } else {
66 if (transparent) {
67 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
68 pal = PALETTE_TO_TRANSPARENT;
69 }
70 DrawGroundSprite(image, pal, nullptr, offs_x, offs_y);
71 }
72 }
73 }
74}
75
86 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)
87{
88 Point child_offset = {0, 0};
89
90 bool skip_childs = false;
91 for (const DrawTileSeqStruct &dtss : dts->GetSequence()) {
92 SpriteID image = dtss.image.sprite;
93 PaletteID pal = dtss.image.pal;
94
95 if (skip_childs) {
96 if (!dtss.IsParentSprite()) continue;
97 skip_childs = false;
98 }
99
100 /* TTD sprite 0 means no sprite */
101 if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
102 skip_childs = dtss.IsParentSprite();
103 continue;
104 }
105
106 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
107 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
108
109 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
110
111 if (dtss.IsParentSprite()) {
112 Point pt = RemapCoords(dtss.origin.x, dtss.origin.y, dtss.origin.z);
113 DrawSprite(image, pal, x + UnScaleGUI(pt.x), y + UnScaleGUI(pt.y));
114
115 const Sprite *spr = GetSprite(image & SPRITE_MASK, SpriteType::Normal);
116 child_offset.x = UnScaleGUI(pt.x + spr->x_offs);
117 child_offset.y = UnScaleGUI(pt.y + spr->y_offs);
118 } else {
119 int offs_x = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.origin.x) : dtss.origin.x;
120 int offs_y = child_offset_is_unsigned ? static_cast<uint8_t>(dtss.origin.y) : dtss.origin.y;
121 DrawSprite(image, pal, x + child_offset.x + ScaleSpriteTrad(offs_x), y + child_offset.y + ScaleSpriteTrad(offs_y));
122 }
123 }
124}
GB
static constexpr uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition bitmath_func.hpp:32
SetBit
constexpr T SetBit(T &x, const uint8_t y)
Set a bit in a variable.
Definition bitmath_func.hpp:121
HasBit
constexpr bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
Definition bitmath_func.hpp:103
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:1034
SpriteID
uint32_t SpriteID
The number of a sprite, without mapping bits and colourtables.
Definition gfx_type.h:17
SpriteType::Normal
@ Normal
The most basic (normal) sprite.
PaletteID
uint32_t PaletteID
The number of the palette.
Definition gfx_type.h:18
landscape.h
Functions related to OTTD's landscape.
RemapCoords
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:81
safeguards.h
A number of safeguards to prevent using unsafe methods.
DrawCommonTileSeq
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
DrawCommonTileSeqInGUI
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:86
sprite.h
Base for drawing complex sprites.
SpriteLayoutPaletteTransform
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
spritecache.h
Functions to cache sprites in memory.
SPRITE_MODIFIER_OPAQUE
static constexpr uint8_t SPRITE_MODIFIER_OPAQUE
Set when a sprite must not ever be displayed transparently.
Definition sprites.h:1560
SPRITE_MASK
static constexpr uint32_t SPRITE_MASK
The mask to for the main sprite.
Definition sprites.h:1570
PALETTE_MODIFIER_TRANSPARENT
static constexpr uint8_t PALETTE_MODIFIER_TRANSPARENT
when a sprite is to be displayed transparently, this bit needs to be set.
Definition sprites.h:1561
SPRITE_MODIFIER_CUSTOM_SPRITE
static constexpr uint8_t SPRITE_MODIFIER_CUSTOM_SPRITE
these masks change the colours of the palette for a sprite.
Definition sprites.h:1559
SPRITE_WIDTH
static constexpr uint8_t SPRITE_WIDTH
number of bits for the sprite number
Definition sprites.h:1549
PALETTE_TO_TRANSPARENT
static const PaletteID PALETTE_TO_TRANSPARENT
This sets the sprite to transparent.
Definition sprites.h:1616
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
Coord2D< int >
Coord2D::y
T y
Y coordinate.
Definition geometry_type.hpp:35
Coord2D::x
T x
X coordinate.
Definition geometry_type.hpp:34
DrawTileSeqStruct
A tile child sprite and palette to draw for stations etc, with 3D bounding box.
Definition sprite.h:33
DrawTileSprites
Ground palette sprite of a tile, together with its sprite layout.
Definition sprite.h:52
Sprite
Data structure describing a sprite.
Definition spritecache_type.h:16
Sprite::y_offs
int16_t y_offs
Number of pixels to shift the sprite downwards.
Definition spritecache_type.h:20
Sprite::x_offs
int16_t x_offs
Number of pixels to shift the sprite to the right.
Definition spritecache_type.h:19
TileInfo
Tile information, used while rendering the tile.
Definition tile_cmd.h:30
IsTransparencySet
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...
Definition transparency.h:50
IsInvisibilitySet
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...
Definition transparency.h:61
TransparencyOption
TransparencyOption
Transparency option bits: which position in _transparency_opt stands for which transparency.
Definition transparency.h:23
AddSortableSpriteToDraw
void AddSortableSpriteToDraw(SpriteID image, PaletteID pal, int x, int y, int z, const SpriteBounds &bounds, bool transparent, const SubSprite *sub)
Draw a (transparent) sprite at given coordinates with a given bounding box.
Definition viewport.cpp:663
AddChildSpriteScreen
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:824
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:579
viewport_func.h
Functions related to (drawing on) viewports.
zoom_func.h
Functions related to zooming.
ScaleSpriteTrad
int ScaleSpriteTrad(int value)
Scale traditional pixel dimensions to GUI zoom level, for drawing sprites.
Definition zoom_func.h:107
UnScaleGUI
int UnScaleGUI(int value)
Short-hand to apply GUI zoom level.
Definition zoom_func.h:77