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(Core)
add_subdirectory(ModuleLib)
add_subdirectory(RenderModule)
add_subdirectory(TestGame)
add_subdirectory(ProjectGenerator)
-2
View File
@@ -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})
+39 -31
View File
@@ -1,17 +1,22 @@
#include "CoreInstance.h"
#include "SystemCalls.h"
#include "GLFW/glfw3.h"
#include <exception>
#include <cstring>
#include <fstream>
#include <iostream>
#include <thread>
#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<SaveMap> CoreInstance::MainConfig::save()
{
return std::make_shared<SaveMap>("MainConfig");
@@ -28,7 +33,7 @@ void CoreInstance::MainConfig::load(std::shared_ptr<SaveMap> 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<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()
{
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;
}
}
+20 -12
View File
@@ -2,13 +2,17 @@
#include <string>
#include <list>
#include <memory>
#include <vector>
#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<Module> modules;
double memorySize_;
std::list<Module> 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; }
};
@@ -1,15 +1,10 @@
#include "RenderEngine.h"
#include <stdexcept>
#include <vector>
#include <string>
#include "Core/CoreInstance.h"
PROTECTION_FROM_GB(RenderEngine)
#ifdef _DEBUG
//#include "Log/Log.h"
#include "Log/Log.h"
#include <assert.h>
#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<PFN_vkCreateDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"));
return debug_creator(instance, create_info, nullptr, &debug_messenger_);
PFN_vkCreateDebugUtilsMessengerEXT debug_creator = reinterpret_cast<PFN_vkCreateDebugUtilsMessengerEXT>(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<const char*> 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()
@@ -1,31 +1,27 @@
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <vulkan/vulkan.h>
#include "GLFW/glfw3.h"
#include "ModuleInstance.h"
#include <vector>
template<typename T>
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<const char*> get_required_extensions();
void stop_render();
public:
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()
{
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 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<std::string>& 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;
}
-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":[]}