Mercurial > hg > openttd
changeset 16761:89c7d67629e8 draft
(svn r21494) -Codechange: split the tile iterator in two classes
author | rubidium <rubidium@openttd.org> |
---|---|
date | Mon, 13 Dec 2010 11:25:26 +0000 |
parents | 9e31dde37aed |
children | c0ddbaaeb487 |
files | src/tilearea_type.h |
diffstat | 1 files changed, 31 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/tilearea_type.h +++ b/src/tilearea_type.h @@ -60,10 +60,37 @@ } }; -/** Iterator to iterate over a tile area (rectangle) of the map. */ +/** Base class for tile iterators. */ class TileIterator { +protected: + TileIndex tile; ///< The current tile we are at. + + /** + * Initialise the iterator starting at this tile. + * @param tile The tile we start iterating from. + */ + TileIterator(TileIndex tile) : tile(tile) + { + } +public: + /** + * 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. + */ + virtual TileIterator& operator ++() = 0; +}; + +/** Iterator to iterate over a tile area (rectangle) of the map. */ +class OrthogonalTileIterator : public 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. @@ -73,18 +100,8 @@ * 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) + OrthogonalTileIterator(const TileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_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; } /** @@ -112,6 +129,6 @@ * 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) +#define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var) #endif /* TILEAREA_TYPE_H */