use unique_ptr when dumping intervalmap to array

vertex-deduplication
EmaMaker 2023-03-12 18:31:22 +01:00
parent 383fb60686
commit f7eb04a5f1
3 changed files with 10 additions and 11 deletions

View File

@ -15,7 +15,7 @@
#include "intervalmap.hpp" #include "intervalmap.hpp"
#include "shader.hpp" #include "shader.hpp"
#define CHUNK_SIZE 16 #define CHUNK_SIZE 2
#define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE) #define CHUNK_VOLUME (CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE)
#define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1) #define CHUNK_MAX_INDEX (CHUNK_VOLUME - 1)
@ -47,7 +47,7 @@ namespace Chunk
void setBlocks(int start, int end, Block b); void setBlocks(int start, int end, Block b);
Block getBlock(int x, int y, int z); Block getBlock(int x, int y, int z);
IntervalMap<Block>& getBlocks() { return (this->blocks); } IntervalMap<Block>& getBlocks() { return (this->blocks); }
Block* getBlocksArray(int* len) { return (this->blocks.toArray(len)); } std::unique_ptr<Block[]> getBlocksArray(int* len) { return (this->blocks.toArray(len)); }
public: public:
GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, vIndex{0}; GLuint VAO{0}, VBO{0}, EBO{0}, colorBuffer{0}, vIndex{0};
@ -66,4 +66,4 @@ namespace Chunk
}; };
}; };
#endif #endif

View File

@ -63,7 +63,7 @@ public:
std::cout << "end key: " << std::prev(treemap.end())->first << "\n"; std::cout << "end key: " << std::prev(treemap.end())->first << "\n";
} }
V *toArray(int *length) std::unique_ptr<V[]> toArray(int *length)
{ {
if (treemap.empty()) if (treemap.empty())
{ {
@ -78,7 +78,7 @@ public:
if(*length == 0) return nullptr; if(*length == 0) return nullptr;
// std::cout << "Length: " << *length << "\n"; // std::cout << "Length: " << *length << "\n";
V *arr{new V[*length]}; std::unique_ptr<V[]> arr(new V[*length]);
auto start = treemap.begin(); auto start = treemap.begin();
for (auto i = std::next(treemap.begin()); i != treemap.end(); i++) for (auto i = std::next(treemap.begin()); i != treemap.end(); i++)
@ -117,4 +117,4 @@ private:
std::map<int, V> treemap{}; std::map<int, V> treemap{};
}; };
#endif #endif

View File

@ -42,8 +42,10 @@ void mesh(Chunk::Chunk* chunk)
// convert tree to array since it is easier to work with it // convert tree to array since it is easier to work with it
int length{0}; int length{0};
Block *blocks = chunk->getBlocksArray(&length); std::unique_ptr<Block[]> blocks = chunk->getBlocksArray(&length);
if(length == 0) return; if(length == 0) {
return;
}
int k, l, u, v, w, h, n, j, i; int k, l, u, v, w, h, n, j, i;
int x[]{0, 0, 0}; int x[]{0, 0, 0};
@ -179,9 +181,6 @@ void mesh(Chunk::Chunk* chunk)
} }
} }
} }
delete[] blocks;
} }
void draw(Chunk::Chunk* chunk, glm::mat4 model) void draw(Chunk::Chunk* chunk, glm::mat4 model)