OpenTTD Source: src/bitmap_type.h Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#ifndef BITMAP_TYPE_HPP
11#define BITMAP_TYPE_HPP
12
13
18 class 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
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
106 class BitmapTileIterator : public OrthogonalTileIterator {
107protected:
108 const BitmapTileArea *bitmap;
109public:
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 */
BitmapTileArea
Represents a tile area containing containing individually set tiles.
Definition bitmap_type.h:18
BitmapTileArea::SetTile
void SetTile(TileIndex tile)
Add a tile as part of the tile area.
Definition bitmap_type.h:79
BitmapTileArea::ClrTile
void ClrTile(TileIndex tile)
Clear a tile from the tile area.
Definition bitmap_type.h:89
BitmapTileArea::HasTile
bool HasTile(TileIndex tile) const
Test if a tile is part of the tile area.
Definition bitmap_type.h:99
BitmapTileArea::Initialize
void Initialize(const Rect &r)
Initialize the BitmapTileArea with the specified Rect.
Definition bitmap_type.h:57
BitmapTileArea::Reset
void Reset()
Reset and clear the BitmapTileArea.
Definition bitmap_type.h:45
BitmapTileIterator
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition bitmap_type.h:106
BitmapTileIterator::operator++
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition bitmap_type.h:119
BitmapTileIterator::Clone
std::unique_ptr< TileIterator > Clone() const override
Allocate a new iterator that is a copy of this one.
Definition bitmap_type.h:128
BitmapTileIterator::BitmapTileIterator
BitmapTileIterator(const BitmapTileArea &bitmap)
Construct the iterator.
Definition bitmap_type.h:114
OrthogonalTileIterator
Iterator to iterate over a tile area (rectangle) of the map.
Definition tilearea_type.h:168
OrthogonalTileIterator::operator++
TileIterator & operator++() override
Move ourselves to the next tile in the rectangle on the map.
Definition tilearea_type.h:196
TileIterator
Base class for tile iterators.
Definition tilearea_type.h:105
TileXY
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition map_func.h:385
TileY
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition map_func.h:438
TileX
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition map_func.h:428
OrthogonalTileArea
Represents the covered area of e.g.
Definition tilearea_type.h:18
OrthogonalTileArea::Contains
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition tilearea.cpp:104
OrthogonalTileArea::w
uint16_t w
The width of the area.
Definition tilearea_type.h:20
OrthogonalTileArea::tile
TileIndex tile
The base tile of the area.
Definition tilearea_type.h:19
OrthogonalTileArea::h
uint16_t h
The height of the area.
Definition tilearea_type.h:21
Rect
Specification of a rectangle with absolute coordinates of all edges.
Definition geometry_type.hpp:101
Rect::Width
int Width() const
Get width of Rect.
Definition geometry_type.hpp:111
Rect::Height
int Height() const
Get height of Rect.
Definition geometry_type.hpp:117
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
INVALID_TILE
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95