BitMsg.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  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. #ifndef __BITMSG_H__
  21. #define __BITMSG_H__
  22. /*
  23. ================================================
  24. idBitMsg operates on a sequence of individual bits. It handles byte ordering and
  25. avoids alignment errors. It allows concurrent writing and reading. The data set with Init
  26. is never free-d.
  27. ================================================
  28. */
  29. class idBitMsg {
  30. public:
  31. idBitMsg() { InitWrite( NULL, 0 ); }
  32. idBitMsg( byte * data, int length ) { InitWrite( data, length ); }
  33. idBitMsg( const byte * data, int length ) { InitRead( data, length ); }
  34. // both read & write
  35. void InitWrite( byte *data, int length );
  36. // read only
  37. void InitRead( const byte *data, int length );
  38. // get data for writing
  39. byte * GetWriteData();
  40. // get data for reading
  41. const byte * GetReadData() const;
  42. // get the maximum message size
  43. int GetMaxSize() const;
  44. // generate error if not set and message is overflowed
  45. void SetAllowOverflow( bool set );
  46. // returns true if the message was overflowed
  47. bool IsOverflowed() const;
  48. // size of the message in bytes
  49. int GetSize() const;
  50. // set the message size
  51. void SetSize( int size );
  52. // get current write bit
  53. int GetWriteBit() const;
  54. // set current write bit
  55. void SetWriteBit( int bit );
  56. // returns number of bits written
  57. int GetNumBitsWritten() const;
  58. // space left in bytes for writing
  59. int GetRemainingSpace() const;
  60. // space left in bits for writing
  61. int GetRemainingWriteBits() const;
  62. //------------------------
  63. // Write State
  64. //------------------------
  65. // save the write state
  66. void SaveWriteState( int &s, int &b, uint64 &t ) const;
  67. // restore the write state
  68. void RestoreWriteState( int s, int b, uint64 t );
  69. //------------------------
  70. // Reading
  71. //------------------------
  72. // bytes read so far
  73. int GetReadCount() const;
  74. // set the number of bytes and bits read
  75. void SetReadCount( int bytes );
  76. // get current read bit
  77. int GetReadBit() const;
  78. // set current read bit
  79. void SetReadBit( int bit );
  80. // returns number of bits read
  81. int GetNumBitsRead() const;
  82. // number of bytes left to read
  83. int GetRemainingData() const;
  84. // number of bits left to read
  85. int GetRemainingReadBits() const;
  86. // save the read state
  87. void SaveReadState( int &c, int &b ) const;
  88. // restore the read state
  89. void RestoreReadState( int c, int b );
  90. //------------------------
  91. // Writing
  92. //------------------------
  93. // begin writing
  94. void BeginWriting();
  95. // write up to the next byte boundary
  96. void WriteByteAlign();
  97. // write the specified number of bits
  98. void WriteBits( int value, int numBits );
  99. void WriteBool( bool c );
  100. void WriteChar( int8 c );
  101. void WriteByte( uint8 c );
  102. void WriteShort( int16 c );
  103. void WriteUShort( uint16 c );
  104. void WriteLong( int32 c );
  105. void WriteLongLong( int64 c );
  106. void WriteFloat( float f );
  107. void WriteFloat( float f, int exponentBits, int mantissaBits );
  108. void WriteAngle8( float f );
  109. void WriteAngle16( float f );
  110. void WriteDir( const idVec3 &dir, int numBits );
  111. void WriteString( const char *s, int maxLength = -1, bool make7Bit = true );
  112. void WriteData( const void *data, int length );
  113. void WriteNetadr( const netadr_t adr );
  114. void WriteUNorm8( float f ) { WriteByte( idMath::Ftob( f * 255.0f ) ); }
  115. void WriteUNorm16( float f ) { WriteUShort( idMath::Ftoi( f * 65535.0f ) ); }
  116. void WriteNorm16( float f ) { WriteShort( idMath::Ftoi( f * 32767.0f ) ); }
  117. void WriteDeltaChar( int8 oldValue, int8 newValue ) { WriteByte( newValue - oldValue ); }
  118. void WriteDeltaByte( uint8 oldValue, uint8 newValue ) { WriteByte( newValue - oldValue ); }
  119. void WriteDeltaShort( int16 oldValue, int16 newValue ) { WriteUShort( newValue - oldValue ); }
  120. void WriteDeltaUShort( uint16 oldValue, uint16 newValue ) { WriteUShort( newValue - oldValue ); }
  121. void WriteDeltaLong( int32 oldValue, int32 newValue ) { WriteLong( newValue - oldValue ); }
  122. void WriteDeltaFloat( float oldValue, float newValue ) { WriteFloat( newValue - oldValue ); }
  123. void WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits ) { WriteFloat( newValue - oldValue, exponentBits, mantissaBits ); }
  124. bool WriteDeltaDict( const idDict &dict, const idDict *base );
  125. template< int _max_, int _numBits_ >
  126. void WriteQuantizedFloat( float value );
  127. template< int _max_, int _numBits_ >
  128. void WriteQuantizedUFloat( float value ); // Quantize a float to a variable number of bits (assumes unsigned, uses simple quantization)
  129. template< typename T >
  130. void WriteVectorFloat( const T & v ) { for ( int i = 0; i < v.GetDimension(); i++ ) { WriteFloat( v[i] ); } }
  131. template< typename T >
  132. void WriteVectorUNorm8( const T & v ) { for ( int i = 0; i < v.GetDimension(); i++ ) { WriteUNorm8( v[i] ); } }
  133. template< typename T >
  134. void WriteVectorUNorm16( const T & v ) { for ( int i = 0; i < v.GetDimension(); i++ ) { WriteUNorm16( v[i] ); } }
  135. template< typename T >
  136. void WriteVectorNorm16( const T & v ) { for ( int i = 0; i < v.GetDimension(); i++ ) { WriteNorm16( v[i] ); } }
  137. // Compress a vector to a variable number of bits (assumes signed, uses simple quantization)
  138. template< typename T, int _max_, int _numBits_ >
  139. void WriteQuantizedVector( const T & v ) { for ( int i = 0; i < v.GetDimension(); i++ ) { WriteQuantizedFloat< _max_, _numBits_ >( v[i] ); } }
  140. // begin reading.
  141. void BeginReading() const;
  142. // read up to the next byte boundary
  143. void ReadByteAlign() const;
  144. // read the specified number of bits
  145. int ReadBits( int numBits ) const;
  146. bool ReadBool() const;
  147. int ReadChar() const;
  148. int ReadByte() const;
  149. int ReadShort() const;
  150. int ReadUShort() const;
  151. int ReadLong() const;
  152. int64 ReadLongLong() const;
  153. float ReadFloat() const;
  154. float ReadFloat( int exponentBits, int mantissaBits ) const;
  155. float ReadAngle8() const;
  156. float ReadAngle16() const;
  157. idVec3 ReadDir( int numBits ) const;
  158. int ReadString( char *buffer, int bufferSize ) const;
  159. int ReadString( idStr & str ) const;
  160. int ReadData( void *data, int length ) const;
  161. void ReadNetadr( netadr_t *adr ) const;
  162. float ReadUNorm8() const { return ReadByte() / 255.0f; }
  163. float ReadUNorm16() const { return ReadUShort() / 65535.0f; }
  164. float ReadNorm16() const { return ReadShort() / 32767.0f; }
  165. int8 ReadDeltaChar( int8 oldValue ) const { return oldValue + ReadByte(); }
  166. uint8 ReadDeltaByte( uint8 oldValue ) const { return oldValue + ReadByte(); }
  167. int16 ReadDeltaShort( int16 oldValue ) const { return oldValue + ReadUShort(); }
  168. uint16 ReadDeltaUShort( uint16 oldValue ) const { return oldValue + ReadUShort(); }
  169. int32 ReadDeltaLong( int32 oldValue ) const { return oldValue + ReadLong(); }
  170. float ReadDeltaFloat( float oldValue ) const { return oldValue + ReadFloat(); }
  171. float ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const { return oldValue + ReadFloat( exponentBits, mantissaBits ); }
  172. bool ReadDeltaDict( idDict &dict, const idDict *base ) const;
  173. template< int _max_, int _numBits_ >
  174. float ReadQuantizedFloat() const;
  175. template< int _max_, int _numBits_ >
  176. float ReadQuantizedUFloat() const;
  177. template< typename T >
  178. void ReadVectorFloat( T & v ) const { for ( int i = 0; i < v.GetDimension(); i++ ) { v[i] = ReadFloat(); } }
  179. template< typename T >
  180. void ReadVectorUNorm8( T & v ) const { for ( int i = 0; i < v.GetDimension(); i++ ) { v[i] = ReadUNorm8(); } }
  181. template< typename T >
  182. void ReadVectorUNorm16( T & v ) const { for ( int i = 0; i < v.GetDimension(); i++ ) { v[i] = ReadUNorm16(); } }
  183. template< typename T >
  184. void ReadVectorNorm16( T & v ) const { for ( int i = 0; i < v.GetDimension(); i++ ) { v[i] = ReadNorm16(); } }
  185. template< typename T, int _max_, int _numBits_ >
  186. void ReadQuantizedVector( T & v ) const { for ( int i = 0; i < v.GetDimension(); i++ ) { v[i] = ReadQuantizedFloat< _max_, _numBits_ >(); } }
  187. static int DirToBits( const idVec3 &dir, int numBits );
  188. static idVec3 BitsToDir( int bits, int numBits );
  189. void SetHasChanged( bool b ) { hasChanged = b; }
  190. bool HasChanged() const { return hasChanged; }
  191. private:
  192. byte * writeData; // pointer to data for writing
  193. const byte * readData; // pointer to data for reading
  194. int maxSize; // maximum size of message in bytes
  195. int curSize; // current size of message in bytes
  196. mutable int writeBit; // number of bits written to the last written byte
  197. mutable int readCount; // number of bytes read so far
  198. mutable int readBit; // number of bits read from the last read byte
  199. bool allowOverflow; // if false, generate error when the message is overflowed
  200. bool overflowed; // set true if buffer size failed (with allowOverflow set)
  201. bool hasChanged; // Hack
  202. mutable uint64 tempValue;
  203. private:
  204. bool CheckOverflow( int numBits );
  205. byte * GetByteSpace( int length );
  206. };
  207. /*
  208. ========================
  209. idBitMsg::InitWrite
  210. ========================
  211. */
  212. ID_INLINE void idBitMsg::InitWrite( byte *data, int length ) {
  213. writeData = data;
  214. readData = data;
  215. maxSize = length;
  216. curSize = 0;
  217. writeBit = 0;
  218. readCount = 0;
  219. readBit = 0;
  220. allowOverflow = false;
  221. overflowed = false;
  222. tempValue = 0;
  223. }
  224. /*
  225. ========================
  226. idBitMsg::InitRead
  227. ========================
  228. */
  229. ID_INLINE void idBitMsg::InitRead( const byte *data, int length ) {
  230. writeData = NULL;
  231. readData = data;
  232. maxSize = length;
  233. curSize = length;
  234. writeBit = 0;
  235. readCount = 0;
  236. readBit = 0;
  237. allowOverflow = false;
  238. overflowed = false;
  239. tempValue = 0;
  240. }
  241. /*
  242. ========================
  243. idBitMsg::GetWriteData
  244. ========================
  245. */
  246. ID_INLINE byte *idBitMsg::GetWriteData() {
  247. return writeData;
  248. }
  249. /*
  250. ========================
  251. idBitMsg::GetReadData
  252. ========================
  253. */
  254. ID_INLINE const byte *idBitMsg::GetReadData() const {
  255. return readData;
  256. }
  257. /*
  258. ========================
  259. idBitMsg::GetMaxSize
  260. ========================
  261. */
  262. ID_INLINE int idBitMsg::GetMaxSize() const {
  263. return maxSize;
  264. }
  265. /*
  266. ========================
  267. idBitMsg::SetAllowOverflow
  268. ========================
  269. */
  270. ID_INLINE void idBitMsg::SetAllowOverflow( bool set ) {
  271. allowOverflow = set;
  272. }
  273. /*
  274. ========================
  275. idBitMsg::IsOverflowed
  276. ========================
  277. */
  278. ID_INLINE bool idBitMsg::IsOverflowed() const {
  279. return overflowed;
  280. }
  281. /*
  282. ========================
  283. idBitMsg::GetSize
  284. ========================
  285. */
  286. ID_INLINE int idBitMsg::GetSize() const {
  287. return curSize + ( writeBit != 0 );
  288. }
  289. /*
  290. ========================
  291. idBitMsg::SetSize
  292. ========================
  293. */
  294. ID_INLINE void idBitMsg::SetSize( int size ) {
  295. assert( writeBit == 0 );
  296. if ( size > maxSize ) {
  297. curSize = maxSize;
  298. } else {
  299. curSize = size;
  300. }
  301. }
  302. /*
  303. ========================
  304. idBitMsg::GetWriteBit
  305. ========================
  306. */
  307. ID_INLINE int idBitMsg::GetWriteBit() const {
  308. return writeBit;
  309. }
  310. /*
  311. ========================
  312. idBitMsg::SetWriteBit
  313. ========================
  314. */
  315. ID_INLINE void idBitMsg::SetWriteBit( int bit ) {
  316. // see idBitMsg::WriteByteAlign
  317. assert( false );
  318. writeBit = bit & 7;
  319. if ( writeBit ) {
  320. writeData[curSize - 1] &= ( 1 << writeBit ) - 1;
  321. }
  322. }
  323. /*
  324. ========================
  325. idBitMsg::GetNumBitsWritten
  326. ========================
  327. */
  328. ID_INLINE int idBitMsg::GetNumBitsWritten() const {
  329. return ( curSize << 3 ) + writeBit;
  330. }
  331. /*
  332. ========================
  333. idBitMsg::GetRemainingSpace
  334. ========================
  335. */
  336. ID_INLINE int idBitMsg::GetRemainingSpace() const {
  337. return maxSize - GetSize();
  338. }
  339. /*
  340. ========================
  341. idBitMsg::GetRemainingWriteBits
  342. ========================
  343. */
  344. ID_INLINE int idBitMsg::GetRemainingWriteBits() const {
  345. return ( maxSize << 3 ) - GetNumBitsWritten();
  346. }
  347. /*
  348. ========================
  349. idBitMsg::SaveWriteState
  350. ========================
  351. */
  352. ID_INLINE void idBitMsg::SaveWriteState( int &s, int &b, uint64 &t ) const {
  353. s = curSize;
  354. b = writeBit;
  355. t = tempValue;
  356. }
  357. /*
  358. ========================
  359. idBitMsg::RestoreWriteState
  360. ========================
  361. */
  362. ID_INLINE void idBitMsg::RestoreWriteState( int s, int b, uint64 t ) {
  363. curSize = s;
  364. writeBit = b & 7;
  365. if ( writeBit ) {
  366. writeData[curSize] &= ( 1 << writeBit ) - 1;
  367. }
  368. tempValue = t;
  369. }
  370. /*
  371. ========================
  372. idBitMsg::GetReadCount
  373. ========================
  374. */
  375. ID_INLINE int idBitMsg::GetReadCount() const {
  376. return readCount;
  377. }
  378. /*
  379. ========================
  380. idBitMsg::SetReadCount
  381. ========================
  382. */
  383. ID_INLINE void idBitMsg::SetReadCount( int bytes ) {
  384. readCount = bytes;
  385. }
  386. /*
  387. ========================
  388. idBitMsg::GetReadBit
  389. ========================
  390. */
  391. ID_INLINE int idBitMsg::GetReadBit() const {
  392. return readBit;
  393. }
  394. /*
  395. ========================
  396. idBitMsg::SetReadBit
  397. ========================
  398. */
  399. ID_INLINE void idBitMsg::SetReadBit( int bit ) {
  400. readBit = bit & 7;
  401. }
  402. /*
  403. ========================
  404. idBitMsg::GetNumBitsRead
  405. ========================
  406. */
  407. ID_INLINE int idBitMsg::GetNumBitsRead() const {
  408. return ( ( readCount << 3 ) - ( ( 8 - readBit ) & 7 ) );
  409. }
  410. /*
  411. ========================
  412. idBitMsg::GetRemainingData
  413. ========================
  414. */
  415. ID_INLINE int idBitMsg::GetRemainingData() const {
  416. assert( writeBit == 0 );
  417. return curSize - readCount;
  418. }
  419. /*
  420. ========================
  421. idBitMsg::GetRemainingReadBits
  422. ========================
  423. */
  424. ID_INLINE int idBitMsg::GetRemainingReadBits() const {
  425. assert( writeBit == 0 );
  426. return ( curSize << 3 ) - GetNumBitsRead();
  427. }
  428. /*
  429. ========================
  430. idBitMsg::SaveReadState
  431. ========================
  432. */
  433. ID_INLINE void idBitMsg::SaveReadState( int &c, int &b ) const {
  434. assert( writeBit == 0 );
  435. c = readCount;
  436. b = readBit;
  437. }
  438. /*
  439. ========================
  440. idBitMsg::RestoreReadState
  441. ========================
  442. */
  443. ID_INLINE void idBitMsg::RestoreReadState( int c, int b ) {
  444. assert( writeBit == 0 );
  445. readCount = c;
  446. readBit = b & 7;
  447. }
  448. /*
  449. ========================
  450. idBitMsg::BeginWriting
  451. ========================
  452. */
  453. ID_INLINE void idBitMsg::BeginWriting() {
  454. curSize = 0;
  455. overflowed = false;
  456. writeBit = 0;
  457. tempValue = 0;
  458. }
  459. /*
  460. ========================
  461. idBitMsg::WriteByteAlign
  462. ========================
  463. */
  464. ID_INLINE void idBitMsg::WriteByteAlign() {
  465. // it is important that no uninitialized data slips in the msg stream,
  466. // because we use memcmp to decide if entities have changed and wether we should transmit them
  467. // this function has the potential to leave uninitialized bits into the stream,
  468. // however idBitMsg::WriteBits is properly initializing the byte to 0 so hopefully we are still safe
  469. // adding this extra check just in case
  470. curSize += writeBit != 0;
  471. assert( writeBit == 0 || ( ( writeData[curSize - 1] >> writeBit ) == 0 ) ); // had to early out writeBit == 0 because when writeBit == 0 writeData[curSize - 1] may be the previous byte written and trigger false positives
  472. writeBit = 0;
  473. tempValue = 0;
  474. }
  475. /*
  476. ========================
  477. idBitMsg::WriteBool
  478. ========================
  479. */
  480. ID_INLINE void idBitMsg::WriteBool( bool c ) {
  481. WriteBits( c, 1 );
  482. }
  483. /*
  484. ========================
  485. idBitMsg::WriteChar
  486. ========================
  487. */
  488. ID_INLINE void idBitMsg::WriteChar( int8 c ) {
  489. WriteBits( c, -8 );
  490. }
  491. /*
  492. ========================
  493. idBitMsg::WriteByte
  494. ========================
  495. */
  496. ID_INLINE void idBitMsg::WriteByte( uint8 c ) {
  497. WriteBits( c, 8 );
  498. }
  499. /*
  500. ========================
  501. idBitMsg::WriteShort
  502. ========================
  503. */
  504. ID_INLINE void idBitMsg::WriteShort( int16 c ) {
  505. WriteBits( c, -16 );
  506. }
  507. /*
  508. ========================
  509. idBitMsg::WriteUShort
  510. ========================
  511. */
  512. ID_INLINE void idBitMsg::WriteUShort( uint16 c ) {
  513. WriteBits( c, 16 );
  514. }
  515. /*
  516. ========================
  517. idBitMsg::WriteLong
  518. ========================
  519. */
  520. ID_INLINE void idBitMsg::WriteLong( int32 c ) {
  521. WriteBits( c, 32 );
  522. }
  523. /*
  524. ========================
  525. idBitMsg::WriteLongLong
  526. ========================
  527. */
  528. ID_INLINE void idBitMsg::WriteLongLong( int64 c ) {
  529. int a = c;
  530. int b = c >> 32;
  531. WriteBits( a, 32 );
  532. WriteBits( b, 32 );
  533. }
  534. /*
  535. ========================
  536. idBitMsg::WriteFloat
  537. ========================
  538. */
  539. ID_INLINE void idBitMsg::WriteFloat( float f ) {
  540. WriteBits( *reinterpret_cast<int *>(&f), 32 );
  541. }
  542. /*
  543. ========================
  544. idBitMsg::WriteFloat
  545. ========================
  546. */
  547. ID_INLINE void idBitMsg::WriteFloat( float f, int exponentBits, int mantissaBits ) {
  548. int bits = idMath::FloatToBits( f, exponentBits, mantissaBits );
  549. WriteBits( bits, 1 + exponentBits + mantissaBits );
  550. }
  551. /*
  552. ========================
  553. idBitMsg::WriteAngle8
  554. ========================
  555. */
  556. ID_INLINE void idBitMsg::WriteAngle8( float f ) {
  557. WriteByte( ANGLE2BYTE( f ) );
  558. }
  559. /*
  560. ========================
  561. idBitMsg::WriteAngle16
  562. ========================
  563. */
  564. ID_INLINE void idBitMsg::WriteAngle16( float f ) {
  565. WriteShort( ANGLE2SHORT(f) );
  566. }
  567. /*
  568. ========================
  569. idBitMsg::WriteDir
  570. ========================
  571. */
  572. ID_INLINE void idBitMsg::WriteDir( const idVec3 &dir, int numBits ) {
  573. WriteBits( DirToBits( dir, numBits ), numBits );
  574. }
  575. /*
  576. ========================
  577. idBitMsg::BeginReading
  578. ========================
  579. */
  580. ID_INLINE void idBitMsg::BeginReading() const {
  581. readCount = 0;
  582. readBit = 0;
  583. writeBit = 0;
  584. tempValue = 0;
  585. }
  586. /*
  587. ========================
  588. idBitMsg::ReadByteAlign
  589. ========================
  590. */
  591. ID_INLINE void idBitMsg::ReadByteAlign() const {
  592. readBit = 0;
  593. }
  594. /*
  595. ========================
  596. idBitMsg::ReadBool
  597. ========================
  598. */
  599. ID_INLINE bool idBitMsg::ReadBool() const {
  600. return ( ReadBits( 1 ) == 1 ) ? true : false;
  601. }
  602. /*
  603. ========================
  604. idBitMsg::ReadChar
  605. ========================
  606. */
  607. ID_INLINE int idBitMsg::ReadChar() const {
  608. return (signed char)ReadBits( -8 );
  609. }
  610. /*
  611. ========================
  612. idBitMsg::ReadByte
  613. ========================
  614. */
  615. ID_INLINE int idBitMsg::ReadByte() const {
  616. return (unsigned char)ReadBits( 8 );
  617. }
  618. /*
  619. ========================
  620. idBitMsg::ReadShort
  621. ========================
  622. */
  623. ID_INLINE int idBitMsg::ReadShort() const {
  624. return (short)ReadBits( -16 );
  625. }
  626. /*
  627. ========================
  628. idBitMsg::ReadUShort
  629. ========================
  630. */
  631. ID_INLINE int idBitMsg::ReadUShort() const {
  632. return (unsigned short)ReadBits( 16 );
  633. }
  634. /*
  635. ========================
  636. idBitMsg::ReadLong
  637. ========================
  638. */
  639. ID_INLINE int idBitMsg::ReadLong() const {
  640. return ReadBits( 32 );
  641. }
  642. /*
  643. ========================
  644. idBitMsg::ReadLongLong
  645. ========================
  646. */
  647. ID_INLINE int64 idBitMsg::ReadLongLong() const {
  648. int64 a = ReadBits( 32 );
  649. int64 b = ReadBits( 32 );
  650. int64 c = ( 0x00000000ffffffff & a ) | ( b << 32 );
  651. return c;
  652. }
  653. /*
  654. ========================
  655. idBitMsg::ReadFloat
  656. ========================
  657. */
  658. ID_INLINE float idBitMsg::ReadFloat() const {
  659. float value;
  660. *reinterpret_cast<int *>(&value) = ReadBits( 32 );
  661. return value;
  662. }
  663. /*
  664. ========================
  665. idBitMsg::ReadFloat
  666. ========================
  667. */
  668. ID_INLINE float idBitMsg::ReadFloat( int exponentBits, int mantissaBits ) const {
  669. int bits = ReadBits( 1 + exponentBits + mantissaBits );
  670. return idMath::BitsToFloat( bits, exponentBits, mantissaBits );
  671. }
  672. /*
  673. ========================
  674. idBitMsg::ReadAngle8
  675. ========================
  676. */
  677. ID_INLINE float idBitMsg::ReadAngle8() const {
  678. return BYTE2ANGLE( ReadByte() );
  679. }
  680. /*
  681. ========================
  682. idBitMsg::ReadAngle16
  683. ========================
  684. */
  685. ID_INLINE float idBitMsg::ReadAngle16() const {
  686. return SHORT2ANGLE( ReadShort() );
  687. }
  688. /*
  689. ========================
  690. idBitMsg::ReadDir
  691. ========================
  692. */
  693. ID_INLINE idVec3 idBitMsg::ReadDir( int numBits ) const {
  694. return BitsToDir( ReadBits( numBits ), numBits );
  695. }
  696. /*
  697. ========================
  698. idBitMsg::WriteQuantizedFloat
  699. ========================
  700. */
  701. template< int _max_, int _numBits_ >
  702. ID_INLINE void idBitMsg::WriteQuantizedFloat( float value ) {
  703. enum { storeMax = ( 1 << ( _numBits_ - 1 ) ) - 1 };
  704. if ( _max_ > storeMax ) {
  705. // Scaling down (scale should be < 1)
  706. const float scale = (float)storeMax / (float)_max_;
  707. WriteBits( idMath::ClampInt( -storeMax, storeMax, idMath::Ftoi( value * scale ) ), -_numBits_ );
  708. } else {
  709. // Scaling up (scale should be >= 1) (Preserve whole numbers when possible)
  710. enum { scale = storeMax / _max_ };
  711. WriteBits( idMath::ClampInt( -storeMax, storeMax, idMath::Ftoi( value * scale ) ), -_numBits_ );
  712. }
  713. }
  714. /*
  715. ========================
  716. idBitMsg::WriteQuantizedUFloat
  717. ========================
  718. */
  719. template< int _max_, int _numBits_ >
  720. ID_INLINE void idBitMsg::WriteQuantizedUFloat( float value ) {
  721. enum { storeMax = ( 1 << _numBits_ ) - 1 };
  722. if ( _max_ > storeMax ) {
  723. // Scaling down (scale should be < 1)
  724. const float scale = (float)storeMax / (float)_max_;
  725. WriteBits( idMath::ClampInt( 0, storeMax, idMath::Ftoi( value * scale ) ), _numBits_ );
  726. } else {
  727. // Scaling up (scale should be >= 1) (Preserve whole numbers when possible)
  728. enum { scale = storeMax / _max_ };
  729. WriteBits( idMath::ClampInt( 0, storeMax, idMath::Ftoi( value * scale ) ), _numBits_ );
  730. }
  731. }
  732. /*
  733. ========================
  734. idBitMsg::ReadQuantizedFloat
  735. ========================
  736. */
  737. template< int _max_, int _numBits_ >
  738. ID_INLINE float idBitMsg::ReadQuantizedFloat() const {
  739. enum { storeMax = ( 1 << ( _numBits_ - 1 ) ) - 1 };
  740. if ( _max_ > storeMax ) {
  741. // Scaling down (scale should be < 1)
  742. const float invScale = (float)_max_ / (float)storeMax;
  743. return (float)ReadBits( -_numBits_ ) * invScale;
  744. } else {
  745. // Scaling up (scale should be >= 1) (Preserve whole numbers when possible)
  746. // Scale will be a whole number.
  747. // We use a float to get rid of (potential divide by zero) which is handled above, but the compiler is dumb
  748. const float scale = storeMax / _max_;
  749. const float invScale = 1.0f / scale;
  750. return (float)ReadBits( -_numBits_ ) * invScale;
  751. }
  752. }
  753. /*
  754. ========================
  755. idBitMsg::ReadQuantizedUFloat
  756. ========================
  757. */
  758. template< int _max_, int _numBits_ >
  759. float idBitMsg::ReadQuantizedUFloat() const {
  760. enum { storeMax = ( 1 << _numBits_ ) - 1 };
  761. if ( _max_ > storeMax ) {
  762. // Scaling down (scale should be < 1)
  763. const float invScale = (float)_max_ / (float)storeMax;
  764. return (float)ReadBits( _numBits_ ) * invScale;
  765. } else {
  766. // Scaling up (scale should be >= 1) (Preserve whole numbers when possible)
  767. // Scale will be a whole number.
  768. // We use a float to get rid of (potential divide by zero) which is handled above, but the compiler is dumb
  769. const float scale = storeMax / _max_;
  770. const float invScale = 1.0f / scale;
  771. return (float)ReadBits( _numBits_ ) * invScale;
  772. }
  773. }
  774. /*
  775. ================
  776. WriteFloatArray
  777. Writes all the values from the array to the bit message.
  778. ================
  779. */
  780. template< class _arrayType_ >
  781. void WriteFloatArray( idBitMsg & message, const _arrayType_ & sourceArray ) {
  782. for( int i = 0; i < idTupleSize< _arrayType_ >::value; ++i ) {
  783. message.WriteFloat( sourceArray[i] );
  784. }
  785. }
  786. /*
  787. ================
  788. WriteFloatArrayDelta
  789. Writes _num_ values from the array to the bit message.
  790. ================
  791. */
  792. template< class _arrayType_ >
  793. void WriteDeltaFloatArray( idBitMsg & message, const _arrayType_ & oldArray, const _arrayType_ & newArray ) {
  794. for( int i = 0; i < idTupleSize< _arrayType_ >::value; ++i ) {
  795. message.WriteDeltaFloat( oldArray[i], newArray[i] );
  796. }
  797. }
  798. /*
  799. ================
  800. ReadFloatArray
  801. Reads _num_ values from the array to the bit message.
  802. ================
  803. */
  804. template< class _arrayType_ >
  805. _arrayType_ ReadFloatArray( const idBitMsg & message ) {
  806. _arrayType_ result;
  807. for( int i = 0; i < idTupleSize< _arrayType_ >::value; ++i ) {
  808. result[i] = message.ReadFloat();
  809. }
  810. return result;
  811. }
  812. /*
  813. ================
  814. ReadDeltaFloatArray
  815. Reads _num_ values from the array to the bit message.
  816. ================
  817. */
  818. template< class _arrayType_ >
  819. _arrayType_ ReadDeltaFloatArray( const idBitMsg & message, const _arrayType_ & oldArray ) {
  820. _arrayType_ result;
  821. for( int i = 0; i < idTupleSize< _arrayType_ >::value; ++i ) {
  822. result[i] = message.ReadDeltaFloat( oldArray[i] );
  823. }
  824. return result;
  825. }
  826. #endif /* !__BITMSG_H__ */