Translations update
[openttd/fttd.git] / src / sprite.cpp
blob54586cd9c0e07436843468bce581f5cccb782d85
1 /* $Id$ */
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 */
10 /** @file sprite.cpp Handling of sprites */
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"
20 /**
21 * Draws a tile sprite sequence.
22 * @param ti The tile to draw on
23 * @param seq Sprite and subsprites to draw
24 * @param to The transparency bit that toggles drawing of these sprites
25 * @param orig_offset Sprite-Offset for original sprites
26 * @param newgrf_offset Sprite-Offset for NewGRF defined sprites
27 * @param default_palette The default recolour sprite to use (typically company colour)
28 * @param child_offset_is_unsigned Whether child sprite offsets are interpreted signed or unsigned
30 void DrawCommonTileSeq (const TileInfo *ti, const DrawTileSeqStruct *seq,
31 TransparencyOption to, int32 orig_offset, uint32 newgrf_offset,
32 PaletteID default_palette, bool child_offset_is_unsigned)
34 bool parent_sprite_encountered = false;
35 const DrawTileSeqStruct *dtss;
36 bool skip_childs = false;
37 foreach_draw_tile_seq(dtss, seq) {
38 SpriteID image = dtss->image.sprite;
39 PaletteID pal = dtss->image.pal;
41 if (skip_childs) {
42 if (!dtss->IsParentSprite()) continue;
43 skip_childs = false;
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;
53 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
54 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
56 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
58 bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
59 if (dtss->IsParentSprite()) {
60 parent_sprite_encountered = true;
61 AddSortableSpriteToDraw (ti->vd,
62 image, pal,
63 ti->x + dtss->delta_x, ti->y + dtss->delta_y,
64 dtss->size_x, dtss->size_y,
65 dtss->size_z, ti->z + dtss->delta_z,
66 transparent
68 } else {
69 int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
70 int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
71 if (parent_sprite_encountered) {
72 AddChildSpriteScreen (ti->vd, image, pal, offs_x, offs_y, transparent);
73 } else {
74 if (transparent) {
75 SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
76 pal = PALETTE_TO_TRANSPARENT;
78 DrawGroundSprite (ti, image, pal, NULL, offs_x, offs_y);
84 /**
85 * Draws a tile sprite sequence in the GUI
86 * @param dpi Area to draw on.
87 * @param x X position to draw to
88 * @param y Y position to draw to
89 * @param seq Sprite and subsprites to draw
90 * @param orig_offset Sprite-Offset for original sprites
91 * @param newgrf_offset Sprite-Offset for NewGRF defined sprites
92 * @param default_palette The default recolour sprite to use (typically company colour)
93 * @param child_offset_is_unsigned Whether child sprite offsets are interpreted signed or unsigned
95 void DrawCommonTileSeqInGUI (BlitArea *dpi, int x, int y,
96 const DrawTileSeqStruct *seq, int32 orig_offset, uint32 newgrf_offset,
97 PaletteID default_palette, bool child_offset_is_unsigned)
99 const DrawTileSeqStruct *dtss;
100 Point child_offset = {0, 0};
102 bool skip_childs = false;
103 foreach_draw_tile_seq(dtss, seq) {
104 SpriteID image = dtss->image.sprite;
105 PaletteID pal = dtss->image.pal;
107 if (skip_childs) {
108 if (!dtss->IsParentSprite()) continue;
109 skip_childs = false;
112 /* TTD sprite 0 means no sprite */
113 if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
114 skip_childs = dtss->IsParentSprite();
115 continue;
118 image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
119 if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
121 pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
123 if (dtss->IsParentSprite()) {
124 Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
125 DrawSprite (dpi, image, pal, x + UnScaleGUI(pt.x), y + UnScaleGUI(pt.y));
127 const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
128 child_offset.x = UnScaleGUI(pt.x + spr->x_offs);
129 child_offset.y = UnScaleGUI(pt.y + spr->y_offs);
130 } else {
131 int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
132 int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
133 DrawSprite (dpi, image, pal, x + child_offset.x + ScaleGUITrad(offs_x), y + child_offset.y + ScaleGUITrad(offs_y));