Переместил менеджер моделей в UwURenderEngine, так как у него нет прямой зависимости от ядра и у ядра нет зависимости от него

This commit is contained in:
Jiga228
2025-10-27 23:15:02 +07:00
parent 740c80e8c7
commit 1ab74b8128
10 changed files with 8102 additions and 16 deletions
-5
View File
@@ -1,11 +1,9 @@
#include "GameInstance.hpp" #include "GameInstance.hpp"
#include <filesystem> #include <filesystem>
#include <fstream>
#include <chrono> #include <chrono>
#include <thread> #include <thread>
#include "ModelManager.hpp"
#include "Core/CoreInstance.hpp" #include "Core/CoreInstance.hpp"
#include "Game/WorldFactory.hpp" #include "Game/WorldFactory.hpp"
#include "Game/World/World.hpp" #include "Game/World/World.hpp"
@@ -19,8 +17,6 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
// Init directories // Init directories
std::filesystem::create_directories(std::filesystem::path("./Resources")); std::filesystem::create_directories(std::filesystem::path("./Resources"));
current_model_manager_ = new ModelManager();
for (auto& factories : world_factories) for (auto& factories : world_factories)
{ {
if (factories.world_name == base_world) if (factories.world_name == base_world)
@@ -38,7 +34,6 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
GameInstance::~GameInstance() GameInstance::~GameInstance()
{ {
delete world; delete world;
delete current_model_manager_;
} }
void GameInstance::start() void GameInstance::start()
-3
View File
@@ -5,7 +5,6 @@
class CoreInstance; class CoreInstance;
class World; class World;
class ModelManager;
#define GENERATE_FACTORY_GAME_INSTANCE(Class) \ #define GENERATE_FACTORY_GAME_INSTANCE(Class) \
GameInstance* GameFactory(CoreInstance& core) { return new Class(core); } GameInstance* GameFactory(CoreInstance& core) { return new Class(core); }
@@ -14,7 +13,6 @@ class GameInstance
{ {
CoreInstance& core; CoreInstance& core;
World* world = nullptr; World* world = nullptr;
ModelManager* current_model_manager_ = nullptr;
std::atomic<bool> is_running = true; std::atomic<bool> is_running = true;
@@ -31,7 +29,6 @@ public:
World* GetWorld() const { return world; } World* GetWorld() const { return world; }
CoreInstance& GetCore() const { return core; } CoreInstance& GetCore() const { return core; }
ModelManager* GetCurrentModelManager() const { return current_model_manager_; }
friend class CoreInstance; friend class CoreInstance;
}; };
+1 -1
View File
@@ -1,7 +1,7 @@
file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.hpp" "*.h") file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.hpp" "*.h")
add_library(UwURenderEngine ${SRC}) add_library(UwURenderEngine ${SRC})
target_include_directories(UwURenderEngine PRIVATE ${PROJECT_SOURCE_DIR}/RenderEngineSDK) target_include_directories(UwURenderEngine PUBLIC ${PROJECT_SOURCE_DIR}/RenderEngineSDK)
target_link_libraries(UwURenderEngine PRIVATE RenderEngineSDK) target_link_libraries(UwURenderEngine PRIVATE RenderEngineSDK)
target_compile_features(UwURenderEngine PRIVATE cxx_std_17) target_compile_features(UwURenderEngine PRIVATE cxx_std_17)
@@ -4,6 +4,7 @@
#include "Game/GameInstance.hpp" #include "Game/GameInstance.hpp"
#include "Game/SaveMap/SaveMap.hpp" #include "Game/SaveMap/SaveMap.hpp"
#include "Game/World/World.hpp" #include "Game/World/World.hpp"
#include "../../../RenderEngine/UwURenderEngine.hpp"
StaticMesh::StaticMesh() StaticMesh::StaticMesh()
{ {
@@ -23,12 +24,13 @@ void StaticMesh::load(std::shared_ptr<SaveMap> save)
void StaticMesh::load_model(const std::string& model_name) void StaticMesh::load_model(const std::string& model_name)
{ {
model_name_ = model_name; model_name_ = model_name;
model_ = static_cast<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->LoadModel(model_name);
SetModelName(model_name); SetModelName(model_name);
model_ = GetWorld()->GetGameInstance().GetCurrentModelManager()->LoadModel(model_name);
} }
void StaticMesh::OnDestroy() void StaticMesh::OnDestroy()
{ {
Mesh::OnDestroy(); Mesh::OnDestroy();
GetWorld()->GetGameInstance().GetCurrentModelManager()->FreeModel(model_name_);
static_cast<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->FreeModel(model_name_);
} }
@@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Mesh.hpp" #include "Mesh.hpp"
#include "Game/ModelManager.hpp" #include "../../../RenderEngine/ModelManager.hpp"
GENERATE_META(StaticMesh) GENERATE_META(StaticMesh)
@@ -0,0 +1,65 @@
#include "ModelManager.hpp"
#include "Log/Log.hpp"
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;
}
ModelManager::~ModelManager()
{
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);
// Load model_data
owner_counter new_counter;
new_counter.counter.store(1);
new_counter.model = new StaticModel();
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)
{
Loging::Log("Free model: " + name);
models.erase(hash);
delete model;
}
}
@@ -0,0 +1,35 @@
#pragma once
#include <unordered_map>
#include <string>
#include <atomic>
class ModelManager
{
public:
class StaticModel
{
//DATA
};
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);
public:
~ModelManager();
StaticModel* LoadModel(const std::string& name);
void FreeModel(const std::string& name);
};
@@ -1,7 +1,6 @@
#include "UwURenderEngine.hpp" #include "UwURenderEngine.hpp"
#include <array> #include <array>
#include <queue>
#include <set> #include <set>
#include <stdexcept> #include <stdexcept>
#include <fstream> #include <fstream>
@@ -18,6 +17,8 @@ const std::vector<const char*> UwURenderEngine::deviceExtensions = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME VK_KHR_SWAPCHAIN_EXTENSION_NAME
}; };
RENDER_ENGINE_FACTORY_GENERATE(UwURenderEngine)
#ifdef _DEBUG #ifdef _DEBUG
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
@@ -1051,7 +1052,8 @@ void UwURenderEngine::create_surface()
} }
UwURenderEngine::UwURenderEngine(CoreInstance& core): UwURenderEngine::UwURenderEngine(CoreInstance& core):
core_(core) core_(core),
model_manager_(new ModelManager)
#ifdef _DEBUG #ifdef _DEBUG
,debug_messenger_(nullptr) ,debug_messenger_(nullptr)
#endif #endif
@@ -1,10 +1,12 @@
#pragma once #pragma once
#include "RenderEngineBase.hpp" #include "RenderEngineBase.hpp"
#include "ModelManager.hpp"
#include <optional> #include <optional>
#include <vector> #include <vector>
#include <string> #include <string>
#include <memory>
#define GLM_FORCE_RADIANS #define GLM_FORCE_RADIANS
#include <glm/glm.hpp> #include <glm/glm.hpp>
@@ -68,6 +70,7 @@ class UwURenderEngine : public RenderEngineBase
int current_frame_ = 0; int current_frame_ = 0;
CoreInstance& core_; CoreInstance& core_;
std::unique_ptr<ModelManager> model_manager_;
VkInstance instance_ = VK_NULL_HANDLE; VkInstance instance_ = VK_NULL_HANDLE;
VkSurfaceKHR surface_ = VK_NULL_HANDLE; VkSurfaceKHR surface_ = VK_NULL_HANDLE;
@@ -153,6 +156,5 @@ public:
void start() override; void start() override;
void stop_render() const override; void stop_render() const override;
ModelManager* GetModelManager() const { return model_manager_.get(); }
}; };
RENDER_ENGINE_FACTORY_GENERATE(UwURenderEngine)
File diff suppressed because it is too large Load Diff