From f7eb04a5f1c7025ca6e625f4df4bb3f3c66a7d91 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Sun, 12 Mar 2023 18:31:22 +0100 Subject: [PATCH] use unique_ptr when dumping intervalmap to array --- include/chunk.hpp | 6 +++--- include/intervalmap.hpp | 6 +++--- src/chunkmesher.cpp | 9 ++++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/include/chunk.hpp b/include/chunk.hpp index 40ee472..f69c5ec 100644 --- a/include/chunk.hpp +++ b/include/chunk.hpp @@ -15,7 +15,7 @@ #include "intervalmap.hpp" #include "shader.hpp" -#define CHUNK_SIZE 16 +#define CHUNK_SIZE 2 #define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) #define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1) @@ -47,7 +47,7 @@ namespace Chunk void setBlocks(int start, int end, Block b); Block getBlock(int x, int y, int z); IntervalMap& getBlocks() { return (this->blocks); } - Block* getBlocksArray(int* len) { return (this->blocks.toArray(len)); } + std::unique_ptr getBlocksArray(int* len) { return (this->blocks.toArray(len)); } public: GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, vIndex{0}; @@ -66,4 +66,4 @@ namespace Chunk }; }; -#endif \ No newline at end of file +#endif diff --git a/include/intervalmap.hpp b/include/intervalmap.hpp index 5f6151e..d5e48d1 100644 --- a/include/intervalmap.hpp +++ b/include/intervalmap.hpp @@ -63,7 +63,7 @@ public: std::cout << "end key: " << std::prev(treemap.end())->first << "\n"; } - V *toArray(int *length) + std::unique_ptr toArray(int *length) { if (treemap.empty()) { @@ -78,7 +78,7 @@ public: if(*length == 0) return nullptr; // std::cout << "Length: " << *length << "\n"; - V *arr{new V[*length]}; + std::unique_ptr arr(new V[*length]); auto start = treemap.begin(); for (auto i = std::next(treemap.begin()); i != treemap.end(); i++) @@ -117,4 +117,4 @@ private: std::map treemap{}; }; -#endif \ No newline at end of file +#endif diff --git a/src/chunkmesher.cpp b/src/chunkmesher.cpp index 3c6bb3b..80ee130 100755 --- a/src/chunkmesher.cpp +++ b/src/chunkmesher.cpp @@ -42,8 +42,10 @@ void mesh(Chunk::Chunk* chunk) // convert tree to array since it is easier to work with it int length{0}; - Block *blocks = chunk->getBlocksArray(&length); - if(length == 0) return; + std::unique_ptr blocks = chunk->getBlocksArray(&length); + if(length == 0) { + return; + } int k, l, u, v, w, h, n, j, i; int x[]{0, 0, 0}; @@ -179,9 +181,6 @@ void mesh(Chunk::Chunk* chunk) } } } - - delete[] blocks; - } void draw(Chunk::Chunk* chunk, glm::mat4 model)