krmath.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under the LGPL. 
00008         
00009         I kindly request you display a splash screen (provided in the HTML documentation)
00010         to promote Kyra and acknowledge the software and everyone who has contributed to it, 
00011         but it is not required by the license.
00012 
00013 --- LGPL License --
00014 
00015     This library is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU Lesser General Public
00017     License as published by the Free Software Foundation; either
00018     version 2.1 of the License, or (at your option) any later version.
00019 
00020     This library is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023     Lesser General Public License for more details.
00024 
00025     You should have received a copy of the GNU Lesser General Public
00026     License along with this library; if not, write to the Free Software
00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 
00029         The full text of the license can be found in lgpl.txt
00030 */
00031 
00032 #ifndef IO_MATH_INCLUDED
00033 #define IO_MATH_INCLUDED
00034 
00035 #include "../../grinliz/glutil.h"
00036 #include "../util/glfixed.h"
00037 #include "../../grinliz/glgeometry.h"
00038 
00039 typedef grinliz::Rectangle2I KrRect;
00040 
00041 
00046 class KrMatrix2
00047 {
00048   public:
00049         GlFixed x, y;
00050         GlFixed xScale, yScale;
00051 
00053         void Composite( const KrMatrix2& other )
00054         {
00055                 // The if check helps performance a little bit. The ".v" usage
00056                 // makes a surprising difference. The opitimizer is doing less
00057                 // than I expected.
00058                 if (    other.xScale.v == GlFixed_1 
00059                          && other.yScale.v == GlFixed_1 )
00060                 {
00061                         x.v = other.x.v + x.v;          // bypass the compiler opt
00062                         y.v = other.y.v + y.v;
00063                 }
00064                 else
00065                 {
00066                         x = other.x + other.xScale * x;
00067                         y = other.y + other.yScale * y;
00068                         xScale = other.xScale * xScale;
00069                         yScale = other.yScale * yScale;
00070                 }
00071         }
00072 
00073         void Set( GlFixed _x = 0, GlFixed _y = 0, GlFixed _xScale = 1, GlFixed _yScale = 1 )
00074         {
00075                 x.v = _x.v;
00076                 y.v = _y.v;
00077                 xScale.v = _xScale.v;
00078                 yScale.v = _yScale.v;
00079         }
00080 
00081         // return true if there is any scaling term:
00082         bool IsScaled() const { return ( xScale.v != GlFixed_1 ) || ( yScale.v != GlFixed_1 ); }
00083 
00084         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 ); }
00085         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 ); }
00086 };
00087 
00088 
00089 // Scale then Translate using the give matrix.
00090 inline void ScaleTranslate( grinliz::Rectangle2I* rect, const KrMatrix2& matrix )
00091 {
00092         GlFixed xmin = rect->min.x;
00093         GlFixed ymin = rect->min.y;
00094         GlFixed xmax = rect->max.x;
00095         GlFixed ymax = rect->max.y;
00096 
00097         //Scale( matrix.xScale, matrix.yScale );
00098         xmin *= matrix.xScale;
00099         ymin *= matrix.yScale;
00100         xmax *= matrix.xScale;
00101         ymax *= matrix.yScale;
00102 
00103         rect->min.x = (xmin + matrix.x).ToInt();
00104         rect->max.x = (xmax + matrix.x).ToInt();
00105         rect->min.y = (ymin + matrix.y).ToInt();
00106         rect->max.y = (ymax + matrix.y).ToInt();        
00107 }
00108 
00109 
00110 #endif

Generated on Thu Jul 20 20:45:32 2006 for Kyra by  doxygen 1.4.7