1
/* $Id: animated_tile.cpp 15299 2009-01-31 20:16:06Z smatz $ */
3
/** @file animated_tile.cpp Everything related to animated tiles. */
6
#include "core/alloc_func.hpp"
9
/** The table/list with animated tiles. */
10
TileIndex *_animated_tile_list = NULL;
11
/** The number of animated tiles in the current state. */
12
uint _animated_tile_count = 0;
13
/** The number of slots for animated tiles allocated currently. */
14
uint _animated_tile_allocated = 0;
17
* Removes the given tile from the animated tile table.
18
* @param tile the tile to remove
20
void DeleteAnimatedTile(TileIndex tile)
22
for (TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) {
25
* The order of the remaining elements must stay the same, otherwise the animation loop
26
* may miss a tile; that's why we must use memmove instead of just moving the last element.
28
memmove(ti, ti + 1, (_animated_tile_list + _animated_tile_count - (ti + 1)) * sizeof(*ti));
29
_animated_tile_count--;
30
MarkTileDirtyByTile(tile);
37
* Add the given tile to the animated tile table (if it does not exist
38
* on that table yet). Also increases the size of the table if necessary.
39
* @param tile the tile to make animated
41
void AddAnimatedTile(TileIndex tile)
43
MarkTileDirtyByTile(tile);
45
for (const TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) {
46
if (tile == *ti) return;
49
/* Table not large enough, so make it larger */
50
if (_animated_tile_count == _animated_tile_allocated) {
51
_animated_tile_allocated *= 2;
52
_animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, _animated_tile_allocated);
55
_animated_tile_list[_animated_tile_count] = tile;
56
_animated_tile_count++;
60
* Animate all tiles in the animated tile list, i.e.\ call AnimateTile on them.
62
void AnimateAnimatedTiles()
64
const TileIndex *ti = _animated_tile_list;
65
while (ti < _animated_tile_list + _animated_tile_count) {
66
const TileIndex curr = *ti;
68
/* During the AnimateTile call, DeleteAnimatedTile could have been called,
69
* deleting an element we've already processed and pushing the rest one
70
* slot to the left. We can detect this by checking whether the index
71
* in the current slot has changed - if it has, an element has been deleted,
72
* and we should process the current slot again instead of going forward.
73
* NOTE: this will still break if more than one animated tile is being
74
* deleted during the same AnimateTile call, but no code seems to
75
* be doing this anyway.
77
if (*ti == curr) ++ti;
82
* Initialize all animated tile variables to some known begin point
84
void InitializeAnimatedTiles()
86
_animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, 256);
87
_animated_tile_count = 0;
88
_animated_tile_allocated = 256;