OpenTTD Source 20261112-master-gbcbec5e63f
tilearea_bitmap.h
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef BITMAP_TYPE_HPP
11#define BITMAP_TYPE_HPP
12
13#include "tilearea_orthogonal.h"
14
20private:
21 std::vector<bool> data;
23
29 inline uint Index(TileIndex tile) const
30 {
31 const auto [x, y] = TileIndexToTileIndexDiffC(tile, this->ta.tile);
32 return y * this->ta.w + x;
33 }
34
35public:
39 BitmapTileArea() = default;
40
41 explicit BitmapTileArea(const OrthogonalTileArea &ta);
42
46 void Clear()
47 {
48 this->ta.Clear();
49 this->data.clear();
50 }
51
52 bool IsEmpty() const;
53 void Initialize(const OrthogonalTileArea &ta);
54
59 inline void Add(TileIndex tile)
60 {
61 assert(this->ta.Contains(tile));
62 this->data[this->Index(tile)] = true;
63 }
64
69 inline void Remove(TileIndex tile)
70 {
71 assert(this->ta.Contains(tile));
72 this->data[this->Index(tile)] = false;
73 }
74
80 inline bool Contains(TileIndex tile) const
81 {
82 return this->ta.Contains(tile) && this->data[this->Index(tile)];
83 }
84
86 class iterator {
87 private:
90
91 public:
93 using difference_type = std::ptrdiff_t;
94 using iterator_category = std::forward_iterator_tag;
95 using pointer = void;
96 using reference = void;
97
103 {
104 if (*this->iter != INVALID_TILE && !this->bitmap->Contains(*this->iter)) ++this->iter;
105 }
106
112 bool operator==(const iterator &other) const
113 {
114 return this->bitmap == other.bitmap && this->iter == other.iter;
115 }
116
121 bool operator==(const std::default_sentinel_t &) const
122 {
123 return **this == INVALID_TILE;
124 }
125
131 {
132 return *this->iter;
133 }
134
140 {
141 do {
142 ++this->iter;
143 } while (*this->iter != INVALID_TILE && !this->bitmap->Contains(*this->iter));
144 return *this;
145 }
146 };
147
152 inline iterator begin() const
153 {
154 return iterator{*this};
155 }
156
161 inline std::default_sentinel_t end() const
162 {
163 return std::default_sentinel_t();
164 }
165};
166
167#endif /* BITMAP_TYPE_HPP */
Iterator to iterate over all tiles belonging to a BitmapTileArea.
TileIndex value_type
value_type iterator trait
iterator(const BitmapTileArea &bitmap)
Construct the iterator.
bool operator==(const std::default_sentinel_t &) const
Compare with end sentinel.
TileIndex operator*() const
Get the current tile.
void reference
reference iterator trait
OrthogonalTileArea::iterator iter
The underlying tile area iterator.
bool operator==(const iterator &other) const
Compare with other iterator.
iterator & operator++()
Increment the iterator and set it to the next position.
void pointer
pointer iterator trait
const BitmapTileArea * bitmap
The bitmap tile area being iterated.
std::ptrdiff_t difference_type
difference_type iterator trait
std::forward_iterator_tag iterator_category
iterator_category iterator trait
void Remove(TileIndex tile)
Remove a tile from the tile area.
BitmapTileArea()=default
Construct an empty bitmap tile area.
std::default_sentinel_t end() const
Returns an iterator to the end of the tile area.
OrthogonalTileArea ta
Underlying tile area.
std::vector< bool > data
Storage of tiles contained within the tile area.
void Add(TileIndex tile)
Add a tile as part of the tile area.
iterator begin() const
Returns an iterator to the start of the tile area.
bool IsEmpty() const
Test if this tile area is empty.
uint Index(TileIndex tile) const
Get the internal index for the given tile.
void Clear()
Reset and clear the BitmapTileArea.
void Initialize(const OrthogonalTileArea &ta)
Initialize the BitmapTileArea with the specified OrthogonalTileArea.
bool Contains(TileIndex tile) const
Test if a tile is part of the tile area.
Iterator to iterate over a tile area (rectangle) of the map.
Represents the covered area of e.g.
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
uint16_t w
The width of the area.
void Clear()
Clears the 'tile area', i.e.
TileIndex tile
The base tile of the area.
TileIndexDiffC TileIndexToTileIndexDiffC(TileIndex tile_a, TileIndex tile_b)
Returns the diff between two tiles.
Definition map_func.h:535
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
Definition tile_type.h:92
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:100
Type for storing and iterating orthogonal tile areas.