home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / DVDStar / Editace / quake4_sdkv10.exe / source / idlib / Lib.cpp < prev    next >
C/C++ Source or Header  |  2005-11-14  |  11KB  |  513 lines

  1.  
  2. #include "precompiled.h"
  3. #pragma hdrstop
  4.  
  5. #if defined( MACOS_X )
  6. #include <signal.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #endif
  10.  
  11. /*
  12. ===============================================================================
  13.  
  14.     idLib
  15.  
  16. ===============================================================================
  17. */
  18.  
  19. idSys *            idLib::sys            = NULL;
  20. idCommon *        idLib::common        = NULL;
  21. idCVarSystem *    idLib::cvarSystem    = NULL;
  22. idFileSystem *    idLib::fileSystem    = NULL;
  23. int                idLib::frameNumber    = 0;
  24.  
  25. /*
  26. ================
  27. idLib::Init
  28. ================
  29. */
  30. void idLib::Init( void ) {
  31.  
  32.     // initialize little/big endian conversion
  33.     Swap_Init();
  34.  
  35.     // initialize memory manager
  36.     Mem_Init();
  37.  
  38. // RAVEN BEGIN
  39. // dluetscher: added the following code to initialize each of the memory heaps immediately
  40. //               following Mem_Init()
  41. #ifdef _RV_MEM_SYS_SUPPORT
  42.     // initialize each of the memory heaps
  43.     common->InitHeaps();
  44. #endif
  45. // RAVEN END
  46.  
  47.     // init string memory allocator
  48.     idStr::InitMemory();
  49.  
  50.     // initialize generic SIMD implementation
  51.     idSIMD::Init();
  52.  
  53.     // initialize math
  54.     idMath::Init();
  55.  
  56. // RAVEN BEGIN
  57. // jsinger: There is no reason for us to be doing this on the Xenon
  58. #ifndef _XENON
  59.     // test idMatX
  60.     //idMatX::Test();
  61.  
  62.     // test idPolynomial
  63.     idPolynomial::Test();
  64. #endif
  65. // RAVEN END
  66.  
  67.     // initialize the dictionary string pools
  68.     idDict::Init();
  69. }
  70.  
  71. /*
  72. ================
  73. idLib::ShutDown
  74. ================
  75. */
  76. void idLib::ShutDown( void ) {
  77.  
  78.     // shut down the dictionary string pools
  79.     idDict::Shutdown();
  80.  
  81.     // shut down the string memory allocator
  82.     idStr::ShutdownMemory();
  83.  
  84.     // shut down the SIMD engine
  85.     idSIMD::Shutdown();
  86.  
  87. // RAVEN BEGIN
  88. // dluetscher: added the following code to shutdown each of the memory heaps immediately
  89. //               before Mem_Shutdown()
  90. #ifdef _RV_MEM_SYS_SUPPORT
  91.     // shutdown each of the memory heaps
  92.     common->ShutdownHeaps();
  93. #endif
  94. // RAVEN END
  95.  
  96.     // shut down the memory manager
  97.     Mem_Shutdown();
  98. }
  99.  
  100.  
  101. /*
  102. ===============================================================================
  103.  
  104.     Colors
  105.  
  106. ===============================================================================
  107. */
  108.  
  109. idVec4    colorBlack    = idVec4( 0.00f, 0.00f, 0.00f, 1.00f );
  110. idVec4    colorWhite    = idVec4( 1.00f, 1.00f, 1.00f, 1.00f );
  111. idVec4    colorRed    = idVec4( 1.00f, 0.00f, 0.00f, 1.00f );
  112. idVec4    colorGreen    = idVec4( 0.00f, 1.00f, 0.00f, 1.00f );
  113. idVec4    colorBlue    = idVec4( 0.00f, 0.00f, 1.00f, 1.00f );
  114. idVec4    colorYellow    = idVec4( 1.00f, 1.00f, 0.00f, 1.00f );
  115. idVec4    colorMagenta= idVec4( 1.00f, 0.00f, 1.00f, 1.00f );
  116. idVec4    colorCyan    = idVec4( 0.00f, 1.00f, 1.00f, 1.00f );
  117. idVec4    colorOrange    = idVec4( 1.00f, 0.50f, 0.00f, 1.00f );
  118. idVec4    colorPurple    = idVec4( 0.60f, 0.00f, 0.60f, 1.00f );
  119. idVec4    colorPink    = idVec4( 0.73f, 0.40f, 0.48f, 1.00f );
  120. idVec4    colorBrown    = idVec4( 0.40f, 0.35f, 0.08f, 1.00f );
  121. idVec4    colorLtGrey    = idVec4( 0.75f, 0.75f, 0.75f, 1.00f );
  122. idVec4    colorMdGrey    = idVec4( 0.50f, 0.50f, 0.50f, 1.00f );
  123. idVec4    colorDkGrey    = idVec4( 0.25f, 0.25f, 0.25f, 1.00f );
  124.  
  125. static dword colorMask[2] = { 255, 0 };
  126.  
  127. /*
  128. ================
  129. ColorFloatToByte
  130. ================
  131. */
  132. ID_INLINE static byte ColorFloatToByte( float c ) {
  133.     return (byte) ( ( (dword) ( c * 255.0f ) ) & colorMask[FLOATSIGNBITSET(c)] );
  134. }
  135.  
  136. /*
  137. ================
  138. PackColor
  139. ================
  140. */
  141. dword PackColor( const idVec4 &color ) {
  142.     dword dw, dx, dy, dz;
  143.  
  144.     dx = ColorFloatToByte( color.x );
  145.     dy = ColorFloatToByte( color.y );
  146.     dz = ColorFloatToByte( color.z );
  147.     dw = ColorFloatToByte( color.w );
  148.  
  149. // RAVEN BEGIN
  150. // jnewquist: Big endian support
  151. #ifdef _LITTLE_ENDIAN
  152.     return ( dx << 0 ) | ( dy << 8 ) | ( dz << 16 ) | ( dw << 24 );
  153. #else
  154.     return ( dx << 24 ) | ( dy << 16 ) | ( dz << 8 ) | ( dw << 0 );
  155. #endif
  156. // RAVEN END
  157. }
  158.  
  159. /*
  160. ================
  161. UnpackColor
  162. ================
  163. */
  164. void UnpackColor( const dword color, idVec4 &unpackedColor ) {
  165. // RAVEN BEGIN
  166. // jnewquist: Xenon is big endian
  167. #ifdef _LITTLE_ENDIAN
  168.     unpackedColor.Set( ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ),
  169.                         ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ), 
  170.                         ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ),
  171.                         ( ( color >> 24 ) & 255 ) * ( 1.0f / 255.0f ) );
  172. #else
  173.     unpackedColor.Set( ( ( color >> 24 ) & 255 ) * ( 1.0f / 255.0f ),
  174.                         ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ), 
  175.                         ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ),
  176.                         ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ) );
  177. #endif
  178. // RAVEN END
  179. }
  180.  
  181. /*
  182. ================
  183. PackColor
  184. ================
  185. */
  186. dword PackColor( const idVec3 &color ) {
  187.     dword dx, dy, dz;
  188.  
  189.     dx = ColorFloatToByte( color.x );
  190.     dy = ColorFloatToByte( color.y );
  191.     dz = ColorFloatToByte( color.z );
  192.  
  193. // RAVEN BEGIN
  194. // jnewquist: Xenon is big endian
  195. #ifdef _LITTLE_ENDIAN
  196.     return ( dx << 0 ) | ( dy << 8 ) | ( dz << 16 );
  197. #else
  198.     return ( dy << 16 ) | ( dz << 8 ) | ( dx << 0 );
  199. #endif
  200. // RAVEN END
  201. }
  202.  
  203. /*
  204. ================
  205. UnpackColor
  206. ================
  207. */
  208. void UnpackColor( const dword color, idVec3 &unpackedColor ) {
  209. // RAVEN BEGIN
  210. // jnewquist: Xenon is big endian
  211. #ifdef _LITTLE_ENDIAN
  212.     unpackedColor.Set( ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ),
  213.                         ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ), 
  214.                         ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ) );
  215. #else
  216.     unpackedColor.Set( ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ),
  217.                         ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ),
  218.                         ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ) );
  219. #endif
  220. // RAVEN END
  221. }
  222.  
  223.  
  224. /*
  225. ===============================================================================
  226.  
  227.     Byte order functions
  228.  
  229. ===============================================================================
  230. */
  231.  
  232. // can't just use function pointers, or dll linkage can mess up
  233. static short    (*_BigShort)( short l );
  234. static short    (*_LittleShort)( short l );
  235. static int        (*_BigLong)( int l );
  236. static int        (*_LittleLong)( int l );
  237. static float    (*_BigFloat)( float l );
  238. static float    (*_LittleFloat)( float l );
  239. static void        (*_BigRevBytes)( void *bp, int elsize, int elcount );
  240. static void        (*_LittleRevBytes)( void *bp, int elsize, int elcount );
  241. static void        (*_SixtetsForInt)( byte *out, int src );
  242. static int        (*_IntForSixtets)( byte *in );
  243.  
  244. short    BigShort( short l ) { return _BigShort( l ); }
  245. short    LittleShort( short l ) { return _LittleShort( l ); }
  246. int        BigLong( int l ) { return _BigLong( l ); }
  247. int        LittleLong( int l ) { return _LittleLong( l ); }
  248. float    BigFloat( float l ) { return _BigFloat( l ); }
  249. float    LittleFloat( float l ) { return _LittleFloat( l ); }
  250. void    BigRevBytes( void *bp, int elsize, int elcount ) { _BigRevBytes( bp, elsize, elcount ); }
  251. void    LittleRevBytes( void *bp, int elsize, int elcount ){ _LittleRevBytes( bp, elsize, elcount ); }
  252.  
  253. void    SixtetsForInt( byte *out, int src) { _SixtetsForInt( out, src ); }
  254. int        IntForSixtets( byte *in ) { return _IntForSixtets( in ); }
  255.  
  256. /*
  257. ================
  258. ShortSwap
  259. ================
  260. */
  261. short ShortSwap( short l ) {
  262.     byte    b1,b2;
  263.  
  264.     b1 = l&255;
  265.     b2 = (l>>8)&255;
  266.  
  267.     return (b1<<8) + b2;
  268. }
  269.  
  270. /*
  271. ================
  272. ShortNoSwap
  273. ================
  274. */
  275. short ShortNoSwap( short l ) {
  276.     return l;
  277. }
  278.  
  279. /*
  280. ================
  281. LongSwap
  282. ================
  283. */
  284. int LongSwap ( int l ) {
  285.     byte    b1,b2,b3,b4;
  286.  
  287.     b1 = l&255;
  288.     b2 = (l>>8)&255;
  289.     b3 = (l>>16)&255;
  290.     b4 = (l>>24)&255;
  291.  
  292.     return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  293. }
  294.  
  295. /*
  296. ================
  297. LongNoSwap
  298. ================
  299. */
  300. int    LongNoSwap( int l ) {
  301.     return l;
  302. }
  303.  
  304. /*
  305. ================
  306. FloatSwap
  307. ================
  308. */
  309. float FloatSwap( float f ) {
  310.     union {
  311.         float    f;
  312.         byte    b[4];
  313.     } dat1, dat2;
  314.     
  315.     
  316.     dat1.f = f;
  317.     dat2.b[0] = dat1.b[3];
  318.     dat2.b[1] = dat1.b[2];
  319.     dat2.b[2] = dat1.b[1];
  320.     dat2.b[3] = dat1.b[0];
  321.     return dat2.f;
  322. }
  323.  
  324. /*
  325. ================
  326. FloatNoSwap
  327. ================
  328. */
  329. float FloatNoSwap( float f ) {
  330.     return f;
  331. }
  332.  
  333. /*
  334. =====================================================================
  335. RevBytesSwap
  336.  
  337. Reverses byte order in place.
  338.  
  339. INPUTS
  340.    bp       bytes to reverse
  341.    elsize   size of the underlying data type
  342.    elcount  number of elements to swap
  343.  
  344. RESULTS
  345.    Reverses the byte order in each of elcount elements.
  346. ===================================================================== */
  347. void RevBytesSwap( void *bp, int elsize, int elcount ) {
  348.     register unsigned char *p, *q;
  349.  
  350.     p = ( unsigned char * ) bp;
  351.  
  352.     if ( elsize == 2 ) {
  353.         q = p + 1;
  354.         while ( elcount-- ) {
  355.             *p ^= *q;
  356.             *q ^= *p;
  357.             *p ^= *q;
  358.             p += 2;
  359.             q += 2;
  360.         }
  361.         return;
  362.     }
  363.  
  364.     while ( elcount-- ) {
  365.         q = p + elsize - 1;
  366.         while ( p < q ) {
  367.             *p ^= *q;
  368.             *q ^= *p;
  369.             *p ^= *q;
  370.             ++p;
  371.             --q;
  372.         }
  373.         p += elsize >> 1;
  374.     }
  375. }
  376.  
  377. /*
  378. ================
  379. RevBytesNoSwap
  380. ================
  381. */
  382. void RevBytesNoSwap( void *bp, int elsize, int elcount ) {
  383.     return;
  384. }
  385.  
  386. /*
  387. ================
  388. SixtetsForIntLittle
  389. ================
  390. */
  391. void SixtetsForIntLittle( byte *out, int src) {
  392.     byte *b = (byte *)&src;
  393.     out[0] = ( b[0] & 0xfc ) >> 2;
  394.     out[1] = ( ( b[0] & 0x3 ) << 4 ) + ( ( b[1] & 0xf0 ) >> 4 );
  395.     out[2] = ( ( b[1] & 0xf ) << 2 ) + ( ( b[2] & 0xc0 ) >> 6 );
  396.     out[3] = b[2] & 0x3f;
  397. }
  398.  
  399. /*
  400. ================
  401. SixtetsForIntBig
  402. TTimo: untested - that's the version from initial base64 encode
  403. ================
  404. */
  405. void SixtetsForIntBig( byte *out, int src) {
  406.     for( int i = 0 ; i < 4 ; i++ ) {
  407.         out[i] = src & 0x3f;
  408.         src >>= 6;
  409.     }
  410. }
  411.  
  412. /*
  413. ================
  414. IntForSixtetsLittle
  415. ================
  416. */
  417. int IntForSixtetsLittle( byte *in ) {
  418.     int ret = 0;
  419.     byte *b = (byte *)&ret;
  420.     b[0] |= in[0] << 2;
  421.     b[0] |= ( in[1] & 0x30 ) >> 4;
  422.     b[1] |= ( in[1] & 0xf ) << 4;
  423.     b[1] |= ( in[2] & 0x3c ) >> 2;
  424.     b[2] |= ( in[2] & 0x3 ) << 6;
  425.     b[2] |= in[3];
  426.     return ret;
  427. }
  428.  
  429. /*
  430. ================
  431. IntForSixtetsBig
  432. TTimo: untested - that's the version from initial base64 decode
  433. ================
  434. */
  435. int IntForSixtetsBig( byte *in ) {
  436.     int ret = 0;
  437.     ret |= in[0];
  438.     ret |= in[1] << 6;
  439.     ret |= in[2] << 2*6;
  440.     ret |= in[3] << 3*6;
  441.     return ret;
  442. }
  443.  
  444. /*
  445. ================
  446. Swap_Init
  447. ================
  448. */
  449. void Swap_Init( void ) {
  450.     byte    swaptest[2] = {1,0};
  451.  
  452.     // set the byte swapping variables in a portable manner    
  453.     if ( *(short *)swaptest == 1) {
  454.         // little endian ex: x86
  455.         _BigShort = ShortSwap;
  456.         _LittleShort = ShortNoSwap;
  457.         _BigLong = LongSwap;
  458.         _LittleLong = LongNoSwap;
  459.         _BigFloat = FloatSwap;
  460.         _LittleFloat = FloatNoSwap;
  461.         _BigRevBytes = RevBytesSwap;
  462.         _LittleRevBytes = RevBytesNoSwap;
  463.         _SixtetsForInt = SixtetsForIntLittle;
  464.         _IntForSixtets = IntForSixtetsLittle;
  465.     } else {
  466.         // big endian ex: ppc
  467.         _BigShort = ShortNoSwap;
  468.         _LittleShort = ShortSwap;
  469.         _BigLong = LongNoSwap;
  470.         _LittleLong = LongSwap;
  471.         _BigFloat = FloatNoSwap;
  472.         _LittleFloat = FloatSwap;
  473.         _BigRevBytes = RevBytesNoSwap;
  474.         _LittleRevBytes = RevBytesSwap;
  475.         _SixtetsForInt = SixtetsForIntBig;
  476.         _IntForSixtets = IntForSixtetsBig;
  477.     }
  478. }
  479.  
  480. /*
  481. ==========
  482. Swap_IsBigEndian
  483. ==========
  484. */
  485. bool Swap_IsBigEndian( void ) {
  486.     byte    swaptest[2] = {1,0};
  487.     return *(short *)swaptest != 1;
  488. }
  489.  
  490. /*
  491. ===============================================================================
  492.  
  493.     Assertion
  494.  
  495. ===============================================================================
  496. */
  497.  
  498. void AssertFailed( const char *file, int line, const char *expression ) {
  499.     if ( idLib::sys ) {
  500.         idLib::sys->DebugPrintf( "\n\nASSERTION FAILED!\n%s(%d): '%s'\n", file, line, expression );
  501.     }
  502. #ifdef _WIN32
  503. // RAVEN BEGIN
  504. // jnewquist: Visual Studio platform independent breakpoint
  505.     __debugbreak();
  506. // RAVEN END
  507. #elif defined( __linux__ )
  508.     __asm__ __volatile__ ("int $0x03");
  509. #elif defined( MACOS_X )
  510.     kill( getpid(), SIGINT );
  511. #endif
  512. }
  513.