OpenTTD Source 20260911-master-gee2b2ac12a
bitmap_type.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
18class BitmapTileArea : public TileArea {
19protected:
20 std::vector<bool> data;
21
22 inline uint Index(uint x, uint y) const { return y * this->w + x; }
23
24 inline uint Index(TileIndex tile) const { return Index(TileX(tile) - TileX(this->tile), TileY(tile) - TileY(this->tile)); }
25
26public:
27 BitmapTileArea()
28 {
29 this->tile = INVALID_TILE;
30 this->w = 0;
31 this->h = 0;
32 }
33
34 BitmapTileArea(const TileArea &ta)
35 {
36 this->tile = ta.tile;
37 this->w = ta.w;
38 this->h = ta.h;
39 this->data.resize(Index(this->w, this->h));
40 }
41
45 void Reset()
46 {
47 this->tile = INVALID_TILE;
48 this->w = 0;
49 this->h = 0;
50 this->data.clear();
51 }
52
53 void Initialize(const TileArea &ta)
54 {
55 this->tile = ta.tile;
56 this->w = ta.w;
57 this->h = ta.h;
58 this->data.clear();
59 this->data.resize(Index(w, h));
60 }
61
66 inline void SetTile(TileIndex tile)
67 {
68 assert(this->Contains(tile));
69 this->data[Index(tile)] = true;
70 }
71
76 inline void ClrTile(TileIndex tile)
77 {
78 assert(this->Contains(tile));
79 this->data[Index(tile)] = false;
80 }
81
87 inline bool HasTile(TileIndex tile) const
88 {
89 return this->Contains(tile) && this->data[Index(tile)];
90 }
91};
92
95protected:
96 const BitmapTileArea *bitmap;
97public:
102 BitmapTileIterator(const BitmapTileArea &bitmap) : OrthogonalTileIterator(bitmap), bitmap(&bitmap)
103 {
104 if (!this->bitmap->HasTile(TileIndex(this->tile))) ++(*this);
105 }
106
107 inline TileIterator& operator ++() override
108 {
110 while (this->tile != INVALID_TILE && !this->bitmap->HasTile(TileIndex(this->tile))) {
112 }
113 return *this;
114 }
115};
116
117#endif /* BITMAP_TYPE_HPP */
Represents a tile area containing containing individually set tiles.
Definition bitmap_type.h:18
void SetTile(TileIndex tile)
Add a tile as part of the tile area.
Definition bitmap_type.h:66
void ClrTile(TileIndex tile)
Clear a tile from the tile area.
Definition bitmap_type.h:76
bool HasTile(TileIndex tile) const
Test if a tile is part of the tile area.
Definition bitmap_type.h:87
void Reset()
Reset and clear the BitmapTileArea.
Definition bitmap_type.h:45
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
BitmapTileIterator(const BitmapTileArea &bitmap)
Construct the iterator.
OrthogonalTileIterator(const OrthogonalTileArea &ta)
Construct the iterator.
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
TileIterator(TileIndex tile=INVALID_TILE)
Initialise the iterator starting at this tile.
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:429
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:419
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition tilearea.cpp:104
uint16_t w
The width of the area.
TileIndex tile
The base tile of the area.
uint16_t h
The height of the area.
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
OrthogonalTileArea TileArea
Shorthand for the much more common orthogonal tile area.