From 430d23b719042ad46fce97bbbab3b25412c7e836 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Mon, 16 Mar 2026 18:06:57 +0800 Subject: [PATCH] Replace GLFW window management with OpenGLDevice from engine --- engine/src/RHI/OpenGL/OpenGLDevice.cpp | 8 +++++ tests/OpenGL/main.cpp | 46 ++++++++++++-------------- 2 files changed, 29 insertions(+), 25 deletions(-) diff --git a/engine/src/RHI/OpenGL/OpenGLDevice.cpp b/engine/src/RHI/OpenGL/OpenGLDevice.cpp index 7231ca50..3782c001 100644 --- a/engine/src/RHI/OpenGL/OpenGLDevice.cpp +++ b/engine/src/RHI/OpenGL/OpenGLDevice.cpp @@ -21,6 +21,12 @@ bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, return true; } + static bool glfwInitialized = false; + if (!glfwInitialized) { + glfwInit(); + glfwInitialized = true; + } + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); @@ -30,6 +36,8 @@ bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, return false; } + glfwSetWindowShouldClose(m_window, GLFW_FALSE); + m_ownsWindow = true; return InitializeWithExistingWindow(m_window); } diff --git a/tests/OpenGL/main.cpp b/tests/OpenGL/main.cpp index 77e99adf..856615f4 100644 --- a/tests/OpenGL/main.cpp +++ b/tests/OpenGL/main.cpp @@ -18,6 +18,7 @@ #include "XCEngine/Debug/ConsoleLogSink.h" #include "XCEngine/Debug/FileLogSink.h" #include "XCEngine/RHI/OpenGL/OpenGLShader.h" +#include "XCEngine/RHI/OpenGL/OpenGLDevice.h" using namespace XCEngine::Debug; using namespace XCEngine::RHI; @@ -260,6 +261,7 @@ private: Model* model = nullptr; OpenGLShader* shader = nullptr; +OpenGLDevice* device = nullptr; int frameCount = 0; void framebuffer_size_callback(GLFWwindow* window, int width, int height) @@ -347,54 +349,48 @@ int main() AllocConsole(); freopen("CONOUT$", "w", stdout); - glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "XCRender", NULL, NULL); - if (window == NULL) + device = new OpenGLDevice(); + if (!device->CreateRenderWindow(SCR_WIDTH, SCR_HEIGHT, "XCRender", false)) { - Log("Failed to create GLFW window"); + Log("Failed to create window"); glfwTerminate(); return -1; } - glfwMakeContextCurrent(window); - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + glfwSetFramebufferSizeCallback(device->GetWindow(), framebuffer_size_callback); - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) - { - Log("Failed to initialize GLAD"); - return -1; - } - - Log("OpenGL Version: %s", glGetString(GL_VERSION)); - Log("Renderer: %s", glGetString(GL_RENDERER)); + const OpenGLDeviceInfo& info = device->GetDeviceInfo(); + Log("OpenGL Version: %s", info.version.c_str()); + Log("Renderer: %s", info.renderer.c_str()); Initialize(); - while (!glfwWindowShouldClose(window)) + device->PollEvents(); + + while (!glfwWindowShouldClose(device->GetWindow())) { - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(window, true); + if (glfwGetKey(device->GetWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(device->GetWindow(), GLFW_TRUE); Render(); - glfwSwapBuffers(window); - glfwPollEvents(); + device->SwapBuffers(); + device->PollEvents(); frameCount++; if (frameCount == 30) { Log("Saving screenshot at frame %d", frameCount); SaveScreenshot("screenshot.ppm"); - glfwSetWindowShouldClose(window, true); + glfwSetWindowShouldClose(device->GetWindow(), GLFW_TRUE); } } Log("Application closed"); - glfwTerminate(); + delete device; + delete shader; + delete model; + Logger::Get().Shutdown(); return 0; }