2010-01-16 22:25:44 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file sprite.cpp Handling of sprites */
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "sprite.h"
|
|
|
|
#include "viewport_func.h"
|
2010-01-16 22:44:03 +00:00
|
|
|
#include "landscape.h"
|
|
|
|
#include "spritecache.h"
|
2011-11-24 12:38:48 +00:00
|
|
|
#include "zoom_func.h"
|
2010-01-16 22:25:44 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "safeguards.h"
|
|
|
|
|
2010-01-16 22:25:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a tile sprite sequence.
|
|
|
|
* @param ti The tile to draw on
|
|
|
|
* @param dts Sprite and subsprites to draw
|
2013-01-08 22:46:42 +00:00
|
|
|
* @param to The transparency bit that toggles drawing of these sprites
|
2010-01-16 22:25:44 +00:00
|
|
|
* @param orig_offset Sprite-Offset for original sprites
|
|
|
|
* @param newgrf_offset Sprite-Offset for NewGRF defined sprites
|
|
|
|
* @param default_palette The default recolour sprite to use (typically company colour)
|
2010-01-17 15:05:25 +00:00
|
|
|
* @param child_offset_is_unsigned Whether child sprite offsets are interpreted signed or unsigned
|
2010-01-16 22:25:44 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
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)
|
2010-01-16 22:25:44 +00:00
|
|
|
{
|
2010-01-30 10:08:39 +00:00
|
|
|
bool parent_sprite_encountered = false;
|
2010-01-16 22:25:44 +00:00
|
|
|
const DrawTileSeqStruct *dtss;
|
2011-05-29 16:56:22 +00:00
|
|
|
bool skip_childs = false;
|
2010-01-16 22:25:44 +00:00
|
|
|
foreach_draw_tile_seq(dtss, dts->seq) {
|
|
|
|
SpriteID image = dtss->image.sprite;
|
2011-05-29 16:56:22 +00:00
|
|
|
PaletteID pal = dtss->image.pal;
|
|
|
|
|
|
|
|
if (skip_childs) {
|
|
|
|
if (!dtss->IsParentSprite()) continue;
|
|
|
|
skip_childs = false;
|
|
|
|
}
|
2010-01-16 22:25:44 +00:00
|
|
|
|
2010-01-17 15:05:25 +00:00
|
|
|
/* TTD sprite 0 means no sprite */
|
2011-08-06 11:14:07 +00:00
|
|
|
if ((GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) ||
|
|
|
|
(IsInvisibilitySet(to) && !HasBit(image, SPRITE_MODIFIER_OPAQUE))) {
|
2011-05-29 16:56:22 +00:00
|
|
|
skip_childs = dtss->IsParentSprite();
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-17 15:05:25 +00:00
|
|
|
|
2010-01-17 01:22:43 +00:00
|
|
|
image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
|
2011-05-29 16:56:22 +00:00
|
|
|
if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
|
2010-01-16 22:25:44 +00:00
|
|
|
|
2011-05-29 16:56:22 +00:00
|
|
|
pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
|
2010-01-16 22:25:44 +00:00
|
|
|
|
2011-02-19 13:16:34 +00:00
|
|
|
if (dtss->IsParentSprite()) {
|
2010-01-30 10:08:39 +00:00
|
|
|
parent_sprite_encountered = true;
|
2010-01-16 22:25:44 +00:00
|
|
|
AddSortableSpriteToDraw(
|
|
|
|
image, pal,
|
|
|
|
ti->x + dtss->delta_x, ti->y + dtss->delta_y,
|
|
|
|
dtss->size_x, dtss->size_y,
|
|
|
|
dtss->size_z, ti->z + dtss->delta_z,
|
|
|
|
!HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to)
|
|
|
|
);
|
|
|
|
} else {
|
2024-01-07 16:41:53 +00:00
|
|
|
int offs_x = child_offset_is_unsigned ? (uint8_t)dtss->delta_x : dtss->delta_x;
|
|
|
|
int offs_y = child_offset_is_unsigned ? (uint8_t)dtss->delta_y : dtss->delta_y;
|
2010-01-30 10:08:39 +00:00
|
|
|
bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
|
|
|
|
if (parent_sprite_encountered) {
|
|
|
|
AddChildSpriteScreen(image, pal, offs_x, offs_y, transparent);
|
|
|
|
} else {
|
|
|
|
if (transparent) {
|
|
|
|
SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
|
|
|
|
pal = PALETTE_TO_TRANSPARENT;
|
|
|
|
}
|
2019-04-10 21:07:06 +00:00
|
|
|
DrawGroundSprite(image, pal, nullptr, offs_x, offs_y);
|
2010-01-30 10:08:39 +00:00
|
|
|
}
|
2010-01-16 22:25:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-16 22:44:03 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws a tile sprite sequence in the GUI
|
|
|
|
* @param x X position to draw to
|
|
|
|
* @param y Y position to draw to
|
|
|
|
* @param dts Sprite and subsprites to draw
|
|
|
|
* @param orig_offset Sprite-Offset for original sprites
|
|
|
|
* @param newgrf_offset Sprite-Offset for NewGRF defined sprites
|
|
|
|
* @param default_palette The default recolour sprite to use (typically company colour)
|
2010-01-17 15:05:25 +00:00
|
|
|
* @param child_offset_is_unsigned Whether child sprite offsets are interpreted signed or unsigned
|
2010-01-16 22:44:03 +00:00
|
|
|
*/
|
2024-01-07 16:41:53 +00:00
|
|
|
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)
|
2010-01-16 22:44:03 +00:00
|
|
|
{
|
|
|
|
const DrawTileSeqStruct *dtss;
|
|
|
|
Point child_offset = {0, 0};
|
|
|
|
|
2011-05-29 16:56:22 +00:00
|
|
|
bool skip_childs = false;
|
2010-01-16 22:44:03 +00:00
|
|
|
foreach_draw_tile_seq(dtss, dts->seq) {
|
|
|
|
SpriteID image = dtss->image.sprite;
|
2011-05-29 16:56:22 +00:00
|
|
|
PaletteID pal = dtss->image.pal;
|
|
|
|
|
|
|
|
if (skip_childs) {
|
|
|
|
if (!dtss->IsParentSprite()) continue;
|
|
|
|
skip_childs = false;
|
|
|
|
}
|
2010-01-17 15:05:25 +00:00
|
|
|
|
|
|
|
/* TTD sprite 0 means no sprite */
|
2011-05-29 16:56:22 +00:00
|
|
|
if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
|
|
|
|
skip_childs = dtss->IsParentSprite();
|
|
|
|
continue;
|
|
|
|
}
|
2010-01-17 15:05:25 +00:00
|
|
|
|
2010-01-17 01:22:43 +00:00
|
|
|
image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
|
2011-05-29 16:56:22 +00:00
|
|
|
if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
|
2010-01-16 22:44:03 +00:00
|
|
|
|
2011-05-29 16:56:22 +00:00
|
|
|
pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
|
2010-01-16 22:44:03 +00:00
|
|
|
|
2011-02-19 13:16:34 +00:00
|
|
|
if (dtss->IsParentSprite()) {
|
2010-01-16 22:44:03 +00:00
|
|
|
Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
|
2015-02-01 20:54:24 +00:00
|
|
|
DrawSprite(image, pal, x + UnScaleGUI(pt.x), y + UnScaleGUI(pt.y));
|
2010-01-16 22:44:03 +00:00
|
|
|
|
2023-08-22 21:16:04 +00:00
|
|
|
const Sprite *spr = GetSprite(image & SPRITE_MASK, SpriteType::Normal, ZoomMask(_cur_dpi->zoom));
|
2015-02-01 20:54:24 +00:00
|
|
|
child_offset.x = UnScaleGUI(pt.x + spr->x_offs);
|
|
|
|
child_offset.y = UnScaleGUI(pt.y + spr->y_offs);
|
2010-01-16 22:44:03 +00:00
|
|
|
} else {
|
2024-01-07 16:41:53 +00:00
|
|
|
int offs_x = child_offset_is_unsigned ? (uint8_t)dtss->delta_x : dtss->delta_x;
|
|
|
|
int offs_y = child_offset_is_unsigned ? (uint8_t)dtss->delta_y : dtss->delta_y;
|
2022-09-05 20:05:18 +00:00
|
|
|
DrawSprite(image, pal, x + child_offset.x + ScaleSpriteTrad(offs_x), y + child_offset.y + ScaleSpriteTrad(offs_y));
|
2010-01-16 22:44:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|