Update: Translations from eints
[openttd-github.git] / src / bitmap_type.h
blob4dd356e48a6bb80422452226fd455c720b88569d
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 */
8 /** @file bitmap_type.hpp Bitmap functions. */
10 #ifndef BITMAP_TYPE_HPP
11 #define BITMAP_TYPE_HPP
14 /** Represents a tile area containing containing individually set tiles.
15 * Each tile must be contained within the preallocated area.
16 * A std::vector<bool> is used to mark which tiles are contained.
18 class BitmapTileArea : public TileArea {
19 protected:
20 std::vector<bool> data;
22 inline uint Index(uint x, uint y) const { return y * this->w + x; }
24 inline uint Index(TileIndex tile) const { return Index(TileX(tile) - TileX(this->tile), TileY(tile) - TileY(this->tile)); }
26 public:
27 BitmapTileArea()
29 this->tile = INVALID_TILE;
30 this->w = 0;
31 this->h = 0;
34 BitmapTileArea(const TileArea &ta)
36 this->tile = ta.tile;
37 this->w = ta.w;
38 this->h = ta.h;
39 this->data.resize(Index(this->w, this->h));
42 /**
43 * Reset and clear the BitmapTileArea.
45 void Reset()
47 this->tile = INVALID_TILE;
48 this->w = 0;
49 this->h = 0;
50 this->data.clear();
53 /**
54 * Initialize the BitmapTileArea with the specified Rect.
55 * @param rect Rect to use.
57 void Initialize(const Rect &r)
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));
66 void Initialize(const TileArea &ta)
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));
75 /**
76 * Add a tile as part of the tile area.
77 * @param tile Tile to add.
79 inline void SetTile(TileIndex tile)
81 assert(this->Contains(tile));
82 this->data[Index(tile)] = true;
85 /**
86 * Clear a tile from the tile area.
87 * @param tile Tile to clear
89 inline void ClrTile(TileIndex tile)
91 assert(this->Contains(tile));
92 this->data[Index(tile)] = false;
95 /**
96 * Test if a tile is part of the tile area.
97 * @param tile Tile to check
99 inline bool HasTile(TileIndex tile) const
101 return this->Contains(tile) && this->data[Index(tile)];
105 /** Iterator to iterate over all tiles belonging to a bitmaptilearea. */
106 class BitmapTileIterator : public OrthogonalTileIterator {
107 protected:
108 const BitmapTileArea *bitmap;
109 public:
111 * Construct the iterator.
112 * @param bitmap BitmapTileArea to iterate.
114 BitmapTileIterator(const BitmapTileArea &bitmap) : OrthogonalTileIterator(bitmap), bitmap(&bitmap)
116 if (!this->bitmap->HasTile(TileIndex(this->tile))) ++(*this);
119 inline TileIterator& operator ++() override
121 (*this).OrthogonalTileIterator::operator++();
122 while (this->tile != INVALID_TILE && !this->bitmap->HasTile(TileIndex(this->tile))) {
123 (*this).OrthogonalTileIterator::operator++();
125 return *this;
128 std::unique_ptr<TileIterator> Clone() const override
130 return std::make_unique<BitmapTileIterator>(*this);
134 #endif /* BITMAP_TYPE_HPP */