fix: rebuilding grid fully stops threads first

This commit is contained in:
Sara Gerretsen 2026-02-25 23:05:03 +01:00
parent 1e44fcd09f
commit 0d982a751c
2 changed files with 24 additions and 12 deletions

View file

@ -92,13 +92,20 @@ void Terrain::generate_meshes_thread(void *terrain) {
}
void Terrain::construct_chunk_grid() {
this->workload_lock.lock();
this->threads_stop = true;
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.wait_to_finish();
}
this->workload_lock.lock();
for (TerrainChunkMesh *mesh : this->meshes) {
mesh->queue_free();
}
this->meshes.clear();
size_t const chunks_per_side{ this->side_length / this->chunk_size };
Vector3 const origin{ (float)this->chunk_size / 2.f, 0.f, (float)this->chunk_size / 2.f };
this->workload_lock.lock();
this->meshes.clear();
this->workload.clear();
for (size_t y{ 0 }; y < chunks_per_side; ++y) {
for (size_t x{ 0 }; x < chunks_per_side; ++x) {
TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) };
@ -112,7 +119,12 @@ void Terrain::construct_chunk_grid() {
this->workload.push_back(chunk);
}
}
this->threads_stop = false;
this->dirty_meshes.clear();
this->workload_lock.unlock();
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
}
float Terrain::height_at(Vector2 world_coordinate) {