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 /** The table/list with animated tiles. */
18 TileIndex
*_animated_tile_list
= NULL
;
19 /** The number of animated tiles in the current state. */
20 uint _animated_tile_count
= 0;
21 /** The number of slots for animated tiles allocated currently. */
22 uint _animated_tile_allocated
= 0;
25 * Removes the given tile from the animated tile table.
26 * @param tile the tile to remove
28 void DeleteAnimatedTile(TileIndex tile
)
30 for (TileIndex
*ti
= _animated_tile_list
; ti
< _animated_tile_list
+ _animated_tile_count
; ti
++) {
33 * The order of the remaining elements must stay the same, otherwise the animation loop
34 * may miss a tile; that's why we must use memmove instead of just moving the last element.
36 memmove(ti
, ti
+ 1, (_animated_tile_list
+ _animated_tile_count
- (ti
+ 1)) * sizeof(*ti
));
37 _animated_tile_count
--;
38 MarkTileDirtyByTile(tile
);
45 * Add the given tile to the animated tile table (if it does not exist
46 * on that table yet). Also increases the size of the table if necessary.
47 * @param tile the tile to make animated
49 void AddAnimatedTile(TileIndex tile
)
51 MarkTileDirtyByTile(tile
);
53 for (const TileIndex
*ti
= _animated_tile_list
; ti
< _animated_tile_list
+ _animated_tile_count
; ti
++) {
54 if (tile
== *ti
) return;
57 /* Table not large enough, so make it larger */
58 if (_animated_tile_count
== _animated_tile_allocated
) {
59 _animated_tile_allocated
*= 2;
60 _animated_tile_list
= ReallocT
<TileIndex
>(_animated_tile_list
, _animated_tile_allocated
);
63 _animated_tile_list
[_animated_tile_count
] = tile
;
64 _animated_tile_count
++;
68 * Animate all tiles in the animated tile list, i.e.\ call AnimateTile on them.
70 void AnimateAnimatedTiles()
72 const TileIndex
*ti
= _animated_tile_list
;
73 while (ti
< _animated_tile_list
+ _animated_tile_count
) {
74 const TileIndex curr
= *ti
;
76 /* During the AnimateTile call, DeleteAnimatedTile could have been called,
77 * deleting an element we've already processed and pushing the rest one
78 * slot to the left. We can detect this by checking whether the index
79 * in the current slot has changed - if it has, an element has been deleted,
80 * and we should process the current slot again instead of going forward.
81 * NOTE: this will still break if more than one animated tile is being
82 * deleted during the same AnimateTile call, but no code seems to
83 * be doing this anyway.
85 if (*ti
== curr
) ++ti
;
90 * Initialize all animated tile variables to some known begin point
92 void InitializeAnimatedTiles()
94 _animated_tile_list
= ReallocT
<TileIndex
>(_animated_tile_list
, 256);
95 _animated_tile_count
= 0;
96 _animated_tile_allocated
= 256;