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/>.
10 /** @file animated_tile.cpp Everything related to animated tiles. */
13 #include "core/alloc_func.hpp"
15 #include "viewport_func.h"
17 #include "safeguards.h"
19 /** The table/list with animated tiles. */
20 TileIndex
*_animated_tile_list
= nullptr;
21 /** The number of animated tiles in the current state. */
22 uint _animated_tile_count
= 0;
23 /** The number of slots for animated tiles allocated currently. */
24 uint _animated_tile_allocated
= 0;
27 * Removes the given tile from the animated tile table.
28 * @param tile the tile to remove
30 void DeleteAnimatedTile(TileIndex tile
)
32 for (TileIndex
*ti
= _animated_tile_list
; ti
< _animated_tile_list
+ _animated_tile_count
; ti
++) {
35 * The order of the remaining elements must stay the same, otherwise the animation loop
36 * may miss a tile; that's why we must use memmove instead of just moving the last element.
38 memmove(ti
, ti
+ 1, (_animated_tile_list
+ _animated_tile_count
- (ti
+ 1)) * sizeof(*ti
));
39 _animated_tile_count
--;
40 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
47 * Add the given tile to the animated tile table (if it does not exist
48 * on that table yet). Also increases the size of the table if necessary.
49 * @param tile the tile to make animated
51 void AddAnimatedTile(TileIndex tile
)
53 MarkTileDirtyByTile(tile
, ZOOM_LVL_DRAW_MAP
);
55 for (const TileIndex
*ti
= _animated_tile_list
; ti
< _animated_tile_list
+ _animated_tile_count
; ti
++) {
56 if (tile
== *ti
) return;
59 /* Table not large enough, so make it larger */
60 if (_animated_tile_count
== _animated_tile_allocated
) {
61 _animated_tile_allocated
*= 2;
62 _animated_tile_list
= ReallocT
<TileIndex
>(_animated_tile_list
, _animated_tile_allocated
);
65 _animated_tile_list
[_animated_tile_count
] = tile
;
66 _animated_tile_count
++;
70 * Animate all tiles in the animated tile list, i.e.\ call AnimateTile on them.
72 void AnimateAnimatedTiles()
74 const TileIndex
*ti
= _animated_tile_list
;
75 while (ti
< _animated_tile_list
+ _animated_tile_count
) {
76 const TileIndex curr
= *ti
;
78 /* During the AnimateTile call, DeleteAnimatedTile could have been called,
79 * deleting an element we've already processed and pushing the rest one
80 * slot to the left. We can detect this by checking whether the index
81 * in the current slot has changed - if it has, an element has been deleted,
82 * and we should process the current slot again instead of going forward.
83 * NOTE: this will still break if more than one animated tile is being
84 * deleted during the same AnimateTile call, but no code seems to
85 * be doing this anyway.
87 if (*ti
== curr
) ++ti
;
92 * Initialize all animated tile variables to some known begin point
94 void InitializeAnimatedTiles()
96 _animated_tile_list
= ReallocT
<TileIndex
>(_animated_tile_list
, 256);
97 _animated_tile_count
= 0;
98 _animated_tile_allocated
= 256;