From: Daniel Karbach Date: Tue, 23 Jun 2015 08:26:33 +0000 (+0200) Subject: use a timer to limit chunk generation X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=4db5e3765bc93606e340f704ae27ca840eae7045;p=blank.git use a timer to limit chunk generation this should reduce load when updating more than once per frame which is planned someday --- diff --git a/src/world/ChunkLoader.hpp b/src/world/ChunkLoader.hpp index b1d0c7b..98b352f 100644 --- a/src/world/ChunkLoader.hpp +++ b/src/world/ChunkLoader.hpp @@ -2,6 +2,7 @@ #define BLANK_WORLD_CHUNKLOADER_HPP_ #include "Chunk.hpp" +#include "../app/IntervalTimer.hpp" #include @@ -17,6 +18,7 @@ public: struct Config { int load_dist = 6; int unload_dist = 8; + int gen_limit = 16; }; ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept; @@ -31,8 +33,11 @@ public: bool Known(const Chunk::Pos &) noexcept; Chunk &ForceLoad(const Chunk::Pos &); + bool OutOfRange(const Chunk &c) const noexcept { return OutOfRange(c.Position()); } + bool OutOfRange(const Chunk::Pos &) const noexcept; + void Rebase(const Chunk::Pos &); - void Update(); + void Update(int dt); private: Chunk &Generate(const Chunk::Pos &pos); @@ -49,6 +54,8 @@ private: std::list to_generate; std::list to_free; + IntervalTimer gen_timer; + int load_dist; int unload_dist; diff --git a/src/world/World.cpp b/src/world/World.cpp index e426f07..529968e 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -199,7 +199,7 @@ void World::Update(int dt) { entity.Update(dt); } chunks.Rebase(player->ChunkCoords()); - chunks.Update(); + chunks.Update(dt); } diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index 569bb11..c114526 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -627,9 +627,10 @@ ChunkLoader::ChunkLoader(const Config &config, const BlockTypeRegistry ®, con , loaded() , to_generate() , to_free() +, gen_timer(config.gen_limit) , load_dist(config.load_dist) , unload_dist(config.unload_dist) { - + gen_timer.Start(); } namespace { @@ -774,6 +775,12 @@ Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) { return Generate(pos); } +bool ChunkLoader::OutOfRange(const Chunk::Pos &pos) const noexcept { + return std::abs(base.x - pos.x) > unload_dist + || std::abs(base.y - pos.y) > unload_dist + || std::abs(base.z - pos.z) > unload_dist; +} + void ChunkLoader::Rebase(const Chunk::Pos &new_base) { if (new_base == base) { return; @@ -782,9 +789,7 @@ void ChunkLoader::Rebase(const Chunk::Pos &new_base) { // unload far away chunks for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) { - if (std::abs(base.x - iter->Position().x) > unload_dist - || std::abs(base.y - iter->Position().y) > unload_dist - || std::abs(base.z - iter->Position().z) > unload_dist) { + if (OutOfRange(*iter)) { auto saved = iter; Remove(*saved); ++iter; @@ -795,9 +800,7 @@ void ChunkLoader::Rebase(const Chunk::Pos &new_base) { } // abort far away queued chunks for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) { - if (std::abs(base.x - iter->x) > unload_dist - || std::abs(base.y - iter->y) > unload_dist - || std::abs(base.z - iter->z) > unload_dist) { + if (OutOfRange(*iter)) { iter = to_generate.erase(iter); } else { ++iter; @@ -812,8 +815,9 @@ void ChunkLoader::GenerateSurrounding(const Chunk::Pos &pos) { Generate(pos - offset, pos + offset); } -void ChunkLoader::Update() { - if (to_generate.empty()) { +void ChunkLoader::Update(int dt) { + gen_timer.Update(dt); + if (!gen_timer.Hit() || to_generate.empty()) { return; } @@ -824,7 +828,6 @@ void ChunkLoader::Update() { if (iter->Position() == pos) { iter->Relink(); loaded.splice(loaded.end(), to_free, iter); - return; } }