summaryrefslogtreecommitdiff
path: root/src/tilearea_type.h
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2010-12-12 20:58:33 +0000
committerrubidium <rubidium@openttd.org>2010-12-12 20:58:33 +0000
commitcdd35c6a51f456ff1aaa309c88f65e9a1076eb39 (patch)
tree73e415133d775f6ca8c387f220bfc882101969b2 /src/tilearea_type.h
parente0351ab4140e433e8d3529ce9162d5075994dcd8 (diff)
downloadopenttd-cdd35c6a51f456ff1aaa309c88f65e9a1076eb39.tar.xz
(svn r21490) -Codechange: abstract/encapsulate the map area iterating a bit
Diffstat (limited to 'src/tilearea_type.h')
-rw-r--r--src/tilearea_type.h54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/tilearea_type.h b/src/tilearea_type.h
index 3207c9e3f..feaf919e8 100644
--- a/src/tilearea_type.h
+++ b/src/tilearea_type.h
@@ -60,4 +60,58 @@ struct TileArea {
}
};
+/** Iterator to iterate over a tile area (rectangle) of the map. */
+class TileIterator {
+private:
+ TileIndex tile; ///< The current tile we are at.
+ int w; ///< The width of the iterated area.
+ int x; ///< The current 'x' position in the rectangle.
+ int y; ///< The current 'y' position in the rectangle.
+
+public:
+ /**
+ * Construct the iterator.
+ * @param ta Area, i.e. begin point and width/height of to-be-iterated area.
+ */
+ TileIterator(const TileArea &ta) : tile(ta.tile), w(ta.w), x(ta.w), y(ta.h)
+ {
+ if (ta.w == 0 || ta.h == 0) this->tile = INVALID_TILE;
+ }
+
+ /**
+ * Get the tile we are currently at.
+ * @return The tile we are at, or INVALID_TILE when we're done.
+ */
+ FORCEINLINE operator TileIndex () const
+ {
+ return this->tile;
+ }
+
+ /**
+ * Move ourselves to the next tile in the rectange on the map.
+ */
+ FORCEINLINE TileIterator& operator ++()
+ {
+ assert(this->tile != INVALID_TILE);
+
+ if (--this->x > 0) {
+ this->tile++;
+ } else if (--this->y > 0) {
+ this->x = this->w;
+ this->tile += TileDiffXY(1, 1) - this->w;
+ } else {
+ this->tile = INVALID_TILE;
+ }
+ return *this;
+ }
+};
+
+/**
+ * A loop which iterates over the tiles of a TileArea.
+ * @param var The name of the variable which contains the current tile.
+ * This variable will be allocated in this \c for of this loop.
+ * @param ta The tile area to search over.
+ */
+#define TILE_AREA_LOOP(var, ta) for (TileIterator var(ta); var != INVALID_TILE; ++var)
+
#endif /* TILEAREA_TYPE_H */