2026-03-16 16:07:12 +08:00
|
|
|
#define GLFW_INCLUDE_NONE
|
2026-03-16 17:22:45 +08:00
|
|
|
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
|
2026-03-16 16:07:12 +08:00
|
|
|
#include <glad/glad.h>
|
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
|
|
|
|
|
|
namespace XCEngine {
|
|
|
|
|
namespace RHI {
|
|
|
|
|
|
|
|
|
|
OpenGLDevice::OpenGLDevice()
|
|
|
|
|
: m_window(nullptr)
|
|
|
|
|
, m_initialized(false)
|
|
|
|
|
, m_ownsWindow(false) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OpenGLDevice::~OpenGLDevice() {
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 17:22:45 +08:00
|
|
|
bool OpenGLDevice::CreateRenderWindow(int width, int height, const char* title, bool enableDebug) {
|
2026-03-16 16:07:12 +08:00
|
|
|
if (m_initialized) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
|
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
|
|
|
|
|
|
m_window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_ownsWindow = true;
|
|
|
|
|
return InitializeWithExistingWindow(m_window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::InitializeWithExistingWindow(GLFWwindow* window) {
|
|
|
|
|
if (m_initialized) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_window = window;
|
|
|
|
|
if (!m_window) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
glfwMakeContextCurrent(m_window);
|
|
|
|
|
|
|
|
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_deviceInfo.vendor = reinterpret_cast<const char*>(glGetString(GL_VENDOR));
|
|
|
|
|
m_deviceInfo.renderer = reinterpret_cast<const char*>(glGetString(GL_RENDERER));
|
|
|
|
|
m_deviceInfo.version = reinterpret_cast<const char*>(glGetString(GL_VERSION));
|
|
|
|
|
|
|
|
|
|
glGetIntegerv(GL_MAJOR_VERSION, &m_deviceInfo.majorVersion);
|
|
|
|
|
glGetIntegerv(GL_MINOR_VERSION, &m_deviceInfo.minorVersion);
|
|
|
|
|
|
|
|
|
|
m_initialized = true;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::Shutdown() {
|
|
|
|
|
if (m_ownsWindow && m_window) {
|
|
|
|
|
glfwDestroyWindow(m_window);
|
|
|
|
|
}
|
|
|
|
|
m_window = nullptr;
|
|
|
|
|
m_initialized = false;
|
|
|
|
|
m_ownsWindow = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::SwapBuffers() {
|
|
|
|
|
if (m_window) {
|
|
|
|
|
glfwSwapBuffers(m_window);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::PollEvents() {
|
|
|
|
|
glfwPollEvents();
|
|
|
|
|
return !glfwWindowShouldClose(m_window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OpenGLDevice::SetShouldClose(bool shouldClose) {
|
|
|
|
|
if (m_window) {
|
|
|
|
|
glfwSetWindowShouldClose(m_window, shouldClose ? GLFW_TRUE : GLFW_FALSE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool OpenGLDevice::ShouldClose() const {
|
|
|
|
|
return m_window && glfwWindowShouldClose(m_window) == GLFW_TRUE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace RHI
|
|
|
|
|
} // namespace XCEngine
|