OpenTTD Source: src/animated_tile.cpp Source File

OpenTTD Source 15.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 "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
27 void DeleteAnimatedTile(TileIndex tile, bool immediate)
28{
29 if (immediate) {
30 if (GetAnimatedTileState(tile) == AnimatedTileState::None) return;
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. */
34 SetAnimatedTileState(tile, AnimatedTileState::None);
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. */
48 if (GetAnimatedTileState(tile) == AnimatedTileState::Animated) SetAnimatedTileState(tile, AnimatedTileState::Deleted);
49}
50
56 void 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
69 SetAnimatedTileState(tile, AnimatedTileState::Animated);
70}
71
75 void AnimateAnimatedTiles()
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
82 if (GetAnimatedTileState(tile) != AnimatedTileState::Animated) {
83 /* Tile should not be animated any more, mark it as not animated and erase it from the list. */
84 SetAnimatedTileState(tile, AnimatedTileState::None);
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
106 void InitializeAnimatedTiles()
107{
108 _animated_tiles.clear();
109}
AnimateAnimatedTiles
void AnimateAnimatedTiles()
Animate all tiles in the animated tile list, i.e. call AnimateTile on them.
Definition animated_tile.cpp:75
_animated_tiles
std::vector< TileIndex > _animated_tiles
The table/list with animated tiles.
Definition animated_tile.cpp:20
AddAnimatedTile
void AddAnimatedTile(TileIndex tile, bool mark_dirty)
Add the given tile to the animated tile table (if it does not exist yet).
Definition animated_tile.cpp:56
InitializeAnimatedTiles
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
Definition animated_tile.cpp:106
DeleteAnimatedTile
void DeleteAnimatedTile(TileIndex tile, bool immediate)
Stops animation on the given tile.
Definition animated_tile.cpp:27
animated_tile_func.h
Tile animation!
animated_tile_map.h
Maps accessors for animated tiles.
AnimatedTileState
AnimatedTileState
Animation state of a possibly-animated tile.
Definition animated_tile_map.h:19
AnimatedTileState::Animated
@ Animated
Tile is animated.
AnimatedTileState::Deleted
@ Deleted
Tile was animated but should be removed.
AnimatedTileState::None
@ None
Tile is not animated.
SetAnimatedTileState
void SetAnimatedTileState(Tile t, AnimatedTileState state)
Set the animated state of a tile.
Definition animated_tile_map.h:39
GetAnimatedTileState
AnimatedTileState GetAnimatedTileState(Tile t)
Get the animated state of a tile.
Definition animated_tile_map.h:30
PerformanceAccumulator
RAII class for measuring multi-step elements of performance.
Definition framerate_type.h:114
framerate_type.h
Types for recording game performance data.
PFE_GL_LANDSCAPE
@ PFE_GL_LANDSCAPE
Time spent processing other world features.
Definition framerate_type.h:55
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:2057
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
tile_cmd.h
Generic 'commands' that can be performed on all tiles.
viewport_func.h
Functions related to (drawing on) viewports.