diff --git a/include/Cubed/gameplay/entity.hpp b/include/Cubed/gameplay/entity.hpp index ad2e5ee..2916901 100644 --- a/include/Cubed/gameplay/entity.hpp +++ b/include/Cubed/gameplay/entity.hpp @@ -6,8 +6,6 @@ #include namespace Cubed { -using ModelID = uint32_t; - struct Position { glm::vec3 value{0.0f}; }; @@ -25,10 +23,6 @@ struct EntityInfo { std::string uuid; }; -struct Model { - ModelID id; -}; - struct Health { float hp = 20; float max_hp = 20; diff --git a/include/Cubed/gameplay/model.hpp b/include/Cubed/gameplay/model.hpp new file mode 100644 index 0000000..3899181 --- /dev/null +++ b/include/Cubed/gameplay/model.hpp @@ -0,0 +1,7 @@ +#pragma once +#include + +using ModelID = uint32_t; +struct Model { + ModelID id; +}; \ No newline at end of file diff --git a/include/Cubed/render/model_manager.hpp b/include/Cubed/render/model_manager.hpp index 18af757..ab78f6e 100644 --- a/include/Cubed/render/model_manager.hpp +++ b/include/Cubed/render/model_manager.hpp @@ -1,6 +1,9 @@ #pragma once +#include "Cubed/gameplay/model.hpp" #include "Cubed/render/model_node.hpp" #include "Cubed/tools/model_loader.hpp" + +#include namespace Cubed { class ModelManager { public: @@ -11,11 +14,20 @@ public: ModelManager& operator=(ModelManager&&) = delete; ~ModelManager(); const ModelNode& get_model(const std::string& model_name); + const ModelNode& get_model(ModelID id); + ModelID get_model_id(const std::string& name); + const std::string& get_model_name(ModelID id); void init(); private: ModelLoader m_loader; - std::unordered_map m_models; - const ModelNode& load_model(const std::string& model_name); + ModelID m_next = 0; + using ModelMap = tbb::concurrent_hash_map; + using IDMap = tbb::concurrent_hash_map; + using NameMap = tbb::concurrent_hash_map; + ModelMap m_models; + IDMap m_id_map; + NameMap m_name_map; + const ModelNode& load_model(std::string_view model_name); }; } // namespace Cubed \ No newline at end of file diff --git a/src/render/model_manager.cpp b/src/render/model_manager.cpp index fc568fa..6cd047f 100644 --- a/src/render/model_manager.cpp +++ b/src/render/model_manager.cpp @@ -1,5 +1,6 @@ #include "Cubed/render/model_manager.hpp" +#include "Cubed/tools/cubed_assert.hpp" #include "Cubed/tools/log.hpp" namespace Cubed { @@ -12,21 +13,68 @@ void ModelManager::init() { }; const ModelNode& ModelManager::get_model(const std::string& model_name) { - auto it = m_models.find(model_name); - if (it == m_models.end()) { - return load_model(model_name); - } - return it->second; + return get_model(get_model_id(model_name)); } -const ModelNode& ModelManager::load_model(const std::string& model_name) { - std::string path = ASSETS_PATH + model_name; - auto model = m_loader.load(path); - auto [it, instert] = m_models.try_emplace(model_name, std::move(model)); - if (!instert) { - Logger::error("Model Key {} already exist!", model_name); +const ModelNode& ModelManager::get_model(ModelID id) { + + ModelMap::const_accessor cacc; + if (!m_models.find(cacc, id)) { + return cacc->second; } - return it->second; + return load_model(get_model_name(id)); +} + +const ModelNode& get_model(const std::string& model_name); +const ModelNode& get_model(ModelID id); +ModelID ModelManager::get_model_id(const std::string& name) { + IDMap::const_accessor cacc; + if (m_id_map.find(cacc, name)) { + return cacc->second; + } + ASSERT_MSG(false, std::format("ModelManager: Can't find {}", name)); + return 0; +} + +const std::string& ModelManager::get_model_name(ModelID id) { + NameMap::const_accessor cacc; + if (m_name_map.find(cacc, id)) { + return cacc->second; + } + ASSERT_MSG(false, std::format("ModelManager: Can't find {}", id)); + static std::string n = ""; + return n; +} + +const ModelNode& ModelManager::load_model(std::string_view model_name) { + auto p = model_name.find(':'); + if (p == std::string::npos) { + Logger::error("Can't Parse Model name {}", model_name); + ASSERT(false); + } + auto name = model_name.substr(p + 1); + auto space = model_name.substr(0, p); + if (name.empty()) { + Logger::error("Can't Parse Model name {}", model_name); + ASSERT(false); + } + std::string path; + if (space == "cubed") { + path = std::format("{}model/creature/{}", ASSETS_PATH, name); + } else { + path = std::format("./{}/model/creature/{}", space, name); + } + auto model = m_loader.load(path); + ModelMap::accessor acc; + if (m_models.insert(acc, m_next++)) { + acc->second = std::move(model); + } else { + Logger::error("Can't Insert Model {}", model_name); + ASSERT(false); + } + m_id_map.emplace(model_name, acc->first); + m_name_map.emplace(acc->first, model_name); + return acc->second; } } // namespace Cubed \ No newline at end of file