diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 23fc87c3..4b1779d4 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -458,14 +458,13 @@ void BlocksRenderer::build(const Chunk* chunk, const ChunksStorage* chunks) { render(voxels); } -Mesh* BlocksRenderer::createMesh() { +std::shared_ptr BlocksRenderer::createMesh() { const vattr attrs[]{ {3}, {2}, {1}, {0} }; size_t vcount = vertexOffset / BlocksRenderer::VERTEX_SIZE; - Mesh* mesh = new Mesh(vertexBuffer, vcount, indexBuffer, indexSize, attrs); - return mesh; + return std::make_shared(vertexBuffer, vcount, indexBuffer, indexSize, attrs); } -Mesh* BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) { +std::shared_ptr BlocksRenderer::render(const Chunk* chunk, const ChunksStorage* chunks) { build(chunk, chunks); return createMesh(); } diff --git a/src/graphics/render/BlocksRenderer.hpp b/src/graphics/render/BlocksRenderer.hpp index 88596c86..23b6d625 100644 --- a/src/graphics/render/BlocksRenderer.hpp +++ b/src/graphics/render/BlocksRenderer.hpp @@ -94,8 +94,8 @@ public: virtual ~BlocksRenderer(); void build(const Chunk* chunk, const ChunksStorage* chunks); - Mesh* render(const Chunk* chunk, const ChunksStorage* chunks); - Mesh* createMesh(); + std::shared_ptr render(const Chunk* chunk, const ChunksStorage* chunks); + std::shared_ptr createMesh(); VoxelsVolume* getVoxelsBuffer() const; }; diff --git a/src/graphics/render/ChunksRenderer.cpp b/src/graphics/render/ChunksRenderer.cpp index 6781c971..7f3f3959 100644 --- a/src/graphics/render/ChunksRenderer.cpp +++ b/src/graphics/render/ChunksRenderer.cpp @@ -41,7 +41,7 @@ ChunksRenderer::ChunksRenderer( "chunks-render-pool", [=](){return std::make_shared(level, cache, settings);}, [=](RendererResult& mesh){ - meshes[mesh.key].reset(mesh.renderer->createMesh()); + meshes[mesh.key] = mesh.renderer->createMesh(); inwork.erase(mesh.key); }) { @@ -59,7 +59,7 @@ std::shared_ptr ChunksRenderer::render(std::shared_ptr chunk, bool chunk->setModified(false); if (important) { - std::shared_ptr mesh (renderer->render(chunk.get(), level->chunksStorage.get())); + auto mesh = renderer->render(chunk.get(), level->chunksStorage.get()); meshes[glm::ivec2(chunk->x, chunk->z)] = mesh; return mesh; } diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 05164275..f6dda983 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -121,8 +121,8 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) { continue; indices.push_back(i); } - int px = camera->position.x / (float)CHUNK_W - 0.5f; - int pz = camera->position.z / (float)CHUNK_D - 0.5f; + float px = camera->position.x / (float)CHUNK_W - 0.5f; + float pz = camera->position.z / (float)CHUNK_D - 0.5f; std::sort(indices.begin(), indices.end(), [chunks, px, pz](auto i, auto j) { const auto& a = chunks->chunks[i]; const auto& b = chunks->chunks[j]; diff --git a/src/voxels/Chunks.cpp b/src/voxels/Chunks.cpp index 11c7fb6b..42f70114 100644 --- a/src/voxels/Chunks.cpp +++ b/src/voxels/Chunks.cpp @@ -39,7 +39,7 @@ voxel* Chunks::get(int32_t x, int32_t y, int32_t z) { int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) return nullptr; - std::shared_ptr chunk = chunks[cz * w + cx]; + auto& chunk = chunks[cz * w + cx]; // not thread safe if (chunk == nullptr) return nullptr; int lx = x - cx * CHUNK_W; @@ -103,7 +103,7 @@ ubyte Chunks::getLight(int32_t x, int32_t y, int32_t z, int channel){ int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) return 0; - auto chunk = chunks[(cy * d + cz) * w + cx]; + const auto& chunk = chunks[(cy * d + cz) * w + cx]; if (chunk == nullptr) return 0; int lx = x - cx * CHUNK_W; @@ -120,7 +120,7 @@ light_t Chunks::getLight(int32_t x, int32_t y, int32_t z){ int cz = floordiv(z, CHUNK_D); if (cx < 0 || cy < 0 || cz < 0 || cx >= int(w) || cy >= 1 || cz >= int(d)) return 0; - auto chunk = chunks[(cy * d + cz) * w + cx]; + const auto& chunk = chunks[(cy * d + cz) * w + cx]; if (chunk == nullptr) return 0; int lx = x - cx * CHUNK_W; diff --git a/src/voxels/ChunksStorage.cpp b/src/voxels/ChunksStorage.cpp index 3cc59bd3..fa28f4f6 100644 --- a/src/voxels/ChunksStorage.cpp +++ b/src/voxels/ChunksStorage.cpp @@ -1,8 +1,5 @@ #include "ChunksStorage.hpp" -#include -#include - #include "VoxelsVolume.hpp" #include "Chunk.hpp" #include "Block.hpp" @@ -14,6 +11,9 @@ #include "../lighting/Lightmap.hpp" #include "../items/Inventories.hpp" #include "../typedefs.hpp" +#include "../debug/Logger.hpp" + +static debug::Logger logger("chunks-storage"); ChunksStorage::ChunksStorage(Level* level) : level(level) { } @@ -41,10 +41,11 @@ static void verifyLoadedChunk(ContentIndices* indices, Chunk* chunk) { for (size_t i = 0; i < CHUNK_VOL; i++) { blockid_t id = chunk->voxels[i].id; if (indices->getBlockDef(id) == nullptr) { - std::cout << "corruped block detected at " << i << " of chunk "; - std::cout << chunk->x << "x" << chunk->z; - std::cout << " -> " << (int)id << std::endl; - chunk->voxels[i].id = 11; + auto logline = logger.error(); + logline << "corruped block detected at " << i << " of chunk "; + logline << chunk->x << "x" << chunk->z; + logline << " -> " << id; + chunk->voxels[i].id = BLOCK_AIR; } } } @@ -101,7 +102,7 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const { // cw*ch chunks will be scanned for (int cz = scz; cz < scz + ch; cz++) { for (int cx = scx; cx < scx + cw; cx++) { - auto found = chunksMap.find(glm::ivec2(cx, cz)); + const auto& found = chunksMap.find(glm::ivec2(cx, cz)); if (found == chunksMap.end()) { // no chunk loaded -> filling with BLOCK_VOID for (int ly = y; ly < y + h; ly++) {