chunkmgr: properly dispose allocated chunk memory

intervalmaps-array-y
EmaMaker 2022-11-11 20:36:39 +01:00 committed by emamaker
parent a5ddb0e37f
commit c8f429564c
7 changed files with 19 additions and 12 deletions

View File

@ -29,6 +29,7 @@ namespace Chunk
public:
Chunk(glm::vec3 pos = glm::vec3(0.0f)); // a default value for the argument satisfies the need for a default constructor when using the type in an unordered_map (i.e. in chunkmanager)
~Chunk();
public:
glm::vec3 getPosition() { return this->position; }

View File

@ -6,6 +6,7 @@ namespace chunkmanager
void init();
void update(float deltaTime);
void updateChunk(uint32_t, uint16_t, uint16_t, uint16_t);
void destroy();
}
#endif

View File

@ -14,10 +14,12 @@ class ChunkMesh
public:
ChunkMesh(Chunk::Chunk *c);
~ChunkMesh();
void mesh();
void draw();
Chunk::Chunk *chunk;
Chunk::Chunk *chunk{nullptr};
// static Shader theShader("shaders/shader.vs", "shaders/shader.fs");
private:
@ -33,7 +35,7 @@ private:
glm::mat4 model = glm::mat4(1.0f);
GLuint VAO, VBO, EBO, colorBuffer, vIndex{0};
Shader *theShader;
Shader *theShader{nullptr};
};
#endif

View File

@ -18,6 +18,10 @@ namespace Chunk
// std::cout << "CHUNK" << std::endl;
}
Chunk ::~Chunk()
{
}
Block Chunk::getBlock(int x, int y, int z)
{
return blocks[utils::coord3DTo1D(x, y, z, CHUNK_SIZE, CHUNK_SIZE, CHUNK_SIZE)];

View File

@ -12,7 +12,7 @@
#include <unordered_map>
#include <string>
std::unordered_map<std::uint32_t, ChunkMesh> chunks;
std::unordered_map<std::uint32_t, ChunkMesh *> chunks;
namespace chunkmanager
{
@ -86,17 +86,17 @@ namespace chunkmanager
// std::cout << "Checking" << i << ", " << j << ", " << k <<std::endl;
if (chunks.find(index) == chunks.end())
{
chunks.insert(std::make_pair(index, ChunkMesh(new Chunk::Chunk(glm::vec3(i, j, k)))));
generateChunk(chunks.at(index) .chunk);
chunks.at(index).mesh();
chunks.insert(std::make_pair(index, new ChunkMesh(new Chunk::Chunk(glm::vec3(i, j, k)))));
generateChunk(chunks.at(index)->chunk);
chunks.at(index)->mesh();
// std::cout << "Creating new chunk" << i << ", " << j << ", " << k <<std::endl;
}
else
chunks.at(index).draw();
chunks.at(index)->draw();
}
void destroy()
{
for (auto &n : chunks) delete n.second;
}
};

View File

@ -8,10 +8,8 @@
#include "utils.hpp"
ChunkMesh::~ChunkMesh(){
// std::cout << "Destroy" << this->chunk << std::endl;
// if(this->chunk != nullptr) delete this->chunk;
// this->chunk = nullptr;
// delete[] (this->theShader);
delete this->chunk;
delete (this->theShader);
}
ChunkMesh::ChunkMesh(Chunk::Chunk *c)

View File

@ -86,6 +86,7 @@ int main()
glfwPollEvents();
}
chunkmanager::destroy();
glfwTerminate();
return 0;
}