Переместил менеджер моделей в UwURenderEngine, так как у него нет прямой зависимости от ядра и у ядра нет зависимости от него
This commit is contained in:
@@ -1,11 +1,9 @@
|
||||
#include "GameInstance.hpp"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "ModelManager.hpp"
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Game/WorldFactory.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
@@ -19,8 +17,6 @@ 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)
|
||||
@@ -38,7 +34,6 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
|
||||
GameInstance::~GameInstance()
|
||||
{
|
||||
delete world;
|
||||
delete current_model_manager_;
|
||||
}
|
||||
|
||||
void GameInstance::start()
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
class CoreInstance;
|
||||
class World;
|
||||
class ModelManager;
|
||||
|
||||
#define GENERATE_FACTORY_GAME_INSTANCE(Class) \
|
||||
GameInstance* GameFactory(CoreInstance& core) { return new Class(core); }
|
||||
@@ -14,7 +13,6 @@ class GameInstance
|
||||
{
|
||||
CoreInstance& core;
|
||||
World* world = nullptr;
|
||||
ModelManager* current_model_manager_ = nullptr;
|
||||
|
||||
std::atomic<bool> is_running = true;
|
||||
|
||||
@@ -31,7 +29,6 @@ public:
|
||||
|
||||
World* GetWorld() const { return world; }
|
||||
CoreInstance& GetCore() const { return core; }
|
||||
ModelManager* GetCurrentModelManager() const { return current_model_manager_; }
|
||||
|
||||
friend class CoreInstance;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.hpp" "*.h")
|
||||
|
||||
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_compile_features(UwURenderEngine PRIVATE cxx_std_17)
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
#include "../../../RenderEngine/UwURenderEngine.hpp"
|
||||
|
||||
StaticMesh::StaticMesh()
|
||||
{
|
||||
@@ -23,12 +24,13 @@ void StaticMesh::load(std::shared_ptr<SaveMap> save)
|
||||
void StaticMesh::load_model(const std::string& model_name)
|
||||
{
|
||||
model_name_ = model_name;
|
||||
model_ = static_cast<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->LoadModel(model_name);
|
||||
SetModelName(model_name);
|
||||
model_ = GetWorld()->GetGameInstance().GetCurrentModelManager()->LoadModel(model_name);
|
||||
}
|
||||
|
||||
void StaticMesh::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
|
||||
|
||||
#include "Mesh.hpp"
|
||||
#include "Game/ModelManager.hpp"
|
||||
#include "../../../RenderEngine/ModelManager.hpp"
|
||||
|
||||
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 <array>
|
||||
#include <queue>
|
||||
#include <set>
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
@@ -18,6 +17,8 @@ const std::vector<const char*> UwURenderEngine::deviceExtensions = {
|
||||
VK_KHR_SWAPCHAIN_EXTENSION_NAME
|
||||
};
|
||||
|
||||
RENDER_ENGINE_FACTORY_GENERATE(UwURenderEngine)
|
||||
|
||||
#ifdef _DEBUG
|
||||
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
|
||||
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
||||
@@ -1051,7 +1052,8 @@ void UwURenderEngine::create_surface()
|
||||
}
|
||||
|
||||
UwURenderEngine::UwURenderEngine(CoreInstance& core):
|
||||
core_(core)
|
||||
core_(core),
|
||||
model_manager_(new ModelManager)
|
||||
#ifdef _DEBUG
|
||||
,debug_messenger_(nullptr)
|
||||
#endif
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "RenderEngineBase.hpp"
|
||||
#include "ModelManager.hpp"
|
||||
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
#define GLM_FORCE_RADIANS
|
||||
#include <glm/glm.hpp>
|
||||
@@ -68,6 +70,7 @@ class UwURenderEngine : public RenderEngineBase
|
||||
int current_frame_ = 0;
|
||||
|
||||
CoreInstance& core_;
|
||||
std::unique_ptr<ModelManager> model_manager_;
|
||||
|
||||
VkInstance instance_ = VK_NULL_HANDLE;
|
||||
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
||||
@@ -153,6 +156,5 @@ public:
|
||||
void start() 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
Reference in New Issue
Block a user