OpenTTD Source: src/bitmap_type.h Source File

OpenTTD Source 13.2.1

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 #include <vector>
14
19 class BitmapTileArea : public TileArea {
20 protected:
21 std::vector<bool> data;
22
23 inline uint Index(uint x, uint y) const { return y * this->w + x; }
24
25 inline uint Index(TileIndex tile) const { return Index(TileX(tile) - TileX(this->tile), TileY(tile) - TileY(this->tile)); }
26
27 public:
28 BitmapTileArea()
29 {
30 this->tile = INVALID_TILE;
31 this->w = 0;
32 this->h = 0;
33 }
34
35 BitmapTileArea(const TileArea &ta)
36 {
37 this->tile = ta.tile;
38 this->w = ta.w;
39 this->h = ta.h;
40 this->data.resize(Index(this->w, this->h));
41 }
42
46 void Reset()
47 {
48 this->tile = INVALID_TILE;
49 this->w = 0;
50 this->h = 0;
51 this->data.clear();
52 }
53
58 void Initialize(const Rect &r)
59 {
60 this->tile = TileXY(r.left, r.top);
61 this->w = r.Width();
62 this->h = r.Height();
63 this->data.clear();
64 this->data.resize(Index(w, h));
65 }
66
67 void Initialize(const TileArea &ta)
68 {
69 this->tile = ta.tile;
70 this->w = ta.w;
71 this->h = ta.h;
72 this->data.clear();
73 this->data.resize(Index(w, h));
74 }
75
80 inline void SetTile(TileIndex tile)
81 {
82 assert(this->Contains(tile));
83 this->data[Index(tile)] = true;
84 }
85
90 inline void ClrTile(TileIndex tile)
91 {
92 assert(this->Contains(tile));
93 this->data[Index(tile)] = false;
94 }
95
100 inline bool HasTile(TileIndex tile) const
101 {
102 return this->Contains(tile) && this->data[Index(tile)];
103 }
104 };
105
107 class BitmapTileIterator : public OrthogonalTileIterator {
108 protected:
109 const BitmapTileArea *bitmap;
110 public:
115 BitmapTileIterator(const BitmapTileArea &bitmap) : OrthogonalTileIterator(bitmap), bitmap(&bitmap)
116 {
117 if (!this->bitmap->HasTile(TileIndex(this->tile))) ++(*this);
118 }
119
120 inline TileIterator& operator ++()
121 {
122 (*this).OrthogonalTileIterator::operator++();
123 while (this->tile != INVALID_TILE && !this->bitmap->HasTile(TileIndex(this->tile))) {
124 (*this).OrthogonalTileIterator::operator++();
125 }
126 return *this;
127 }
128
129 virtual TileIterator *Clone() const
130 {
131 return new BitmapTileIterator(*this);
132 }
133 };
134
135 #endif /* BITMAP_TYPE_HPP */
Rect::Height
int Height() const
Get height of Rect.
Definition: geometry_type.hpp:85
BitmapTileArea::Initialize
void Initialize(const Rect &r)
Initialize the BitmapTileArea with the specified Rect.
Definition: bitmap_type.h:58
BitmapTileIterator::operator++
TileIterator & operator++()
Move ourselves to the next tile in the rectangle on the map.
Definition: bitmap_type.h:120
BitmapTileArea::SetTile
void SetTile(TileIndex tile)
Add a tile as part of the tile area.
Definition: bitmap_type.h:80
BitmapTileIterator
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:107
INVALID_TILE
static constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:108
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:215
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:205
OrthogonalTileIterator
Iterator to iterate over a tile area (rectangle) of the map.
Definition: tilearea_type.h:183
BitmapTileArea
Represents a tile area containing containing individually set tiles.
Definition: bitmap_type.h:19
TileIterator
Base class for tile iterators.
Definition: tilearea_type.h:105
BitmapTileArea::Reset
void Reset()
Reset and clear the BitmapTileArea.
Definition: bitmap_type.h:46
OrthogonalTileArea::w
uint16 w
The width of the area.
Definition: tilearea_type.h:20
OrthogonalTileArea
Represents the covered area of e.g.
Definition: tilearea_type.h:18
BitmapTileArea::HasTile
bool HasTile(TileIndex tile) const
Test if a tile is part of the tile area.
Definition: bitmap_type.h:100
BitmapTileIterator::Clone
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: bitmap_type.h:129
BitmapTileArea::ClrTile
void ClrTile(TileIndex tile)
Clear a tile from the tile area.
Definition: bitmap_type.h:90
OrthogonalTileArea::h
uint16 h
The height of the area.
Definition: tilearea_type.h:21
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:163
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
BitmapTileIterator::BitmapTileIterator
BitmapTileIterator(const BitmapTileArea &bitmap)
Construct the iterator.
Definition: bitmap_type.h:115
Rect::Width
int Width() const
Get width of Rect.
Definition: geometry_type.hpp:79
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition: geometry_type.hpp:69
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:104