Вынес движок рендера в отдельный модуль ввиде статической библиотеки
This commit is contained in:
+5
-2
@@ -6,12 +6,15 @@ file(GLOB_RECURSE SRC "*.cpp" "*.c" "*.h" "*.hpp")
|
||||
|
||||
add_library(${CORE_NAME} STATIC ${SRC})
|
||||
target_compile_features(${CORE_NAME} PRIVATE cxx_std_17)
|
||||
target_include_directories(${CORE_NAME} PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/FastRTTI
|
||||
${PROJECT_SOURCE_DIR}/Delegate
|
||||
)
|
||||
target_include_directories(${CORE_NAME}
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${PROJECT_SOURCE_DIR}/FastRTTI
|
||||
${PROJECT_SOURCE_DIR}/Delegate
|
||||
${PROJECT_SOURCE_DIR}/glfw/Include
|
||||
${PROJECT_SOURCE_DIR}/RenderEngineSDK
|
||||
${Vulkan_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
|
||||
+23
-17
@@ -3,16 +3,16 @@
|
||||
#include "SystemCalls.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "RenderEngine/RenderEngine.hpp"
|
||||
#include "RenderEngineBase.hpp"
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
|
||||
extern GameInstance* GameFactory(CoreInstance&);
|
||||
RenderEngineBase* RenderEngineFactory(CoreInstance& core);
|
||||
|
||||
std::shared_ptr<SaveMap> CoreInstance::MainConfig::save()
|
||||
{
|
||||
@@ -50,22 +50,31 @@ CoreInstance::CoreInstance()
|
||||
|
||||
main_config_.load(std::make_shared<SaveMap>(load_main_config));
|
||||
|
||||
if (glfwInit() == GLFW_FALSE)
|
||||
throw std::runtime_error("Fail init glfw");
|
||||
// if (glfwInit() == GLFW_FALSE)
|
||||
// throw std::runtime_error("Fail init glfw");
|
||||
//
|
||||
// glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
// glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
// window_ = glfwCreateWindow(800, 600, main_config_.game_name.c_str(), nullptr, nullptr);
|
||||
// if (window_ == nullptr)
|
||||
// {
|
||||
// std::cout << "[!] Init render engine: Fail create window\n";
|
||||
// return;
|
||||
// }
|
||||
// try {
|
||||
// render_engine_ = new RenderEngine(*this, window_);
|
||||
// } catch (const std::exception& e)
|
||||
// {
|
||||
// std::cout << "[!] Init render engine: " << e.what() << '\n';
|
||||
// return;
|
||||
// }
|
||||
|
||||
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
|
||||
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
||||
window_ = glfwCreateWindow(800, 600, main_config_.game_name.c_str(), nullptr, nullptr);
|
||||
if (window_ == nullptr)
|
||||
try
|
||||
{
|
||||
std::cout << "[!] Init render engine: Fail create window\n";
|
||||
return;
|
||||
}
|
||||
try {
|
||||
render_engine_ = new RenderEngine(*this, window_);
|
||||
render_engine_ = RenderEngineFactory(*this);
|
||||
} catch (const std::exception& e)
|
||||
{
|
||||
std::cout << "[!] Init render engine: " << e.what() << '\n';
|
||||
std::cerr << "[!] Init render engine: " << e.what() << '\n';
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -86,9 +95,6 @@ CoreInstance::~CoreInstance()
|
||||
|
||||
delete game_;
|
||||
delete render_engine_;
|
||||
|
||||
glfwDestroyWindow(window_);
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
void CoreInstance::start()
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
#include "Game/SaveMap/ISave.h"
|
||||
|
||||
class SaveMap;
|
||||
class RenderEngine;
|
||||
class RenderEngineBase;
|
||||
class GameInstance;
|
||||
struct GLFWwindow;
|
||||
|
||||
@@ -43,7 +43,7 @@ class CoreInstance {
|
||||
std::list<Module> modules_;
|
||||
|
||||
GLFWwindow* window_ = nullptr;
|
||||
RenderEngine* render_engine_;
|
||||
RenderEngineBase* render_engine_;
|
||||
GameInstance* game_;
|
||||
|
||||
bool is_ready = false;
|
||||
@@ -60,5 +60,5 @@ public:
|
||||
const std::string& getBaseWorldName() const { return main_config_.base_world; }
|
||||
const std::string& getGameName() const { return main_config_.game_name; }
|
||||
GameInstance* GetGameInstance() const { return game_; }
|
||||
RenderEngine* GetRenderEngine() const { return render_engine_; }
|
||||
RenderEngineBase* GetRenderEngine() const { return render_engine_; }
|
||||
};
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,170 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
#define GLFW_INCLUDE_VULKAN
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#define GLM_FORCE_RADIANS
|
||||
#include <glm/glm.hpp>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
#include "ModelManager.hpp"
|
||||
|
||||
#ifdef _DEBUG
|
||||
#include <assert.h>
|
||||
#define VK_CHECK(res) assert(res == VK_SUCCESS)
|
||||
#else
|
||||
#define VK_CHECK(res) if ((res) != VK_SUCCESS) throw std::runtime_error("Vulkan error: " + std::to_string(res))
|
||||
#endif
|
||||
|
||||
class CoreInstance;
|
||||
|
||||
class RenderEngine
|
||||
{
|
||||
struct QueueFamilyIndices
|
||||
{
|
||||
std::optional<uint32_t> graphics_family;
|
||||
std::optional<uint32_t> present_family;
|
||||
std::optional<uint32_t> transfer_family;
|
||||
};
|
||||
struct SwapchainSupportDetails
|
||||
{
|
||||
VkSurfaceCapabilitiesKHR capabilities;
|
||||
std::vector<VkSurfaceFormatKHR> formats;
|
||||
std::vector<VkPresentModeKHR> present_modes;
|
||||
};
|
||||
struct Vertex
|
||||
{
|
||||
glm::vec3 pos;
|
||||
glm::vec3 color;
|
||||
static VkVertexInputBindingDescription get_binding_description();
|
||||
static std::array<VkVertexInputAttributeDescription, 2> get_vertex_attribute_descriptions();
|
||||
};
|
||||
struct UniformBufferObject {
|
||||
glm::mat4 model;
|
||||
glm::mat4 view;
|
||||
glm::mat4 proj;
|
||||
};
|
||||
|
||||
const std::vector<Vertex> vertices_ = {
|
||||
{{-0.5f, -0.5f, 0.5f}, {1.0f, 0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, 0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f, 0.5f}, {0.0f, 0.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, 0.5f}, {1.0f, 1.0f, 1.0f}},
|
||||
{{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}},
|
||||
{{0.5f, -0.5f, -0.5f}, {0.0f, 1.0f, 0.0f}},
|
||||
{{0.5f, 0.5f, -0.5f}, {0.0f, 0.0f, 1.0f}},
|
||||
{{-0.5f, 0.5f, -0.5f}, {1.0f, 1.0f, 1.0f}}
|
||||
};
|
||||
const std::vector<uint16_t> indices_ = {
|
||||
0, 1, 2, 2, 3, 0,
|
||||
2, 1, 5, 5, 6, 2,
|
||||
0, 3, 7, 7, 4, 0,
|
||||
3, 2, 6, 6, 7, 3,
|
||||
7, 6, 5, 5, 4, 7,
|
||||
4, 5, 1, 1, 0, 4
|
||||
};
|
||||
|
||||
ModelManager model_manager_;
|
||||
|
||||
static const std::vector<const char*> deviceExtensions;
|
||||
|
||||
int current_frame_ = 0;
|
||||
|
||||
CoreInstance& core_;
|
||||
|
||||
GLFWwindow* window_;
|
||||
|
||||
VkInstance instance_ = VK_NULL_HANDLE;
|
||||
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
||||
VkPhysicalDevice physical_device_ = VK_NULL_HANDLE;
|
||||
VkDevice device_ = VK_NULL_HANDLE;
|
||||
VkQueue graphics_queue_ = VK_NULL_HANDLE;
|
||||
VkQueue present_queue_ = VK_NULL_HANDLE;
|
||||
VkQueue transfer_queue_ = VK_NULL_HANDLE;
|
||||
VkSwapchainKHR swapchain_ = VK_NULL_HANDLE;
|
||||
std::vector<VkImage> swapchain_images_;
|
||||
VkFormat swapchain_image_format_;
|
||||
VkExtent2D swapchain_extent_;
|
||||
std::vector<VkImageView> swapchain_image_views_;
|
||||
VkRenderPass render_pass_ = VK_NULL_HANDLE;
|
||||
VkDescriptorSetLayout ubo_layout_binding_ = VK_NULL_HANDLE;
|
||||
VkDescriptorPool descriptor_pool_ = VK_NULL_HANDLE;
|
||||
std::vector<VkDescriptorSet> descriptor_sets_;
|
||||
VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE;
|
||||
VkPipeline graphics_pipeline_ = VK_NULL_HANDLE;
|
||||
std::vector<VkFramebuffer> framebuffers_;
|
||||
VkCommandPool command_pool_ = VK_NULL_HANDLE;
|
||||
std::vector<VkCommandBuffer> command_buffers_;
|
||||
VkBuffer vertex_buffer_ = VK_NULL_HANDLE;
|
||||
VkDeviceMemory vertex_buffer_memory_ = VK_NULL_HANDLE;
|
||||
VkBuffer index_buffer_ = VK_NULL_HANDLE;
|
||||
VkDeviceMemory index_buffer_memory_ = VK_NULL_HANDLE;
|
||||
std::vector<VkBuffer> uniform_buffers_;
|
||||
std::vector<VkDeviceMemory> uniform_buffers_memory_;
|
||||
std::vector<void*> uniform_buffers_mapped_;
|
||||
|
||||
// Sync objects
|
||||
std::vector<VkSemaphore> image_available_semaphores_, render_finished_semaphores_;
|
||||
std::vector<VkFence> in_flight_fences_;
|
||||
|
||||
#ifdef _DEBUG
|
||||
VkResult enable_layer_validation(const VkDebugUtilsMessengerCreateInfoEXT* create_info);
|
||||
VkDebugUtilsMessengerEXT debug_messenger_;
|
||||
#endif
|
||||
SwapchainSupportDetails query_swapchain_details() const;
|
||||
|
||||
static std::vector<const char*> get_required_extensions();
|
||||
static bool is_device_suitable(VkPhysicalDevice device, VkSurfaceKHR surface);
|
||||
static bool check_device_extensions_support(VkPhysicalDevice device);
|
||||
static QueueFamilyIndices find_queue_family_indices(VkPhysicalDevice physical_device, VkSurfaceKHR surface);
|
||||
static VkSurfaceFormatKHR choose_surface_format(const std::vector<VkSurfaceFormatKHR>& surface_formats);
|
||||
static VkPresentModeKHR choose_present_mode(const std::vector<VkPresentModeKHR>& present_modes, VkPresentModeKHR desired_present_mode);
|
||||
static VkExtent2D choose_extent(const VkSurfaceCapabilitiesKHR& capabilities, GLFWwindow* window);
|
||||
VkShaderModule create_shader_module(const std::string& code) const;
|
||||
void create_buffer(VkDeviceSize size, VkBufferUsageFlags usage,
|
||||
VkBuffer* buffer, const std::vector<uint32_t>& queue_families);
|
||||
void allocate_memory(VkBuffer buffer, VkMemoryPropertyFlags property, VkDeviceMemory* device_memory);
|
||||
uint32_t find_memory_type(uint32_t typeFilter, VkMemoryPropertyFlags properties) const;
|
||||
std::vector<uint32_t> get_unique_family_indices(const QueueFamilyIndices& indices);
|
||||
|
||||
void create_instance();
|
||||
void create_surface();
|
||||
void pick_physical_device();
|
||||
void create_logical_device();
|
||||
void create_swapchain(VkPresentModeKHR desired_present_mode);
|
||||
void create_image_views();
|
||||
void create_render_pass();
|
||||
void create_description_set_layout();
|
||||
void create_uniform_buffers();
|
||||
void create_descriptor_pool();
|
||||
void create_descriptor_sets();
|
||||
void create_graphics_pipeline();
|
||||
void create_framebuffers();
|
||||
void create_command_pool();
|
||||
void allocate_command_buffers();
|
||||
void create_sync_objects();
|
||||
void allocate_vertex_buffer();
|
||||
void allocate_index_buffer();
|
||||
|
||||
void copy_memory(VkBuffer src, VkBuffer dst, VkDeviceSize size);
|
||||
void record_command_buffer(VkCommandBuffer command_buffer, uint32_t image_index) const;
|
||||
void update_uniform_buffer(uint32_t current_frame);
|
||||
void draw_frame();
|
||||
public:
|
||||
// Can throw the exception
|
||||
RenderEngine(CoreInstance& core, GLFWwindow* window, VkPresentModeKHR desired_present_mode = VK_PRESENT_MODE_MAILBOX_KHR);
|
||||
~RenderEngine();
|
||||
|
||||
void start();
|
||||
void stop_render() const;
|
||||
|
||||
[[nodiscard]]
|
||||
GLFWwindow* get_window() const { return window_; }
|
||||
|
||||
ModelManager* GetActiveModelManager() { return &model_manager_; }
|
||||
};
|
||||
@@ -1,9 +1,7 @@
|
||||
#include "StaticMesh.hpp"
|
||||
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Core/RenderEngine/RenderEngine.hpp"
|
||||
#include "Game/GameInstance.hpp"
|
||||
#include "Log/Log.hpp"
|
||||
#include "Game/SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
|
||||
@@ -26,11 +24,11 @@ void StaticMesh::load_model(const std::string& model_name)
|
||||
{
|
||||
model_name_ = model_name;
|
||||
SetModelName(model_name);
|
||||
model_ = GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->LoadModel(model_name);
|
||||
model_ = GetWorld()->GetGameInstance().GetCurrentModelManager()->LoadModel(model_name);
|
||||
}
|
||||
|
||||
void StaticMesh::OnDestroy()
|
||||
{
|
||||
Mesh::OnDestroy();
|
||||
GetWorld()->GetGameInstance().GetCore().GetRenderEngine()->GetActiveModelManager()->FreeModel(model_name_);
|
||||
GetWorld()->GetGameInstance().GetCurrentModelManager()->FreeModel(model_name_);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Mesh.hpp"
|
||||
#include "Core/RenderEngine/ModelManager.hpp"
|
||||
#include "Game/ModelManager.hpp"
|
||||
|
||||
GENERATE_META(StaticMesh)
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
#include "ModelManager.hpp"
|
||||
#include "Core/CoreInstance.hpp"
|
||||
#include "Game/WorldFactory.hpp"
|
||||
#include "SaveMap/SaveMap.hpp"
|
||||
#include "Game/World/World.hpp"
|
||||
|
||||
extern std::vector<WorldFactory> world_factories;
|
||||
@@ -19,6 +19,8 @@ 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)
|
||||
@@ -36,6 +38,7 @@ GameInstance::GameInstance(CoreInstance& core) : core(core)
|
||||
GameInstance::~GameInstance()
|
||||
{
|
||||
delete world;
|
||||
delete current_model_manager_;
|
||||
}
|
||||
|
||||
void GameInstance::start()
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
class CoreInstance;
|
||||
class World;
|
||||
class ModelManager;
|
||||
|
||||
#define GENERATE_FACTORY_GAME_INSTANCE(Class) \
|
||||
GameInstance* GameFactory(CoreInstance& core) { return new Class(core); }
|
||||
@@ -13,11 +14,12 @@ class GameInstance
|
||||
{
|
||||
CoreInstance& core;
|
||||
World* world = nullptr;
|
||||
ModelManager* current_model_manager_ = nullptr;
|
||||
|
||||
std::atomic<bool> is_running = true;
|
||||
|
||||
void start();
|
||||
// Stop call from core
|
||||
// Stop call from the core
|
||||
void stop();
|
||||
public:
|
||||
// Stop call from game
|
||||
@@ -29,6 +31,7 @@ public:
|
||||
|
||||
World* GetWorld() const { return world; }
|
||||
CoreInstance& GetCore() const { return core; }
|
||||
ModelManager* GetCurrentModelManager() const { return current_model_manager_; }
|
||||
|
||||
friend class CoreInstance;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user