OpenTTD
animated_tile.cpp
Go to the documentation of this file.
1 /* $Id$ */
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 "core/smallvec_type.hpp"
15 #include "tile_cmd.h"
16 #include "viewport_func.h"
17 #include "framerate_type.h"
18 
19 #include "safeguards.h"
20 
23 
29 {
30  TileIndex *to_remove = _animated_tiles.Find(tile);
31  if (to_remove != _animated_tiles.End()) {
32  /* The order of the remaining elements must stay the same, otherwise the animation loop may miss a tile. */
33  _animated_tiles.ErasePreservingOrder(to_remove);
34  MarkTileDirtyByTile(tile);
35  }
36 }
37 
44 {
45  MarkTileDirtyByTile(tile);
46  _animated_tiles.Include(tile);
47 }
48 
53 {
55 
56  const TileIndex *ti = _animated_tiles.Begin();
57  while (ti < _animated_tiles.End()) {
58  const TileIndex curr = *ti;
59  AnimateTile(curr);
60  /* During the AnimateTile call, DeleteAnimatedTile could have been called,
61  * deleting an element we've already processed and pushing the rest one
62  * slot to the left. We can detect this by checking whether the index
63  * in the current slot has changed - if it has, an element has been deleted,
64  * and we should process the current slot again instead of going forward.
65  * NOTE: this will still break if more than one animated tile is being
66  * deleted during the same AnimateTile call, but no code seems to
67  * be doing this anyway.
68  */
69  if (*ti == curr) ++ti;
70  }
71 }
72 
77 {
78  _animated_tiles.Clear();
79 }
void AddAnimatedTile(TileIndex tile)
Add the given tile to the animated tile table (if it does not exist on that table yet)...
Simple vector class that allows allocating an item without the need to copy this->data needlessly...
void ErasePreservingOrder(uint pos, uint count=1)
Remove items from the vector while preserving the order of other items.
Types for recording game performance data.
void Clear()
Remove all items from the list.
const T * Begin() const
Get the pointer to the first item (const)
Simple vector template class.
const T * End() const
Get the pointer behind the last valid item (const)
Functions related to (drawing on) viewports.
Functions related to the allocation of memory.
void DeleteAnimatedTile(TileIndex tile)
Removes the given tile from the animated tile table.
Definition of base types and functions in a cross-platform compatible way.
A number of safeguards to prevent using unsafe methods.
const T * Find(const T &item) const
Search for the first occurrence of an item.
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:1785
Time spent processing other world features.
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
void InitializeAnimatedTiles()
Initialize all animated tile variables to some known begin point.
bool Include(const T &item)
Tests whether a item is present in the vector, and appends it to the end if not.
RAII class for measuring multi-step elements of performance.
void AnimateAnimatedTiles()
Animate all tiles in the animated tile list, i.e. call AnimateTile on them.
Generic &#39;commands&#39; that can be performed on all tiles.
SmallVector< TileIndex, 256 > _animated_tiles
The table/list with animated tiles.