From dc1c6404ceb9eb55b0d58e9982fc301345a9731d Mon Sep 17 00:00:00 2001 From: Glenn Fiedler Date: Wed, 27 Dec 2023 10:00:50 -0500 Subject: [PATCH] Update README.md --- README.md | 82 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 1fe07be..40464ea 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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 ); @@ -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 ); @@ -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 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 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 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.