OpenTTD

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

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 }