Main Page   Class Hierarchy   Compound List   File List   Compound Members  

glutil.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 
00070 #ifndef IO_UTIL_INCLUDED
00071 #define IO_UTIL_INCLUDED
00072 
00073 #include "gltypes.h"
00074 
00075 
00076 //#ifdef _MSC_VER
00077 //      #define OPTIMIZE_NO_ALIAS_ON optimize ( "w", on )
00078 //#else
00079 //      #define OPTIMIZE_NO_ALIAS_ON 
00080 //#endif
00081 //
00082 //
00083 //#ifdef _MSC_VER
00084 //      #define OPTIMIZE_NO_ALIAS_OFF optimize ( "w", off )
00085 //#else
00086 //      #define OPTIMIZE_NO_ALIAS_OFF 
00087 //#endif
00088 
00089 
00090 template <class T> inline T             GlMin( T a, T b )               { return ( a < b ) ? a : b; }
00091 template <class T> inline T             GlMax( T a, T b )               { return ( a > b ) ? a : b; }
00092 template <class T> inline void  GlSwap( T& a, T& b )    { T temp; temp = a; a = b; b = temp; }
00093 template <class T> inline bool  GlInRange( T a, T lower, T upper )      { return a >= lower && a <= upper; }
00094 template <class T> inline T             GlClamp( const T& a, T lower, T upper ) 
00095                                                                 { 
00096                                                                         if ( a < lower )
00097                                                                                 return lower;
00098                                                                         else if ( a > upper )
00099                                                                                 return upper;
00100                                                                         return a;
00101                                                                 }
00102 template <class T> inline T GlInterpolate( T rangeLow, T rangeHigh, T rangeVal,
00103                                                                                    T low, T high )
00104 {
00105         return ( ( ( rangeVal - rangeLow ) * ( high - low ) / ( rangeHigh - rangeLow ) ) ) + low;
00106 }
00107 
00108 
00109 /*      A class to store a set of bit flags, and set and get them in a standard way.
00110         Class T must be some kind of integer type.
00111 */
00112 template < class T >
00113 class GlFlag
00114 {
00115   public:
00116         GlFlag()                                                        { store = 0; }
00117 
00118         inline void Set( T flag )                       { store |= flag; }
00119         inline void Clear( T flag )                     { store &= ~flag; }
00120         inline bool IsSet( T flag ) const       { return ( store & flag ) != 0; }
00121 
00122         inline U32  ToU32() const                       { return store; }
00123         inline void FromU32( U32 s )            { store = (T) s; }
00124 
00125         inline void ClearAll()                          { store = 0; }
00126 
00127   private:
00128         T store;
00129 };      
00130 
00131 /*      A strange class: it creates bitmaps used for collision detection. Given
00132         an unsigned something, this is a utility class to pack bits...starting
00133         with the highest bit.
00134 */
00135 
00136 template < class T >
00137 class GlHighBitWriter 
00138 {
00139   public:
00140         enum
00141         {
00142                 MAXBIT = ( sizeof(T)*8-1 ),
00143                 NUMBIT = ( sizeof(T)*8 ),
00144                 ALLSET = T( -1 )
00145         };
00146 
00147         GlHighBitWriter( T* _data ) : data( _data ), bitPos( MAXBIT )   {}
00148 
00149         void Skip()
00150         {
00151                 if ( bitPos == 0 )
00152                 {
00153                         ++data;
00154                         bitPos = MAXBIT;
00155                 }
00156                 else
00157                 {
00158                         --bitPos;
00159                 }
00160         }
00161 
00162         void Skip_N( unsigned n )
00163         {
00164                 bitPos -= n % NUMBIT;
00165                 if ( bitPos < 0 )
00166                 {
00167                         bitPos += NUMBIT;
00168                         ++data;
00169                 }
00170                 data += n / NUMBIT;
00171         }
00172         
00173         void Push_1()   
00174         {
00175                 *data |= ( 1 << bitPos );
00176                 Skip();
00177         }
00178 
00179         void Push_1N( unsigned n )
00180         {
00181                 // Push bits to T boundary
00182                 while( n && bitPos != MAXBIT )
00183                 {
00184                         Push_1();
00185                         --n;
00186                 }
00187 
00188                 // Write Full T size
00189                 while( n >= NUMBIT )
00190                 {
00191                         *data = ALLSET;
00192                         ++data;
00193                         n -= NUMBIT;
00194                 }
00195 
00196                 // Write the remainder
00197                 while ( n )
00198                 {
00199                         Push_1();
00200                         --n;
00201                 }
00202         }
00203 
00204   private:
00205         T*      data;
00206         int bitPos;
00207 };
00208 
00209 
00210 
00211 
00212 #endif

Generated on Fri Feb 7 20:44:20 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001