tilearea_type.h

Go to the documentation of this file.
00001 /* $Id: tilearea_type.h 23640 2011-12-20 17:57:56Z truebrain $ */
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   bool Contains(TileIndex tile) const;
00052 
00053   void ClampToMap();
00054 
00059   TileIndex GetCenterTile() const
00060   {
00061     return TILE_ADDXY(this->tile, this->w / 2, this->h / 2);
00062   }
00063 };
00064 
00066 class TileIterator {
00067 protected:
00068   TileIndex tile; 
00069 
00074   TileIterator(TileIndex tile) : tile(tile)
00075   {
00076   }
00077 
00078 public:
00080   virtual ~TileIterator()
00081   {
00082   }
00083 
00088   inline operator TileIndex () const
00089   {
00090     return this->tile;
00091   }
00092 
00096   virtual TileIterator& operator ++() = 0;
00097 
00101   virtual TileIterator *Clone() const = 0;
00102 };
00103 
00105 class OrthogonalTileIterator : public TileIterator {
00106 private:
00107   int w;          
00108   int x;          
00109   int y;          
00110 
00111 public:
00116   OrthogonalTileIterator(const TileArea &ta) : TileIterator(ta.w == 0 || ta.h == 0 ? INVALID_TILE : ta.tile), w(ta.w), x(ta.w), y(ta.h)
00117   {
00118   }
00119 
00123   inline TileIterator& operator ++()
00124   {
00125     assert(this->tile != INVALID_TILE);
00126 
00127     if (--this->x > 0) {
00128       this->tile++;
00129     } else if (--this->y > 0) {
00130       this->x = this->w;
00131       this->tile += TileDiffXY(1, 1) - this->w;
00132     } else {
00133       this->tile = INVALID_TILE;
00134     }
00135     return *this;
00136   }
00137 
00138   virtual TileIterator *Clone() const
00139   {
00140     return new OrthogonalTileIterator(*this);
00141   }
00142 };
00143 
00145 class DiagonalTileIterator : public TileIterator {
00146 private:
00147   uint base_x; 
00148   uint base_y; 
00149   int a_cur;   
00150   int b_cur;   
00151   int a_max;   
00152   int b_max;   
00153 
00154 public:
00155   DiagonalTileIterator(TileIndex begin, TileIndex end);
00156 
00157   TileIterator& operator ++();
00158 
00159   virtual TileIterator *Clone() const
00160   {
00161     return new DiagonalTileIterator(*this);
00162   }
00163 };
00164 
00171 #define TILE_AREA_LOOP(var, ta) for (OrthogonalTileIterator var(ta); var != INVALID_TILE; ++var)
00172 
00173 #endif /* TILEAREA_TYPE_H */