Files
XCEngine/engine/src/RHI/OpenGL/OpenGLSwapChain.cpp

150 lines
3.2 KiB
C++
Raw Normal View History

2026-03-16 18:41:05 +08:00
#define GLFW_INCLUDE_NONE
#include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h"
#include <GLFW/glfw3.h>
#include <glad/glad.h>
2026-03-16 18:41:05 +08:00
namespace XCEngine {
namespace RHI {
static int ToGLFWPresentMode(PresentMode mode) {
switch (mode) {
case PresentMode::Immediate: return 0;
case PresentMode::VSync: return 1;
case PresentMode::Mailbox: return -1;
case PresentMode::Fifo: return 1;
default: return 1;
}
}
OpenGLSwapChain::OpenGLSwapChain()
: m_window(nullptr)
, m_width(0)
, m_height(0)
, m_framebufferWidth(0)
, m_framebufferHeight(0)
, m_vsync(true)
, m_presentMode(PresentMode::VSync) {
2026-03-16 18:41:05 +08:00
}
OpenGLSwapChain::~OpenGLSwapChain() {
Shutdown();
2026-03-16 18:41:05 +08:00
}
bool OpenGLSwapChain::Initialize(GLFWwindow* window, bool vsync) {
2026-03-16 18:41:05 +08:00
m_window = window;
m_vsync = vsync;
m_presentMode = vsync ? PresentMode::VSync : PresentMode::Immediate;
int w, h;
glfwGetFramebufferSize(window, &w, &h);
m_framebufferWidth = w;
m_framebufferHeight = h;
glfwGetWindowSize(window, &w, &h);
m_width = w;
m_height = h;
glfwSwapInterval(m_vsync ? 1 : 0);
return true;
}
bool OpenGLSwapChain::Initialize(GLFWwindow* window, int width, int height, PresentMode mode) {
m_window = window;
m_width = width;
m_height = height;
m_presentMode = mode;
m_vsync = (mode == PresentMode::VSync || mode == PresentMode::Fifo);
glfwSetWindowSize(window, width, height);
int w, h;
glfwGetFramebufferSize(window, &w, &h);
m_framebufferWidth = w;
m_framebufferHeight = h;
glfwSwapInterval(m_vsync ? 1 : 0);
2026-03-16 18:41:05 +08:00
return true;
}
void OpenGLSwapChain::Shutdown() {
m_window = nullptr;
}
void OpenGLSwapChain::Present() {
if (m_window) {
glfwSwapBuffers(m_window);
}
2026-03-16 18:41:05 +08:00
}
void OpenGLSwapChain::SwapBuffers() {
if (m_window) {
glfwSwapBuffers(m_window);
}
}
void OpenGLSwapChain::Resize(int width, int height) {
m_width = width;
m_height = height;
if (m_window) {
glfwSetWindowSize(m_window, width, height);
}
}
void OpenGLSwapChain::SetVSync(bool enabled) {
m_vsync = enabled;
glfwSwapInterval(enabled ? 1 : 0);
}
void OpenGLSwapChain::SetFramebufferSize(int width, int height) {
m_framebufferWidth = width;
m_framebufferHeight = height;
}
bool OpenGLSwapChain::ShouldClose() const {
return m_window && glfwWindowShouldClose(m_window);
}
void OpenGLSwapChain::SetShouldClose(bool shouldClose) {
if (m_window) {
glfwSetWindowShouldClose(m_window, shouldClose ? GLFW_TRUE : GLFW_FALSE);
}
}
void OpenGLSwapChain::PollEvents() {
glfwPollEvents();
2026-03-16 18:41:05 +08:00
}
void OpenGLSwapChain::Present(uint32_t syncInterval, uint32_t flags) {
if (m_window) {
glfwSwapBuffers(m_window);
}
}
void OpenGLSwapChain::Resize(uint32_t width, uint32_t height) {
Resize(static_cast<int>(width), static_cast<int>(height));
}
void OpenGLSwapChain::SetFullscreen(bool fullscreen) {
}
bool OpenGLSwapChain::IsFullscreen() const {
return false;
}
uint32_t OpenGLSwapChain::GetCurrentBackBufferIndex() const {
return 0;
}
RHITexture* OpenGLSwapChain::GetCurrentBackBuffer() {
return nullptr;
}
void* OpenGLSwapChain::GetNativeHandle() {
return nullptr;
}
2026-03-16 18:41:05 +08:00
} // namespace RHI
} // namespace XCEngine