Integrate RenderEngine to Core

This commit is contained in:
Jiga228
2025-09-16 21:56:34 +07:00
parent f15c8b09cd
commit 25cc75ba69
10 changed files with 93 additions and 106 deletions
-1
View File
@@ -20,6 +20,5 @@ add_subdirectory(FastRTTI)
add_subdirectory(glfw) add_subdirectory(glfw)
add_subdirectory(Core) add_subdirectory(Core)
add_subdirectory(ModuleLib) add_subdirectory(ModuleLib)
add_subdirectory(RenderModule)
add_subdirectory(TestGame) add_subdirectory(TestGame)
add_subdirectory(ProjectGenerator) add_subdirectory(ProjectGenerator)
-2
View File
@@ -2,8 +2,6 @@ set(CORE_NAME Core)
find_package(Vulkan REQUIRED) find_package(Vulkan REQUIRED)
set(CORE_NAME Core)
file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp") file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp")
add_library(${CORE_NAME} STATIC ${SRC}) add_library(${CORE_NAME} STATIC ${SRC})
+39 -31
View File
@@ -1,17 +1,22 @@
#include "CoreInstance.h" #include "CoreInstance.h"
#include "SystemCalls.h" #include "SystemCalls.h"
#include "GLFW/glfw3.h"
#include <exception> #include <exception>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <thread>
#include "RenderEngine.h"
#include "Game/GameInstance.h"
#include "Log/Log.h" #include "Log/Log.h"
#include "Game/SaveMap/SaveMap.h"
CoreInstance* CoreInstance::self = nullptr; CoreInstance* CoreInstance::self = nullptr;
extern GameInstance* GameFactory(CoreInstance&);
std::shared_ptr<SaveMap> CoreInstance::MainConfig::save() std::shared_ptr<SaveMap> CoreInstance::MainConfig::save()
{ {
return std::make_shared<SaveMap>("MainConfig"); return std::make_shared<SaveMap>("MainConfig");
@@ -28,7 +33,7 @@ void CoreInstance::MainConfig::load(std::shared_ptr<SaveMap> save)
void CoreInstance::Quit_callback() void CoreInstance::Quit_callback()
{ {
self->game->quit(); self->game_->quit();
} }
CoreInstance::CoreInstance() CoreInstance::CoreInstance()
@@ -37,16 +42,8 @@ CoreInstance::CoreInstance()
callbacks_.Quit = &Quit_callback; callbacks_.Quit = &Quit_callback;
countCPU = System::getCountCPU(); countCPU_ = System::getCountCPU();
memorySize = System::getMemorySize(); memorySize_ = System::getMemorySize();
if (!glfwInit())
{
std::string msg;
const char* error;
glfwGetError(&error);
throw std::runtime_error("Fail initialize GLFW");
}
std::ifstream main_config_file(main_config_name); std::ifstream main_config_file(main_config_name);
if (main_config_file.fail()) if (main_config_file.fail())
@@ -56,35 +53,40 @@ CoreInstance::CoreInstance()
std::getline(main_config_file, payload); std::getline(main_config_file, payload);
SaveMap load_main_config(payload); SaveMap load_main_config(payload);
main_config.load(std::make_shared<SaveMap>(load_main_config)); main_config_.load(std::make_shared<SaveMap>(load_main_config));
render_engine_ = new RenderEngine();
game_ = GameFactory(*this);
if (game_ == nullptr)
throw std::runtime_error("Fail create game instance!");
} }
CoreInstance::~CoreInstance() CoreInstance::~CoreInstance()
{ {
delete game; for (auto& module : modules_)
for (auto& module : modules)
System::QuitModule(module.handler); System::QuitModule(module.handler);
modules.clear(); modules_.clear();
}
extern GameInstance* GameFactory(CoreInstance&); delete render_engine_;
delete game_;
}
void CoreInstance::start() void CoreInstance::start()
{ {
for (auto& name : main_config.modules_names) for (auto& name : main_config_.modules_names)
{ {
try try
{ {
void* handler = System::InitModule(name.c_str(), &callbacks_); 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) } catch (const std::exception& e)
{ {
std::cout << e.what() << '\n'; std::cout << e.what() << '\n';
} }
} }
for (auto& module : modules) for (auto& module : modules_)
{ {
try try
{ {
@@ -95,15 +97,21 @@ void CoreInstance::start()
} }
} }
game = GameFactory(*this); std::thread game_thread(&GameInstance::start, game_);
if (game == nullptr) render_engine_->start();
throw std::runtime_error("Fail create game instance!"); game_->quit();
game->start(); game_thread.join();
}
void CoreInstance::quit()
{
// Stop the main loop
render_engine_->stop_render();
} }
void* CoreInstance::getHandlerModule(const char* name) const void* CoreInstance::getHandlerModule(const char* name) const
{ {
for (const auto& module : modules) for (const auto& module : modules_)
{ {
if (std::strcmp(module.name, name) == 0) if (std::strcmp(module.name, name) == 0)
return module.handler; return module.handler;
@@ -113,25 +121,25 @@ void* CoreInstance::getHandlerModule(const char* name) const
void* CoreInstance::enableModule(const char* name) void* CoreInstance::enableModule(const char* name)
{ {
for (const auto& module : modules) for (const auto& module : modules_)
{ {
if (std::strcmp(module.name, name) == 0) if (std::strcmp(module.name, name) == 0)
throw std::runtime_error("Module already enabled"); throw std::runtime_error("Module already enabled");
} }
void* module = System::InitModule(name, &callbacks_); void* module = System::InitModule(name, &callbacks_);
modules.push_back({ name, module }); modules_.push_back({ name, module });
return module; return module;
} }
void CoreInstance::disableModule(const char* name) 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) if (std::strcmp(i->name, name) == 0)
{ {
System::QuitModule(i->handler); System::QuitModule(i->handler);
modules.erase(i); modules_.erase(i);
break; break;
} }
} }
+20 -12
View File
@@ -2,13 +2,17 @@
#include <string> #include <string>
#include <list> #include <list>
#include <memory>
#include <vector>
#include "Game/GameInstance.h" #include "Game/SaveMap/ISave.h"
#include "Game/SaveMap/SaveMap.h"
#include "Core/CoreCallBacks.h" #include "Core/CoreCallBacks.h"
class SaveMap;
class RenderEngine;
class GameInstance;
class CoreInstance { class CoreInstance {
static CoreInstance* self;
struct Module { struct Module {
const char* name; const char* name;
void* handler; void* handler;
@@ -28,14 +32,17 @@ class CoreInstance {
}; };
const char* main_config_name = "main_config.conf"; const char* main_config_name = "main_config.conf";
MainConfig main_config; MainConfig main_config_;
int countCPU; int countCPU_;
// Memory in MB // Memory in MB
double memorySize; double memorySize_;
std::list<Module> modules; std::list<Module> modules_;
GameInstance* game = nullptr; RenderEngine* render_engine_;
GameInstance* game_;
static CoreInstance* self;
CoreCallBacks callbacks_; CoreCallBacks callbacks_;
#pragma region Callbacks #pragma region Callbacks
@@ -46,14 +53,15 @@ public:
~CoreInstance(); ~CoreInstance();
void start(); void start();
void quit();
/** /**
* This method quit game * This method quit game
* Async * Async
*/ */
int getCountCPU() const { return countCPU; } int getCountCPU() const { return countCPU_; }
double getMemorySize() const { return memorySize; } double getMemorySize() const { return memorySize_; }
/** /**
* @throws std::runtime_error if module isn't enabled * @throws std::runtime_error if module isn't enabled
@@ -74,6 +82,6 @@ public:
*/ */
void disableModule(const char* name) noexcept(false); void disableModule(const char* name) noexcept(false);
const std::string& getBaseWorldName() const { return main_config.base_world; } const std::string& getBaseWorldName() const { return main_config_.base_world; }
const std::string& getGameName() const { return main_config.game_name; } const std::string& getGameName() const { return main_config_.game_name; }
}; };
@@ -1,15 +1,10 @@
#include "RenderEngine.h" #include "RenderEngine.h"
#include <stdexcept> #include <stdexcept>
#include <vector>
#include <string> #include <string>
#include "Core/CoreInstance.h"
PROTECTION_FROM_GB(RenderEngine)
#ifdef _DEBUG #ifdef _DEBUG
//#include "Log/Log.h" #include "Log/Log.h"
#include <assert.h> #include <assert.h>
#define VK_CHECK(res) assert(res == VK_SUCCESS) #define VK_CHECK(res) assert(res == VK_SUCCESS)
@@ -21,15 +16,15 @@ static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
std::string msg = "validation layer: "; std::string msg = "validation layer: ";
msg += pCallbackData->pMessage; msg += pCallbackData->pMessage;
//Log(msg); Log(msg);
return VK_FALSE; 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<PFN_vkCreateDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT")); PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance_, "vkCreateDebugUtilsMessengerEXT"));
return debug_creator(instance, create_info, nullptr, &debug_messenger_); return debug_creator(instance_, create_info, nullptr, &debug_messenger_);
} }
#else #else
#define VK_CHECK(res) if ((res) != VK_SUCCESS) throw std::runtime_error("Vulkan error: " + std::to_string(res)) #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) , debug_messenger_(nullptr)
#endif #endif
{ {
onStop.bind(this, &RenderEngine::stop_render);
if (glfwInit() == GLFW_FALSE) if (glfwInit() == GLFW_FALSE)
throw std::runtime_error("Fail init glfw"); 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<const char*> extensions = get_required_extensions(); std::vector<const char*> extensions = get_required_extensions();
@@ -91,7 +91,7 @@ RenderEngine::RenderEngine() : window(nullptr)
#endif #endif
VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_)); VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_));
#ifdef _DEBUG #ifdef _DEBUG
VK_CHECK(enable_layer_validation(instance_, &debug_messenger_create_info)); VK_CHECK(enable_layer_validation(&debug_messenger_create_info));
#endif #endif
} }
@@ -128,19 +128,11 @@ RenderEngine::~RenderEngine()
void RenderEngine::start() void RenderEngine::start()
{ {
window = glfwCreateWindow(640, 480, "UwU Engine", nullptr, nullptr);
if (!window)
{
glfwTerminate();
throw std::runtime_error("Fail create window");
}
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
QuitGame();
} }
void RenderEngine::stop_render() void RenderEngine::stop_render()
@@ -1,31 +1,27 @@
#pragma once #pragma once
#define GLFW_INCLUDE_VULKAN #define GLFW_INCLUDE_VULKAN
#include <vulkan/vulkan.h>
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "ModuleInstance.h"
#include <vector> #include <vector>
template<typename T> class RenderEngine
class Delegate;
class RenderEngine : public ModuleInstance
{ {
GLFWwindow* window; GLFWwindow* window;
VkInstance instance_; VkInstance instance_;
#ifdef _DEBUG #ifdef _DEBUG
VkResult enable_layer_validation(VkInstance instance, VkDebugUtilsMessengerCreateInfoEXT* create_info); VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info);
VkDebugUtilsMessengerEXT debug_messenger_; VkDebugUtilsMessengerEXT debug_messenger_;
#endif #endif
static std::vector<const char*> get_required_extensions(); static std::vector<const char*> get_required_extensions();
void stop_render();
public: public:
RenderEngine(); RenderEngine();
~RenderEngine() override; ~RenderEngine();
void start() override; void start();
void stop_render();
}; };
+1
View File
@@ -66,4 +66,5 @@ void GameInstance::start()
void GameInstance::quit() void GameInstance::quit()
{ {
is_running = false; is_running = false;
core.quit();
} }
+13 -5
View File
@@ -66,7 +66,11 @@ SaveMap::SaveMap(const std::string& json_data)
size_t begin = json_data.find(':', i) + 2; size_t begin = json_data.find(':', i) + 2;
size_t end = i = json_data.find('\"', begin); size_t end = i = json_data.find('\"', begin);
i++; 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; save_string[name.c_str() + 1] = value;
} else if (name[0] == 'o') } else if (name[0] == 'o')
{ {
@@ -147,12 +151,16 @@ SaveMap::SaveMap(const std::string& json_data)
continue; continue;
std::vector<std::string>& vector_strings = save_vector_strings[key]; std::vector<std::string>& vector_strings = save_vector_strings[key];
size_t j = 2; size_t j = 0;
while (j < arr_data.length()) while (j < arr_data.length())
{ {
size_t begin = j; size_t begin = arr_data.find('\"', j) + 1;
size_t end = j = arr_data.find('\"', begin + 1); size_t end = j = arr_data.find('\"', begin);
std::string value = arr_data.substr(begin, end - begin); std::string value;
if (begin != end)
value = arr_data.substr(begin, end - begin);
else
value = "";
vector_strings.push_back(value); vector_strings.push_back(value);
j += 3; j += 3;
} }
-23
View File
@@ -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
)
+1 -1
View File
@@ -1 +1 @@
{"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":["RenderModule"]} {"Class name":"MainConfig","dmin_memory_size":"4096.0","imin_CPU_count":"1","sgame_name":"Test Game","sbase_world":"TestWorld","vsmodules_names":[]}