Replace GLFW window management with OpenGLDevice from engine

This commit is contained in:
2026-03-16 18:06:57 +08:00
parent 220494c3c5
commit 430d23b719
2 changed files with 29 additions and 25 deletions

View File

@@ -21,6 +21,12 @@ bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title,
return true; return true;
} }
static bool glfwInitialized = false;
if (!glfwInitialized) {
glfwInit();
glfwInitialized = true;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@@ -30,6 +36,8 @@ bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title,
return false; return false;
} }
glfwSetWindowShouldClose(m_window, GLFW_FALSE);
m_ownsWindow = true; m_ownsWindow = true;
return InitializeWithExistingWindow(m_window); return InitializeWithExistingWindow(m_window);
} }

View File

@@ -18,6 +18,7 @@
#include "XCEngine/Debug/ConsoleLogSink.h" #include "XCEngine/Debug/ConsoleLogSink.h"
#include "XCEngine/Debug/FileLogSink.h" #include "XCEngine/Debug/FileLogSink.h"
#include "XCEngine/RHI/OpenGL/OpenGLShader.h" #include "XCEngine/RHI/OpenGL/OpenGLShader.h"
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
using namespace XCEngine::Debug; using namespace XCEngine::Debug;
using namespace XCEngine::RHI; using namespace XCEngine::RHI;
@@ -260,6 +261,7 @@ private:
Model* model = nullptr; Model* model = nullptr;
OpenGLShader* shader = nullptr; OpenGLShader* shader = nullptr;
OpenGLDevice* device = nullptr;
int frameCount = 0; int frameCount = 0;
void framebuffer_size_callback(GLFWwindow* window, int width, int height) void framebuffer_size_callback(GLFWwindow* window, int width, int height)
@@ -347,54 +349,48 @@ int main()
AllocConsole(); AllocConsole();
freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stdout);
glfwInit(); device = new OpenGLDevice();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); if (!device->CreateRenderWindow(SCR_WIDTH, SCR_HEIGHT, "XCRender", false))
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)
{ {
Log("Failed to create GLFW window"); Log("Failed to create window");
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(device->GetWindow(), framebuffer_size_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) const OpenGLDeviceInfo& info = device->GetDeviceInfo();
{ Log("OpenGL Version: %s", info.version.c_str());
Log("Failed to initialize GLAD"); Log("Renderer: %s", info.renderer.c_str());
return -1;
}
Log("OpenGL Version: %s", glGetString(GL_VERSION));
Log("Renderer: %s", glGetString(GL_RENDERER));
Initialize(); Initialize();
while (!glfwWindowShouldClose(window)) device->PollEvents();
while (!glfwWindowShouldClose(device->GetWindow()))
{ {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) if (glfwGetKey(device->GetWindow(), GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(device->GetWindow(), GLFW_TRUE);
Render(); Render();
glfwSwapBuffers(window); device->SwapBuffers();
glfwPollEvents(); device->PollEvents();
frameCount++; frameCount++;
if (frameCount == 30) { if (frameCount == 30) {
Log("Saving screenshot at frame %d", frameCount); Log("Saving screenshot at frame %d", frameCount);
SaveScreenshot("screenshot.ppm"); SaveScreenshot("screenshot.ppm");
glfwSetWindowShouldClose(window, true); glfwSetWindowShouldClose(device->GetWindow(), GLFW_TRUE);
} }
} }
Log("Application closed"); Log("Application closed");
glfwTerminate(); delete device;
delete shader;
delete model;
Logger::Get().Shutdown(); Logger::Get().Shutdown();
return 0; return 0;
} }