#include <BitStream.h>
Public Member Functions | |
BitStream () | |
Default Constructor. | |
BitStream (int initialBytesToAllocate) | |
BitStream (unsigned char *_data, unsigned int lengthInBytes, bool _copyData) | |
~BitStream () | |
Destructor. | |
void | Reset (void) |
Resets the bitstream for reuse. | |
template<class templateType> | |
bool | Serialize (bool writeToBitstream, templateType &var) |
template<class templateType> | |
bool | SerializeDelta (bool writeToBitstream, templateType ¤tValue, templateType lastValue) |
template<class templateType> | |
bool | SerializeDelta (bool writeToBitstream, templateType ¤tValue) |
template<class templateType> | |
bool | SerializeCompressed (bool writeToBitstream, templateType &var) |
template<class templateType> | |
bool | SerializeCompressedDelta (bool writeToBitstream, templateType ¤tValue, templateType lastValue) |
template<class templateType> | |
bool | SerializeCompressedDelta (bool writeToBitstream, templateType ¤tValue) |
Save as SerializeCompressedDelta(templateType ¤tValue, templateType lastValue) when we have an unknown second parameter. | |
bool | Serialize (bool writeToBitstream, char *input, const int numberOfBytes) |
template<class templateType> | |
bool | SerializeNormVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | SerializeVector (bool writeToBitstream, templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | SerializeNormQuat (bool writeToBitstream, templateType &w, templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | SerializeOrthMatrix (bool writeToBitstream, templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22) |
bool | SerializeBits (bool writeToBitstream, unsigned char *input, int numberOfBitsToSerialize, const bool rightAlignedBits=true) |
template<class templateType> | |
void | Write (templateType var) |
template<class templateType> | |
void | WriteDelta (templateType currentValue, templateType lastValue) |
template<class templateType> | |
void | WriteDelta (templateType currentValue) |
template<class templateType> | |
void | WriteCompressed (templateType var) |
template<class templateType> | |
void | WriteCompressedDelta (templateType currentValue, templateType lastValue) |
template<class templateType> | |
void | WriteCompressedDelta (templateType currentValue) |
Save as WriteCompressedDelta(templateType currentValue, templateType lastValue) when we have an unknown second parameter. | |
template<class templateType> | |
bool | Read (templateType &var) |
template<class templateType> | |
bool | ReadDelta (templateType &var) |
template<class templateType> | |
bool | ReadCompressed (templateType &var) |
template<class templateType> | |
bool | ReadCompressedDelta (templateType &var) |
void | Write (const char *input, const int numberOfBytes) |
void | Write (BitStream *bitStream, int numberOfBits) |
void | Write (BitStream *bitStream) |
template<class templateType> | |
void | WriteNormVector (templateType x, templateType y, templateType z) |
template<class templateType> | |
void | WriteVector (templateType x, templateType y, templateType z) |
template<class templateType> | |
void | WriteNormQuat (templateType w, templateType x, templateType y, templateType z) |
template<class templateType> | |
void | WriteOrthMatrix (templateType m00, templateType m01, templateType m02, templateType m10, templateType m11, templateType m12, templateType m20, templateType m21, templateType m22) |
bool | Read (char *output, const int numberOfBytes) |
template<class templateType> | |
bool | ReadNormVector (templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | ReadVector (templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | ReadNormQuat (templateType &w, templateType &x, templateType &y, templateType &z) |
template<class templateType> | |
bool | ReadOrthMatrix (templateType &m00, templateType &m01, templateType &m02, templateType &m10, templateType &m11, templateType &m12, templateType &m20, templateType &m21, templateType &m22) |
void | ResetReadPointer (void) |
Sets the read pointer back to the beginning of your data. | |
void | ResetWritePointer (void) |
Sets the write pointer back to the beginning of your data. | |
void | AssertStreamEmpty (void) |
void | PrintBits (void) const |
printf the bits in the stream. Great for debugging. | |
void | IgnoreBits (const int numberOfBits) |
void | SetWriteOffset (const int offset) |
int | GetNumberOfBitsUsed (void) const |
Returns the length in bits of the stream. | |
int | GetWriteOffset (void) const |
int | GetNumberOfBytesUsed (void) const |
Returns the length in bytes of the stream. | |
int | GetReadOffset (void) const |
Returns the number of bits into the stream that we have read. | |
void | SetReadOffset (int newReadOffset) |
int | GetNumberOfUnreadBits (void) const |
Returns the number of bits left in the stream that haven't been read. | |
int | CopyData (unsigned char **_data) const |
void | SetData (unsigned char *input) |
unsigned char * | GetData (void) const |
void | WriteBits (const unsigned char *input, int numberOfBitsToWrite, const bool rightAlignedBits=true) |
void | WriteAlignedBytes (const unsigned char *input, const int numberOfBytesToWrite) |
bool | ReadAlignedBytes (unsigned char *output, const int numberOfBytesToRead) |
void | AlignWriteToByteBoundary (void) |
void | AlignReadToByteBoundary (void) |
bool | ReadBits (unsigned char *output, int numberOfBitsToRead, const bool alignBitsToRight=true) |
void | Write0 (void) |
Write a 0. | |
void | Write1 (void) |
Write a 1. | |
bool | ReadBit (void) |
Reads 1 bit and returns true if that bit is 1 and false if it is 0. | |
void | AssertCopyData (void) |
void | SetNumberOfBitsAllocated (const unsigned int lengthInBits) |
void | AddBitsAndReallocate (const int numberOfBitsToWrite) |
Reallocates (if necessary) in preparation of writing numberOfBitsToWrite. | |
template<> | |
void | Write (bool var) |
template<> | |
void | Write (PlayerID var) |
template<> | |
void | Write (NetworkID var) |
template<> | |
void | WriteDelta (PlayerID currentValue, PlayerID lastValue) |
template<> | |
void | WriteDelta (NetworkID currentValue, NetworkID lastValue) |
template<> | |
void | WriteDelta (bool currentValue, bool lastValue) |
template<> | |
void | WriteCompressed (PlayerID var) |
template<> | |
void | WriteCompressed (NetworkID var) |
template<> | |
void | WriteCompressed (bool var) |
template<> | |
void | WriteCompressed (float var) |
For values between -1 and 1. | |
template<> | |
void | WriteCompressed (double var) |
For values between -1 and 1. | |
template<> | |
void | WriteCompressedDelta (bool currentValue, bool lastValue) |
template<> | |
void | WriteCompressedDelta (bool currentValue) |
Save as WriteCompressedDelta(bool currentValue, templateType lastValue) when we have an unknown second bool. | |
template<> | |
bool | Read (bool &var) |
template<> | |
bool | Read (PlayerID &var) |
template<> | |
bool | Read (NetworkID &var) |
template<> | |
bool | ReadDelta (bool &var) |
template<> | |
bool | ReadCompressed (PlayerID &var) |
template<> | |
bool | ReadCompressed (NetworkID &var) |
template<> | |
bool | ReadCompressed (bool &var) |
template<> | |
bool | ReadCompressed (float &var) |
For values between -1 and 1. | |
template<> | |
bool | ReadCompressed (double &var) |
For values between -1 and 1. | |
template<> | |
bool | ReadCompressedDelta (bool &var) |
Private Member Functions | |
void | WriteCompressed (const unsigned char *input, const int size, const bool unsignedData) |
Assume the input source points to a native type, compress and write it. | |
bool | ReadCompressed (unsigned char *output, const int size, const bool unsignedData) |
Assume the input source points to a compressed native type. Decompress and read it. | |
void | ReverseBytes (unsigned char *input, unsigned char *output, int length) |
bool | DoEndianSwap (void) const |
Private Attributes | |
int | numberOfBitsUsed |
int | numberOfBitsAllocated |
int | readOffset |
unsigned char * | data |
bool | copyData |
true if the internal buffer is copy of the data passed to the constructor | |
unsigned char | stackData [BITSTREAM_STACK_ALLOCATION_SIZE] |
BitStreams that use less than BITSTREAM_STACK_ALLOCATION_SIZE use the stack, rather than the heap to store data. It switches over if BITSTREAM_STACK_ALLOCATION_SIZE is exceeded. |
|
Create the bitstream, with some number of bytes to immediately allocate. There is no benefit to calling this, unless you know exactly how many bytes you need and it is greater than BITSTREAM_STACK_ALLOCATION_SIZE. In that case all it does is save you one or more realloc calls.
|
|
Initialize the BitStream, immediately setting the data it contains to a predefined pointer. Set _copyData to true if you want to make an internal copy of the data you are passing. Set it to false to just save a pointer to the data. You shouldn't call Write functions with _copyData as false, as this will write to unallocated memory 99% of the time you will use this function to cast Packet::data to a bitstream for reading, in which case you should write something as follows:
///
|
|
Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning. |
|
Align the next write and/or read to a byte boundary. This can be used to 'waste' bits to byte align for efficiency reasons It can also be used to force coalesced bitstreams to start on byte boundaries so so WriteAlignedBits and ReadAlignedBits both calculate the same offset when aligning. |
|
If we used the constructor version with copy data off, this makes sure it is set to on and the data pointed to is copied. |
|
This is good to call when you are done with the stream to make sure you didn't leave any data left over void |
|
Makes a copy of the internal data for you _data will point to the stream. Returns the length in bits of the stream. Partial bytes are left aligned
|
|
Gets the data that BitStream is writing to / reading from Partial bytes are left aligned.
|
|
Ignore data we don't intend to read
|
|
Read an networkID from a bitstream
|
|
Read a playerID from a bitstream
|
|
Read a bool from a bitstream
|
|
Read an array or casted stream of byte. The array is raw data. There is no automatic endian conversion with this function
|
|
Read any integral type from a bitstream. Define __BITSTREAM_NATIVE_END if you need endian swapping.
|
|
Read bits, starting at the next aligned bits. Note that the modulus 8 starting offset of the sequence must be the same as was used with WriteBits. This will be a problem with packet coalescence unless you byte align the coalesced packets.
|
|
Read numberOfBitsToRead bits to the output source alignBitsToRight should be set to true to convert internal bitstream data to userdata. It should be false if you used WriteBits with rightAlignedBits false
|
|
Read any integral type from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
|
|
Read a bool from a bitstream
|
|
Read any integral type from a bitstream. If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. the current value will be updated. For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte ReadCompressedDelta is only valid from a previous call to WriteDelta
|
|
Read a bool from a bitstream
|
|
Read any integral type from a bitstream. If the written value differed from the value compared against in the write function, var will be updated. Otherwise it will retain the current value. ReadDelta is only valid from a previous call to WriteDelta
|
|
Read a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes.
|
|
Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.
|
|
Read an orthogonal matrix from a quaternion, reading 3 components of the quaternion in 2 bytes each and extrapolatig the 4th. for 6 bytes instead of 36 Lossy, although the result is renormalized |
|
Read 3 floats or doubles, using 10 bytes, where those float or doubles comprise a vector Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
|
|
Bidirectional serialize/deserialize an array or casted stream or raw data. This does NOT do endian swapping.
|
|
Bidirectional serialize/deserialize any integral type to/from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
|
|
Bidirectional serialize/deserialize numberToSerialize bits to/from the input. Right aligned data means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation) You would set this to true when writing user data, and false when copying bitstream data, such as writing one bitstream to another
|
|
Bidirectional serialize/deserialize any integral type to/from a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type
|
|
Bidirectional serialize/deserialize any integral type to/from a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
|
|
Bidirectional version of SerializeDelta when you don't know what the last value is, or there is no last value.
|
|
Bidirectional serialize/deserialize any integral type to/from a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written
|
|
Bidirectional serialize/deserialize a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
|
|
Bidirectional serialize/deserialize a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.
|
|
Bidirectional serialize/deserialize an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each for 6 bytes instead of 36 Lossy, although the result is renormalized |
|
Bidirectional serialize/deserialize a vector, using 10 bytes instead of 12. Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
|
|
Set the stream to some initial data. |
|
Use this if you pass a pointer copy to the constructor (_copyData==false) and want to overallocate to prevent reallocation |
|
Move the write pointer to a position on the array.
|
|
Write an networkID to a bitstream
|
|
Write a playerID to a bitstream
|
|
Write a bool to a bitstream
|
|
Write one bitstream to another
|
|
Write an array or casted stream or raw data. This does NOT do endian swapping.
|
|
Write any integral type to a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping.
|
|
Align the bitstream to the byte boundary and then write the specified number of bits. This is faster than WriteBits but wastes the bits to do the alignment and requires you to call ReadAlignedBits at the corresponding read position.
|
|
Write numberToWrite bits from the input source Right aligned data means in the case of a partial byte, the bits are aligned from the right (bit 0) rather than the left (as in the normal internal representation) You would set this to true when writing user data, and false when copying bitstream data, such as writing one bitstream to another
|
|
Write any integral type to a bitstream. Undefine __BITSTREAM_NATIVE_END if you need endian swapping. If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type
|
|
Write a bool delta. Same thing as just calling Write
|
|
Write any integral type to a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written For floating point, this is lossy, using 2 bytes for a float and 4 for a double. The range must be between -1 and +1. For non-floating point, this is lossless, but only has benefit if you use less than half the range of the type If you are not using __BITSTREAM_NATIVE_END the opposite is true for types larger than 1 byte
|
|
Write a bool delta. Same thing as just calling Write
|
|
Write a playerID. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written
|
|
Write a playerID. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written
|
|
WriteDelta when you don't know what the last value is, or there is no last value.
|
|
Write any integral type to a bitstream. If the current value is different from the last value the current value will be written. Otherwise, a single bit will be written
|
|
Write a normalized quaternion in 6 bytes + 4 bits instead of 16 bytes. Slightly lossy.
|
|
Read a normalized 3D vector, using (at most) 4 bytes + 3 bits instead of 12-24 bytes. Will further compress y or z axis aligned vectors. Accurate to 1/32767.5.
|
|
Write an orthogonal matrix by creating a quaternion, and writing 3 components of the quaternion in 2 bytes each for 6 bytes instead of 36 Lossy, although the result is renormalized |
|
Write a vector, using 10 bytes instead of 12. Loses accuracy to about 3/10ths and only saves 2 bytes, so only use if accuracy is not important.
|