From 3201d1bf169ca99e4cdbfad7601f6aed89b3c76c Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 4 Apr 2014 23:16:52 +0100 Subject: Nibbletypes are compressed + Added nibble compression * Fixed an off by one --- src/ChunkDef.h | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) (limited to 'src/ChunkDef.h') diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 9c7753820..3793bd95a 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -230,6 +230,21 @@ public: ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); return 0; } + + + static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int a_BlockIdx) + { + if ((a_BlockIdx > -1) && (a_BlockIdx < NumBlocks)) + { + if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + { + return 0; + } + return (a_Buffer[a_BlockIdx / 2] >> ((a_BlockIdx & 1) * 4)) & 0x0f; + } + ASSERT(!"cChunkDef::GetNibble(): index out of chunk range!"); + return 0; + } static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, int x, int y, int z) @@ -244,6 +259,22 @@ public: } + static NIBBLETYPE GetNibble(const std::vector & a_Buffer, int x, int y, int z) + { + if ((x < Width) && (x > -1) && (y < Height) && (y > -1) && (z < Width) && (z > -1)) + { + int Index = MakeIndexNoCheck(x, y, z); + if (a_Buffer.empty() || (Index / 2 > a_Buffer.size() - 1)) + { + return 0; + } + return (a_Buffer[Index / 2] >> ((Index & 1) * 4)) & 0x0f; + } + ASSERT(!"cChunkDef::GetNibble(): coords out of chunk range!"); + return 0; + } + + static void SetNibble(NIBBLETYPE * a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) { if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) @@ -256,6 +287,24 @@ public: ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set ); } + + + static void SetNibble(std::vector & a_Buffer, int a_BlockIdx, NIBBLETYPE a_Nibble) + { + if ((a_BlockIdx < 0) || (a_BlockIdx >= NumBlocks)) + { + ASSERT(!"cChunkDef::SetNibble(): index out of range!"); + return; + } + if (a_Buffer.empty() || (a_BlockIdx / 2 > a_Buffer.size() - 1)) + { + a_Buffer.resize((a_BlockIdx / 2) + 1); + } + a_Buffer[a_BlockIdx / 2] = static_cast( + (a_Buffer[a_BlockIdx / 2] & (0xf0 >> ((a_BlockIdx & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((a_BlockIdx & 1) * 4)) // The nibble being set + ); + } static void SetNibble(NIBBLETYPE * a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) @@ -278,13 +327,37 @@ public: } - inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos ) + static void SetNibble(std::vector & a_Buffer, int x, int y, int z, NIBBLETYPE a_Nibble) + { + if ( + (x >= Width) || (x < 0) || + (y >= Height) || (y < 0) || + (z >= Width) || (z < 0) + ) + { + ASSERT(!"cChunkDef::SetNibble(): index out of range!"); + return; + } + + int Index = MakeIndexNoCheck(x, y, z); + if (a_Buffer.empty() || (Index / 2 > a_Buffer.size() - 1)) + { + a_Buffer.resize((Index / 2) + 1); + } + a_Buffer[Index / 2] = static_cast( + (a_Buffer[Index / 2] & (0xf0 >> ((Index & 1) * 4))) | // The untouched nibble + ((a_Nibble & 0x0f) << ((Index & 1) * 4)) // The nibble being set + ); + } + + + inline static NIBBLETYPE GetNibble(const NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos) { return GetNibble(a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z ); } - inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value ) + inline static void SetNibble(NIBBLETYPE * a_Buffer, const Vector3i & a_BlockPos, NIBBLETYPE a_Value) { SetNibble( a_Buffer, a_BlockPos.x, a_BlockPos.y, a_BlockPos.z, a_Value ); } -- cgit v1.2.3