OpenTTD Source  20241108-master-g80f628063a
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 
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  }
32 }
33 
39 void AddAnimatedTile(TileIndex tile, bool mark_dirty)
40 {
41  if (mark_dirty) MarkTileDirtyByTile(tile);
42  include(_animated_tiles, tile);
43 }
44 
49 {
51 
52  const TileIndex *ti = _animated_tiles.data();
53  while (ti < _animated_tiles.data() + _animated_tiles.size()) {
54  const TileIndex curr = *ti;
55  AnimateTile(curr);
56  /* During the AnimateTile call, DeleteAnimatedTile could have been called,
57  * deleting an element we've already processed and pushing the rest one
58  * slot to the left. We can detect this by checking whether the index
59  * in the current slot has changed - if it has, an element has been deleted,
60  * and we should process the current slot again instead of going forward.
61  * NOTE: this will still break if more than one animated tile is being
62  * deleted during the same AnimateTile call, but no code seems to
63  * be doing this anyway.
64  */
65  if (*ti == curr) ++ti;
66  }
67 }
68 
73 {
74  _animated_tiles.clear();
75 }
void AnimateAnimatedTiles()
Animate all tiles in the animated tile list, i.e. call AnimateTile on them.
void DeleteAnimatedTile(TileIndex tile)
Removes the given tile from the animated tile table.
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
void AddAnimatedTile(TileIndex tile, bool mark_dirty)
Add the given tile to the animated tile table (if it does not exist yet).
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
RAII class for measuring multi-step elements of performance.
Some simple functions to help with accessing containers.
bool include(Container &container, typename Container::const_reference &item)
Helper function to append an item to a container if it is not already contained.
Types for recording game performance data.
@ PFE_GL_LANDSCAPE
Time spent processing other world features.
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:2057
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
Generic 'commands' that can be performed on all tiles.
Functions related to (drawing on) viewports.