1
0

Fixed cChunkData formatting.

This commit is contained in:
madmaxoft 2014-05-28 11:35:55 +02:00
parent 5a9e49fbeb
commit 7ec44951a0
2 changed files with 101 additions and 39 deletions

View File

@ -5,64 +5,86 @@
cChunkData::cChunkData() cChunkData::cChunkData()
#if __cplusplus < 201103L #if __cplusplus < 201103L
// auto_ptr style interface for memory management // auto_ptr style interface for memory management
: IsOwner(true) : m_IsOwner(true)
#endif #endif
{ {
memset(m_Sections, 0, sizeof(m_Sections)); for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
m_Sections[i] = NULL;
}
} }
cChunkData::~cChunkData() cChunkData::~cChunkData()
{ {
#if __cplusplus < 201103L #if __cplusplus < 201103L
// auto_ptr style interface for memory management // auto_ptr style interface for memory management
if (!IsOwner) if (!m_IsOwner)
{ {
return; return;
} }
#endif #endif
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
if (m_Sections[i] == NULL) Free(m_Sections[i]);; Free(m_Sections[i]);
} }
} }
#if __cplusplus < 201103L #if __cplusplus < 201103L
// auto_ptr style interface for memory management // auto_ptr style interface for memory management
cChunkData::cChunkData(const cChunkData& other) : cChunkData::cChunkData(const cChunkData & a_Other) :
IsOwner(true) m_IsOwner(true)
{ {
// Move contents and ownership from a_Other to this, pointer-wise:
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
m_Sections[i] = other.m_Sections[i]; m_Sections[i] = a_Other.m_Sections[i];
} }
other.IsOwner = false; a_Other.m_IsOwner = false;
} }
cChunkData& cChunkData::operator=(const cChunkData& other)
cChunkData & cChunkData::operator =(const cChunkData & a_Other)
{ {
if (&other != this) // If assigning to self, no-op
if (&a_Other == this)
{
return *this;
}
// Free any currently held contents:
if (m_IsOwner)
{ {
if (IsOwner)
{
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
if (m_Sections[i]) Free(m_Sections[i]);;
}
}
IsOwner = true;
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
m_Sections[i] = other.m_Sections[i]; Free(m_Sections[i]);
} }
other.IsOwner = false;
} }
// Move contents and ownership from a_Other to this, pointer-wise:
m_IsOwner = true;
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{
m_Sections[i] = a_Other.m_Sections[i];
}
a_Other.m_IsOwner = false;
return *this; return *this;
} }
#else #else
// unique_ptr style interface for memory management // unique_ptr style interface for memory management
cChunkData::cChunkData(cChunkData&& other) cChunkData::cChunkData(cChunkData && other)
{ {
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
@ -70,14 +92,18 @@ cChunkData::~cChunkData()
other.m_Sections[i] = NULL; other.m_Sections[i] = NULL;
} }
} }
cChunkData& cChunkData::operator=(cChunkData&& other) cChunkData & cChunkData::operator =(cChunkData && other)
{ {
if (&other != this) if (&other != this)
{ {
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
Free(m_Sections[i]);; Free(m_Sections[i]);
m_Sections[i] = other.m_Sections[i]; m_Sections[i] = other.m_Sections[i];
other.m_Sections[i] = NULL; other.m_Sections[i] = NULL;
} }
@ -86,6 +112,10 @@ cChunkData::~cChunkData()
} }
#endif #endif
BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const
{ {
ASSERT((a_X >= 0) && (a_X < cChunkDef::Width)); ASSERT((a_X >= 0) && (a_X < cChunkDef::Width));
@ -103,6 +133,10 @@ BLOCKTYPE cChunkData::GetBlock(int a_X, int a_Y, int a_Z) const
} }
} }
void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block) void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block)
{ {
if ( if (
@ -134,6 +168,10 @@ void cChunkData::SetBlock(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a_Block)
m_Sections[Section]->m_BlockTypes[Index] = a_Block; m_Sections[Section]->m_BlockTypes[Index] = a_Block;
} }
NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
{ {
if ( if (
@ -156,6 +194,10 @@ NIBBLETYPE cChunkData::GetMeta(int a_RelX, int a_RelY, int a_RelZ) const
return 0; return 0;
} }
bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble) bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble)
{ {
if ( if (
@ -192,9 +234,17 @@ bool cChunkData::SetMeta(int a_RelX, int a_RelY, int a_RelZ, NIBBLETYPE a_Nibble
return oldval == a_Nibble; return oldval == a_Nibble;
} }
NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const
{ {
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) if (
(a_RelX < cChunkDef::Width) && (a_RelX > -1) &&
(a_RelY < cChunkDef::Height) && (a_RelY > -1) &&
(a_RelZ < cChunkDef::Width) && (a_RelZ > -1)
)
{ {
int Section = a_RelY / CHUNK_SECTION_HEIGHT; int Section = a_RelY / CHUNK_SECTION_HEIGHT;
if (m_Sections[Section] != NULL) if (m_Sections[Section] != NULL)
@ -211,6 +261,10 @@ NIBBLETYPE cChunkData::GetBlockLight(int a_RelX, int a_RelY, int a_RelZ) const
return 0; return 0;
} }
NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
{ {
if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1)) if ((a_RelX < cChunkDef::Width) && (a_RelX > -1) && (a_RelY < cChunkDef::Height) && (a_RelY > -1) && (a_RelZ < cChunkDef::Width) && (a_RelZ > -1))
@ -230,7 +284,11 @@ NIBBLETYPE cChunkData::GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const
return 0; return 0;
} }
cChunkData cChunkData::Copy() const
cChunkData cChunkData::Copy(void) const
{ {
cChunkData copy; cChunkData copy;
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
@ -248,11 +306,11 @@ cChunkData cChunkData::Copy() const
void cChunkData::CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const void cChunkData::CopyBlocks(BLOCKTYPE * a_dest, size_t a_Idx, size_t length) const
{ {
for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++) for (size_t i = 0; i < CHUNK_SECTION_COUNT; i++)
{ {
const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16; const size_t segment_length = CHUNK_SECTION_HEIGHT * 16 * 16;
if (a_Idx > 0) if (a_Idx > 0)
{ {
a_Idx = std::max(a_Idx - length, (size_t) 0); a_Idx = std::max(a_Idx - length, (size_t) 0);
@ -588,6 +646,8 @@ cChunkData::sChunkSection * cChunkData::Allocate() const
void cChunkData::Free(cChunkData::sChunkSection * ptr) const void cChunkData::Free(cChunkData::sChunkSection * ptr) const
{ {
delete ptr; delete ptr;
@ -595,6 +655,8 @@ void cChunkData::Free(cChunkData::sChunkSection * ptr) const
void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const void cChunkData::ZeroSection(cChunkData::sChunkSection * ptr) const
{ {
memset( memset(

View File

@ -25,12 +25,12 @@ public:
#if __cplusplus < 201103L #if __cplusplus < 201103L
// auto_ptr style interface for memory management // auto_ptr style interface for memory management
cChunkData(const cChunkData& other); cChunkData(const cChunkData & other);
cChunkData& operator=(const cChunkData& other); cChunkData & operator =(const cChunkData & other);
#else #else
// unique_ptr style interface for memory management // unique_ptr style interface for memory management
cChunkData(cChunkData&& other); cChunkData(cChunkData && other);
cChunkData& operator=(cChunkData&& other); cChunkData & operator =(cChunkData && other);
#endif #endif
BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const; BLOCKTYPE GetBlock(int a_X, int a_Y, int a_Z) const;
@ -43,15 +43,15 @@ public:
NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const; NIBBLETYPE GetSkyLight(int a_RelX, int a_RelY, int a_RelZ) const;
cChunkData Copy() const; cChunkData Copy(void) const;
void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const; void CopyBlocks (BLOCKTYPE * a_dest, size_t a_Idx = 0, size_t length = cChunkDef::NumBlocks) const;
void CopyMeta (NIBBLETYPE * a_dest) const; void CopyMeta (NIBBLETYPE * a_dest) const;
void CopyBlockLight (NIBBLETYPE * a_dest) const; void CopyBlockLight(NIBBLETYPE * a_dest) const;
void CopySkyLight (NIBBLETYPE * a_dest) const; void CopySkyLight (NIBBLETYPE * a_dest) const;
void SetBlocks (const BLOCKTYPE * a_src); void SetBlocks (const BLOCKTYPE * a_src);
void SetMeta (const NIBBLETYPE * a_src); void SetMeta (const NIBBLETYPE * a_src);
void SetBlockLight (const NIBBLETYPE * a_src); void SetBlockLight(const NIBBLETYPE * a_src);
void SetSkyLight (const NIBBLETYPE * a_src); void SetSkyLight (const NIBBLETYPE * a_src);
private: private:
@ -61,19 +61,19 @@ private:
#if __cplusplus < 201103L #if __cplusplus < 201103L
// auto_ptr style interface for memory management // auto_ptr style interface for memory management
mutable bool IsOwner; mutable bool m_IsOwner;
#endif #endif
struct sChunkSection { struct sChunkSection {
BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16] ; BLOCKTYPE m_BlockTypes [CHUNK_SECTION_HEIGHT * 16 * 16];
NIBBLETYPE m_BlockMeta [CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; NIBBLETYPE m_BlockMeta [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
NIBBLETYPE m_BlockLight [CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; NIBBLETYPE m_BlockLight [CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2]; NIBBLETYPE m_BlockSkyLight[CHUNK_SECTION_HEIGHT * 16 * 16 / 2];
}; };
sChunkSection *m_Sections[CHUNK_SECTION_COUNT]; sChunkSection * m_Sections[CHUNK_SECTION_COUNT];
sChunkSection * Allocate() const; sChunkSection * Allocate(void) const;
void Free(sChunkSection * ptr) const; void Free(sChunkSection * ptr) const;
void ZeroSection(sChunkSection * ptr) const; void ZeroSection(sChunkSection * ptr) const;