OpenTTD Source 20241224-master-gf74b0cf984
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 "animated_tile_func.h"
12#include "animated_tile_map.h"
13#include "tile_cmd.h"
14#include "viewport_func.h"
15#include "framerate_type.h"
16
17#include "safeguards.h"
18
20std::vector<TileIndex> _animated_tiles;
21
27void DeleteAnimatedTile(TileIndex tile, bool immediate)
28{
29 if (immediate) {
31
32 /* The tile may be switched to a non-animatable tile soon, so we should remove it from the
33 * animated tile list early. */
35
36 /* To avoid having to move everything after this tile in the animated tile list, look for this tile
37 * in the animated tile list and replace with last entry if not last. */
38 auto it = std::ranges::find(_animated_tiles, tile);
39 if (it == std::end(_animated_tiles)) return;
40
41 if (std::next(it) != std::end(_animated_tiles)) *it = _animated_tiles.back();
42 _animated_tiles.pop_back();
43
44 return;
45 }
46
47 /* If the tile was animated, mark it for deletion from the tile list on the next animation loop. */
49}
50
56void AddAnimatedTile(TileIndex tile, bool mark_dirty)
57{
58 if (mark_dirty) MarkTileDirtyByTile(tile);
59
60 const AnimatedTileState state = GetAnimatedTileState(tile);
61
62 /* Tile is already animated so nothing needs to happen. */
63 if (state == AnimatedTileState::Animated) return;
64
65 /* Tile has no previous animation state, so add to the tile list. If the state is anything
66 * other than None (e.g. Deleted) then the tile will still be in the list and does not need to be added again. */
67 if (state == AnimatedTileState::None) _animated_tiles.push_back(tile);
68
70}
71
76{
77 PerformanceAccumulator landscape_framerate(PFE_GL_LANDSCAPE);
78
79 for (auto it = std::begin(_animated_tiles); it != std::end(_animated_tiles); /* nothing */) {
80 TileIndex &tile = *it;
81
83 /* Tile should not be animated any more, mark it as not animated and erase it from the list. */
85
86 /* Removing the last entry, no need to swap and continue. */
87 if (std::next(it) == std::end(_animated_tiles)) {
88 _animated_tiles.pop_back();
89 break;
90 }
91
92 /* Replace the current list entry with the back of the list to avoid moving elements. */
93 *it = _animated_tiles.back();
94 _animated_tiles.pop_back();
95 continue;
96 }
97
98 AnimateTile(tile);
99 ++it;
100 }
101}
102
107{
108 _animated_tiles.clear();
109}
void AnimateAnimatedTiles()
Animate all tiles in the animated tile list, i.e. call AnimateTile on them.
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.
void DeleteAnimatedTile(TileIndex tile, bool immediate)
Stops animation on the given tile.
Tile animation!
Maps accessors for animated tiles.
AnimatedTileState
Animation state of a possibly-animated tile.
@ Animated
Tile is animated.
@ Deleted
Tile was animated but should be removed.
@ None
Tile is not animated.
void SetAnimatedTileState(Tile t, AnimatedTileState state)
Set the animated state of a tile.
AnimatedTileState GetAnimatedTileState(Tile t)
Get the animated state of a tile.
RAII class for measuring multi-step elements of performance.
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.
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.