# tilearea_type.h

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #ifndef TILEAREA_TYPE_H
    00013 #define TILEAREA_TYPE_H
    00014
    00015 #include "map_func.h"
    00016
    00018 struct TileArea {
    00019   TileIndex tile;
    00020   uint16 w;
    00021   uint16 h;
    00022
    00024   TileArea() {}
    00025
    00032   TileArea(TileIndex tile, uint8 w, uint8 h) : tile(tile), w(w), h(h) {}
    00033
    00034   TileArea(TileIndex start, TileIndex end);
    00035
    00036
    00037   void Add(TileIndex to_add);
    00038
    00042   void Clear()
    00043   {
    00044     this->tile = INVALID_TILE;
    00045     this->w    = 0;
    00046     this->h    = 0;
    00047   }
    00048
    00049   bool Intersects(const TileArea &ta) const;
    00050
    00051   void ClampToMap();
    00052
    00057   TileIndex GetCenterTile() const
    00058   {
    00059     return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
    00060   }
    00061 };
    00062
    00064 class TileIterator {
    00065 protected:
    00066   TileIndex tile;
    00067
    00072   TileIterator(TileIndex tile) : tile(tile)
    00073   {
    00074   }
    00075
    00076 public:
    00078   virtual ~TileIterator()
    00079   {
    00080   }
    00081
    00086   FORCEINLINE operator TileIndex () const
    00087   {
    00088     return this->tile;
    00089   }
    00090
    00094   virtual TileIterator& operator ++() = 0;
    00095 };
    00096
    00098 class OrthogonalTileIterator : public TileIterator {
    00099 private:
    00100   int w;
    00101   int x;
    00102   int y;
    00103
    00104 public:
    00109   OrthogonalTileIterator(const TileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
    00110   {
    00111   }
    00112
    00116   FORCEINLINE TileIterator& operator ++()
    00117   {
    00118     assert(this->tile != INVALID_TILE);
    00119
    00120     if (--this->x > 0) {
    00121       this->tile++;
    00122     } else if (--this->y > 0) {
    00123       this->x = this->w;
    00124       this->tile += TileDiffXY(1, 1) - this->w;
    00125     } else {
    00126       this->tile = INVALID_TILE;
    00127     }
    00128     return *this;
    00129   }
    00130 };
    00131
    00133 class DiagonalTileIterator : public TileIterator {
    00134 private:
    00135   uint base_x, base_y;
    00136   int a_cur, b_cur;
    00137   int a_max, b_max;
    00138
    00139 public:
    00140   DiagonalTileIterator(TileIndex begin, TileIndex end);
    00141
    00142   TileIterator& operator ++();
    00143 };
    00144
    00151 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
    00152
    00153 #endif /* TILEAREA_TYPE_H */