Вынес движок рендера в отдельный модуль ввиде статической библиотеки

This commit is contained in:
Jiga228
2025-10-25 20:33:21 +07:00
parent e52df7ef12
commit 29f700d3a5
17 changed files with 217 additions and 118 deletions
+2 -4
View File
@@ -1,9 +1,7 @@
#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"
@@ -26,11 +24,11 @@ void StaticMesh::load_model(const std::string& model_name)
{
model_name_ = model_name;
SetModelName(model_name);
model_ = GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->LoadModel(model_name);
model_ = GetWorld()->GetGameInstance().GetCurrentModelManager()->LoadModel(model_name);
}
void StaticMesh::OnDestroy()
{
Mesh::OnDestroy();
GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->FreeModel(model_name_);
GetWorld()->GetGameInstance().GetCurrentModelManager()->FreeModel(model_name_);
}
+1 -1
View File
@@ -1,7 +1,7 @@
#pragma once
#include "Mesh.hpp"
#include "Core/RenderEngine/ModelManager.hpp"
#include "Game/ModelManager.hpp"
GENERATE_META(StaticMesh)
+4 -1
View File
@@ -5,9 +5,9 @@
#include <chrono>
#include <thread>
#include "ModelManager.hpp"
#include "Core/CoreInstance.hpp"
#include "Game/WorldFactory.hpp"
#include "SaveMap/SaveMap.hpp"
#include "Game/World/World.hpp"
extern std::vector<WorldFactory> world_factories;
@@ -19,6 +19,8 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
// Init directories
std::filesystem::create_directories(std::filesystem::path("./Resources"));
current_model_manager_ = new ModelManager();
for (auto& factories : world_factories)
{
if (factories.world_name == base_world)
@@ -36,6 +38,7 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
GameInstance::~GameInstance()
{
delete world;
delete current_model_manager_;
}
void GameInstance::start()
+4 -1
View File
@@ -5,6 +5,7 @@
class CoreInstance;
class World;
class ModelManager;
#define GENERATE_FACTORY_GAME_INSTANCE(Class) \
GameInstance* GameFactory(CoreInstance& core) { return new Class(core); }
@@ -13,11 +14,12 @@ class GameInstance
{
CoreInstance& core;
World* world = nullptr;
ModelManager* current_model_manager_ = nullptr;
std::atomic<bool> is_running = true;
void start();
// Stop call from core
// Stop call from the core
void stop();
public:
// Stop call from game
@@ -29,6 +31,7 @@ public:
World* GetWorld() const { return world; }
CoreInstance& GetCore() const { return core; }
ModelManager* GetCurrentModelManager() const { return current_model_manager_; }
friend class CoreInstance;
};
+107
View File
@@ -0,0 +1,107 @@
#include "ModelManager.hpp"
#include "Log/Log.hpp"
ModelManager::StaticModel::StaticModel(const std::vector<Voxel>& voxels, std::string name):
name_(std::move(name)),
voxels_(voxels)
{
glm::ivec3 sum_moments{0, 0, 0};
int sum_masses = 0;
for (auto& i : voxels_)
{
sum_moments += glm::ivec3{i.loc.x * i.mass, i.loc.y * i.mass, i.loc.z * i.mass};
sum_masses += i.mass;
}
if(sum_masses != 0)
sum_moments /= sum_masses;
else if (!voxels_.empty())
{
sum_moments.x /= static_cast<int>(voxels_.size());
sum_moments.y /= static_cast<int>(voxels_.size());
sum_moments.z /= static_cast<int>(voxels_.size());
}
mass_center_ = sum_moments;
}
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;
while (*buf)
{
h_val ^= static_cast<unsigned int>(*buf++);
h_val *= 0x01000193;
}
return h_val;
}
void ModelManager::OnDestroySometimeModelCaller(const std::string& name)
{
Loging::Log("Free model: " + name);
OnDestroySometimeModel.Call(name);
}
ModelManager::~ModelManager()
{
for (const auto& [it, counter] : models)
{
counter.model->OnDestroy.unbind(this, &ModelManager::OnDestroySometimeModelCaller);
}
models.clear();
}
ModelManager::StaticModel* ModelManager::LoadModel(const std::string& name)
{
if (name.empty())
return nullptr;
unsigned int hash = FNV1aHash(name.c_str());
auto counter = models.find(hash);
if (counter != models.cend())
{
++counter->second.counter;
return counter->second.model;
}
Loging::Log("Load new model: " + name);
std::vector<Voxel> model_data;
// Load model_data
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);
OnLoadSometimeModel.Call(new_counter.model);
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;
}
}
+58
View File
@@ -0,0 +1,58 @@
#pragma once
#include <unordered_map>
#include <vector>
#include <string>
#include <Delegate/Delegate.h>
#include <glm/glm.hpp>
class ModelManager
{
public:
struct Voxel
{
glm::vec3 loc, color;
int mass;
};
class StaticModel
{
std::string name_;
std::vector<Voxel> voxels_;
glm::vec3 mass_center_;
public:
Delegate<const std::string&> OnDestroy;
StaticModel(const std::vector<Voxel>& voxels, std::string name);
~StaticModel();
const std::vector<Voxel>& GetVoxels() const { return voxels_; }
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, owner_counter> models;
static unsigned int FNV1aHash (const char *buf);
void OnDestroySometimeModelCaller(const std::string& name);
public:
Delegate<std::string> OnDestroySometimeModel;
Delegate<StaticModel*> OnLoadSometimeModel;
~ModelManager();
StaticModel* LoadModel(const std::string& name);
void FreeModel(const std::string& name);
};