00001 /*
00002 Copyright (c) 2000-2003 Lee Thomason (www.grinninglizard.com)
00003
00004 Grinning Lizard Utilities. Note that software that uses the
00005 utility package (including Lilith3D and Kyra) have more restrictive
00006 licences which applies to code outside of the utility package.
00007
00008
00009 This software is provided 'as-is', without any express or implied
00010 warranty. In no event will the authors be held liable for any
00011 damages arising from the use of this software.
00012
00013 Permission is granted to anyone to use this software for any
00014 purpose, including commercial applications, and to alter it and
00015 redistribute it freely, subject to the following restrictions:
00016
00017 1. The origin of this software must not be misrepresented; you must
00018 not claim that you wrote the original software. If you use this
00019 software in a product, an acknowledgment in the product documentation
00020 would be appreciated but is not required.
00021
00022 2. Altered source versions must be plainly marked as such, and
00023 must not be misrepresented as being the original software.
00024
00025 3. This notice may not be removed or altered from any source
00026 distribution.
00027 */
00028
00029
00030 #ifndef IO_UTIL_INCLUDED
00031 #define IO_UTIL_INCLUDED
00032
00033 #pragma warning ( disable : 4786 )
00034 #include "gltypes.h"
00035
00036
00037 //#ifdef _MSC_VER
00038 // #define OPTIMIZE_NO_ALIAS_ON optimize ( "w", on )
00039 //#else
00040 // #define OPTIMIZE_NO_ALIAS_ON
00041 //#endif
00042 //
00043 //
00044 //#ifdef _MSC_VER
00045 // #define OPTIMIZE_NO_ALIAS_OFF optimize ( "w", off )
00046 //#else
00047 // #define OPTIMIZE_NO_ALIAS_OFF
00048 //#endif
00049
00050
00051 template <class T> inline T GlMin( T a, T b ) { return ( a < b ) ? a : b; }
00052 template <class T> inline T GlMax( T a, T b ) { return ( a > b ) ? a : b; }
00053 template <class T> inline T GlMin( T a, T b, T c ) { return GlMin( a, GlMin( b, c ) ); }
00054 template <class T> inline T GlMax( T a, T b, T c ) { return GlMax( a, GlMax( b, c ) ); }
00055
00056
00057 template <class T> inline void GlSwap( T& a, T& b ) { T temp; temp = a; a = b; b = temp; }
00058 template <class T> inline bool GlInRange( T a, T lower, T upper ) { return a >= lower && a <= upper; }
00059 template <class T> inline T GlClamp( const T& a, T lower, T upper )
00060 {
00061 if ( a < lower )
00062 return lower;
00063 else if ( a > upper )
00064 return upper;
00065 return a;
00066 }
00067 template <class T> inline T GlInterpolate( T rangeLow, T rangeHigh, T rangeVal,
00068 T low, T high )
00069 {
00070 return ( ( ( rangeVal - rangeLow ) * ( high - low ) / ( rangeHigh - rangeLow ) ) ) + low;
00071 }
00072
00073
00074 /* A class to store a set of bit flags, and set and get them in a standard way.
00075 Class T must be some kind of integer type.
00076 */
00077 template < class T >
00078 class GlFlag
00079 {
00080 public:
00081 GlFlag() { store = 0; }
00082
00083 inline void Set( T flag ) { store |= flag; }
00084 inline void Clear( T flag ) { store &= ~flag; }
00085 inline bool IsSet( T flag ) const { return ( store & flag ) != 0; }
00086
00087 inline U32 ToU32() const { return store; }
00088 inline void FromU32( U32 s ) { store = (T) s; }
00089
00090 inline void ClearAll() { store = 0; }
00091
00092 private:
00093 T store;
00094 };
00095
00096 /* A strange class: it creates bitmaps used for collision detection. Given
00097 an unsigned something, this is a utility class to pack bits...starting
00098 with the highest bit.
00099 */
00100
00101 template < class T >
00102 class GlHighBitWriter
00103 {
00104 public:
00105 enum
00106 {
00107 MAXBIT = ( sizeof(T)*8-1 ),
00108 NUMBIT = ( sizeof(T)*8 ),
00109 ALLSET = T( -1 )
00110 };
00111
00112 GlHighBitWriter( T* _data ) : data( _data ), bitPos( MAXBIT ) {}
00113
00114 void Skip()
00115 {
00116 if ( bitPos == 0 )
00117 {
00118 ++data;
00119 bitPos = MAXBIT;
00120 }
00121 else
00122 {
00123 --bitPos;
00124 }
00125 }
00126
00127 void Skip_N( unsigned n )
00128 {
00129 bitPos -= n % NUMBIT;
00130 if ( bitPos < 0 )
00131 {
00132 bitPos += NUMBIT;
00133 ++data;
00134 }
00135 data += n / NUMBIT;
00136 }
00137
00138 void Push_1()
00139 {
00140 *data |= ( 1 << bitPos );
00141 Skip();
00142 }
00143
00144 void Push_1N( unsigned n )
00145 {
00146 // Push bits to T boundary
00147 while( n && bitPos != MAXBIT )
00148 {
00149 Push_1();
00150 --n;
00151 }
00152
00153 // Write Full T size
00154 while( n >= NUMBIT )
00155 {
00156 *data = ALLSET;
00157 ++data;
00158 n -= NUMBIT;
00159 }
00160
00161 // Write the remainder
00162 while ( n )
00163 {
00164 Push_1();
00165 --n;
00166 }
00167 }
00168
00169 private:
00170 T* data;
00171 int bitPos;
00172 };
00173
00174
00175
00176
00177 #endif
1.2.11.1 written by Dimitri van Heesch,
© 1997-2001