Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames authored Dec 27, 2023
1 parent a87067d commit dc1c640
Showing 1 changed file with 63 additions and 19 deletions.
82 changes: 63 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ It has the following features:

# Usage

You can use the bitwriter and bitreader classes directly:
You can use the bitpacker directly:

```c++
const int BufferSize = 256;
Expand All @@ -24,11 +24,6 @@ You can use the bitwriter and bitreader classes directly:

serialize::BitWriter writer( buffer, BufferSize );

serialize_check( writer.GetData() == buffer );
serialize_check( writer.GetBitsWritten() == 0 );
serialize_check( writer.GetBytesWritten() == 0 );
serialize_check( writer.GetBitsAvailable() == BufferSize * 8 );

writer.WriteBits( 0, 1 );
writer.WriteBits( 1, 1 );
writer.WriteBits( 10, 8 );
Expand All @@ -38,23 +33,10 @@ You can use the bitwriter and bitreader classes directly:
writer.WriteBits( 9999999, 32 );
writer.FlushBits();

const int bitsWritten = 1 + 1 + 8 + 8 + 10 + 16 + 32;

serialize_check( writer.GetBytesWritten() == 10 );
serialize_check( writer.GetBitsWritten() == bitsWritten );
serialize_check( writer.GetBitsAvailable() == BufferSize * 8 - bitsWritten );

const int bytesWritten = writer.GetBytesWritten();

serialize_check( bytesWritten == 10 );

memset( buffer + bytesWritten, 0, BufferSize - bytesWritten );

serialize::BitReader reader( buffer, bytesWritten );

serialize_check( reader.GetBitsRead() == 0 );
serialize_check( reader.GetBitsRemaining() == bytesWritten * 8 );

uint32_t a = reader.ReadBits( 1 );
uint32_t b = reader.ReadBits( 1 );
uint32_t c = reader.ReadBits( 8 );
Expand All @@ -64,6 +46,68 @@ You can use the bitwriter and bitreader classes directly:
uint32_t g = reader.ReadBits( 32 );
```
Or you can write serialize methods for your types and these handle both read and write:
```c++
struct Vector
{
float x,y,z;
template <typename Stream> bool Serialize( Stream & stream )
{
serialize_float( stream, x );
serialize_float( stream, y );
serialize_float( stream, z );
return true;
}
};
struct Quaternion
{
float x,y,z,w;
template <typename Stream> bool Serialize( Stream & stream )
{
serialize_float( stream, x );
serialize_float( stream, y );
serialize_float( stream, z );
serialize_float( stream, w );
return true;
}
};
struct RigidBody
{
Vector position;
Quaternion orientation;
Vector linearVelocity;
Vector angularVelocity;
bool atRest;
template <typename Stream> bool Serialize( Stream & stream )
{
serialize_object( stream, position );
serialize_object( stream, orientation );
serialize_bool( stream, atRest );
if ( !atRest )
{
serialize_object( stream, position );
serialize_object( stream, orientation );
}
if ( Stream::IsReading && atRest )
{
linearVelocity.x = linearVelocity.y = linearVelocity.z = 0.0;
angularVelocity.x = angularVelocity.y = angularVelocity.z = 0.0;
}
return true;
}
};
```

# Author

The author of this library is Glenn Fiedler.
Expand Down

0 comments on commit dc1c640

Please sign in to comment.