Skip to content

Commit

Permalink
memory leak is less but still there
Browse files Browse the repository at this point in the history
  • Loading branch information
Kade-github committed Apr 17, 2024
1 parent e414610 commit 435e964
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 84 deletions.
115 changes: 57 additions & 58 deletions src/Game/Objects/Base/Chunk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,57 +225,57 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)
if (w.z > CHUNK_SIZE - 1)
return;

subChunk& sbc = GetSubChunk(y);
subChunk* sbc = GetSubChunk(y);

if (sbc.y <= -1 && id > 0) // cant create air
if (sbc->y <= -1 && id > 0) // cant create air
{
// create subchunk

myData.blocks[(int)w.x][(int)w.z][(int)w.y] = id;

sbc = CreateSubChunk(y);

if (sbc.y > -1)
if (sbc->y > -1)
subChunks.push_back(sbc);
}
else
{
if (sbc.blocks[(int)w.x][(int)w.z] != nullptr) // if it exists, delete it if necessary
if (sbc->blocks[(int)w.x][(int)w.z] != nullptr) // if it exists, delete it if necessary
{
if (id <= 0)
delete sbc.blocks[(int)w.x][(int)w.z];
delete sbc->blocks[(int)w.x][(int)w.z];
}

if (id <= 0) // destroyed block
{
sbc.blocks[(int)w.x][(int)w.z] = nullptr;
sbc->blocks[(int)w.x][(int)w.z] = nullptr;
myData.blocks[(int)w.x][(int)w.z][(int)w.y] = 0;
}
else
{
sbc.blocks[(int)w.x][(int)w.z] = CreateBlock(w.x, w.y, w.z, id);
sbc->blocks[(int)w.x][(int)w.z] = CreateBlock(w.x, w.y, w.z, id);

myData.blocks[(int)w.x][(int)w.z][(int)w.y] = id;
}
}

subChunk sbcBelow = GetSubChunk(y - 1);
subChunk* sbcBelow = GetSubChunk(y - 1);

if (sbcBelow.y <= -1) // create below
if (sbcBelow->y <= -1) // create below
{
sbcBelow = CreateSubChunk(y - 1);

if (sbcBelow.y > -1)
if (sbcBelow->y > -1)
subChunks.push_back(sbcBelow);
}

subChunk sbcAbove = GetSubChunk(y + 1);
subChunk* sbcAbove = GetSubChunk(y + 1);

if (sbcAbove.y <= -1) // create above
if (sbcAbove->y <= -1) // create above
{
sbcAbove = CreateSubChunk(y + 1);

if (sbcAbove.y > -1)
if (sbcAbove->y > -1)
subChunks.push_back(sbcAbove);
}

Expand All @@ -291,13 +291,13 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)
{
// check if we need to create a subchunk

subChunk s = c->GetSubChunk(y);
subChunk* s = c->GetSubChunk(y);

if (s.y <= -1)
if (s->y <= -1)
{
s = c->CreateSubChunk(y);

if (s.y > -1)
if (s->y > -1)
c->subChunks.push_back(s);

gp->QueueLoadBlocks(c);
Expand All @@ -311,13 +311,13 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)

if (c != nullptr)
{
subChunk s = c->GetSubChunk(y);
subChunk* s = c->GetSubChunk(y);

if (s.y <= -1)
if (s->y <= -1)
{
s = c->CreateSubChunk(y);

if (s.y > -1)
if (s->y > -1)
c->subChunks.push_back(s);

gp->QueueLoadBlocks(c);
Expand All @@ -331,13 +331,13 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)

if (c != nullptr)
{
subChunk s = c->GetSubChunk(y);
subChunk* s = c->GetSubChunk(y);

if (s.y <= -1)
if (s->y <= -1)
{
s = c->CreateSubChunk(y);

if (s.y > -1)
if (s->y > -1)
c->subChunks.push_back(s);

gp->QueueLoadBlocks(c);
Expand All @@ -351,13 +351,13 @@ void Chunk::ModifyBlock(float x, float y, float z, int id)

if (c != nullptr)
{
subChunk s = c->GetSubChunk(y);
subChunk* s = c->GetSubChunk(y);

if (s.y <= -1)
if (s->y <= -1)
{
s = c->CreateSubChunk(y);

if (s.y > -1)
if (s->y > -1)
c->subChunks.push_back(s);

gp->QueueLoadBlocks(c);
Expand Down Expand Up @@ -460,16 +460,16 @@ void Chunk::CreateFaces(Block* b)
}
}

void Chunk::RenderSubChunk(subChunk& sbc)
void Chunk::RenderSubChunk(subChunk* sbc)
{
if (sbc.y <= -1)
if (sbc->y <= -1)
return;

for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
Block* block = sbc.getBlock(x, z);
Block* block = sbc->getBlock(x, z);
if (block == nullptr)
continue;

Expand All @@ -492,8 +492,8 @@ void Chunk::RenderSubChunks()

for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];
if (sbc.y <= -1)
subChunk* sbc = subChunks[i];
if (sbc->y <= -1)
continue;

RenderSubChunk(sbc);
Expand All @@ -512,19 +512,16 @@ Chunk::Chunk(Texture* _txp, glm::vec3 _pos) : GameObject(_pos)
txp = _txp;
}

subChunk& Chunk::GetSubChunk(int y)
subChunk* Chunk::GetSubChunk(int y)
{
static subChunk empty;

for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];
subChunk* sbc = subChunks[i];

if (sbc.y == y)
if (sbc->y == y)
return sbc;
}
empty.y = -1;
return empty;
return nullptr;
}

Data::Chunk Chunk::GetChunkData()
Expand All @@ -535,9 +532,9 @@ Data::Chunk Chunk::GetChunkData()

for (int y = 0; y < CHUNK_HEIGHT; y++)
{
subChunk& sbc = GetSubChunk(y);
subChunk* sbc = GetSubChunk(y);

if (sbc.y <= -1) // not loaded
if (sbc->y <= -1) // not loaded
{
for (int x = 0; x < CHUNK_SIZE; x++)
{
Expand All @@ -551,8 +548,8 @@ Data::Chunk Chunk::GetChunkData()
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
if (sbc.blocks[x][z] != nullptr)
c.blocks[x][z][y] = sbc.blocks[x][z]->type;
if (sbc->blocks[x][z] != nullptr)
c.blocks[x][z][y] = sbc->blocks[x][z]->type;
else
c.blocks[x][z][y] = 0;
}
Expand All @@ -574,16 +571,16 @@ bool Chunk::IsInChunk(float x, float z)
}


void Chunk::RenderSubChunkShadow(subChunk& sbc)
void Chunk::RenderSubChunkShadow(subChunk* sbc)
{
if (sbc.y <= -1)
if (sbc->y <= -1)
return;

for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
Block* block = sbc.getBlock(x, z);
Block* block = sbc->getBlock(x, z);
if (block == nullptr)
continue;

Expand Down Expand Up @@ -632,19 +629,19 @@ void Chunk::RenderSubChunksShadow()

for (int i = 0; i < subChunks.size(); i++)
{
subChunk& sbc = subChunks[i];
if (sbc.y <= -1)
subChunk* sbc = subChunks[i];
if (sbc->y <= -1)
continue;

RenderSubChunkShadow(sbc);
}
}

subChunk Chunk::CreateSubChunk(int y)
subChunk* Chunk::CreateSubChunk(int y)
{
subChunk sbc;
subChunk* sbc = new subChunk();

sbc.y = y;
sbc->y = y;

bool isOccluded = true;

Expand Down Expand Up @@ -676,14 +673,14 @@ subChunk Chunk::CreateSubChunk(int y)
isOccluded = false;
}

sbc.blocks[x][z] = CreateBlock(x, y, z, id);
sbc->blocks[x][z] = CreateBlock(x, y, z, id);
}
}

