Moved window create from RenderEngine to CoreInstance

This commit is contained in:
Jiga228
2025-09-17 21:51:31 +07:00
parent 25cc75ba69
commit e151663de5
6 changed files with 33 additions and 37 deletions
+14 -7
View File
@@ -8,7 +8,6 @@
#include <iostream>
#include <thread>
#include "RenderEngine.h"
#include "Game/GameInstance.h"
#include "Log/Log.h"
#include "Game/SaveMap/SaveMap.h"
@@ -55,11 +54,19 @@ CoreInstance::CoreInstance()
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!");
if (glfwInit() == GLFW_FALSE)
throw std::runtime_error("Fail init glfw");
window_ = glfwCreateWindow(800, 600, main_config_.game_name.c_str(), nullptr, nullptr);
if (window_ == nullptr)
{
glfwTerminate();
throw std::runtime_error("Fail create window");
}
render_engine_ = new RenderEngine(window_);
game_ = GameFactory(*this);
if (game_ == nullptr)
throw std::runtime_error("Fail create game instance!");
}
CoreInstance::~CoreInstance()
@@ -99,7 +106,7 @@ void CoreInstance::start()
std::thread game_thread(&GameInstance::start, game_);
render_engine_->start();
game_->quit();
game_->stop();
game_thread.join();
}
+3 -6
View File
@@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include "RenderEngine.h"
#include "Game/SaveMap/ISave.h"
#include "Core/CoreCallBacks.h"
@@ -25,7 +26,7 @@ class CoreInstance {
std::vector<std::string> modules_names;
double min_memory_size = -1;
int min_CPU_count = -1;
long long min_CPU_count = -1;
std::shared_ptr<SaveMap> save() override;
void load(std::shared_ptr<SaveMap> save) override;
@@ -39,6 +40,7 @@ class CoreInstance {
double memorySize_;
std::list<Module> modules_;
GLFWwindow* window_ = nullptr;
RenderEngine* render_engine_;
GameInstance* game_;
@@ -54,11 +56,6 @@ public:
void start();
void quit();
/**
* This method quit game
* Async
*/
int getCountCPU() const { return countCPU_; }
double getMemorySize() const { return memorySize_; }
+7 -17
View File
@@ -42,21 +42,11 @@ std::vector<const char*> RenderEngine::get_required_extensions()
return extensions;
}
RenderEngine::RenderEngine() : window(nullptr)
RenderEngine::RenderEngine(GLFWwindow* window) : window_(window)
#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 = {
@@ -121,22 +111,22 @@ RenderEngine::~RenderEngine()
if (instance_ != nullptr)
vkDestroyInstance(instance_, nullptr);
if (window != nullptr)
glfwDestroyWindow(window);
if (window_ != nullptr)
glfwDestroyWindow(window_);
glfwTerminate();
}
void RenderEngine::start()
{
while (!glfwWindowShouldClose(window))
while (!glfwWindowShouldClose(window_))
{
glfwSwapBuffers(window);
glfwSwapBuffers(window_);
glfwPollEvents();
}
}
void RenderEngine::stop_render()
{
if (window != nullptr && !glfwWindowShouldClose(window))
glfwSetWindowShouldClose(window, true);
if (window_ != nullptr && !glfwWindowShouldClose(window_))
glfwSetWindowShouldClose(window_, true);
}
+2 -2
View File
@@ -8,7 +8,7 @@
class RenderEngine
{
GLFWwindow* window;
GLFWwindow* window_;
VkInstance instance_;
#ifdef _DEBUG
@@ -19,7 +19,7 @@ class RenderEngine
static std::vector<const char*> get_required_extensions();
public:
RenderEngine();
RenderEngine(GLFWwindow* window);
~RenderEngine();
void start();
+6 -1
View File
@@ -63,8 +63,13 @@ void GameInstance::start()
}
}
void GameInstance::quit()
void GameInstance::stop()
{
is_running = false;
}
void GameInstance::quit()
{
stop();
core.quit();
}
+1 -4
View File
@@ -16,11 +16,8 @@ class GameInstance
std::atomic<bool> is_running = true;
// Create a window
// Render
// Pull events
void start();
void stop();
public:
void quit();