chunkmanager: unload chunks after a time treshold

vertex-deduplication
EmaMaker 2023-03-13 13:33:32 +01:00
parent b7077af00f
commit 8040868055
2 changed files with 23 additions and 10 deletions

View File

@ -1,6 +1,9 @@
#ifndef CHUNKMANAGER_H
#define CHUNKMANAGER_H
// Second to be passed outside of render distance for a chunk to be destroyed
#define UNLOAD_TIMEOUT 10
#include <thread>
namespace chunkmanager

View File

@ -94,10 +94,13 @@ namespace chunkmanager
int rr{RENDER_DISTANCE * RENDER_DISTANCE};
uint8_t f = 0;
glm::vec4 frustumPlanes[6];
std::set<uint32_t> old_chunks;
std::unordered_map<uint32_t, std::time_t> to_delete;
std::set<uint32_t> to_delete_delete;
void update(float deltaTime)
{
int nUnloaded{0};
f = 0;
f |= mutex_queue_generate.try_lock();
f |= mutex_queue_mesh.try_lock() << 1;
@ -108,6 +111,7 @@ namespace chunkmanager
theCamera.getFrustumPlanes(frustumPlanes, true);
int chunkX{(static_cast<int>(cameraPos.x)) / CHUNK_SIZE}, chunkY{(static_cast<int>(cameraPos.y)) / CHUNK_SIZE}, chunkZ{(static_cast<int>(cameraPos.z)) / CHUNK_SIZE};
std::time_t currentTime = std::time(nullptr);
// Check for far chunks that need to be cleaned up from memory
for(const auto& n : chunks){
Chunk::Chunk* c = n.second;
@ -115,15 +119,21 @@ namespace chunkmanager
int y{(int)(c->getPosition().y)};
int z{(int)(c->getPosition().z)};
if( (chunkX-x)*(chunkX-x) + (chunkY-y)*(chunkY-y) + (chunkZ-z)*(chunkZ-z) >=
(int)(RENDER_DISTANCE*1.5)*(int)(RENDER_DISTANCE*1.5)){
delete c;
(int)(RENDER_DISTANCE*1.5)*(int)(RENDER_DISTANCE*1.5))
if(to_delete.find(n.first) == to_delete.end())
to_delete.insert(std::make_pair(n.first, currentTime));
}
for(const auto& n :to_delete){
if( currentTime>=n.second + UNLOAD_TIMEOUT) {
delete chunks.at(n.first);
chunks.erase(n.first);
nUnloaded++;
old_chunks.insert(n.first);
to_delete_delete.insert(n.first);
}
}
for(uint32_t i : old_chunks) chunks.erase(i);
old_chunks.clear();
for(uint32_t i : to_delete_delete) to_delete.erase(i);
to_delete_delete.clear();
if(nUnloaded) std::cout << "Unloaded " << nUnloaded << " chunks\n";
// Possible change: coordinates everything at the origin, then translate later?