diff --git a/src/voxels/Chunk.h b/src/voxels/Chunk.h index 850530f7..0f67e884 100644 --- a/src/voxels/Chunk.h +++ b/src/voxels/Chunk.h @@ -16,8 +16,7 @@ public: Lightmap* lightmap; bool modified = true; bool ready = false; - bool accepted = false; - bool generated = false; + bool loaded = false; int references = 1; Chunk(int x, int y, int z); ~Chunk(); diff --git a/src/voxels/ChunksController.cpp b/src/voxels/ChunksController.cpp index 3024c346..8cdf29d1 100644 --- a/src/voxels/ChunksController.cpp +++ b/src/voxels/ChunksController.cpp @@ -73,7 +73,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){ return false; chunk = new Chunk(nearX+ox,nearY+oy,nearZ+oz); if (worldFiles->getChunk(chunk->x, chunk->z, (char*)chunk->voxels)) - chunk->generated = true; + chunk->loaded = true; chunks->chunks[index] = chunk; @@ -99,7 +99,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){ oz += 1; closes[(oy * 3 + oz) * 3 + ox] = other; } - freeLoader->perform(chunk, (const Chunk**)closes); + freeLoader->perform(chunk, (Chunk**)closes); return true; } @@ -144,29 +144,6 @@ bool ChunksController::_buildMeshes(VoxelRenderer* renderer, int tick) { Chunk* chunk = chunks->chunks[index]; if (chunk == nullptr){ - for (int y = 0; y < h; y++){ - for (int z = 1; z < d-1; z++){ - for (int x = 1; x < w-1; x++){ - int index = (y * d + z) * w + x; - chunk = chunks->chunks[index]; - if (chunk != nullptr && chunk->ready && !chunk->accepted){ - int lx = x - w / 2; - int ly = y - h / 2; - int lz = z - d / 2; - int distance = (lx * lx + ly * ly + lz * lz); - lighting->onChunkLoaded(chunk->x, chunk->y, chunk->z, false); - for (int i = 0; i < chunks->volume; i++){ - Chunk* other = chunks->chunks[i]; - if (other) - other->modified = true; - } - chunk->accepted = true; - std::cout << "1: built mesh for " << chunk << std::endl; - return true; - } - } - } - } return false; } Mesh* mesh = chunks->meshes[index]; diff --git a/src/voxels/ChunksLoader.cpp b/src/voxels/ChunksLoader.cpp index 67cfc08f..89963760 100644 --- a/src/voxels/ChunksLoader.cpp +++ b/src/voxels/ChunksLoader.cpp @@ -8,6 +8,8 @@ #include +#define CLOSES_C 27 + void ChunksLoader::_thread(){ Chunks chunks(3,3,3, -1,-1,-1); Lighting lighting(&chunks); @@ -17,25 +19,22 @@ void ChunksLoader::_thread(){ continue; } Chunk* chunk = current; - chunk->incref(); - for (size_t i = 0; i < 27; i++){ + chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1); + for (size_t i = 0; i < CLOSES_C; i++){ Chunk* other = closes[i]; if (other){ - other->incref(); chunks.putChunk(other); } } - chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1); - - if (!chunk->generated){ + if (!chunk->loaded){ WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z); } chunks.putChunk(chunk); lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true); chunks.clear(false); - for (int i = 0; i < 27; i++){ + for (int i = 0; i < CLOSES_C; i++){ Chunk* other = closes[i]; if (other) other->decref(); @@ -43,24 +42,26 @@ void ChunksLoader::_thread(){ chunk->ready = true; current = nullptr; chunk->decref(); - //std::cout << "LOADER: success" << std::endl; } } -void ChunksLoader::perform(Chunk* chunk, const Chunk** cs){ +void ChunksLoader::perform(Chunk* chunk, Chunk** closes_passed){ if (isBusy()){ std::cerr << "performing while busy" << std::endl; return; } + chunk->incref(); if (closes == nullptr){ - closes = new Chunk*[27]; + closes = new Chunk*[CLOSES_C]; } - for (int i = 0; i < 27; i++){ - const Chunk* other = cs[i]; + for (int i = 0; i < CLOSES_C; i++){ + Chunk* other = closes_passed[i]; if (other == nullptr) closes[i] = nullptr; - else - closes[i] = (Chunk*)other;//->clone(); + else { + other->incref(); + closes[i] = other; + } } current = chunk; } diff --git a/src/voxels/ChunksLoader.h b/src/voxels/ChunksLoader.h index 5eef4bc6..ac779b54 100644 --- a/src/voxels/ChunksLoader.h +++ b/src/voxels/ChunksLoader.h @@ -26,7 +26,7 @@ public: return current != nullptr; } - void perform(Chunk* chunk, const Chunk** closes); + void perform(Chunk* chunk, Chunk** closes_passed); void stop(){ working = false;