if (isOccluded)
{
DestroySubChunk(sbc);
return subChunk();
return nullptr;
}

return sbc;
Expand Down Expand Up @@ -734,30 +731,32 @@ Block* Chunk::CreateBlock(int x, int y, int z, int id)

void Chunk::DestroySubChunk(int y)
{
subChunk& sbc = GetSubChunk(y);
subChunk* sbc = GetSubChunk(y);

DestroySubChunk(sbc);
}

void Chunk::DestroySubChunk(subChunk& c)
void Chunk::DestroySubChunk(subChunk* c)
{
for (int x = 0; x < CHUNK_SIZE; x++)
{
for (int z = 0; z < CHUNK_SIZE; z++)
{
Block* b = c.blocks[x][z];
Block* b = c->blocks[x][z];
if (b != nullptr)
std::free(b);

c.blocks[x][z] = nullptr;
c->blocks[x][z] = nullptr;
}
}

delete c;
}

void Chunk::DestroySubChunks()
{
for (int i = 0; i < subChunks.size(); i++)
DestroySubChunk(subChunks[i].y);
DestroySubChunk(subChunks[i]->y);

subChunks.clear();
}
Expand All @@ -768,8 +767,8 @@ void Chunk::CreateSubChunks()

for (int y = CHUNK_HEIGHT - 1; y > -1; y--)
{
subChunk sbc = CreateSubChunk(y);
if (sbc.y > -1)
subChunk* sbc = CreateSubChunk(y);
if (sbc != nullptr)
subChunks.push_back(sbc);
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Game/Objects/Base/Chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class Chunk : public GameObject

Data::Chunk myData;

std::vector<subChunk> subChunks;
std::vector<subChunk*> subChunks;

subChunk& GetSubChunk(int y);
subChunk* GetSubChunk(int y);

Data::Chunk GetChunkData();

Expand All @@ -71,17 +71,17 @@ class Chunk : public GameObject

void ModifyBlock(float x, float y, float z, int id);

void RenderSubChunk(subChunk& c);
void RenderSubChunk(subChunk* c);
void RenderSubChunks();

void RenderSubChunkShadow(subChunk& c);
void RenderSubChunkShadow(subChunk* c);
void RenderSubChunksShadow();

subChunk CreateSubChunk(int y);
subChunk* CreateSubChunk(int y);
Block* CreateBlock(int x, int y, int z, int id);

void DestroySubChunk(int y);
void DestroySubChunk(subChunk& c);
void DestroySubChunk(subChunk* c);
void DestroySubChunks();

void CreateSubChunks();
Expand Down
6 changes: 3 additions & 3 deletions src/Game/Objects/Base/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ void Entity::Footstep()

if (c->DoesBlockExist(position.x, y, position.z))
{
subChunk& sb = c->GetSubChunk(y);
subChunk* sb = c->GetSubChunk(y);

if (sb.y < 0)
if (sb == nullptr)
return;

glm::vec3 _world = c->WorldToChunk(position);

Block* b = sb.getBlock(_world.x, _world.z);
Block* b = sb->getBlock(_world.x, _world.z);

if (b != nullptr)
{
Expand Down
Loading

0 comments on commit 435e964

Please sign in to comment.