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/>.
8 /** @file bitmap_type.hpp Bitmap functions. */
10 #ifndef BITMAP_TYPE_HPP
11 #define BITMAP_TYPE_HPP
15 /** Represents a tile area containing containing individually set tiles.
16 * Each tile must be contained within the preallocated area.
17 * A std::vector<bool> is used to mark which tiles are contained.
19 class BitmapTileArea
: public TileArea
{
21 std::vector
<bool> data
;
23 inline uint
Index(uint x
, uint y
) const { return y
* this->w
+ x
; }
25 inline uint
Index(TileIndex tile
) const { return Index(TileX(tile
) - TileX(this->tile
), TileY(tile
) - TileY(this->tile
)); }
30 this->tile
= INVALID_TILE
;
35 BitmapTileArea(const TileArea
&ta
)
40 this->data
.resize(Index(this->w
, this->h
));
44 * Reset and clear the BitmapTileArea.
48 this->tile
= INVALID_TILE
;
55 * Initialize the BitmapTileArea with the specified Rect.
56 * @param rect Rect to use.
58 void Initialize(const Rect
&r
)
60 this->tile
= TileXY(r
.left
, r
.top
);
61 this->w
= r
.right
- r
.left
+ 1;
62 this->h
= r
.bottom
- r
.top
+ 1;
64 this->data
.resize(Index(w
, h
));
67 void Initialize(const TileArea
&ta
)
73 this->data
.resize(Index(w
, h
));
77 * Add a tile as part of the tile area.
78 * @param tile Tile to add.
80 inline void SetTile(TileIndex tile
)
82 assert(this->Contains(tile
));
83 this->data
[Index(tile
)] = true;
87 * Clear a tile from the tile area.
88 * @param tile Tile to clear
90 inline void ClrTile(TileIndex tile
)
92 assert(this->Contains(tile
));
93 this->data
[Index(tile
)] = false;
97 * Test if a tile is part of the tile area.
98 * @param tile Tile to check
100 inline bool HasTile(TileIndex tile
) const
102 return this->Contains(tile
) && this->data
[Index(tile
)];
106 /** Iterator to iterate over all tiles belonging to a bitmaptilearea. */
107 class BitmapTileIterator
: public OrthogonalTileIterator
{
109 const BitmapTileArea
*bitmap
;
112 * Construct the iterator.
113 * @param bitmap BitmapTileArea to iterate.
115 BitmapTileIterator(const BitmapTileArea
&bitmap
) : OrthogonalTileIterator(bitmap
), bitmap(&bitmap
)
117 if (!this->bitmap
->HasTile(TileIndex(this->tile
))) ++(*this);
120 inline TileIterator
& operator ++()
122 (*this).OrthogonalTileIterator::operator++();
123 while (this->tile
!= INVALID_TILE
&& !this->bitmap
->HasTile(TileIndex(this->tile
))) {
124 (*this).OrthogonalTileIterator::operator++();
129 virtual TileIterator
*Clone() const
131 return new BitmapTileIterator(*this);
135 #endif /* BITMAP_TYPE_HPP */