Reference counting for Chunk | changed ChunksLoader
This commit is contained in:
parent
bc9d29cf68
commit
4651ab776b
@ -35,3 +35,12 @@ Chunk* Chunk::clone() const {
|
|||||||
other->lightmap->set(lightmap);
|
other->lightmap->set(lightmap);
|
||||||
return other;
|
return other;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Chunk::incref(){
|
||||||
|
references++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Chunk::decref(){
|
||||||
|
if (--references <= 0)
|
||||||
|
delete this;
|
||||||
|
}
|
||||||
|
|||||||
@ -18,12 +18,15 @@ public:
|
|||||||
bool ready = false;
|
bool ready = false;
|
||||||
bool accepted = false;
|
bool accepted = false;
|
||||||
bool generated = false;
|
bool generated = false;
|
||||||
|
int references = 1;
|
||||||
Chunk(int x, int y, int z);
|
Chunk(int x, int y, int z);
|
||||||
~Chunk();
|
~Chunk();
|
||||||
|
|
||||||
bool isEmpty();
|
bool isEmpty();
|
||||||
|
|
||||||
Chunk* clone() const;
|
Chunk* clone() const;
|
||||||
|
void incref();
|
||||||
|
void decref();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* VOXELS_CHUNK_H_ */
|
#endif /* VOXELS_CHUNK_H_ */
|
||||||
|
|||||||
@ -278,7 +278,7 @@ void Chunks::translate(WorldFiles* worldFiles, int dx, int dy, int dz){
|
|||||||
Mesh* mesh = meshes[(y * d + z) * w + x];
|
Mesh* mesh = meshes[(y * d + z) * w + x];
|
||||||
if (nx < 0 || ny < 0 || nz < 0 || nx >= w || ny >= h || nz >= d){
|
if (nx < 0 || ny < 0 || nz < 0 || nx >= w || ny >= h || nz >= d){
|
||||||
worldFiles->put((const char*)chunk->voxels, chunk->x, chunk->z);
|
worldFiles->put((const char*)chunk->voxels, chunk->x, chunk->z);
|
||||||
delete chunk;
|
chunk->decref();
|
||||||
delete mesh;
|
delete mesh;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -322,7 +322,7 @@ bool Chunks::putChunk(Chunk* chunk) {
|
|||||||
void Chunks::clear(bool freeMemory){
|
void Chunks::clear(bool freeMemory){
|
||||||
for (size_t i = 0; i < volume; i++){
|
for (size_t i = 0; i < volume; i++){
|
||||||
if (freeMemory){
|
if (freeMemory){
|
||||||
delete chunks[i];
|
chunks[i]->decref();
|
||||||
delete meshes[i];
|
delete meshes[i];
|
||||||
}
|
}
|
||||||
chunks[i] = nullptr;
|
chunks[i] = nullptr;
|
||||||
|
|||||||
@ -17,30 +17,24 @@ void ChunksLoader::_thread(){
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Chunk* chunk = current;
|
Chunk* chunk = current;
|
||||||
//std::cout << "LOADER: received chunk " << chunk->x << " " << chunk->y << " " << chunk->z << std::endl;
|
chunk->incref();
|
||||||
|
|
||||||
chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1);
|
chunks._setOffset(chunk->x-1, chunk->y-1, chunk->z-1);
|
||||||
|
|
||||||
if (!chunk->generated){
|
if (!chunk->generated){
|
||||||
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
|
WorldGenerator::generate(chunk->voxels, chunk->x, chunk->y, chunk->z);
|
||||||
//std::cout << "LOADER: generated chunk" << std::endl;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*for (int i = 0; i < 27; i++){
|
|
||||||
Chunk* other = closes[i];
|
|
||||||
if (other == nullptr)
|
|
||||||
continue;
|
|
||||||
chunks.putChunk(other);
|
|
||||||
}*/
|
|
||||||
chunks.putChunk(chunk);
|
chunks.putChunk(chunk);
|
||||||
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
|
lighting.onChunkLoaded(chunk->x, chunk->y, chunk->z, true);
|
||||||
chunks.clear(false);
|
chunks.clear(false);
|
||||||
for (int i = 0; i < 27; i++){
|
for (int i = 0; i < 27; i++){
|
||||||
Chunk* other = closes[i];
|
Chunk* other = closes[i];
|
||||||
delete other;
|
//delete other;
|
||||||
}
|
}
|
||||||
chunk->ready = true;
|
chunk->ready = true;
|
||||||
current = nullptr;
|
current = nullptr;
|
||||||
|
chunk->decref();
|
||||||
//std::cout << "LOADER: success" << std::endl;
|
//std::cout << "LOADER: success" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,7 +52,7 @@ void ChunksLoader::perform(Chunk* chunk, const Chunk** cs){
|
|||||||
if (other == nullptr)
|
if (other == nullptr)
|
||||||
closes[i] = nullptr;
|
closes[i] = nullptr;
|
||||||
else
|
else
|
||||||
closes[i] = other->clone();
|
closes[i] = (Chunk*)other;//->clone();
|
||||||
}
|
}
|
||||||
current = chunk;
|
current = chunk;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user