Lib.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "precompiled.h"
  21. #pragma hdrstop
  22. #if defined( MACOS_X )
  23. #include <signal.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #endif
  27. /*
  28. ===============================================================================
  29. idLib
  30. ===============================================================================
  31. */
  32. idSys * idLib::sys = NULL;
  33. idCommon * idLib::common = NULL;
  34. idCVarSystem * idLib::cvarSystem = NULL;
  35. idFileSystem * idLib::fileSystem = NULL;
  36. int idLib::frameNumber = 0;
  37. bool idLib::mainThreadInitialized = 0;
  38. ID_TLS idLib::isMainThread = 0;
  39. char idException::error[2048];
  40. /*
  41. ================
  42. idLib::Init
  43. ================
  44. */
  45. void idLib::Init() {
  46. assert( sizeof( bool ) == 1 );
  47. isMainThread = 1;
  48. mainThreadInitialized = 1; // note that the thread-local isMainThread is now valid
  49. // initialize little/big endian conversion
  50. Swap_Init();
  51. // init string memory allocator
  52. idStr::InitMemory();
  53. // initialize generic SIMD implementation
  54. idSIMD::Init();
  55. // initialize math
  56. idMath::Init();
  57. // test idMatX
  58. //idMatX::Test();
  59. // test idPolynomial
  60. #ifdef _DEBUG
  61. idPolynomial::Test();
  62. #endif
  63. // initialize the dictionary string pools
  64. idDict::Init();
  65. }
  66. /*
  67. ================
  68. idLib::ShutDown
  69. ================
  70. */
  71. void idLib::ShutDown() {
  72. // shut down the dictionary string pools
  73. idDict::Shutdown();
  74. // shut down the string memory allocator
  75. idStr::ShutdownMemory();
  76. // shut down the SIMD engine
  77. idSIMD::Shutdown();
  78. }
  79. /*
  80. ===============================================================================
  81. Colors
  82. ===============================================================================
  83. */
  84. idVec4 colorBlack = idVec4( 0.00f, 0.00f, 0.00f, 1.00f );
  85. idVec4 colorWhite = idVec4( 1.00f, 1.00f, 1.00f, 1.00f );
  86. idVec4 colorRed = idVec4( 1.00f, 0.00f, 0.00f, 1.00f );
  87. idVec4 colorGreen = idVec4( 0.00f, 1.00f, 0.00f, 1.00f );
  88. idVec4 colorBlue = idVec4( 0.00f, 0.00f, 1.00f, 1.00f );
  89. idVec4 colorYellow = idVec4( 1.00f, 1.00f, 0.00f, 1.00f );
  90. idVec4 colorMagenta= idVec4( 1.00f, 0.00f, 1.00f, 1.00f );
  91. idVec4 colorCyan = idVec4( 0.00f, 1.00f, 1.00f, 1.00f );
  92. idVec4 colorOrange = idVec4( 1.00f, 0.50f, 0.00f, 1.00f );
  93. idVec4 colorPurple = idVec4( 0.60f, 0.00f, 0.60f, 1.00f );
  94. idVec4 colorPink = idVec4( 0.73f, 0.40f, 0.48f, 1.00f );
  95. idVec4 colorBrown = idVec4( 0.40f, 0.35f, 0.08f, 1.00f );
  96. idVec4 colorLtGrey = idVec4( 0.75f, 0.75f, 0.75f, 1.00f );
  97. idVec4 colorMdGrey = idVec4( 0.50f, 0.50f, 0.50f, 1.00f );
  98. idVec4 colorDkGrey = idVec4( 0.25f, 0.25f, 0.25f, 1.00f );
  99. /*
  100. ================
  101. PackColor
  102. ================
  103. */
  104. dword PackColor( const idVec4 &color ) {
  105. byte dx = idMath::Ftob( color.x * 255.0f );
  106. byte dy = idMath::Ftob( color.y * 255.0f );
  107. byte dz = idMath::Ftob( color.z * 255.0f );
  108. byte dw = idMath::Ftob( color.w * 255.0f );
  109. return ( dx << 0 ) | ( dy << 8 ) | ( dz << 16 ) | ( dw << 24 );
  110. }
  111. /*
  112. ================
  113. UnpackColor
  114. ================
  115. */
  116. void UnpackColor( const dword color, idVec4 &unpackedColor ) {
  117. unpackedColor.Set( ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ),
  118. ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ),
  119. ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ),
  120. ( ( color >> 24 ) & 255 ) * ( 1.0f / 255.0f ) );
  121. }
  122. /*
  123. ================
  124. PackColor
  125. ================
  126. */
  127. dword PackColor( const idVec3 &color ) {
  128. byte dx = idMath::Ftob( color.x * 255.0f );
  129. byte dy = idMath::Ftob( color.y * 255.0f );
  130. byte dz = idMath::Ftob( color.z * 255.0f );
  131. return ( dx << 0 ) | ( dy << 8 ) | ( dz << 16 );
  132. }
  133. /*
  134. ================
  135. UnpackColor
  136. ================
  137. */
  138. void UnpackColor( const dword color, idVec3 &unpackedColor ) {
  139. unpackedColor.Set( ( ( color >> 0 ) & 255 ) * ( 1.0f / 255.0f ),
  140. ( ( color >> 8 ) & 255 ) * ( 1.0f / 255.0f ),
  141. ( ( color >> 16 ) & 255 ) * ( 1.0f / 255.0f ) );
  142. }
  143. /*
  144. ===============
  145. idLib::FatalError
  146. ===============
  147. */
  148. void idLib::FatalError( const char *fmt, ... ) {
  149. va_list argptr;
  150. char text[MAX_STRING_CHARS];
  151. va_start( argptr, fmt );
  152. idStr::vsnPrintf( text, sizeof( text ), fmt, argptr );
  153. va_end( argptr );
  154. common->FatalError( "%s", text );
  155. }
  156. /*
  157. ===============
  158. idLib::Error
  159. ===============
  160. */
  161. void idLib::Error( const char *fmt, ... ) {
  162. va_list argptr;
  163. char text[MAX_STRING_CHARS];
  164. va_start( argptr, fmt );
  165. idStr::vsnPrintf( text, sizeof( text ), fmt, argptr );
  166. va_end( argptr );
  167. common->Error( "%s", text );
  168. }
  169. /*
  170. ===============
  171. idLib::Warning
  172. ===============
  173. */
  174. void idLib::Warning( const char *fmt, ... ) {
  175. va_list argptr;
  176. char text[MAX_STRING_CHARS];
  177. va_start( argptr, fmt );
  178. idStr::vsnPrintf( text, sizeof( text ), fmt, argptr );
  179. va_end( argptr );
  180. common->Warning( "%s", text );
  181. }
  182. /*
  183. ===============
  184. idLib::WarningIf
  185. ===============
  186. */
  187. void idLib::WarningIf( const bool test, const char *fmt, ... ) {
  188. if ( !test ) {
  189. return;
  190. }
  191. va_list argptr;
  192. char text[MAX_STRING_CHARS];
  193. va_start( argptr, fmt );
  194. idStr::vsnPrintf( text, sizeof( text ), fmt, argptr );
  195. va_end( argptr );
  196. common->Warning( "%s", text );
  197. }
  198. /*
  199. ===============
  200. idLib::Printf
  201. ===============
  202. */
  203. void idLib::Printf( const char *fmt, ... ) {
  204. va_list argptr;
  205. va_start( argptr, fmt );
  206. if ( common ) {
  207. common->VPrintf( fmt, argptr );
  208. }
  209. va_end( argptr );
  210. }
  211. /*
  212. ===============
  213. idLib::PrintfIf
  214. ===============
  215. */
  216. void idLib::PrintfIf( const bool test, const char *fmt, ... ) {
  217. if ( !test ) {
  218. return;
  219. }
  220. va_list argptr;
  221. va_start( argptr, fmt );
  222. common->VPrintf( fmt, argptr );
  223. va_end( argptr );
  224. }
  225. /*
  226. ===============================================================================
  227. Byte order functions
  228. ===============================================================================
  229. */
  230. // can't just use function pointers, or dll linkage can mess up
  231. static short (*_BigShort)( short l );
  232. static short (*_LittleShort)( short l );
  233. static int (*_BigLong)( int l );
  234. static int (*_LittleLong)( int l );
  235. static float (*_BigFloat)( float l );
  236. static float (*_LittleFloat)( float l );
  237. static void (*_BigRevBytes)( void *bp, int elsize, int elcount );
  238. static void (*_LittleRevBytes)( void *bp, int elsize, int elcount );
  239. static void (*_LittleBitField)( void *bp, int elsize );
  240. static void (*_SixtetsForInt)( byte *out, int src );
  241. static int (*_IntForSixtets)( byte *in );
  242. short BigShort( short l ) { return _BigShort( l ); }
  243. short LittleShort( short l ) { return _LittleShort( l ); }
  244. int BigLong( int l ) { return _BigLong( l ); }
  245. int LittleLong( int l ) { return _LittleLong( l ); }
  246. float BigFloat( float l ) { return _BigFloat( l ); }
  247. float LittleFloat( float l ) { return _LittleFloat( l ); }
  248. void BigRevBytes( void *bp, int elsize, int elcount ) { _BigRevBytes( bp, elsize, elcount ); }
  249. void LittleRevBytes( void *bp, int elsize, int elcount ){ _LittleRevBytes( bp, elsize, elcount ); }
  250. void LittleBitField( void *bp, int elsize ){ _LittleBitField( bp, elsize ); }
  251. void SixtetsForInt( byte *out, int src) { _SixtetsForInt( out, src ); }
  252. int IntForSixtets( byte *in ) { return _IntForSixtets( in ); }
  253. /*
  254. ================
  255. ShortSwap
  256. ================
  257. */
  258. short ShortSwap( short l ) {
  259. byte b1,b2;
  260. b1 = l&255;
  261. b2 = (l>>8)&255;
  262. return (b1<<8) + b2;
  263. }
  264. /*
  265. ================
  266. ShortNoSwap
  267. ================
  268. */
  269. short ShortNoSwap( short l ) {
  270. return l;
  271. }
  272. /*
  273. ================
  274. LongSwap
  275. ================
  276. */
  277. int LongSwap ( int l ) {
  278. byte b1,b2,b3,b4;
  279. b1 = l&255;
  280. b2 = (l>>8)&255;
  281. b3 = (l>>16)&255;
  282. b4 = (l>>24)&255;
  283. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  284. }
  285. /*
  286. ================
  287. LongNoSwap
  288. ================
  289. */
  290. int LongNoSwap( int l ) {
  291. return l;
  292. }
  293. /*
  294. ================
  295. FloatSwap
  296. ================
  297. */
  298. float FloatSwap( float f ) {
  299. union {
  300. float f;
  301. byte b[4];
  302. } dat1, dat2;
  303. dat1.f = f;
  304. dat2.b[0] = dat1.b[3];
  305. dat2.b[1] = dat1.b[2];
  306. dat2.b[2] = dat1.b[1];
  307. dat2.b[3] = dat1.b[0];
  308. return dat2.f;
  309. }
  310. /*
  311. ================
  312. FloatNoSwap
  313. ================
  314. */
  315. float FloatNoSwap( float f ) {
  316. return f;
  317. }
  318. /*
  319. =====================================================================
  320. RevBytesSwap
  321. Reverses byte order in place.
  322. INPUTS
  323. bp bytes to reverse
  324. elsize size of the underlying data type
  325. elcount number of elements to swap
  326. RESULTS
  327. Reverses the byte order in each of elcount elements.
  328. ===================================================================== */
  329. void RevBytesSwap( void *bp, int elsize, int elcount ) {
  330. register unsigned char *p, *q;
  331. p = ( unsigned char * ) bp;
  332. if ( elsize == 2 ) {
  333. q = p + 1;
  334. while ( elcount-- ) {
  335. *p ^= *q;
  336. *q ^= *p;
  337. *p ^= *q;
  338. p += 2;
  339. q += 2;
  340. }
  341. return;
  342. }
  343. while ( elcount-- ) {
  344. q = p + elsize - 1;
  345. while ( p < q ) {
  346. *p ^= *q;
  347. *q ^= *p;
  348. *p ^= *q;
  349. ++p;
  350. --q;
  351. }
  352. p += elsize >> 1;
  353. }
  354. }
  355. /*
  356. =====================================================================
  357. RevBytesSwap
  358. Reverses byte order in place, then reverses bits in those bytes
  359. INPUTS
  360. bp bitfield structure to reverse
  361. elsize size of the underlying data type
  362. RESULTS
  363. Reverses the bitfield of size elsize.
  364. ===================================================================== */
  365. void RevBitFieldSwap( void *bp, int elsize) {
  366. int i;
  367. unsigned char *p, t, v;
  368. LittleRevBytes( bp, elsize, 1 );
  369. p = (unsigned char *) bp;
  370. while ( elsize-- ) {
  371. v = *p;
  372. t = 0;
  373. for (i = 7; i>=0; i--) {
  374. t <<= 1;
  375. v >>= 1;
  376. t |= v & 1;
  377. }
  378. *p++ = t;
  379. }
  380. }
  381. /*
  382. ================
  383. RevBytesNoSwap
  384. ================
  385. */
  386. void RevBytesNoSwap( void *bp, int elsize, int elcount ) {
  387. return;
  388. }
  389. /*
  390. ================
  391. RevBytesNoSwap
  392. ================
  393. */
  394. void RevBitFieldNoSwap( void *bp, int elsize ) {
  395. return;
  396. }
  397. /*
  398. ================
  399. SixtetsForIntLittle
  400. ================
  401. */
  402. void SixtetsForIntLittle( byte *out, int src) {
  403. byte *b = (byte *)&src;
  404. out[0] = ( b[0] & 0xfc ) >> 2;
  405. out[1] = ( ( b[0] & 0x3 ) << 4 ) + ( ( b[1] & 0xf0 ) >> 4 );
  406. out[2] = ( ( b[1] & 0xf ) << 2 ) + ( ( b[2] & 0xc0 ) >> 6 );
  407. out[3] = b[2] & 0x3f;
  408. }
  409. /*
  410. ================
  411. SixtetsForIntBig
  412. TTimo: untested - that's the version from initial base64 encode
  413. ================
  414. */
  415. void SixtetsForIntBig( byte *out, int src) {
  416. for( int i = 0 ; i < 4 ; i++ ) {
  417. out[i] = src & 0x3f;
  418. src >>= 6;
  419. }
  420. }
  421. /*
  422. ================
  423. IntForSixtetsLittle
  424. ================
  425. */
  426. int IntForSixtetsLittle( byte *in ) {
  427. int ret = 0;
  428. byte *b = (byte *)&ret;
  429. b[0] |= in[0] << 2;
  430. b[0] |= ( in[1] & 0x30 ) >> 4;
  431. b[1] |= ( in[1] & 0xf ) << 4;
  432. b[1] |= ( in[2] & 0x3c ) >> 2;
  433. b[2] |= ( in[2] & 0x3 ) << 6;
  434. b[2] |= in[3];
  435. return ret;
  436. }
  437. /*
  438. ================
  439. IntForSixtetsBig
  440. TTimo: untested - that's the version from initial base64 decode
  441. ================
  442. */
  443. int IntForSixtetsBig( byte *in ) {
  444. int ret = 0;
  445. ret |= in[0];
  446. ret |= in[1] << 6;
  447. ret |= in[2] << 2*6;
  448. ret |= in[3] << 3*6;
  449. return ret;
  450. }
  451. /*
  452. ================
  453. Swap_Init
  454. ================
  455. */
  456. void Swap_Init() {
  457. byte swaptest[2] = {1,0};
  458. // set the byte swapping variables in a portable manner
  459. if ( *(short *)swaptest == 1) {
  460. // little endian ex: x86
  461. _BigShort = ShortSwap;
  462. _LittleShort = ShortNoSwap;
  463. _BigLong = LongSwap;
  464. _LittleLong = LongNoSwap;
  465. _BigFloat = FloatSwap;
  466. _LittleFloat = FloatNoSwap;
  467. _BigRevBytes = RevBytesSwap;
  468. _LittleRevBytes = RevBytesNoSwap;
  469. _LittleBitField = RevBitFieldNoSwap;
  470. _SixtetsForInt = SixtetsForIntLittle;
  471. _IntForSixtets = IntForSixtetsLittle;
  472. } else {
  473. // big endian ex: ppc
  474. _BigShort = ShortNoSwap;
  475. _LittleShort = ShortSwap;
  476. _BigLong = LongNoSwap;
  477. _LittleLong = LongSwap;
  478. _BigFloat = FloatNoSwap;
  479. _LittleFloat = FloatSwap;
  480. _BigRevBytes = RevBytesNoSwap;
  481. _LittleRevBytes = RevBytesSwap;
  482. _LittleBitField = RevBitFieldSwap;
  483. _SixtetsForInt = SixtetsForIntBig;
  484. _IntForSixtets = IntForSixtetsBig;
  485. }
  486. }
  487. /*
  488. ==========
  489. Swap_IsBigEndian
  490. ==========
  491. */
  492. bool Swap_IsBigEndian() {
  493. byte swaptest[2] = {1,0};
  494. return *(short *)swaptest != 1;
  495. }
  496. /*
  497. ========================
  498. BreakOnListGrowth
  499. debug tool to find uses of idlist that are dynamically growing
  500. ========================
  501. */
  502. void BreakOnListGrowth() {
  503. }
  504. /*
  505. ========================
  506. BreakOnListDefault
  507. ========================
  508. */
  509. void BreakOnListDefault() {
  510. }