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;
}
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);
}

View File

@@ -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;
}