PtRect.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 /* GG is a GUI for SDL and OpenGL.
00003    Copyright (C) 2003-2008 T. Zachary Laine
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public License
00007    as published by the Free Software Foundation; either version 2.1
00008    of the License, or (at your option) any later version.
00009    
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014     
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA
00019 
00020    If you do not wish to comply with the terms of the LGPL please
00021    contact the author as other terms are available for a fee.
00022     
00023    Zach Laine
00024    whatwasthataddress@gmail.com */
00025 
00028 #ifndef _GG_PtRect_h_
00029 #define _GG_PtRect_h_
00030 
00031 #include <GG/Base.h>
00032 #include <GG/StrongTypedef.h>
00033 
00034 #include <boost/serialization/access.hpp>
00035 
00036 
00037 namespace GG {
00038 
00043 GG_STRONG_INTEGRAL_TYPEDEF(X, int);
00044 
00049 GG_STRONG_INTEGRAL_TYPEDEF(Y, int);
00050 
00051 // some useful coordinate constants
00052 extern GG_API const X X0;
00053 extern GG_API const X X1;
00054 extern GG_API const Y Y0;
00055 extern GG_API const Y Y1;
00056 
00058 struct GG_API Pt
00059 { 
00061     Pt();               
00062     Pt(X x_, Y y_);     
00063     Pt(X_d x_, Y y_);   
00064     Pt(X x_, Y_d y_);   
00065     Pt(X_d x_, Y_d y_); 
00066 
00067  
00069 
00072     bool Less(const Pt& rhs) const
00073         { return x < rhs.x ? true : (x == rhs.x ? (y < rhs.y ? true : false) : false); }
00075  
00077     void  operator+=(const Pt& rhs)      { x += rhs.x; y += rhs.y; } 
00078     void  operator-=(const Pt& rhs)      { x -= rhs.x; y -= rhs.y; } 
00079     Pt    operator-() const              { return Pt(-x, -y); }      
00080 
00081 
00082     X x; 
00083     Y y; 
00084 
00085 private:
00086     friend class boost::serialization::access;
00087     template <class Archive>
00088     void serialize(Archive& ar, const unsigned int version);
00089 };
00090 
00094 struct GG_API Rect
00095 { 
00097     Rect();                                
00098     Rect(const Pt& pt1, const Pt& pt2);    
00099     Rect(X x1, Y y1, X x2, Y y2);  
00100 
00101  
00103     X  Left() const         { return ul.x; }        
00104     X  Right() const        { return lr.x; }        
00105     Y  Top() const          { return ul.y; }        
00106     Y  Bottom() const       { return lr.y; }        
00107     Pt UpperLeft() const    { return ul; }          
00108     Pt LowerRight() const   { return lr; }          
00109     X  Width() const        { return lr.x - ul.x; } 
00110     Y  Height() const       { return lr.y - ul.y; } 
00111 
00112     bool  Contains(const Pt& pt) const; 
00113 
00114  
00116     void operator+=(const Pt& pt)      { ul += pt; lr += pt; } 
00117     void operator-=(const Pt& pt)      { ul -= pt; lr -= pt; } 
00118 
00119 
00120     Pt ul; 
00121     Pt lr; 
00122 
00123 private:
00124     friend class boost::serialization::access;
00125     template <class Archive>
00126     void serialize(Archive& ar, const unsigned int version);
00127 };
00128 
00129 GG_API inline bool operator==(const Pt& lhs, const Pt& rhs) { return lhs.x == rhs.x && lhs.y == rhs.y; } 
00130 GG_API inline bool operator!=(const Pt& lhs, const Pt& rhs) { return !(lhs == rhs); }                    
00131 GG_API inline bool operator<(const Pt& lhs, const Pt& rhs)  { return lhs.x < rhs.x && lhs.y < rhs.y; }   
00132 GG_API inline bool operator>(const Pt& lhs, const Pt& rhs)  { return lhs.x > rhs.x && lhs.y > rhs.y; }   
00133 GG_API inline bool operator<=(const Pt& lhs, const Pt& rhs) { return lhs.x <= rhs.x && lhs.y <= rhs.y; } 
00134 GG_API inline bool operator>=(const Pt& lhs, const Pt& rhs) { return lhs.x >= rhs.x && lhs.y >= rhs.y; } 
00135 GG_API inline Pt   operator+(const Pt& lhs, const Pt& rhs)  { return Pt(lhs.x + rhs.x, lhs.y + rhs.y); } 
00136 GG_API inline Pt   operator-(const Pt& lhs, const Pt& rhs)  { return Pt(lhs.x - rhs.x, lhs.y - rhs.y); } 
00137 
00138 GG_API std::ostream& operator<<(std::ostream& os, const Pt& pt); 
00139 
00141 GG_API inline bool operator==(const Rect& lhs, const Rect& rhs) { return lhs.ul.x == rhs.ul.x && lhs.lr.x == rhs.lr.x && lhs.lr.x == rhs.lr.x && lhs.lr.y == rhs.lr.y; }
00142 
00144 GG_API inline bool operator!=(const Rect& lhs, const Rect& rhs) { return !(lhs == rhs); }
00145 
00146 GG_API inline Rect operator+(const Rect& rect, const Pt& pt) { return Rect(rect.ul + pt, rect.lr + pt); } 
00147 GG_API inline Rect operator-(const Rect& rect, const Pt& pt) { return Rect(rect.ul - pt, rect.lr - pt); } 
00148 GG_API inline Rect operator+(const Pt& pt, const Rect& rect) { return rect + pt; } 
00149 GG_API inline Rect operator-(const Pt& pt, const Rect& rect) { return rect - pt; } 
00150 
00151 GG_API std::ostream& operator<<(std::ostream& os, const Rect& rect); 
00152 
00153 } // namepace GG
00154 
00155 // template implementations
00156 template <class Archive>
00157 void GG::Pt::serialize(Archive& ar, const unsigned int version)
00158 {
00159     ar  & BOOST_SERIALIZATION_NVP(x)
00160         & BOOST_SERIALIZATION_NVP(y);
00161 }
00162 
00163 template <class Archive>
00164 void GG::Rect::serialize(Archive& ar, const unsigned int version)
00165 {
00166     ar  & BOOST_SERIALIZATION_NVP(ul)
00167         & BOOST_SERIALIZATION_NVP(lr);
00168 }
00169 
00170 #endif // _GG_PtRect_h_

Generated on Sat Mar 26 07:08:37 2011 for GG by  doxygen 1.5.9