From 25cc75ba690fc5be6ed1a2f0c3cc755f2ab7294c Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Tue, 16 Sep 2025 21:56:34 +0700 Subject: [PATCH] Integrate RenderEngine to Core --- CMakeLists.txt | 1 - Core/CMakeLists.txt | 2 - Core/Core/CoreInstance.cpp | 70 +++++++++++--------- Core/Core/CoreInstance.h | 32 +++++---- {RenderModule => Core/Core}/RenderEngine.cpp | 34 ++++------ {RenderModule => Core/Core}/RenderEngine.h | 16 ++--- Core/Game/GameInstance.cpp | 1 + Core/Game/SaveMap/SaveMap.cpp | 18 +++-- RenderModule/CMakeLists.txt | 23 ------- TestGame/main_config.conf | 2 +- 10 files changed, 93 insertions(+), 106 deletions(-) rename {RenderModule => Core/Core}/RenderEngine.cpp (89%) rename {RenderModule => Core/Core}/RenderEngine.h (54%) delete mode 100644 RenderModule/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9bf010c..6d6e0a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,5 @@ add_subdirectory(FastRTTI) add_subdirectory(glfw) add_subdirectory(Core) add_subdirectory(ModuleLib) -add_subdirectory(RenderModule) add_subdirectory(TestGame) add_subdirectory(ProjectGenerator) diff --git a/Core/CMakeLists.txt b/Core/CMakeLists.txt index 83fcbdd..7b97125 100644 --- a/Core/CMakeLists.txt +++ b/Core/CMakeLists.txt @@ -2,8 +2,6 @@ set(CORE_NAME Core) find_package(Vulkan REQUIRED) -set(CORE_NAME Core) - file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp") add_library(${CORE_NAME} STATIC ${SRC}) diff --git a/Core/Core/CoreInstance.cpp b/Core/Core/CoreInstance.cpp index 30e719b..b3799e0 100644 --- a/Core/Core/CoreInstance.cpp +++ b/Core/Core/CoreInstance.cpp @@ -1,17 +1,22 @@ #include "CoreInstance.h" #include "SystemCalls.h" -#include "GLFW/glfw3.h" #include #include #include #include +#include +#include "RenderEngine.h" +#include "Game/GameInstance.h" #include "Log/Log.h" +#include "Game/SaveMap/SaveMap.h" CoreInstance* CoreInstance::self = nullptr; +extern GameInstance* GameFactory(CoreInstance&); + std::shared_ptr CoreInstance::MainConfig::save() { return std::make_shared("MainConfig"); @@ -28,7 +33,7 @@ void CoreInstance::MainConfig::load(std::shared_ptr save) void CoreInstance::Quit_callback() { - self->game->quit(); + self->game_->quit(); } CoreInstance::CoreInstance() @@ -37,16 +42,8 @@ CoreInstance::CoreInstance() callbacks_.Quit = &Quit_callback; - countCPU = System::getCountCPU(); - memorySize = System::getMemorySize(); - - if (!glfwInit()) - { - std::string msg; - const char* error; - glfwGetError(&error); - throw std::runtime_error("Fail initialize GLFW"); - } + countCPU_ = System::getCountCPU(); + memorySize_ = System::getMemorySize(); std::ifstream main_config_file(main_config_name); if (main_config_file.fail()) @@ -56,35 +53,40 @@ CoreInstance::CoreInstance() std::getline(main_config_file, payload); SaveMap load_main_config(payload); - main_config.load(std::make_shared(load_main_config)); + main_config_.load(std::make_shared(load_main_config)); + + render_engine_ = new RenderEngine(); + + game_ = GameFactory(*this); + if (game_ == nullptr) + throw std::runtime_error("Fail create game instance!"); } CoreInstance::~CoreInstance() { - delete game; - - for (auto& module : modules) + for (auto& module : modules_) System::QuitModule(module.handler); - modules.clear(); -} + modules_.clear(); -extern GameInstance* GameFactory(CoreInstance&); + delete render_engine_; + delete game_; +} void CoreInstance::start() { - for (auto& name : main_config.modules_names) + for (auto& name : main_config_.modules_names) { try { void* handler = System::InitModule(name.c_str(), &callbacks_); - modules.push_back(Module {name.c_str(), handler}); + modules_.push_back(Module {name.c_str(), handler}); } catch (const std::exception& e) { std::cout << e.what() << '\n'; } } - for (auto& module : modules) + for (auto& module : modules_) { try { @@ -95,15 +97,21 @@ void CoreInstance::start() } } - game = GameFactory(*this); - if (game == nullptr) - throw std::runtime_error("Fail create game instance!"); - game->start(); + std::thread game_thread(&GameInstance::start, game_); + render_engine_->start(); + game_->quit(); + game_thread.join(); +} + +void CoreInstance::quit() +{ + // Stop the main loop + render_engine_->stop_render(); } void* CoreInstance::getHandlerModule(const char* name) const { - for (const auto& module : modules) + for (const auto& module : modules_) { if (std::strcmp(module.name, name) == 0) return module.handler; @@ -113,25 +121,25 @@ void* CoreInstance::getHandlerModule(const char* name) const void* CoreInstance::enableModule(const char* name) { - for (const auto& module : modules) + for (const auto& module : modules_) { if (std::strcmp(module.name, name) == 0) throw std::runtime_error("Module already enabled"); } void* module = System::InitModule(name, &callbacks_); - modules.push_back({ name, module }); + modules_.push_back({ name, module }); return module; } void CoreInstance::disableModule(const char* name) { - for (auto i = modules.cbegin(); i != modules.cend(); ++i) + for (auto i = modules_.cbegin(); i != modules_.cend(); ++i) { if (std::strcmp(i->name, name) == 0) { System::QuitModule(i->handler); - modules.erase(i); + modules_.erase(i); break; } } diff --git a/Core/Core/CoreInstance.h b/Core/Core/CoreInstance.h index 9668f1c..7fb8f03 100644 --- a/Core/Core/CoreInstance.h +++ b/Core/Core/CoreInstance.h @@ -2,13 +2,17 @@ #include #include +#include +#include -#include "Game/GameInstance.h" -#include "Game/SaveMap/SaveMap.h" +#include "Game/SaveMap/ISave.h" #include "Core/CoreCallBacks.h" +class SaveMap; +class RenderEngine; +class GameInstance; + class CoreInstance { - static CoreInstance* self; struct Module { const char* name; void* handler; @@ -28,14 +32,17 @@ class CoreInstance { }; const char* main_config_name = "main_config.conf"; - MainConfig main_config; + MainConfig main_config_; - int countCPU; + int countCPU_; // Memory in MB - double memorySize; - std::list modules; + double memorySize_; + std::list modules_; - GameInstance* game = nullptr; + RenderEngine* render_engine_; + GameInstance* game_; + + static CoreInstance* self; CoreCallBacks callbacks_; #pragma region Callbacks @@ -46,14 +53,15 @@ public: ~CoreInstance(); void start(); + void quit(); /** * This method quit game * Async */ - int getCountCPU() const { return countCPU; } - double getMemorySize() const { return memorySize; } + int getCountCPU() const { return countCPU_; } + double getMemorySize() const { return memorySize_; } /** * @throws std::runtime_error if module isn't enabled @@ -74,6 +82,6 @@ public: */ void disableModule(const char* name) noexcept(false); - const std::string& getBaseWorldName() const { return main_config.base_world; } - const std::string& getGameName() const { return main_config.game_name; } + const std::string& getBaseWorldName() const { return main_config_.base_world; } + const std::string& getGameName() const { return main_config_.game_name; } }; diff --git a/RenderModule/RenderEngine.cpp b/Core/Core/RenderEngine.cpp similarity index 89% rename from RenderModule/RenderEngine.cpp rename to Core/Core/RenderEngine.cpp index ec07447..196df2f 100644 --- a/RenderModule/RenderEngine.cpp +++ b/Core/Core/RenderEngine.cpp @@ -1,15 +1,10 @@ #include "RenderEngine.h" #include -#include #include -#include "Core/CoreInstance.h" - -PROTECTION_FROM_GB(RenderEngine) - #ifdef _DEBUG -//#include "Log/Log.h" +#include "Log/Log.h" #include #define VK_CHECK(res) assert(res == VK_SUCCESS) @@ -21,15 +16,15 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( std::string msg = "validation layer: "; msg += pCallbackData->pMessage; - //Log(msg); + Log(msg); return VK_FALSE; } -VkResult RenderEngine::enable_layer_validation(VkInstance instance, VkDebugUtilsMessengerCreateInfoEXT* create_info) +VkResult RenderEngine::enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info) { - PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")); - return debug_creator(instance, create_info, nullptr, &debug_messenger_); + PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast(vkGetInstanceProcAddr(instance_, "vkCreateDebugUtilsMessengerEXT")); + return debug_creator(instance_, create_info, nullptr, &debug_messenger_); } #else #define VK_CHECK(res) if ((res) != VK_SUCCESS) throw std::runtime_error("Vulkan error: " + std::to_string(res)) @@ -52,10 +47,15 @@ RenderEngine::RenderEngine() : window(nullptr) , debug_messenger_(nullptr) #endif { - onStop.bind(this, &RenderEngine::stop_render); - if (glfwInit() == GLFW_FALSE) throw std::runtime_error("Fail init glfw"); + + window = glfwCreateWindow(640, 480, "UwU Engine", nullptr, nullptr); + if (!window) + { + glfwTerminate(); + throw std::runtime_error("Fail create window"); + } { std::vector extensions = get_required_extensions(); @@ -91,7 +91,7 @@ RenderEngine::RenderEngine() : window(nullptr) #endif VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_)); #ifdef _DEBUG - VK_CHECK(enable_layer_validation(instance_, &debug_messenger_create_info)); + VK_CHECK(enable_layer_validation(&debug_messenger_create_info)); #endif } @@ -128,19 +128,11 @@ RenderEngine::~RenderEngine() void RenderEngine::start() { - window = glfwCreateWindow(640, 480, "UwU Engine", nullptr, nullptr); - if (!window) - { - glfwTerminate(); - throw std::runtime_error("Fail create window"); - } - while (!glfwWindowShouldClose(window)) { glfwSwapBuffers(window); glfwPollEvents(); } - QuitGame(); } void RenderEngine::stop_render() diff --git a/RenderModule/RenderEngine.h b/Core/Core/RenderEngine.h similarity index 54% rename from RenderModule/RenderEngine.h rename to Core/Core/RenderEngine.h index 3551b85..c9202fc 100644 --- a/RenderModule/RenderEngine.h +++ b/Core/Core/RenderEngine.h @@ -1,31 +1,27 @@ #pragma once #define GLFW_INCLUDE_VULKAN +#include #include "GLFW/glfw3.h" -#include "ModuleInstance.h" - #include -template -class Delegate; - -class RenderEngine : public ModuleInstance +class RenderEngine { GLFWwindow* window; VkInstance instance_; #ifdef _DEBUG - VkResult enable_layer_validation(VkInstance instance, VkDebugUtilsMessengerCreateInfoEXT* create_info); + VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info); VkDebugUtilsMessengerEXT debug_messenger_; #endif static std::vector get_required_extensions(); - void stop_render(); public: RenderEngine(); - ~RenderEngine() override; + ~RenderEngine(); - void start() override; + void start(); + void stop_render(); }; \ No newline at end of file diff --git a/Core/Game/GameInstance.cpp b/Core/Game/GameInstance.cpp index 7442993..066c7ef 100644 --- a/Core/Game/GameInstance.cpp +++ b/Core/Game/GameInstance.cpp @@ -66,4 +66,5 @@ void GameInstance::start() void GameInstance::quit() { is_running = false; + core.quit(); } diff --git a/Core/Game/SaveMap/SaveMap.cpp b/Core/Game/SaveMap/SaveMap.cpp index f6df842..93f1417 100644 --- a/Core/Game/SaveMap/SaveMap.cpp +++ b/Core/Game/SaveMap/SaveMap.cpp @@ -66,7 +66,11 @@ SaveMap::SaveMap(const std::string& json_data) size_t begin = json_data.find(':', i) + 2; size_t end = i = json_data.find('\"', begin); i++; - std::string value = json_data.substr(begin, end - begin); + std::string value; + if (begin != end) + value = json_data.substr(begin, end - begin); + else + value = ""; save_string[name.c_str() + 1] = value; } else if (name[0] == 'o') { @@ -147,12 +151,16 @@ SaveMap::SaveMap(const std::string& json_data) continue; std::vector& vector_strings = save_vector_strings[key]; - size_t j = 2; + size_t j = 0; while (j < arr_data.length()) { - size_t begin = j; - size_t end = j = arr_data.find('\"', begin + 1); - std::string value = arr_data.substr(begin, end - begin); + size_t begin = arr_data.find('\"', j) + 1; + size_t end = j = arr_data.find('\"', begin); + std::string value; + if (begin != end) + value = arr_data.substr(begin, end - begin); + else + value = ""; vector_strings.push_back(value); j += 3; } diff --git a/RenderModule/CMakeLists.txt b/RenderModule/CMakeLists.txt deleted file mode 100644 index 7eda801..0000000 --- a/RenderModule/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -find_package(Vulkan REQUIRED) - -file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp") -add_library(RenderModule SHARED ${SRC}) - -target_compile_features(RenderModule PRIVATE cxx_std_17) - -target_include_directories(RenderModule - PRIVATE - ${PROJECT_SOURCE_DIR}/ModuleLib - ${PROJECT_SOURCE_DIR}/Core - ${PROJECT_SOURCE_DIR}/FastRTTI - ${PROJECT_SOURCE_DIR}/Delegate - ${PROJECT_SOURCE_DIR}/glfw/Include - ${Vulkan_INCLUDE_DIRS} -) - -target_link_libraries(RenderModule PRIVATE - ModuleLib - FastRTTI - glfw - Vulkan::Vulkan -) \ No newline at end of file diff --git a/TestGame/main_config.conf b/TestGame/main_config.conf index c99154d..a51e392 100644 --- a/TestGame/main_config.conf +++ b/TestGame/main_config.conf @@ -1 +1 @@ -{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":["RenderModule"]} \ No newline at end of file +{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":[]} \ No newline at end of file