OpenTTD Source  20241120-master-g6d3adc6169
bitmap_type.h
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 #ifndef BITMAP_TYPE_HPP
11 #define BITMAP_TYPE_HPP
12 
13 
18 class BitmapTileArea : public TileArea {
19 protected:
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 
26 public:
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 
57  void Initialize(const Rect &r)
58  {
59  this->tile = TileXY(r.left, r.top);
60  this->w = r.Width();
61  this->h = r.Height();
62  this->data.clear();
63  this->data.resize(Index(w, h));
64  }
65 
66  void Initialize(const TileArea &ta)
67  {
68  this->tile = ta.tile;
69  this->w = ta.w;
70  this->h = ta.h;
71  this->data.clear();
72  this->data.resize(Index(w, h));
73  }
74 
79  inline void SetTile(TileIndex tile)
80  {
81  assert(this->Contains(tile));
82  this->data[Index(tile)] = true;
83  }
84 
89  inline void ClrTile(TileIndex tile)
90  {
91  assert(this->Contains(tile));
92  this->data[Index(tile)] = false;
93  }
94 
99  inline bool HasTile(TileIndex tile) const
100  {
101  return this->Contains(tile) && this->data[Index(tile)];
102  }
103 };
104 
107 protected:
108  const BitmapTileArea *bitmap;
109 public:
114  BitmapTileIterator(const BitmapTileArea &bitmap) : OrthogonalTileIterator(bitmap), bitmap(&bitmap)
115  {
116  if (!this->bitmap->HasTile(TileIndex(this->tile))) ++(*this);
117  }
118 
119  inline TileIterator& operator ++() override
120  {
121  (*this).OrthogonalTileIterator::operator++();
122  while (this->tile != INVALID_TILE && !this->bitmap->HasTile(TileIndex(this->tile))) {
123  (*this).OrthogonalTileIterator::operator++();
124  }
125  return *this;
126  }
127 
128  std::unique_ptr<TileIterator> Clone() const override
129  {
130  return std::make_unique<BitmapTileIterator>(*this);
131  }
132 };
133 
134 #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:79
void ClrTile(TileIndex tile)
Clear a tile from the tile area.
Definition: bitmap_type.h:89
bool HasTile(TileIndex tile) const
Test if a tile is part of the tile area.
Definition: bitmap_type.h:99
void Initialize(const Rect &r)
Initialize the BitmapTileArea with the specified Rect.
Definition: bitmap_type.h:57
void Reset()
Reset and clear the BitmapTileArea.
Definition: bitmap_type.h:45
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:106
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
Definition: bitmap_type.h:128
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition: bitmap_type.h:119
BitmapTileIterator(const BitmapTileArea &bitmap)
Construct the iterator.
Definition: bitmap_type.h:114
Iterator to iterate over a tile area (rectangle) of the map.
Base class for tile iterators.
static debug_inline TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:373
static debug_inline uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:425
static debug_inline uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:415
Represents the covered area of e.g.
Definition: tilearea_type.h:18
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:104
uint16_t w
The width of the area.
Definition: tilearea_type.h:20
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
uint16_t h
The height of the area.
Definition: tilearea_type.h:21
Specification of a rectangle with absolute coordinates of all edges.
int Width() const
Get width of Rect.
int Height() const
Get height of Rect.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:95