Main Page   Class Hierarchy   Compound List   File List   Compound Members  

krmath.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 #ifndef IO_MATH_INCLUDED
00070 #define IO_MATH_INCLUDED
00071 
00072 #include "../util/glutil.h"
00073 #include "../util/gltypes.h"
00074 #include "../util/glfixed.h"
00075 
00076 
00077 // A point or other 2d object.
00078 struct KrVector2
00079 {
00080         int x;          
00081         int y;          
00082 
00083         void operator+=( const KrVector2& rhs ) { x += rhs.x; y += rhs.y; }
00084         void Set( int _x = 0, int _y = 0 )              { x = _x; y = _y; }
00085 };
00086 
00087 
00088 template <class T>
00089 struct KrVector2T
00090 {
00091         T x;
00092         T y;
00093 
00094         void operator+=( const KrVector2T& rhs )        { x += rhs.x; y += rhs.y; }
00095         void Set( T _x, T _y )                                          { x = _x; y = _y; }
00096 };
00097 
00098 
00103 class KrMatrix2
00104 {
00105   public:
00106         GlFixed x, y;
00107         GlFixed xScale, yScale;
00108 
00110         void Composite( const KrMatrix2& other )
00111         {
00112                 // The if check helps performance a little bit. The ".v" usage
00113                 // makes a surprising difference. The opitimizer is doing less
00114                 // than I expected.
00115                 if (    other.xScale.v == GlFixed_1 
00116                          && other.yScale.v == GlFixed_1 )
00117                 {
00118                         x.v = other.x.v + x.v;          // bypass the compiler opt
00119                         y.v = other.y.v + y.v;
00120                 }
00121                 else
00122                 {
00123                         x = other.x + other.xScale * x;
00124                         y = other.y + other.yScale * y;
00125                         xScale = other.xScale * xScale;
00126                         yScale = other.yScale * yScale;
00127                 }
00128         }
00129 
00130         void Set( GlFixed _x = 0, GlFixed _y = 0, GlFixed _xScale = 1, GlFixed _yScale = 1 )
00131         {
00132                 x.v = _x.v;
00133                 y.v = _y.v;
00134                 xScale.v = _xScale.v;
00135                 yScale.v = _yScale.v;
00136         }
00137 
00138         // return true if there is any scaling term:
00139         bool IsScaled() const { return ( xScale.v != GlFixed_1 ) || ( yScale.v != GlFixed_1 ); }
00140 
00141         inline friend bool operator == (const KrMatrix2& a, const KrMatrix2& b)    { return (a.x.v == b.x.v && a.y.v == b.y.v && a.xScale.v == b.xScale.v && a.yScale.v == b.yScale.v ); }
00142         inline friend bool operator != (const KrMatrix2& a, const KrMatrix2& b)    { return (a.x.v != b.x.v || a.y.v != b.y.v || a.xScale.v != b.xScale.v || a.yScale.v != b.yScale.v ); }
00143 };
00144 
00145 
00148 struct KrRect
00149 {
00150         int xmin;       
00151         int ymin;       
00152         int xmax;       
00153         int ymax;       
00154 
00155         int Width()      const  { return xmax - xmin + 1; }             
00156         int Height() const      { return ymax - ymin + 1; }             
00157         int Area()   const      { return ( xmax - xmin + 1 ) * ( ymax - ymin + 1 );     }
00158 
00160         void Set( int _xmin, int _ymin, int _xmax, int _ymax )  { xmin = _xmin; ymin = _ymin; xmax = _xmax; ymax = _ymax; }
00161 
00163         void SetInvalid()       { xmin = -1; xmax = -2; ymin = -1; ymax = -2; }
00164 
00166         void Zero()
00167         {
00168                 xmin = ymin = xmax = ymax = 0;
00169         }
00170 
00172         bool IsValid() const
00173         {
00174                 return ( xmin <= xmax ) && ( ymin <= ymax );
00175         }
00176 
00180         void FromPair( int x0, int y0, int x1, int y1 );
00181 
00183         bool Intersect( const KrRect& rect ) const
00184         {
00185                 if (    rect.xmax < xmin
00186                          || rect.xmin > xmax
00187                          || rect.ymax < ymin
00188                          || rect.ymin > ymax )
00189                 {
00190                         return false;
00191                 }
00192                 return true;
00193         }       
00194 
00195         bool Intersect( const KrVector2& point ) const
00196         {
00197                 if (    point.x < xmin
00198                          || point.x > xmax
00199                          || point.y < ymin
00200                          || point.y > ymax )
00201                 {
00202                         return false;
00203                 }
00204                 return true;
00205         }
00206 
00207         bool Intersect( int x, int y ) const
00208         {
00209                 if (    x < xmin
00210                          || x > xmax
00211                          || y < ymin
00212                          || y > ymax )
00213                 {
00214                         return false;
00215                 }
00216                 return true;
00217         }
00218 
00219 
00221         bool HasInside( const KrRect& rect ) const
00222         {
00223                 if (    rect.xmin >= xmin
00224                          && rect.xmax <= xmax
00225                          && rect.ymin >= ymin
00226                          && rect.ymax <= ymax )
00227                 {
00228                         return true;
00229                 }
00230                 return false;
00231         }
00232 
00234         bool HasInside( const KrVector2& point ) const
00235         {
00236                 if (    point.x >= xmin
00237                          && point.x <= xmax
00238                          && point.y >= ymin
00239                          && point.y <= ymax )
00240                 {
00241                         return true;
00242                 }
00243                 return false;
00244         }
00245 
00246 
00248         void DoUnion( const KrRect& rect )
00249         {
00250                 xmin = GlMin( xmin, rect.xmin );
00251                 xmax = GlMax( xmax, rect.xmax );
00252                 ymin = GlMin( ymin, rect.ymin );
00253                 ymax = GlMax( ymax, rect.ymax );
00254         }
00255 
00257         void DoIntersection( const KrRect& rect )
00258         {
00259                 xmin = GlMax( xmin, rect.xmin );
00260                 xmax = GlMin( xmax, rect.xmax );
00261                 ymin = GlMax( ymin, rect.ymin );
00262                 ymax = GlMin( ymax, rect.ymax );
00263         }
00264 
00266         void DoClip( const KrRect& rect )
00267         {
00268                 xmin = rect.xmin > xmin ? rect.xmin : xmin;
00269                 xmax = rect.xmax < xmax ? rect.xmax : xmax;
00270                 ymin = rect.ymin > ymin ? rect.ymin : ymin;
00271                 ymax = rect.ymax < ymax ? rect.ymax : ymax;
00272         }
00273 
00274 
00276         void Scale( GlFixed x, GlFixed y )
00277         {
00278                 xmin = ( x * xmin ).ToInt();
00279                 ymin = ( y * ymin ).ToInt();
00280                 xmax = ( x * xmax ).ToInt();
00281                 ymax = ( y * ymax ).ToInt();
00282         }
00283 
00284         // Scale then Translate using the give matrix.
00285         void ScaleTranslate( const KrMatrix2& matrix )
00286         {
00287                 Scale( matrix.xScale, matrix.yScale );
00288                 xmin += matrix.x.ToInt();
00289                 xmax += matrix.x.ToInt();
00290                 ymin += matrix.y.ToInt();
00291                 ymax += matrix.y.ToInt();       
00292         }
00293 
00295         void EdgeAdd( int i )
00296         {
00297                 xmin -= i;
00298                 xmax += i;
00299                 ymin -= i;
00300                 ymax += i;
00301         }
00302 
00303         bool operator==( const KrRect& that ) const { return     ( xmin == that.xmin )
00304                                                                                                         && ( xmax == that.xmax )
00305                                                                                                         && ( ymin == that.ymin )
00306                                                                                                         && ( ymax == that.ymax ); }
00307         bool operator!=( const KrRect& that ) const { return     ( xmin != that.xmin )
00308                                                                                                         || ( xmax != that.xmax )
00309                                                                                                         || ( ymin != that.ymin )
00310                                                                                                         || ( ymax != that.ymax ); }
00311 };
00312 
00313 
00314 /* A rectangle structure.
00315 */
00316 template <class T>
00317 struct KrTRect
00318 {
00319         T xmin; 
00320         T ymin; 
00321         T xmax; 
00322         T ymax; 
00323 
00324         T Width()  const        { return xmax - xmin + 1; }             
00325         T Height() const        { return ymax - ymin + 1; }             
00326         T Area()   const        { return ( xmax - xmin + 1 ) * ( ymax - ymin + 1 );     }
00327 
00329         void Set( T _xmin, T _ymin, T _xmax, T _ymax )  { xmin = _xmin; ymin = _ymin; xmax = _xmax; ymax = _ymax; }
00330 
00332         bool IsValid() const
00333         {
00334                 return ( xmin <= xmax ) && ( ymin <= ymax );
00335         }
00336 
00338         bool Intersect( const KrTRect<T>& rect ) const
00339         {
00340                 if (    rect.xmax < xmin
00341                          || rect.xmin > xmax
00342                          || rect.ymax < ymin
00343                          || rect.ymin > ymax )
00344                 {
00345                         return false;
00346                 }
00347                 return true;
00348         }       
00349 
00350         bool Intersect( T x, T y ) const
00351         {
00352                 if (    x < xmin
00353                          || x > xmax
00354                          || y < ymin
00355                          || y > ymax )
00356                 {
00357                         return false;
00358                 }
00359                 return true;
00360         }
00361 
00362 
00364         bool HasInside( const KrTRect<T>& rect ) const
00365         {
00366                 if (    rect.xmin >= xmin
00367                          && rect.xmax <= xmax
00368                          && rect.ymin >= ymin
00369                          && rect.ymax <= ymax )
00370                 {
00371                         return true;
00372                 }
00373                 return false;
00374         }
00375 
00377         void DoUnion( const KrTRect<T>& rect )
00378         {
00379                 xmin = GlMin( xmin, rect.xmin );
00380                 xmax = GlMax( xmax, rect.xmax );
00381                 ymin = GlMin( ymin, rect.ymin );
00382                 ymax = GlMax( ymax, rect.ymax );
00383         }
00384 
00386         void DoIntersection( const KrTRect<T>& rect )
00387         {
00388                 xmin = GlMax( xmin, rect.xmin );
00389                 xmax = GlMin( xmax, rect.xmax );
00390                 ymin = GlMax( ymin, rect.ymin );
00391                 ymax = GlMin( ymax, rect.ymax );
00392         }
00393 };
00394 
00395 
00396 #endif

Generated on Mon Sep 15 12:01:11 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001