Fix model manager. It's version stable for model manager
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#include "ModelManager.hpp"
|
||||
|
||||
#include "Log/Log.hpp"
|
||||
|
||||
ModelManager::StaticModel::StaticModel(const std::vector<Voxel>& voxels, std::string name):
|
||||
name_(std::move(name)),
|
||||
voxels_(voxels)
|
||||
@@ -30,6 +32,10 @@ ModelManager::StaticModel::~StaticModel()
|
||||
OnDestroy.Call(name_);
|
||||
}
|
||||
|
||||
ModelManager::owner_counter::owner_counter(const owner_counter& other) : counter(other.counter.load()), model(other.model)
|
||||
{
|
||||
}
|
||||
|
||||
unsigned int ModelManager::FNV1aHash(const char* buf)
|
||||
{
|
||||
unsigned int h_val = 0x811c9dc5;
|
||||
@@ -45,29 +51,57 @@ unsigned int ModelManager::FNV1aHash(const char* buf)
|
||||
|
||||
void ModelManager::OnDestroySometimeModelCaller(const std::string& name)
|
||||
{
|
||||
Log("Free model: " + name);
|
||||
OnDestroySometimeModel.Call(name);
|
||||
}
|
||||
|
||||
ModelManager::~ModelManager()
|
||||
{
|
||||
for (const auto& [it, model] : models)
|
||||
for (const auto& [it, counter] : models)
|
||||
{
|
||||
model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
counter.model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
}
|
||||
models.clear();
|
||||
}
|
||||
|
||||
std::shared_ptr<ModelManager::StaticModel> ModelManager::LoadModel(const std::string& name)
|
||||
ModelManager::StaticModel* ModelManager::LoadModel(const std::string& name)
|
||||
{
|
||||
if (name.empty())
|
||||
return nullptr;
|
||||
unsigned int hash = FNV1aHash(name.c_str());
|
||||
auto model = models.find(hash);
|
||||
if (model != models.cend())
|
||||
return model->second;
|
||||
auto counter = models.find(hash);
|
||||
if (counter != models.cend())
|
||||
{
|
||||
++counter->second.counter;
|
||||
return counter->second.model;
|
||||
}
|
||||
|
||||
Log("Load new model: " + name);
|
||||
|
||||
std::vector<Voxel> model_data;
|
||||
// Load model_data
|
||||
auto newModel = std::make_shared<StaticModel>(model_data, name);
|
||||
newModel->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
models[hash] = newModel;
|
||||
return models[hash];
|
||||
owner_counter new_counter;
|
||||
new_counter.counter.store(1);
|
||||
new_counter.model = new StaticModel(model_data, name);
|
||||
new_counter.model->OnDestroy.bind(this, &ModelManager::OnDestroySometimeModelCaller);
|
||||
models.try_emplace(hash, new_counter);
|
||||
|
||||
return new_counter.model;
|
||||
}
|
||||
|
||||
void ModelManager::FreeModel(const std::string& name)
|
||||
{
|
||||
if (name.empty())
|
||||
return;
|
||||
|
||||
unsigned int hash = FNV1aHash(name.c_str());
|
||||
auto counter = models.find(hash);
|
||||
--counter->second.counter;
|
||||
|
||||
StaticModel* model = counter->second.model;
|
||||
if (counter->second.counter.load() == 0)
|
||||
{
|
||||
models.erase(hash);
|
||||
delete model;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,10 +29,19 @@ public:
|
||||
glm::vec3 GetMassCenter() const { return mass_center_; }
|
||||
const std::string& GetName() const { return name_; }
|
||||
};
|
||||
|
||||
struct owner_counter
|
||||
{
|
||||
std::atomic_ullong counter;
|
||||
StaticModel* model;
|
||||
|
||||
owner_counter() = default;
|
||||
owner_counter(const owner_counter& other);
|
||||
};
|
||||
|
||||
private:
|
||||
|
||||
std::unordered_map<unsigned int, std::shared_ptr<StaticModel>> models;
|
||||
std::unordered_map<unsigned int, owner_counter> models;
|
||||
|
||||
static unsigned int FNV1aHash (const char *buf);
|
||||
|
||||
@@ -43,5 +52,6 @@ public:
|
||||
|
||||
~ModelManager();
|
||||
|
||||
std::shared_ptr<StaticModel> LoadModel(const std::string& name);
|
||||
StaticModel* LoadModel(const std::string& name);
|
||||
void FreeModel(const std::string& name);
|
||||
};
|
||||
@@ -1,7 +1,11 @@
|
||||
#include "StaticMesh.hpp"
|
||||
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Core/RenderEngine/RenderEngine.hpp"
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Log/Log.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
|
||||
StaticMesh::StaticMesh()
|
||||
{
|
||||
@@ -20,7 +24,13 @@ void StaticMesh::load(std::shared_ptr<SaveMap> save)
|
||||
|
||||
void StaticMesh::load_model(const std::string& model_name)
|
||||
{
|
||||
model_name_ = model_name;
|
||||
SetModelName(model_name);
|
||||
// TODO
|
||||
Message("Load mesh: " + model_name);
|
||||
model_ = GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->LoadModel(model_name);
|
||||
}
|
||||
|
||||
void StaticMesh::OnDestroy()
|
||||
{
|
||||
Mesh::OnDestroy();
|
||||
GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->FreeModel(model_name_);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mesh.hpp"
|
||||
#include "Core/RenderEngine/ModelManager.hpp"
|
||||
|
||||
GENERATE_META(StaticMesh)
|
||||
|
||||
class StaticMesh : public Mesh
|
||||
{
|
||||
std::string model_name_;
|
||||
ModelManager::StaticModel* model_ = nullptr;
|
||||
public:
|
||||
StaticMesh();
|
||||
|
||||
@@ -14,4 +18,5 @@ public:
|
||||
#pragma endregion
|
||||
|
||||
void load_model(const std::string& model_name) override;
|
||||
void OnDestroy() override;
|
||||
};
|
||||
|
||||
@@ -8,6 +8,8 @@ World::World(GameInstance& game_instance) : game_instance(game_instance)
|
||||
|
||||
World::~World()
|
||||
{
|
||||
for (auto& i : actors_map)
|
||||
i.second->OnDestroy();
|
||||
actors_map.clear();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user