diff --git a/src/engine/tree/Chunk.cpp b/src/engine/tree/Chunk.cpp index cdd1dc6..3c2d389 100644 --- a/src/engine/tree/Chunk.cpp +++ b/src/engine/tree/Chunk.cpp @@ -4,6 +4,8 @@ #include "Chunk.hpp" +#include "engine/math/intersections.hpp" +#include "engine/primitives/boxes/AxisAlignedBoundingCube.hpp" #include "engine/utils.hpp" namespace fgl::engine::tree @@ -18,9 +20,34 @@ namespace fgl::engine::tree return chunk; } + void ChunkManager::cleanup() + { + std::lock_guard guard { m_delete_mtx }; + while ( m_delete_list.size() > 0 ) + { + const auto item { m_delete_list.front() }; + m_delete_list.pop(); + + auto itter { m_chunks.find( item->getID() ) }; + + m_chunks.erase( itter ); + } + } + std::shared_ptr< Chunk > ChunkManager::getChunk( const ChunkID id ) {} + void ChunkManager::markForDeletion( std::shared_ptr< Chunk >& chunk ) + { + m_delete_list.push( chunk ); + } + + ChunkManager& ChunkManager::getInstance() + { + static ChunkManager manager {}; + return manager; + } + ChunkID getID( const glm::vec3 point ) { // the inital chunk starts at 0,0,0. meaning that the bounds of the inital chunk goes from @@ -41,7 +68,30 @@ namespace fgl::engine::tree return hash; } + std::shared_ptr< Chunk > Chunk::getShared() + { + return shared_from_this(); + } + Chunk::Chunk( const ChunkID id ) : m_id( id ), m_center( getPosition( id ) ) {} + ChunkID Chunk::getID() const + { + return m_id; + } + + bool Chunk::isVisible( const Frustum& frustum ) const + { + // Create a bounding box of this chunk + const AxisAlignedBoundingCube< CS::World > bounds { WorldCoordinate( m_center ), CHUNK_HALF }; + + return intersects( frustum, bounds ); + } + + void Chunk::deleteLater() + { + ChunkManager::getInstance()->markForDeletion( this->getShared() ); + } + } // namespace fgl::engine::tree \ No newline at end of file diff --git a/src/engine/tree/Chunk.hpp b/src/engine/tree/Chunk.hpp index 1b4c222..0dc5ff8 100644 --- a/src/engine/tree/Chunk.hpp +++ b/src/engine/tree/Chunk.hpp @@ -8,7 +8,6 @@ #include #include "engine/gameobjects/GameObject.hpp" -#include "glm/vec3.hpp" namespace fgl::engine { @@ -24,21 +23,28 @@ namespace fgl::engine::tree class ChunkManager { std::unordered_map< ChunkID, std::shared_ptr< Chunk > > m_chunks {}; + std::mutex m_delete_mtx; std::queue< std::shared_ptr< Chunk > > m_delete_list {}; - std::shared_ptr< Chunk > createChunk( const ChunkID id ); + std::shared_ptr< Chunk > createChunk( ChunkID id ); //! Deletes any chunks pending deletion void cleanup(); //! Returns a shared pointer to the chunk with the given ID. - std::shared_ptr< Chunk > getChunk( const ChunkID id ); + std::shared_ptr< Chunk > getChunk( ChunkID id ); + + public: + + void markForDeletion( std::shared_ptr< Chunk >& chunk ); + + static ChunkManager& getInstance(); }; ChunkID getID( const glm::vec3 point ); glm::vec3 getPosition( const ChunkID id ); - class Chunk + class Chunk : public std::enable_shared_from_this< Chunk > { //! Determines if the chunk is active to the rendering system bool m_rendering_active { true }; @@ -49,11 +55,15 @@ namespace fgl::engine::tree //! Contains a list of all objects within this chunk std::unordered_map< GameObject::GameObjectID, std::shared_ptr< GameObject > > m_objects {}; + std::shared_ptr< Chunk > getShared(); + public: Chunk() = delete; Chunk( const ChunkID id ); + ChunkID getID() const; + void addGameObject( std::shared_ptr< GameObject > object ); // Size of a chunk from center to @@ -65,6 +75,9 @@ namespace fgl::engine::tree //! Returns true if the bounds of this chunk are visible. bool isVisible( const Frustum& frustum ) const; + + //! Marks this node to be deleted later + void deleteLater(); }; } // namespace fgl::engine::tree