OpenTTD Source: src/animated_tile.cpp Source File

OpenTTD Source 14.0-beta1

animated_tile.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #include "stdafx.h"
11 #include "core/container_func.hpp"
12 #include "tile_cmd.h"
13 #include "viewport_func.h"
14 #include "framerate_type.h"
15
16 #include "safeguards.h"
17
19 std::vector<TileIndex> _animated_tiles;
20
25 void DeleteAnimatedTile(TileIndex tile)
26 {
27 auto to_remove = std::find(_animated_tiles.begin(), _animated_tiles.end(), tile);
28 if (to_remove != _animated_tiles.end()) {
29 /* The order of the remaining elements must stay the same, otherwise the animation loop may miss a tile. */
30 _animated_tiles.erase(to_remove);
31 MarkTileDirtyByTile(tile);
32 }
33 }
34
40 void AddAnimatedTile(TileIndex tile)
41 {
42 MarkTileDirtyByTile(tile);
43 include(_animated_tiles, tile);
44 }
45
49 void AnimateAnimatedTiles()
50 {
51 PerformanceAccumulator framerate(PFE_GL_LANDSCAPE);
52
53 const TileIndex *ti = _animated_tiles.data();
54 while (ti < _animated_tiles.data() + _animated_tiles.size()) {
55 const TileIndex curr = *ti;
56 AnimateTile(curr);
57 /* During the AnimateTile call, DeleteAnimatedTile could have been called,
58 * deleting an element we've already processed and pushing the rest one
59 * slot to the left. We can detect this by checking whether the index
60 * in the current slot has changed - if it has, an element has been deleted,
61 * and we should process the current slot again instead of going forward.
62 * NOTE: this will still break if more than one animated tile is being
63 * deleted during the same AnimateTile call, but no code seems to
64 * be doing this anyway.
65 */
66 if (*ti == curr) ++ti;
67 }
68 }
69
73 void InitializeAnimatedTiles()
74 {
75 _animated_tiles.clear();
76 }
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
_animated_tiles
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
Definition: animated_tile.cpp:19
PFE_GL_LANDSCAPE
@ PFE_GL_LANDSCAPE
Time spent processing other world features.
Definition: framerate_type.h:55
tile_cmd.h
include
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Definition: container_func.hpp:24
safeguards.h
stdafx.h
viewport_func.h
PerformanceAccumulator
RAII class for measuring multi-step elements of performance.
Definition: framerate_type.h:114
DeleteAnimatedTile
void DeleteAnimatedTile(TileIndex tile)
Removes the given tile from the animated tile table.
Definition: animated_tile.cpp:25
framerate_type.h
MarkTileDirtyByTile
void MarkTileDirtyByTile(TileIndex tile, int bridge_level_offset, int tile_height_override)
Mark a tile given by its index dirty for repaint.
Definition: viewport.cpp:2051
container_func.hpp
AnimateAnimatedTiles
void AnimateAnimatedTiles()
Animate all tiles in the animated tile list, i.e. call AnimateTile on them.
Definition: animated_tile.cpp:49
InitializeAnimatedTiles
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
Definition: animated_tile.cpp:73
AddAnimatedTile
void AddAnimatedTile(TileIndex tile)
Add the given tile to the animated tile table (if it does not exist on that table yet).
Definition: animated_tile.cpp:40