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

  1.  
  2. #ifndef __BITMSG_H__
  3. #define __BITMSG_H__
  4.  
  5. /*
  6. ===============================================================================
  7.  
  8.   idBitMsg
  9.  
  10.   Handles byte ordering and avoids alignment errors.
  11.   Allows concurrent writing and reading.
  12.   The data set with Init is never freed.
  13.  
  14. ===============================================================================
  15. */
  16.  
  17. class idBitMsg {
  18. public:
  19.                     idBitMsg();
  20.                     ~idBitMsg() {}
  21.  
  22.     void            Init( byte *data, int length );
  23.     void            Init( const byte *data, int length );
  24.     byte *            GetData( void );                        // get data for writing
  25.     const byte *    GetData( void ) const;                    // get data for reading
  26.     const byte *    GetReadData( void ) const;
  27.     int                GetMaxSize( void ) const;                // get the maximum message size
  28.     void            SetAllowOverflow( bool set );            // generate error if not set and message is overflowed
  29.     bool            IsOverflowed( void ) const;                // returns true if the message was overflowed
  30.  
  31.     int                GetSize( void ) const;                    // size of the message in bytes
  32.     void            SetSize( int size );                    // set the message size
  33.     int                GetWriteBit( void ) const;                // get current write bit
  34.     void            SetWriteBit( int bit );                    // set current write bit
  35.     int                GetNumBitsWritten( void ) const;        // returns number of bits written
  36.     int                GetRemainingSpace( void ) const;        // space left in bytes for writing
  37.     int                GetRemainingWriteBits( void ) const;    // space left in bits for writing
  38.     void            SaveWriteState( int &s, int &b ) const;    // save the write state
  39.     void            RestoreWriteState( int s, int b );        // restore the write state
  40.  
  41.     int                GetReadCount( void ) const;                // bytes read so far
  42.     void            SetReadCount( int bytes );                // set the number of bytes and bits read
  43.     int                GetReadBit( void ) const;                // get current read bit
  44.     void            SetReadBit( int bit );                    // set current read bit
  45.     int                GetNumBitsRead( void ) const;            // returns number of bits read
  46.     int                GetRemainingData( void ) const;            // number of bytes left to read
  47.     int                GetRemainingReadBits( void ) const;        // number of bits left to read
  48.     void            SaveReadState( int &c, int &b ) const;    // save the read state
  49.     void            RestoreReadState( int c, int b );        // restore the read state
  50.  
  51.     void            BeginWriting( void );                    // begin writing
  52.     void            WriteByteAlign( void );                    // write up to the next byte boundary
  53.     void            WriteBits( int value, int numBits );    // write the specified number of bits
  54.     void            WriteChar( int c );
  55.     void            WriteByte( int c );
  56.     void            WriteShort( int c );
  57.     void            WriteLong( int c );
  58.     void            WriteFloat( float f );
  59.     void            WriteFloat( float f, int exponentBits, int mantissaBits );
  60.     void            WriteAngle8( float f );
  61.     void            WriteAngle16( float f );
  62.     void            WriteDir( const idVec3 &dir, int numBits );
  63.     void            WriteString( const char *s, int maxLength = -1 );
  64.     void            WriteData( const void *data, int length );
  65.     void            WriteNetadr( const netadr_t adr );
  66.  
  67.     void            WriteDeltaChar( int oldValue, int newValue );
  68.     void            WriteDeltaByte( int oldValue, int newValue );
  69.     void            WriteDeltaShort( int oldValue, int newValue );
  70.     void            WriteDeltaLong( int oldValue, int newValue );
  71.     void            WriteDeltaFloat( float oldValue, float newValue );
  72.     void            WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits );
  73.     void            WriteDeltaByteCounter( int oldValue, int newValue );
  74.     void            WriteDeltaShortCounter( int oldValue, int newValue );
  75.     void            WriteDeltaLongCounter( int oldValue, int newValue );
  76.     bool            WriteDeltaDict( const idDict &dict, const idDict *base );
  77.  
  78.     void            BeginReading( void ) const;                // begin reading.
  79.     void            ReadByteAlign( void ) const;            // read up to the next byte boundary
  80.     int                ReadBits( int numBits ) const;            // read the specified number of bits
  81.     int                ReadChar( void ) const;
  82.     int                ReadByte( void ) const;
  83.     int                ReadShort( void ) const;
  84.     int                ReadLong( void ) const;
  85.     float            ReadFloat( void ) const;
  86.     float            ReadFloat( int exponentBits, int mantissaBits ) const;
  87.     float            ReadAngle8( void ) const;
  88.     float            ReadAngle16( void ) const;
  89.     idVec3            ReadDir( int numBits ) const;
  90.     int                ReadString( char *buffer, int bufferSize ) const;
  91.     int                ReadData( void *data, int length ) const;
  92.     void            ReadNetadr( netadr_t *adr ) const;
  93.  
  94.     int                ReadDeltaChar( int oldValue ) const;
  95.     int                ReadDeltaByte( int oldValue ) const;
  96.     int                ReadDeltaShort( int oldValue ) const;
  97.     int                ReadDeltaLong( int oldValue ) const;
  98.     float            ReadDeltaFloat( float oldValue ) const;
  99.     float            ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const;
  100.     int                ReadDeltaByteCounter( int oldValue ) const;
  101.     int                ReadDeltaShortCounter( int oldValue ) const;
  102.     int                ReadDeltaLongCounter( int oldValue ) const;
  103.     bool            ReadDeltaDict( idDict &dict, const idDict *base ) const;
  104.  
  105.     static int        DirToBits( const idVec3 &dir, int numBits );
  106.     static idVec3    BitsToDir( int bits, int numBits );
  107.  
  108. private:
  109.     byte *            writeData;            // pointer to data for writing
  110.     const byte *    readData;            // pointer to data for reading
  111.     int                maxSize;            // maximum size of message in bytes
  112.     int                curSize;            // current size of message in bytes
  113.     int                writeBit;            // number of bits written to the last written byte
  114.     mutable int        readCount;            // number of bytes read so far
  115.     mutable int        readBit;            // number of bits read from the last read byte
  116.     int                numValueOverflows;    // number of bit value overflows
  117.     bool            allowOverflow;        // if false, generate an error when the message is overflowed
  118.     bool            overflowed;            // set to true if the buffer size failed (with allowOverflow set)
  119.  
  120. private:
  121.     bool            CheckOverflow( int numBits );
  122.     byte *            GetByteSpace( int length );
  123.     void            WriteDelta( int oldValue, int newValue, int numBits );
  124.     int                ReadDelta( int oldValue, int numBits ) const;
  125. };
  126.  
  127.  
  128. ID_INLINE void idBitMsg::Init( byte *data, int length ) {
  129.     writeData = data;
  130.     readData = data;
  131.     maxSize = length;
  132.     numValueOverflows = 0;
  133. }
  134.  
  135. ID_INLINE void idBitMsg::Init( const byte *data, int length ) {
  136.     writeData = NULL;
  137.     readData = data;
  138.     maxSize = length;
  139.     numValueOverflows = 0;
  140. }
  141.  
  142. ID_INLINE byte *idBitMsg::GetData( void ) {
  143.     return writeData;
  144. }
  145.  
  146. ID_INLINE const byte *idBitMsg::GetData( void ) const {
  147.     return readData;
  148. }
  149.  
  150. ID_INLINE const byte *idBitMsg::GetReadData( void ) const {
  151.     return ( readData + readCount );
  152. }
  153.  
  154. ID_INLINE int idBitMsg::GetMaxSize( void ) const {
  155.     return maxSize;
  156. }
  157.  
  158. ID_INLINE void idBitMsg::SetAllowOverflow( bool set ) {
  159.     allowOverflow = set;
  160. }
  161.  
  162. ID_INLINE bool idBitMsg::IsOverflowed( void ) const {
  163.     return overflowed;
  164. }
  165.  
  166. ID_INLINE int idBitMsg::GetSize( void ) const {
  167.     return curSize;
  168. }
  169.  
  170. ID_INLINE void idBitMsg::SetSize( int size ) {
  171.     if ( size > maxSize ) {
  172.         curSize = maxSize;
  173.     } else {
  174.         curSize = size;
  175.     }
  176. }
  177.  
  178. ID_INLINE int idBitMsg::GetWriteBit( void ) const {
  179.     return writeBit;
  180. }
  181.  
  182. ID_INLINE void idBitMsg::SetWriteBit( int bit ) {
  183.     writeBit = bit & 7;
  184.     if ( writeBit ) {
  185.         writeData[curSize - 1] &= ( 1 << writeBit ) - 1;
  186.     }
  187. }
  188.  
  189. ID_INLINE int idBitMsg::GetNumBitsWritten( void ) const {
  190. //    return ( ( curSize << 3 ) - ( writeBit ? 8 - writeBit : 0 ) );
  191.     return ( ( curSize << 3 ) - ( ( 8 - writeBit ) & 7 ) );
  192. }
  193.  
  194. ID_INLINE int idBitMsg::GetRemainingSpace( void ) const {
  195.     return maxSize - curSize;
  196. }
  197.  
  198. ID_INLINE int idBitMsg::GetRemainingWriteBits( void ) const {
  199.     return ( maxSize << 3 ) - GetNumBitsWritten();
  200. }
  201.  
  202. ID_INLINE void idBitMsg::SaveWriteState( int &s, int &b ) const {
  203.     s = curSize;
  204.     b = writeBit;
  205. }
  206.  
  207. ID_INLINE void idBitMsg::RestoreWriteState( int s, int b ) {
  208.     curSize = s;
  209.     writeBit = b & 7;
  210.     if ( writeBit ) {
  211.         writeData[curSize - 1] &= ( 1 << writeBit ) - 1;
  212.     }
  213. }
  214.  
  215. ID_INLINE int idBitMsg::GetReadCount( void ) const {
  216.     return readCount;
  217. }
  218.  
  219. ID_INLINE void idBitMsg::SetReadCount( int bytes ) {
  220.     readCount = bytes;
  221. }
  222.  
  223. ID_INLINE int idBitMsg::GetReadBit( void ) const {
  224.     return readBit;
  225. }
  226.  
  227. ID_INLINE void idBitMsg::SetReadBit( int bit ) {
  228.     readBit = bit & 7;
  229. }
  230.  
  231. ID_INLINE int idBitMsg::GetNumBitsRead( void ) const {
  232. //    return ( ( readCount << 3 ) - ( readBit ? 8 - readBit : 0 ) );
  233.     return ( ( readCount << 3 ) - ( ( 8 - readBit ) & 7 ) );
  234. }
  235.  
  236. ID_INLINE int idBitMsg::GetRemainingData( void ) const {
  237.     return curSize - readCount;
  238. }
  239.  
  240. ID_INLINE int idBitMsg::GetRemainingReadBits( void ) const {
  241.     return ( curSize << 3 ) - GetNumBitsRead();
  242. }
  243.  
  244. ID_INLINE void idBitMsg::SaveReadState( int &c, int &b ) const {
  245.     c = readCount;
  246.     b = readBit;
  247. }
  248.  
  249. ID_INLINE void idBitMsg::RestoreReadState( int c, int b ) {
  250.     readCount = c;
  251.     readBit = b & 7;
  252. }
  253.  
  254. ID_INLINE void idBitMsg::BeginWriting( void ) {
  255.     curSize = 0;
  256.     overflowed = false;
  257.     writeBit = 0;
  258. }
  259.  
  260. ID_INLINE void idBitMsg::WriteByteAlign( void ) {
  261.     writeBit = 0;
  262. }
  263.  
  264. ID_INLINE void idBitMsg::WriteChar( int c ) {
  265.     WriteBits( c, -8 );
  266. }
  267.  
  268. ID_INLINE void idBitMsg::WriteByte( int c ) {
  269.     WriteBits( c, 8 );
  270. }
  271.  
  272. ID_INLINE void idBitMsg::WriteShort( int c ) {
  273.     WriteBits( c, -16 );
  274. }
  275.  
  276. ID_INLINE void idBitMsg::WriteLong( int c ) {
  277.     WriteBits( c, 32 );
  278. }
  279.  
  280. ID_INLINE void idBitMsg::WriteFloat( float f ) {
  281.     WriteBits( *reinterpret_cast<int *>(&f), 32 );
  282. }
  283.  
  284. ID_INLINE void idBitMsg::WriteFloat( float f, int exponentBits, int mantissaBits ) {
  285.     int bits = idMath::FloatToBits( f, exponentBits, mantissaBits );
  286.     WriteBits( bits, 1 + exponentBits + mantissaBits );
  287. }
  288.  
  289. ID_INLINE void idBitMsg::WriteAngle8( float f ) {
  290.     WriteByte( ANGLE2BYTE( f ) );
  291. }
  292.  
  293. ID_INLINE void idBitMsg::WriteAngle16( float f ) {
  294.     WriteShort( ANGLE2SHORT(f) );
  295. }
  296.  
  297. ID_INLINE void idBitMsg::WriteDir( const idVec3 &dir, int numBits ) {
  298.     WriteBits( DirToBits( dir, numBits ), numBits );
  299. }
  300.  
  301. ID_INLINE void idBitMsg::WriteDeltaChar( int oldValue, int newValue ) {
  302.     WriteDelta( oldValue, newValue, -8 );
  303. }
  304.  
  305. ID_INLINE void idBitMsg::WriteDeltaByte( int oldValue, int newValue ) {
  306.     WriteDelta( oldValue, newValue, 8 );
  307. }
  308.  
  309. ID_INLINE void idBitMsg::WriteDeltaShort( int oldValue, int newValue ) {
  310.     WriteDelta( oldValue, newValue, -16 );
  311. }
  312.  
  313. ID_INLINE void idBitMsg::WriteDeltaLong( int oldValue, int newValue ) {
  314.     WriteDelta( oldValue, newValue, 32 );
  315. }
  316.  
  317. ID_INLINE void idBitMsg::WriteDeltaFloat( float oldValue, float newValue ) {
  318.     WriteDelta( *reinterpret_cast<int *>(&oldValue), *reinterpret_cast<int *>(&newValue), 32 );
  319. }
  320.  
  321. ID_INLINE void idBitMsg::WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits ) {
  322.     int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  323.     int newBits = idMath::FloatToBits( newValue, exponentBits, mantissaBits );
  324.     WriteDelta( oldBits, newBits, 1 + exponentBits + mantissaBits );
  325. }
  326.  
  327. ID_INLINE void idBitMsg::BeginReading( void ) const {
  328.     readCount = 0;
  329.     readBit = 0;
  330. }
  331.  
  332. ID_INLINE void idBitMsg::ReadByteAlign( void ) const {
  333.     readBit = 0;
  334. }
  335.  
  336. ID_INLINE int idBitMsg::ReadChar( void ) const {
  337.     return (signed char)ReadBits( -8 );
  338. }
  339.  
  340. ID_INLINE int idBitMsg::ReadByte( void ) const {
  341.     return (unsigned char)ReadBits( 8 );
  342. }
  343.  
  344. ID_INLINE int idBitMsg::ReadShort( void ) const {
  345.     return (short)ReadBits( -16 );
  346. }
  347.  
  348. ID_INLINE int idBitMsg::ReadLong( void ) const {
  349.     return ReadBits( 32 );
  350. }
  351.  
  352. ID_INLINE float idBitMsg::ReadFloat( void ) const {
  353.     float value;
  354.     *reinterpret_cast<int *>(&value) = ReadBits( 32 );
  355.     return value;
  356. }
  357.  
  358. ID_INLINE float idBitMsg::ReadFloat( int exponentBits, int mantissaBits ) const {
  359.     int bits = ReadBits( 1 + exponentBits + mantissaBits );
  360.     return idMath::BitsToFloat( bits, exponentBits, mantissaBits );
  361. }
  362.  
  363. ID_INLINE float idBitMsg::ReadAngle8( void ) const {
  364.     return BYTE2ANGLE( ReadByte() );
  365. }
  366.  
  367. ID_INLINE float idBitMsg::ReadAngle16( void ) const {
  368.     return SHORT2ANGLE( ReadShort() );
  369. }
  370.  
  371. ID_INLINE idVec3 idBitMsg::ReadDir( int numBits ) const {
  372.     return BitsToDir( ReadBits( numBits ), numBits );
  373. }
  374.  
  375. ID_INLINE int idBitMsg::ReadDeltaChar( int oldValue ) const {
  376.     return (signed char)ReadDelta( oldValue, -8 );
  377. }
  378.  
  379. ID_INLINE int idBitMsg::ReadDeltaByte( int oldValue ) const {
  380.     return (unsigned char)ReadDelta( oldValue, 8 );
  381. }
  382.  
  383. ID_INLINE int idBitMsg::ReadDeltaShort( int oldValue ) const {
  384.     return (short)ReadDelta( oldValue, -16 );
  385. }
  386.  
  387. ID_INLINE int idBitMsg::ReadDeltaLong( int oldValue ) const {
  388.     return ReadDelta( oldValue, 32 );
  389. }
  390.  
  391. ID_INLINE float idBitMsg::ReadDeltaFloat( float oldValue ) const {
  392.     float value;
  393.     *reinterpret_cast<int *>(&value) = ReadDelta( *reinterpret_cast<int *>(&oldValue), 32 );
  394.     return value;
  395. }
  396.  
  397. ID_INLINE float idBitMsg::ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const {
  398.     int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  399.     int newBits = ReadDelta( oldBits, 1 + exponentBits + mantissaBits );
  400.     return idMath::BitsToFloat( newBits, exponentBits, mantissaBits );
  401. }
  402.  
  403.  
  404. /*
  405. ===============================================================================
  406.  
  407.   idBitMsgDelta
  408.  
  409. ===============================================================================
  410. */
  411.  
  412. class idBitMsgDelta {
  413. public:
  414.                     idBitMsgDelta();
  415.                     ~idBitMsgDelta() {}
  416.  
  417.     void            InitWriting( const idBitMsg *base, idBitMsg *newBase, idBitMsg *delta );
  418.     void            InitReading( const idBitMsg *base, idBitMsg *newBase, const idBitMsg *delta );
  419.     bool            HasChanged( void ) const;
  420.  
  421.     void            WriteBits( int value, int numBits );
  422.     void            WriteChar( int c );
  423.     void            WriteByte( int c );
  424.     void            WriteShort( int c );
  425.     void            WriteLong( int c );
  426.     void            WriteFloat( float f );
  427.     void            WriteFloat( float f, int exponentBits, int mantissaBits );
  428. // RAVEN BEGIN
  429. // abahr:
  430.     void            WriteVec3( const idVec3& v );
  431.     void            WriteDeltaVec3( const idVec3& oldValue, const idVec3& newValue );
  432.     void            WriteVec4( const idVec4& v );
  433.     void            WriteDeltaVec4( const idVec4& oldValue, const idVec4& newValue );
  434.     void            WriteQuat( const idQuat& q );
  435.     void            WriteDeltaQuat( const idQuat& oldValue, const idQuat& newValue );
  436.     void            WriteMat3( const idMat3& m );
  437.     void            WriteDeltaMat3( const idMat3& oldValue, const idMat3& newValue );
  438. // RAVEN END
  439.     void            WriteAngle8( float f );
  440.     void            WriteAngle16( float f );
  441.     void            WriteDir( const idVec3 &dir, int numBits );
  442.     void            WriteString( const char *s, int maxLength = -1 );
  443.     void            WriteData( const void *data, int length );
  444.     void            WriteDict( const idDict &dict );
  445.  
  446.     void            WriteDeltaChar( int oldValue, int newValue );
  447.     void            WriteDeltaByte( int oldValue, int newValue );
  448.     void            WriteDeltaShort( int oldValue, int newValue );
  449.     void            WriteDeltaLong( int oldValue, int newValue );
  450.     void            WriteDeltaFloat( float oldValue, float newValue );
  451.     void            WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits );
  452.     void            WriteDeltaByteCounter( int oldValue, int newValue );
  453.     void            WriteDeltaShortCounter( int oldValue, int newValue );
  454.     void            WriteDeltaLongCounter( int oldValue, int newValue );
  455.  
  456.     int                ReadBits( int numBits ) const;
  457.     int                ReadChar( void ) const;
  458.     int                ReadByte( void ) const;
  459.     int                ReadShort( void ) const;
  460.     int                ReadLong( void ) const;
  461.     float            ReadFloat( void ) const;
  462.     float            ReadFloat( int exponentBits, int mantissaBits ) const;
  463. // RAVEN BEGIN
  464. // abahr
  465.     idVec3            ReadVec3( void ) const;
  466.     idVec3            ReadDeltaVec3( const idVec3& oldValue ) const;
  467.     idVec4            ReadVec4( void ) const;
  468.     idVec4            ReadDeltaVec4( const idVec4& oldValue ) const;
  469.     idQuat            ReadQuat( void ) const;
  470.     idQuat            ReadDeltaQuat( const idQuat& oldValue ) const;
  471.     idMat3            ReadMat3( void ) const;
  472.     idMat3            ReadDeltaMat3( const idMat3& oldValue ) const;
  473. // RAVEN END
  474.     float            ReadAngle8( void ) const;
  475.     float            ReadAngle16( void ) const;
  476.     idVec3            ReadDir( int numBits ) const;
  477.     void            ReadString( char *buffer, int bufferSize ) const;
  478.     void            ReadData( void *data, int length ) const;
  479.     void            ReadDict( idDict &dict );
  480.  
  481.     int                ReadDeltaChar( int oldValue ) const;
  482.     int                ReadDeltaByte( int oldValue ) const;
  483.     int                ReadDeltaShort( int oldValue ) const;
  484.     int                ReadDeltaLong( int oldValue ) const;
  485.     float            ReadDeltaFloat( float oldValue ) const;
  486.     float            ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const;
  487.     int                ReadDeltaByteCounter( int oldValue ) const;
  488.     int                ReadDeltaShortCounter( int oldValue ) const;
  489.     int                ReadDeltaLongCounter( int oldValue ) const;
  490.  
  491. private:
  492.     const idBitMsg *base;            // base
  493.     idBitMsg *        newBase;        // new base
  494.     idBitMsg *        writeDelta;        // delta from base to new base for writing
  495.     const idBitMsg *readDelta;        // delta from base to new base for reading
  496.     mutable bool    changed;        // true if the new base is different from the base
  497.  
  498. private:
  499.     void            WriteDelta( int oldValue, int newValue, int numBits );
  500.     int                ReadDelta( int oldValue, int numBits ) const;
  501. };
  502.  
  503. ID_INLINE idBitMsgDelta::idBitMsgDelta() {
  504.     base = NULL;
  505.     newBase = NULL;
  506.     writeDelta = NULL;
  507.     readDelta = NULL;
  508.     changed = false;
  509. }
  510.  
  511. ID_INLINE void idBitMsgDelta::InitWriting( const idBitMsg *base, idBitMsg *newBase, idBitMsg *delta ) {
  512.     this->base = base;
  513.     this->newBase = newBase;
  514.     this->writeDelta = delta;
  515.     this->readDelta = delta;
  516.     this->changed = false;
  517. }
  518.  
  519. ID_INLINE void idBitMsgDelta::InitReading( const idBitMsg *base, idBitMsg *newBase, const idBitMsg *delta ) {
  520.     this->base = base;
  521.     this->newBase = newBase;
  522.     this->writeDelta = NULL;
  523.     this->readDelta = delta;
  524.     this->changed = false;
  525. }
  526.  
  527. ID_INLINE bool idBitMsgDelta::HasChanged( void ) const {
  528.     return changed;
  529. }
  530.  
  531. ID_INLINE void idBitMsgDelta::WriteChar( int c ) {
  532.     WriteBits( c, -8 );
  533. }
  534.  
  535. ID_INLINE void idBitMsgDelta::WriteByte( int c ) {
  536.     WriteBits( c, 8 );
  537. }
  538.  
  539. ID_INLINE void idBitMsgDelta::WriteShort( int c ) {
  540.     WriteBits( c, -16 );
  541. }
  542.  
  543. ID_INLINE void idBitMsgDelta::WriteLong( int c ) {
  544.     WriteBits( c, 32 );
  545. }
  546.  
  547. ID_INLINE void idBitMsgDelta::WriteFloat( float f ) {
  548.     WriteBits( *reinterpret_cast<int *>(&f), 32 );
  549. }
  550.  
  551. ID_INLINE void idBitMsgDelta::WriteFloat( float f, int exponentBits, int mantissaBits ) {
  552.     int bits = idMath::FloatToBits( f, exponentBits, mantissaBits );
  553.     WriteBits( bits, 1 + exponentBits + mantissaBits );
  554. }
  555.  
  556. // RAVEN BEGIN
  557. // abahr
  558. ID_INLINE void idBitMsgDelta::WriteVec3( const idVec3& v ) {
  559.     for( int ix = 0; ix < v.GetDimension(); ++ix ) {
  560.         WriteFloat( v[ix] );
  561.     }
  562. }
  563.  
  564. ID_INLINE void idBitMsgDelta::WriteDeltaVec3( const idVec3& oldValue, const idVec3& newValue ) {
  565.     for( int ix = 0; ix < oldValue.GetDimension(); ++ix ) {
  566.         WriteDeltaFloat( oldValue[ix], newValue[ix] );
  567.     }
  568. }
  569.  
  570. ID_INLINE void idBitMsgDelta::WriteVec4( const idVec4& v ) {
  571.     for( int ix = 0; ix < v.GetDimension(); ++ix ) {
  572.         WriteFloat( v[ix] );
  573.     }
  574. }
  575.  
  576. ID_INLINE void idBitMsgDelta::WriteDeltaVec4( const idVec4& oldValue, const idVec4& newValue ) {
  577.     for( int ix = 0; ix < oldValue.GetDimension(); ++ix ) {
  578.         WriteDeltaFloat( oldValue[ix], newValue[ix] );
  579.     }
  580. }
  581.  
  582. ID_INLINE void idBitMsgDelta::WriteQuat( const idQuat& q ) {
  583.     for( int ix = 0; ix < q.GetDimension(); ++ix ) {
  584.         WriteFloat( q[ix] );
  585.     }
  586. }
  587.  
  588. ID_INLINE void idBitMsgDelta::WriteDeltaQuat( const idQuat& oldValue, const idQuat& newValue ) {
  589.     for( int ix = 0; ix < oldValue.GetDimension(); ++ix ) {
  590.         WriteDeltaFloat( oldValue[ix], newValue[ix] );
  591.     }
  592. }
  593.  
  594. ID_INLINE void idBitMsgDelta::WriteMat3( const idMat3& m ) {
  595.     for( int ix = 0; ix < m.GetVec3Dimension(); ++ix ) {
  596.         WriteVec3( m[ix] );
  597.     }
  598. }
  599.  
  600. ID_INLINE void idBitMsgDelta::WriteDeltaMat3( const idMat3& oldValue, const idMat3& newValue ) {
  601.     for( int ix = 0; ix < oldValue.GetDimension(); ++ix ) {
  602.         WriteDeltaVec3( oldValue[ix], newValue[ix] );
  603.     }
  604. }
  605. // RAVEN END
  606.  
  607. ID_INLINE void idBitMsgDelta::WriteAngle8( float f ) {
  608.     WriteBits( ANGLE2BYTE( f ), 8 );
  609. }
  610.  
  611. ID_INLINE void idBitMsgDelta::WriteAngle16( float f ) {
  612.     WriteBits( ANGLE2SHORT(f), 16 );
  613. }
  614.  
  615. ID_INLINE void idBitMsgDelta::WriteDir( const idVec3 &dir, int numBits ) {
  616.     WriteBits( idBitMsg::DirToBits( dir, numBits ), numBits );
  617. }
  618.  
  619. ID_INLINE void idBitMsgDelta::WriteDeltaChar( int oldValue, int newValue ) {
  620.     WriteDelta( oldValue, newValue, -8 );
  621. }
  622.  
  623. ID_INLINE void idBitMsgDelta::WriteDeltaByte( int oldValue, int newValue ) {
  624.     WriteDelta( oldValue, newValue, 8 );
  625. }
  626.  
  627. ID_INLINE void idBitMsgDelta::WriteDeltaShort( int oldValue, int newValue ) {
  628.     WriteDelta( oldValue, newValue, -16 );
  629. }
  630.  
  631. ID_INLINE void idBitMsgDelta::WriteDeltaLong( int oldValue, int newValue ) {
  632.     WriteDelta( oldValue, newValue, 32 );
  633. }
  634.  
  635. ID_INLINE void idBitMsgDelta::WriteDeltaFloat( float oldValue, float newValue ) {
  636.     WriteDelta( *reinterpret_cast<int *>(&oldValue), *reinterpret_cast<int *>(&newValue), 32 );
  637. }
  638.  
  639. ID_INLINE void idBitMsgDelta::WriteDeltaFloat( float oldValue, float newValue, int exponentBits, int mantissaBits ) {
  640.     int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  641.     int newBits = idMath::FloatToBits( newValue, exponentBits, mantissaBits );
  642.     WriteDelta( oldBits, newBits, 1 + exponentBits + mantissaBits );
  643. }
  644.  
  645. ID_INLINE int idBitMsgDelta::ReadChar( void ) const {
  646.     return (signed char)ReadBits( -8 );
  647. }
  648.  
  649. ID_INLINE int idBitMsgDelta::ReadByte( void ) const {
  650.     return (unsigned char)ReadBits( 8 );
  651. }
  652.  
  653. ID_INLINE int idBitMsgDelta::ReadShort( void ) const {
  654.     return (short)ReadBits( -16 );
  655. }
  656.  
  657. ID_INLINE int idBitMsgDelta::ReadLong( void ) const {
  658.     return ReadBits( 32 );
  659. }
  660.  
  661. ID_INLINE float idBitMsgDelta::ReadFloat( void ) const {
  662.     float value;
  663.     *reinterpret_cast<int *>(&value) = ReadBits( 32 );
  664.     return value;
  665. }
  666.  
  667. ID_INLINE float idBitMsgDelta::ReadFloat( int exponentBits, int mantissaBits ) const {
  668.     int bits = ReadBits( 1 + exponentBits + mantissaBits );
  669.     return idMath::BitsToFloat( bits, exponentBits, mantissaBits );
  670. }
  671.  
  672. // RAVEN BEGIN
  673. // abahr
  674. ID_INLINE idVec3 idBitMsgDelta::ReadVec3() const {
  675.     idVec3 v;
  676.     for( int ix = 0; ix < v.GetDimension(); ++ix ) {
  677.         v[ix] = ReadFloat();
  678.     }
  679.  
  680.     return v;
  681. }
  682.  
  683. ID_INLINE idVec3 idBitMsgDelta::ReadDeltaVec3( const idVec3& oldValue ) const {
  684.     idVec3 value;
  685.     for( int ix = 0; ix < value.GetDimension(); ++ix ) {
  686.         value[ix] = ReadDeltaFloat( oldValue[ix] );
  687.     }
  688.     return value;
  689. }
  690.  
  691. ID_INLINE idVec4 idBitMsgDelta::ReadVec4( void ) const {
  692.     idVec4 v;
  693.     for( int ix = 0; ix < v.GetDimension(); ++ix ) {
  694.         v[ix] = ReadFloat();
  695.     }
  696.  
  697.     return v;
  698. }
  699.  
  700. ID_INLINE idVec4 idBitMsgDelta::ReadDeltaVec4( const idVec4& oldValue ) const {
  701.     idVec4 value;
  702.     for( int ix = 0; ix < value.GetDimension(); ++ix ) {
  703.         value[ix] = ReadDeltaFloat( oldValue[ix] );
  704.     }
  705.     return value;
  706. }
  707.  
  708. ID_INLINE idQuat idBitMsgDelta::ReadQuat( void ) const {
  709.     idQuat q;
  710.     for( int ix = 0; ix < q.GetDimension(); ++ix ) {
  711.         q[ix] = ReadFloat();
  712.     }
  713.  
  714.     return q;
  715. }
  716.  
  717. ID_INLINE idQuat idBitMsgDelta::ReadDeltaQuat( const idQuat& oldValue ) const {
  718.     idQuat value;
  719.     for( int ix = 0; ix < value.GetDimension(); ++ix ) {
  720.         value[ix] = ReadDeltaFloat( oldValue[ix] );
  721.     }
  722.     return value;
  723. }
  724.  
  725. ID_INLINE idMat3 idBitMsgDelta::ReadMat3() const {
  726.     idMat3 m;
  727.     
  728.     for( int ix = 0; ix < m.GetVec3Dimension(); ++ix ) {
  729.         m[ix] = ReadVec3();
  730.     }
  731.  
  732.     return m;
  733. }
  734.  
  735. ID_INLINE idMat3 idBitMsgDelta::ReadDeltaMat3( const idMat3& oldValue ) const {
  736.     idMat3 value;
  737.  
  738.     for( int ix = 0; ix < value.GetVec3Dimension(); ++ix ) {
  739.         value[ix] = ReadDeltaVec3( oldValue[ix] );
  740.     }
  741.  
  742.     return value;
  743. }
  744. // RAVEN END
  745.  
  746. ID_INLINE float idBitMsgDelta::ReadAngle8( void ) const {
  747.     return BYTE2ANGLE( ReadByte() );
  748. }
  749.  
  750. ID_INLINE float idBitMsgDelta::ReadAngle16( void ) const {
  751.     return SHORT2ANGLE( ReadShort() );
  752. }
  753.  
  754. ID_INLINE idVec3 idBitMsgDelta::ReadDir( int numBits ) const {
  755.     return idBitMsg::BitsToDir( ReadBits( numBits ), numBits );
  756. }
  757.  
  758. ID_INLINE int idBitMsgDelta::ReadDeltaChar( int oldValue ) const {
  759.     return (signed char)ReadDelta( oldValue, -8 );
  760. }
  761.  
  762. ID_INLINE int idBitMsgDelta::ReadDeltaByte( int oldValue ) const {
  763.     return (unsigned char)ReadDelta( oldValue, 8 );
  764. }
  765.  
  766. ID_INLINE int idBitMsgDelta::ReadDeltaShort( int oldValue ) const {
  767.     return (short)ReadDelta( oldValue, -16 );
  768. }
  769.  
  770. ID_INLINE int idBitMsgDelta::ReadDeltaLong( int oldValue ) const {
  771.     return ReadDelta( oldValue, 32 );
  772. }
  773.  
  774. ID_INLINE float idBitMsgDelta::ReadDeltaFloat( float oldValue ) const {
  775.     float value;
  776.     *reinterpret_cast<int *>(&value) = ReadDelta( *reinterpret_cast<int *>(&oldValue), 32 );
  777.     return value;
  778. }
  779.  
  780. ID_INLINE float idBitMsgDelta::ReadDeltaFloat( float oldValue, int exponentBits, int mantissaBits ) const {
  781.     int oldBits = idMath::FloatToBits( oldValue, exponentBits, mantissaBits );
  782.     int newBits = ReadDelta( oldBits, 1 + exponentBits + mantissaBits );
  783.     return idMath::BitsToFloat( newBits, exponentBits, mantissaBits );
  784. }
  785.  
  786. #define MAX_MSG_QUEUE_SIZE                16384        // must be a power of 2
  787.  
  788. class idMsgQueue {
  789. public:
  790.                     idMsgQueue();
  791.  
  792.     void            Init( int sequence );
  793.  
  794.     bool            Add( const byte *data, const int size, bool sequencing );
  795. // RAVEN BEGIN
  796. // rjohnson: added check for data overflow
  797.     bool            Get( byte *data, int dataSize, int &size, bool sequencing );
  798. // RAVEN END
  799.     int                GetTotalSize( void ) const;
  800.     int                GetSpaceLeft( void ) const;
  801.     int                GetFirst( void ) const { return first; }
  802.     int                GetLast( void ) const { return last; }
  803.     void            CopyToBuffer( byte *buf ) const;
  804.  
  805.     void            FlushTo( idBitMsg &msg );
  806.     void            ReadFrom( const idBitMsg &msg );
  807.  
  808.     void            Save( idFile *file ) const;
  809.     void            Restore( idFile *file );
  810.  
  811. private:
  812.     byte            buffer[MAX_MSG_QUEUE_SIZE];
  813.     int                first;            // sequence number of first message in queue
  814.     int                last;            // sequence number of last message in queue
  815.     int                startIndex;        // index pointing to the first byte of the first message
  816.     int                endIndex;        // index pointing to the first byte after the last message
  817.  
  818.     void            WriteByte( byte b );
  819.     byte            ReadByte( void );
  820.     void            WriteShort( int s );
  821.     int                ReadShort( void );
  822.     void            WriteLong( int l );
  823.     int                ReadLong( void );
  824.     void            WriteData( const byte *data, const int size );
  825.     void            ReadData( byte *data, const int size );
  826. };
  827.  
  828. #endif /* !__BITMSG_H__ */
  829.