OpenTTD

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

- File List
- Globals

animated_tile.cpp
Go to the documentation of this file.
1 /* $Id: animated_tile.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
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 "core/alloc_func.hpp"
14 #include "tile_cmd.h"
15 #include "viewport_func.h"
16
17 #include "safeguards.h"
18
20 TileIndex *_animated_tile_list = NULL;
22 uint _animated_tile_count = 0;
24 uint _animated_tile_allocated = 0;
25
30 void DeleteAnimatedTile(TileIndex tile)
31 {
32 for (TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) {
33 if (tile == *ti) {
34 /* Remove the hole
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.
37 */
38 memmove(ti, ti + 1, (_animated_tile_list + _animated_tile_count - (ti + 1)) * sizeof(*ti));
39 _animated_tile_count--;
40 MarkTileDirtyByTile(tile);
41 return;
42 }
43 }
44 }
45
51 void AddAnimatedTile(TileIndex tile)
52 {
53 MarkTileDirtyByTile(tile);
54
55 for (const TileIndex *ti = _animated_tile_list; ti < _animated_tile_list + _animated_tile_count; ti++) {
56 if (tile == *ti) return;
57 }
58
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);
63 }
64
65 _animated_tile_list[_animated_tile_count] = tile;
66 _animated_tile_count++;
67 }
68
72 void AnimateAnimatedTiles()
73 {
74 const TileIndex *ti = _animated_tile_list;
75 while (ti < _animated_tile_list + _animated_tile_count) {
76 const TileIndex curr = *ti;
77 AnimateTile(curr);
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.
86 */
87 if (*ti == curr) ++ti;
88 }
89 }
90
94 void InitializeAnimatedTiles()
95 {
96 _animated_tile_list = ReallocT<TileIndex>(_animated_tile_list, 256);
97 _animated_tile_count = 0;
98 _animated_tile_allocated = 256;
99 }