OpenTTD
bitmap_type.h
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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  */
9 
12 #ifndef BITMAP_TYPE_HPP
13 #define BITMAP_TYPE_HPP
14 
15 #include <vector>
16 
21 class BitmapTileArea : public TileArea {
22 protected:
23  std::vector<bool> data;
24 
25  inline uint Index(uint x, uint y) const { return y * this->w + x; }
26 
27  inline uint Index(TileIndex tile) const { return Index(TileX(tile) - TileX(this->tile), TileY(tile) - TileY(this->tile)); }
28 
29 public:
31  {
32  this->tile = INVALID_TILE;
33  this->w = 0;
34  this->h = 0;
35  }
36 
37  BitmapTileArea(const TileArea &ta)
38  {
39  this->tile = ta.tile;
40  this->w = ta.w;
41  this->h = ta.h;
42  this->data.resize(Index(this->w, this->h));
43  }
44 
48  void Reset()
49  {
50  this->tile = INVALID_TILE;
51  this->w = 0;
52  this->h = 0;
53  this->data.clear();
54  }
55 
60  void Initialize(const Rect &r)
61  {
62  this->tile = TileXY(r.left, r.top);
63  this->w = r.right - r.left + 1;
64  this->h = r.bottom - r.top + 1;
65  this->data.clear();
66  this->data.resize(Index(w, h));
67  }
68 
69  void Initialize(const TileArea &ta)
70  {
71  this->tile = ta.tile;
72  this->w = ta.w;
73  this->h = ta.h;
74  this->data.clear();
75  this->data.resize(Index(w, h));
76  }
77 
82  inline void SetTile(TileIndex tile)
83  {
84  assert(this->Contains(tile));
85  this->data[Index(tile)] = true;
86  }
87 
92  inline void ClrTile(TileIndex tile)
93  {
94  assert(this->Contains(tile));
95  this->data[Index(tile)] = false;
96  }
97 
102  inline bool HasTile(TileIndex tile) const
103  {
104  return this->Contains(tile) && this->data[Index(tile)];
105  }
106 };
107 
110 protected:
111  const BitmapTileArea *bitmap;
112 public:
117  BitmapTileIterator(const BitmapTileArea &bitmap) : OrthogonalTileIterator(bitmap), bitmap(&bitmap)
118  {
119  if (!this->bitmap->HasTile(TileIndex(this->tile))) ++(*this);
120  }
121 
122  inline TileIterator& operator ++()
123  {
124  (*this).OrthogonalTileIterator::operator++();
125  while (this->tile != INVALID_TILE && !this->bitmap->HasTile(TileIndex(this->tile))) {
126  (*this).OrthogonalTileIterator::operator++();
127  }
128  return *this;
129  }
130 
131  virtual TileIterator *Clone() const
132  {
133  return new BitmapTileIterator(*this);
134  }
135 };
136 
137 #endif /* BITMAP_TYPE_HPP */
Iterator to iterate over all tiles belonging to a bitmaptilearea.
Definition: bitmap_type.h:109
bool Contains(TileIndex tile) const
Does this tile area contain a tile?
Definition: tilearea.cpp:106
virtual TileIterator * Clone() const
Allocate a new iterator that is a copy of this one.
Definition: bitmap_type.h:131
static uint TileX(TileIndex tile)
Get the X component of a tile.
Definition: map_func.h:207
void Initialize(const Rect &r)
Initialize the BitmapTileArea with the specified Rect.
Definition: bitmap_type.h:60
void Reset()
Reset and clear the BitmapTileArea.
Definition: bitmap_type.h:48
uint16 w
The width of the area.
Definition: tilearea_type.h:20
BitmapTileIterator(const BitmapTileArea &bitmap)
Construct the iterator.
Definition: bitmap_type.h:117
Represents the covered area of e.g.
Definition: tilearea_type.h:18
Represents a tile area containing containing individually set tiles.
Definition: bitmap_type.h:21
Base class for tile iterators.
bool HasTile(TileIndex tile) const
Test if a tile is part of the tile area.
Definition: bitmap_type.h:102
TileIndex tile
The base tile of the area.
Definition: tilearea_type.h:19
void ClrTile(TileIndex tile)
Clear a tile from the tile area.
Definition: bitmap_type.h:92
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
static uint TileY(TileIndex tile)
Get the Y component of a tile.
Definition: map_func.h:217
Iterator to iterate over a tile area (rectangle) of the map.
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:85
Specification of a rectangle with absolute coordinates of all edges.
uint16 h
The height of the area.
Definition: tilearea_type.h:21
static TileIndex TileXY(uint x, uint y)
Returns the TileIndex of a coordinate.
Definition: map_func.h:165
void SetTile(TileIndex tile)
Add a tile as part of the tile area.
Definition: bitmap_type.h:82