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
-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; }
};
+142
View File
@@ -0,0 +1,142 @@
#include "RenderEngine.h"
#include <stdexcept>
#include <string>
#ifdef _DEBUG
#include "Log/Log.h"
#include <assert.h>
#define VK_CHECK(res) assert(res == VK_SUCCESS)
static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback(
VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
VkDebugUtilsMessageTypeFlagsEXT messageType,
const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
void* pUserData) {
std::string msg = "validation layer: ";
msg += pCallbackData->pMessage;
Log(msg);
return VK_FALSE;
}
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_);
}
#else
#define VK_CHECK(res) if ((res) != VK_SUCCESS) throw std::runtime_error("Vulkan error: " + std::to_string(res))
#endif
std::vector<const char*> RenderEngine::get_required_extensions()
{
uint32_t extensions_count = 0;
const char** glfw_extension = glfwGetRequiredInstanceExtensions(&extensions_count);
std::vector<const char*> extensions(glfw_extension, glfw_extension + extensions_count);
#ifdef _DEBUG
extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
#endif
return extensions;
}
RenderEngine::RenderEngine() : window(nullptr)
#ifdef _DEBUG
, debug_messenger_(nullptr)
#endif
{
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();
std::vector<const char*> layers = {
#ifdef _DEBUG
"VK_LAYER_KHRONOS_validation"
#endif
};
VkApplicationInfo app_info{};
app_info.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
app_info.apiVersion = VK_API_VERSION_1_0;
app_info.applicationVersion = VK_MAKE_VERSION(0, 0, 0);
app_info.pApplicationName = "UwU Engine";
app_info.pEngineName = "UwU Engine";
app_info.engineVersion = VK_MAKE_VERSION(0, 0, 0);
VkInstanceCreateInfo instance_create_info{};
instance_create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
instance_create_info.pApplicationInfo = &app_info;
instance_create_info.enabledExtensionCount = extensions.size();
instance_create_info.ppEnabledExtensionNames = extensions.data();
instance_create_info.enabledLayerCount = layers.size();
instance_create_info.ppEnabledLayerNames = layers.data();
#ifdef _DEBUG
VkDebugUtilsMessengerCreateInfoEXT debug_messenger_create_info{};
debug_messenger_create_info.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;
debug_messenger_create_info.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
debug_messenger_create_info.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
debug_messenger_create_info.pfnUserCallback = &debugCallback;
instance_create_info.pNext = &debug_messenger_create_info;
#endif
VK_CHECK(vkCreateInstance(&instance_create_info, nullptr, &instance_));
#ifdef _DEBUG
VK_CHECK(enable_layer_validation(&debug_messenger_create_info));
#endif
}
{
uint32_t device_count = 0;
std::vector<VkPhysicalDevice> devices;
VK_CHECK(vkEnumeratePhysicalDevices(instance_, &device_count, nullptr));
VK_CHECK(vkEnumeratePhysicalDevices(instance_, &device_count, devices.data()));
for (const auto& device : devices)
{
VkPhysicalDeviceProperties device_properties;
vkGetPhysicalDeviceProperties(device, &device_properties);
}
}
}
RenderEngine::~RenderEngine()
{
#ifdef _DEBUG
if (debug_messenger_ != nullptr && instance_ != nullptr) {
PFN_vkDestroyDebugUtilsMessengerEXT destroyer_messenger = reinterpret_cast<PFN_vkDestroyDebugUtilsMessengerEXT>(vkGetInstanceProcAddr(instance_, "vkDestroyDebugUtilsMessengerEXT"));
destroyer_messenger(instance_, debug_messenger_, nullptr);
}
#endif
if (instance_ != nullptr)
vkDestroyInstance(instance_, nullptr);
if (window != nullptr)
glfwDestroyWindow(window);
glfwTerminate();
}
void RenderEngine::start()
{
while (!glfwWindowShouldClose(window))
{
glfwSwapBuffers(window);
glfwPollEvents();
}
}
void RenderEngine::stop_render()
{
if (window != nullptr && !glfwWindowShouldClose(window))
glfwSetWindowShouldClose(window, true);
}
+27
View File
@@ -0,0 +1,27 @@
#pragma once
#define GLFW_INCLUDE_VULKAN
#include <vulkan/vulkan.h>
#include "GLFW/glfw3.h"
#include <vector>
class RenderEngine
{
GLFWwindow* window;
VkInstance instance_;
#ifdef _DEBUG
VkResult enable_layer_validation(VkDebugUtilsMessengerCreateInfoEXT* create_info);
VkDebugUtilsMessengerEXT debug_messenger_;
#endif
static std::vector<const char*> get_required_extensions();
public:
RenderEngine();
~RenderEngine();
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;
}