BitMsg.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 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 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 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 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 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
  25. Handles byte ordering and avoids alignment errors.
  26. Allows concurrent writing and reading.
  27. The data set with Init is never freed.
  28. ===============================================================================
  29. */
  30. class idBitMsg {
  31. public:
  32. idBitMsg();
  33. ~idBitMsg() {}
  34. void Init( byte *data, int length );
  35. void Init( const byte *data, int length );
  36. byte * GetData( void ); // get data for writing
  37. const byte * GetData( void ) const; // get data for reading
  38. int GetMaxSize( void ) const; // get the maximum message size
  39. void SetAllowOverflow( bool set ); // generate error if not set and message is overflowed
  40. bool IsOverflowed( void ) const; // returns true if the message was overflowed
  41. int GetSize( void ) const; // size of the message in bytes
  42. void SetSize( int size ); // set the message size
  43. int GetWriteBit( void ) const; // get current write bit
  44. void SetWriteBit( int bit ); // set current write bit
  45. int GetNumBitsWritten( void ) const; // returns number of bits written
  46. int GetRemainingWriteBits( void ) const; // space left in bits for writing
  47. void SaveWriteState( int &s, int &b ) const; // save the write state
  48. void RestoreWriteState( int s, int b ); // restore the write state
  49. int GetReadCount( void ) const; // bytes read so far
  50. void SetReadCount( int bytes ); // set the number of bytes and bits read
  51. int GetReadBit( void ) const; // get current read bit
  52. void SetReadBit( int bit ); // set current read bit
  53. int GetNumBitsRead( void ) const; // returns number of bits read
  54. int GetRemainingReadBits( void ) const; // number of bits left to read
  55. void SaveReadState( int &c, int &b ) const; // save the read state
  56. void RestoreReadState( int c, int b ); // restore the read state
  57. void BeginWriting( void ); // begin writing
  58. int GetRemainingSpace( void ) const; // space left in bytes
  59. void WriteByteAlign( void ); // write up to the next byte boundary
  60. void WriteBits( int value, int numBits ); // write the specified number of bits
  61. void WriteChar( int c );
  62. void WriteByte( int c );
  63. void WriteShort( int c );
  64. void WriteUShort( int c );
  65. void WriteLong( int c );
  66. void WriteFloat( float f );
  67. void WriteFloat( float f, int exponentBits, int mantissaBits );
  68. void WriteAngle8( float f );
  69. void WriteAngle16( float f );
  70. void WriteDir( const idVec3 &dir, int numBits );
  71. void WriteString( const char *s, int maxLength = -1, bool make7Bit = true );
  72. void WriteData( const void *data, int length );
  73. void WriteNetadr( const netadr_t adr );
  74. void WriteDeltaChar( int oldValue, int newValue );
  75. void WriteDeltaByte( int oldValue, int newValue );
  76. void WriteDeltaShort( int oldValue, int newValue );
  77. void WriteDeltaLong( int oldValue, int newValue );
  78. void WriteDeltaFloat( float oldValue, float newValue );
  79. void WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits );
  80. void WriteDeltaByteCounter( int oldValue, int newValue );
  81. void WriteDeltaShortCounter( int oldValue, int newValue );
  82. void WriteDeltaLongCounter( int oldValue, int newValue );
  83. bool WriteDeltaDict( const idDict &dict, const idDict *base );
  84. void BeginReading( void ) const; // begin reading.
  85. int GetRemaingData( void ) const; // number of bytes left to read
  86. void ReadByteAlign( void ) const; // read up to the next byte boundary
  87. int ReadBits( int numBits ) const; // read the specified number of bits
  88. int ReadChar( void ) const;
  89. int ReadByte( void ) const;
  90. int ReadShort( void ) const;
  91. int ReadUShort( void ) const;
  92. int ReadLong( void ) const;
  93. float ReadFloat( void ) const;
  94. float ReadFloat( int exponentBits, int mantissaBits ) const;
  95. float ReadAngle8( void ) const;
  96. float ReadAngle16( void ) const;
  97. idVec3 ReadDir( int numBits ) const;
  98. int ReadString( char *buffer, int bufferSize ) const;
  99. int ReadData( void *data, int length ) const;
  100. void ReadNetadr( netadr_t *adr ) const;
  101. int ReadDeltaChar( int oldValue ) const;
  102. int ReadDeltaByte( int oldValue ) const;
  103. int ReadDeltaShort( int oldValue ) const;
  104. int ReadDeltaLong( int oldValue ) const;
  105. float ReadDeltaFloat( float oldValue ) const;
  106. float ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const;
  107. int ReadDeltaByteCounter( int oldValue ) const;
  108. int ReadDeltaShortCounter( int oldValue ) const;
  109. int ReadDeltaLongCounter( int oldValue ) const;
  110. bool ReadDeltaDict( idDict &dict, const idDict *base ) const;
  111. static int DirToBits( const idVec3 &dir, int numBits );
  112. static idVec3 BitsToDir( int bits, int numBits );
  113. private:
  114. byte * writeData; // pointer to data for writing
  115. const byte * readData; // pointer to data for reading
  116. int maxSize; // maximum size of message in bytes
  117. int curSize; // current size of message in bytes
  118. int writeBit; // number of bits written to the last written byte
  119. mutable int readCount; // number of bytes read so far
  120. mutable int readBit; // number of bits read from the last read byte
  121. bool allowOverflow; // if false, generate an error when the message is overflowed
  122. bool overflowed; // set to true if the buffer size failed (with allowOverflow set)
  123. private:
  124. bool CheckOverflow( int numBits );
  125. byte * GetByteSpace( int length );
  126. void WriteDelta( int oldValue, int newValue, int numBits );
  127. int ReadDelta( int oldValue, int numBits ) const;
  128. };
  129. ID_INLINE void idBitMsg::Init( byte *data, int length ) {
  130. writeData = data;
  131. readData = data;
  132. maxSize = length;
  133. }
  134. ID_INLINE void idBitMsg::Init( const byte *data, int length ) {
  135. writeData = NULL;
  136. readData = data;
  137. maxSize = length;
  138. }
  139. ID_INLINE byte *idBitMsg::GetData( void ) {
  140. return writeData;
  141. }
  142. ID_INLINE const byte *idBitMsg::GetData( void ) const {
  143. return readData;
  144. }
  145. ID_INLINE int idBitMsg::GetMaxSize( void ) const {
  146. return maxSize;
  147. }
  148. ID_INLINE void idBitMsg::SetAllowOverflow( bool set ) {
  149. allowOverflow = set;
  150. }
  151. ID_INLINE bool idBitMsg::IsOverflowed( void ) const {
  152. return overflowed;
  153. }
  154. ID_INLINE int idBitMsg::GetSize( void ) const {
  155. return curSize;
  156. }
  157. ID_INLINE void idBitMsg::SetSize( int size ) {
  158. if ( size > maxSize ) {
  159. curSize = maxSize;
  160. } else {
  161. curSize = size;
  162. }
  163. }
  164. ID_INLINE int idBitMsg::GetWriteBit( void ) const {
  165. return writeBit;
  166. }
  167. ID_INLINE void idBitMsg::SetWriteBit( int bit ) {
  168. writeBit = bit & 7;
  169. if ( writeBit ) {
  170. writeData[curSize - 1] &= ( 1 << writeBit ) - 1;
  171. }
  172. }
  173. ID_INLINE int idBitMsg::GetNumBitsWritten( void ) const {
  174. return ( ( curSize << 3 ) - ( ( 8 - writeBit ) & 7 ) );
  175. }
  176. ID_INLINE int idBitMsg::GetRemainingWriteBits( void ) const {
  177. return ( maxSize << 3 ) - GetNumBitsWritten();
  178. }
  179. ID_INLINE void idBitMsg::SaveWriteState( int &s, int &b ) const {
  180. s = curSize;
  181. b = writeBit;
  182. }
  183. ID_INLINE void idBitMsg::RestoreWriteState( int s, int b ) {
  184. curSize = s;
  185. writeBit = b & 7;
  186. if ( writeBit ) {
  187. writeData[curSize - 1] &= ( 1 << writeBit ) - 1;
  188. }
  189. }
  190. ID_INLINE int idBitMsg::GetReadCount( void ) const {
  191. return readCount;
  192. }
  193. ID_INLINE void idBitMsg::SetReadCount( int bytes ) {
  194. readCount = bytes;
  195. }
  196. ID_INLINE int idBitMsg::GetReadBit( void ) const {
  197. return readBit;
  198. }
  199. ID_INLINE void idBitMsg::SetReadBit( int bit ) {
  200. readBit = bit & 7;
  201. }
  202. ID_INLINE int idBitMsg::GetNumBitsRead( void ) const {
  203. return ( ( readCount << 3 ) - ( ( 8 - readBit ) & 7 ) );
  204. }
  205. ID_INLINE int idBitMsg::GetRemainingReadBits( void ) const {
  206. return ( curSize << 3 ) - GetNumBitsRead();
  207. }
  208. ID_INLINE void idBitMsg::SaveReadState( int &c, int &b ) const {
  209. c = readCount;
  210. b = readBit;
  211. }
  212. ID_INLINE void idBitMsg::RestoreReadState( int c, int b ) {
  213. readCount = c;
  214. readBit = b & 7;
  215. }
  216. ID_INLINE void idBitMsg::BeginWriting( void ) {
  217. curSize = 0;
  218. overflowed = false;
  219. writeBit = 0;
  220. }
  221. ID_INLINE int idBitMsg::GetRemainingSpace( void ) const {
  222. return maxSize - curSize;
  223. }
  224. ID_INLINE void idBitMsg::WriteByteAlign( void ) {
  225. writeBit = 0;
  226. }
  227. ID_INLINE void idBitMsg::WriteChar( int c ) {
  228. WriteBits( c, -8 );
  229. }
  230. ID_INLINE void idBitMsg::WriteByte( int c ) {
  231. WriteBits( c, 8 );
  232. }
  233. ID_INLINE void idBitMsg::WriteShort( int c ) {
  234. WriteBits( c, -16 );
  235. }
  236. ID_INLINE void idBitMsg::WriteUShort( int c ) {
  237. WriteBits( c, 16 );
  238. }
  239. ID_INLINE void idBitMsg::WriteLong( int c ) {
  240. WriteBits( c, 32 );
  241. }
  242. ID_INLINE void idBitMsg::WriteFloat( float f ) {
  243. WriteBits( *reinterpret_cast<int *>(&f), 32 );
  244. }
  245. ID_INLINE void idBitMsg::WriteFloat( float f, int exponentBits, int mantissaBits ) {
  246. int bits = idMath::FloatToBits( f, exponentBits, mantissaBits );
  247. WriteBits( bits, 1 + exponentBits + mantissaBits );
  248. }
  249. ID_INLINE void idBitMsg::WriteAngle8( float f ) {
  250. WriteByte( ANGLE2BYTE( f ) );
  251. }
  252. ID_INLINE void idBitMsg::WriteAngle16( float f ) {
  253. WriteShort( ANGLE2SHORT(f) );
  254. }
  255. ID_INLINE void idBitMsg::WriteDir( const idVec3 &dir, int numBits ) {
  256. WriteBits( DirToBits( dir, numBits ), numBits );
  257. }
  258. ID_INLINE void idBitMsg::WriteDeltaChar( int oldValue, int newValue ) {
  259. WriteDelta( oldValue, newValue, -8 );
  260. }
  261. ID_INLINE void idBitMsg::WriteDeltaByte( int oldValue, int newValue ) {
  262. WriteDelta( oldValue, newValue, 8 );
  263. }
  264. ID_INLINE void idBitMsg::WriteDeltaShort( int oldValue, int newValue ) {
  265. WriteDelta( oldValue, newValue, -16 );
  266. }
  267. ID_INLINE void idBitMsg::WriteDeltaLong( int oldValue, int newValue ) {
  268. WriteDelta( oldValue, newValue, 32 );
  269. }
  270. ID_INLINE void idBitMsg::WriteDeltaFloat( float oldValue, float newValue ) {
  271. WriteDelta( *reinterpret_cast<int *>(&oldValue), *reinterpret_cast<int *>(&newValue), 32 );
  272. }
  273. ID_INLINE void idBitMsg::WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits ) {
  274. int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  275. int newBits = idMath::FloatToBits( newValue, exponentBits, mantissaBits );
  276. WriteDelta( oldBits, newBits, 1 + exponentBits + mantissaBits );
  277. }
  278. ID_INLINE void idBitMsg::BeginReading( void ) const {
  279. readCount = 0;
  280. readBit = 0;
  281. }
  282. ID_INLINE int idBitMsg::GetRemaingData( void ) const {
  283. return curSize - readCount;
  284. }
  285. ID_INLINE void idBitMsg::ReadByteAlign( void ) const {
  286. readBit = 0;
  287. }
  288. ID_INLINE int idBitMsg::ReadChar( void ) const {
  289. return (signed char)ReadBits( -8 );
  290. }
  291. ID_INLINE int idBitMsg::ReadByte( void ) const {
  292. return (unsigned char)ReadBits( 8 );
  293. }
  294. ID_INLINE int idBitMsg::ReadShort( void ) const {
  295. return (short)ReadBits( -16 );
  296. }
  297. ID_INLINE int idBitMsg::ReadUShort( void ) const {
  298. return (unsigned short)ReadBits( 16 );
  299. }
  300. ID_INLINE int idBitMsg::ReadLong( void ) const {
  301. return ReadBits( 32 );
  302. }
  303. ID_INLINE float idBitMsg::ReadFloat( void ) const {
  304. float value;
  305. *reinterpret_cast<int *>(&value) = ReadBits( 32 );
  306. return value;
  307. }
  308. ID_INLINE float idBitMsg::ReadFloat( int exponentBits, int mantissaBits ) const {
  309. int bits = ReadBits( 1 + exponentBits + mantissaBits );
  310. return idMath::BitsToFloat( bits, exponentBits, mantissaBits );
  311. }
  312. ID_INLINE float idBitMsg::ReadAngle8( void ) const {
  313. return BYTE2ANGLE( ReadByte() );
  314. }
  315. ID_INLINE float idBitMsg::ReadAngle16( void ) const {
  316. return SHORT2ANGLE( ReadShort() );
  317. }
  318. ID_INLINE idVec3 idBitMsg::ReadDir( int numBits ) const {
  319. return BitsToDir( ReadBits( numBits ), numBits );
  320. }
  321. ID_INLINE int idBitMsg::ReadDeltaChar( int oldValue ) const {
  322. return (signed char)ReadDelta( oldValue, -8 );
  323. }
  324. ID_INLINE int idBitMsg::ReadDeltaByte( int oldValue ) const {
  325. return (unsigned char)ReadDelta( oldValue, 8 );
  326. }
  327. ID_INLINE int idBitMsg::ReadDeltaShort( int oldValue ) const {
  328. return (short)ReadDelta( oldValue, -16 );
  329. }
  330. ID_INLINE int idBitMsg::ReadDeltaLong( int oldValue ) const {
  331. return ReadDelta( oldValue, 32 );
  332. }
  333. ID_INLINE float idBitMsg::ReadDeltaFloat( float oldValue ) const {
  334. float value;
  335. *reinterpret_cast<int *>(&value) = ReadDelta( *reinterpret_cast<int *>(&oldValue), 32 );
  336. return value;
  337. }
  338. ID_INLINE float idBitMsg::ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const {
  339. int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  340. int newBits = ReadDelta( oldBits, 1 + exponentBits + mantissaBits );
  341. return idMath::BitsToFloat( newBits, exponentBits, mantissaBits );
  342. }
  343. /*
  344. ===============================================================================
  345. idBitMsgDelta
  346. ===============================================================================
  347. */
  348. class idBitMsgDelta {
  349. public:
  350. idBitMsgDelta();
  351. ~idBitMsgDelta() {}
  352. void Init( const idBitMsg *base, idBitMsg *newBase, idBitMsg *delta );
  353. void Init( const idBitMsg *base, idBitMsg *newBase, const idBitMsg *delta );
  354. bool HasChanged( void ) const;
  355. void WriteBits( int value, int numBits );
  356. void WriteChar( int c );
  357. void WriteByte( int c );
  358. void WriteShort( int c );
  359. void WriteUShort( int c );
  360. void WriteLong( int c );
  361. void WriteFloat( float f );
  362. void WriteFloat( float f, int exponentBits, int mantissaBits );
  363. void WriteAngle8( float f );
  364. void WriteAngle16( float f );
  365. void WriteDir( const idVec3 &dir, int numBits );
  366. void WriteString( const char *s, int maxLength = -1 );
  367. void WriteData( const void *data, int length );
  368. void WriteDict( const idDict &dict );
  369. void WriteDeltaChar( int oldValue, int newValue );
  370. void WriteDeltaByte( int oldValue, int newValue );
  371. void WriteDeltaShort( int oldValue, int newValue );
  372. void WriteDeltaLong( int oldValue, int newValue );
  373. void WriteDeltaFloat( float oldValue, float newValue );
  374. void WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits );
  375. void WriteDeltaByteCounter( int oldValue, int newValue );
  376. void WriteDeltaShortCounter( int oldValue, int newValue );
  377. void WriteDeltaLongCounter( int oldValue, int newValue );
  378. int ReadBits( int numBits ) const;
  379. int ReadChar( void ) const;
  380. int ReadByte( void ) const;
  381. int ReadShort( void ) const;
  382. int ReadUShort( void ) const;
  383. int ReadLong( void ) const;
  384. float ReadFloat( void ) const;
  385. float ReadFloat( int exponentBits, int mantissaBits ) const;
  386. float ReadAngle8( void ) const;
  387. float ReadAngle16( void ) const;
  388. idVec3 ReadDir( int numBits ) const;
  389. void ReadString( char *buffer, int bufferSize ) const;
  390. void ReadData( void *data, int length ) const;
  391. void ReadDict( idDict &dict );
  392. int ReadDeltaChar( int oldValue ) const;
  393. int ReadDeltaByte( int oldValue ) const;
  394. int ReadDeltaShort( int oldValue ) const;
  395. int ReadDeltaLong( int oldValue ) const;
  396. float ReadDeltaFloat( float oldValue ) const;
  397. float ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const;
  398. int ReadDeltaByteCounter( int oldValue ) const;
  399. int ReadDeltaShortCounter( int oldValue ) const;
  400. int ReadDeltaLongCounter( int oldValue ) const;
  401. private:
  402. const idBitMsg *base; // base
  403. idBitMsg * newBase; // new base
  404. idBitMsg * writeDelta; // delta from base to new base for writing
  405. const idBitMsg *readDelta; // delta from base to new base for reading
  406. mutable bool changed; // true if the new base is different from the base
  407. private:
  408. void WriteDelta( int oldValue, int newValue, int numBits );
  409. int ReadDelta( int oldValue, int numBits ) const;
  410. };
  411. ID_INLINE idBitMsgDelta::idBitMsgDelta() {
  412. base = NULL;
  413. newBase = NULL;
  414. writeDelta = NULL;
  415. readDelta = NULL;
  416. changed = false;
  417. }
  418. ID_INLINE void idBitMsgDelta::Init( const idBitMsg *base, idBitMsg *newBase, idBitMsg *delta ) {
  419. this->base = base;
  420. this->newBase = newBase;
  421. this->writeDelta = delta;
  422. this->readDelta = delta;
  423. this->changed = false;
  424. }
  425. ID_INLINE void idBitMsgDelta::Init( const idBitMsg *base, idBitMsg *newBase, const idBitMsg *delta ) {
  426. this->base = base;
  427. this->newBase = newBase;
  428. this->writeDelta = NULL;
  429. this->readDelta = delta;
  430. this->changed = false;
  431. }
  432. ID_INLINE bool idBitMsgDelta::HasChanged( void ) const {
  433. return changed;
  434. }
  435. ID_INLINE void idBitMsgDelta::WriteChar( int c ) {
  436. WriteBits( c, -8 );
  437. }
  438. ID_INLINE void idBitMsgDelta::WriteByte( int c ) {
  439. WriteBits( c, 8 );
  440. }
  441. ID_INLINE void idBitMsgDelta::WriteShort( int c ) {
  442. WriteBits( c, -16 );
  443. }
  444. ID_INLINE void idBitMsgDelta::WriteUShort( int c ) {
  445. WriteBits( c, 16 );
  446. }
  447. ID_INLINE void idBitMsgDelta::WriteLong( int c ) {
  448. WriteBits( c, 32 );
  449. }
  450. ID_INLINE void idBitMsgDelta::WriteFloat( float f ) {
  451. WriteBits( *reinterpret_cast<int *>(&f), 32 );
  452. }
  453. ID_INLINE void idBitMsgDelta::WriteFloat( float f, int exponentBits, int mantissaBits ) {
  454. int bits = idMath::FloatToBits( f, exponentBits, mantissaBits );
  455. WriteBits( bits, 1 + exponentBits + mantissaBits );
  456. }
  457. ID_INLINE void idBitMsgDelta::WriteAngle8( float f ) {
  458. WriteBits( ANGLE2BYTE( f ), 8 );
  459. }
  460. ID_INLINE void idBitMsgDelta::WriteAngle16( float f ) {
  461. WriteBits( ANGLE2SHORT(f), 16 );
  462. }
  463. ID_INLINE void idBitMsgDelta::WriteDir( const idVec3 &dir, int numBits ) {
  464. WriteBits( idBitMsg::DirToBits( dir, numBits ), numBits );
  465. }
  466. ID_INLINE void idBitMsgDelta::WriteDeltaChar( int oldValue, int newValue ) {
  467. WriteDelta( oldValue, newValue, -8 );
  468. }
  469. ID_INLINE void idBitMsgDelta::WriteDeltaByte( int oldValue, int newValue ) {
  470. WriteDelta( oldValue, newValue, 8 );
  471. }
  472. ID_INLINE void idBitMsgDelta::WriteDeltaShort( int oldValue, int newValue ) {
  473. WriteDelta( oldValue, newValue, -16 );
  474. }
  475. ID_INLINE void idBitMsgDelta::WriteDeltaLong( int oldValue, int newValue ) {
  476. WriteDelta( oldValue, newValue, 32 );
  477. }
  478. ID_INLINE void idBitMsgDelta::WriteDeltaFloat( float oldValue, float newValue ) {
  479. WriteDelta( *reinterpret_cast<int *>(&oldValue), *reinterpret_cast<int *>(&newValue), 32 );
  480. }
  481. ID_INLINE void idBitMsgDelta::WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits ) {
  482. int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  483. int newBits = idMath::FloatToBits( newValue, exponentBits, mantissaBits );
  484. WriteDelta( oldBits, newBits, 1 + exponentBits + mantissaBits );
  485. }
  486. ID_INLINE int idBitMsgDelta::ReadChar( void ) const {
  487. return (signed char)ReadBits( -8 );
  488. }
  489. ID_INLINE int idBitMsgDelta::ReadByte( void ) const {
  490. return (unsigned char)ReadBits( 8 );
  491. }
  492. ID_INLINE int idBitMsgDelta::ReadShort( void ) const {
  493. return (short)ReadBits( -16 );
  494. }
  495. ID_INLINE int idBitMsgDelta::ReadUShort( void ) const {
  496. return (unsigned short)ReadBits( 16 );
  497. }
  498. ID_INLINE int idBitMsgDelta::ReadLong( void ) const {
  499. return ReadBits( 32 );
  500. }
  501. ID_INLINE float idBitMsgDelta::ReadFloat( void ) const {
  502. float value;
  503. *reinterpret_cast<int *>(&value) = ReadBits( 32 );
  504. return value;
  505. }
  506. ID_INLINE float idBitMsgDelta::ReadFloat( int exponentBits, int mantissaBits ) const {
  507. int bits = ReadBits( 1 + exponentBits + mantissaBits );
  508. return idMath::BitsToFloat( bits, exponentBits, mantissaBits );
  509. }
  510. ID_INLINE float idBitMsgDelta::ReadAngle8( void ) const {
  511. return BYTE2ANGLE( ReadByte() );
  512. }
  513. ID_INLINE float idBitMsgDelta::ReadAngle16( void ) const {
  514. return SHORT2ANGLE( ReadShort() );
  515. }
  516. ID_INLINE idVec3 idBitMsgDelta::ReadDir( int numBits ) const {
  517. return idBitMsg::BitsToDir( ReadBits( numBits ), numBits );
  518. }
  519. ID_INLINE int idBitMsgDelta::ReadDeltaChar( int oldValue ) const {
  520. return (signed char)ReadDelta( oldValue, -8 );
  521. }
  522. ID_INLINE int idBitMsgDelta::ReadDeltaByte( int oldValue ) const {
  523. return (unsigned char)ReadDelta( oldValue, 8 );
  524. }
  525. ID_INLINE int idBitMsgDelta::ReadDeltaShort( int oldValue ) const {
  526. return (short)ReadDelta( oldValue, -16 );
  527. }
  528. ID_INLINE int idBitMsgDelta::ReadDeltaLong( int oldValue ) const {
  529. return ReadDelta( oldValue, 32 );
  530. }
  531. ID_INLINE float idBitMsgDelta::ReadDeltaFloat( float oldValue ) const {
  532. float value;
  533. *reinterpret_cast<int *>(&value) = ReadDelta( *reinterpret_cast<int *>(&oldValue), 32 );
  534. return value;
  535. }
  536. ID_INLINE float idBitMsgDelta::ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const {
  537. int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  538. int newBits = ReadDelta( oldBits, 1 + exponentBits + mantissaBits );
  539. return idMath::BitsToFloat( newBits, exponentBits, mantissaBits );
  540. }
  541. #endif /* !__BITMSG_H__ */