Onyx-Engine/src/core/systems/WindowManager.cpp

124 lines
3.0 KiB
C++
Raw Normal View History

2025-05-18 17:42:48 +00:00
#include <GL/glew.h>
#include "WindowManager.h"
#include "../Systems/Logger.h"
#include <GLFW/glfw3.h>
#include "Profiler.h"
2025-05-18 17:42:48 +00:00
namespace OX
{
bool WindowManager::Init(const std::string &title, int width, int height)
{
m_width = width;
m_height = height;
2025-05-18 17:42:48 +00:00
if (!glfwInit()) {
Logger::LogError("Failed to initialize GLFW");
return false;
}
2025-05-18 17:42:48 +00:00
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
2025-05-18 17:42:48 +00:00
//glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // REM title bar
2025-05-18 17:42:48 +00:00
m_window = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if (!m_window) {
Logger::LogError("Failed to create GLFW window");
glfwTerminate();
return false;
}
2025-05-18 17:42:48 +00:00
glfwMakeContextCurrent(m_window);
glfwSwapInterval(1); // Enable vsync
2025-05-18 17:42:48 +00:00
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
Logger::LogError("Failed to initialize GLEW: %s", reinterpret_cast<const char *>(glewGetErrorString(err)));
return false;
}
2025-05-18 17:42:48 +00:00
glfwSetFramebufferSizeCallback(m_window, FramebufferSizeCallback);
glfwSetWindowUserPointer(m_window, this);
2025-05-18 17:42:48 +00:00
glEnable(GL_DEPTH_TEST);
2025-05-18 17:42:48 +00:00
return true;
}
2025-05-18 17:42:48 +00:00
void WindowManager::SetWindowTitle(const std::string &title) const
{
if (m_window) {
glfwSetWindowTitle(m_window, title.c_str());
}
else {
Logger::LogWarning("Failed to set Window Title: 'Not Initialized'");
}
2025-05-18 17:42:48 +00:00
}
void WindowManager::FramebufferSizeCallback(GLFWwindow *window, int width, int height)
{
OX_PROFILE_FUNCTION();
2025-05-18 17:42:48 +00:00
auto *wm = static_cast<WindowManager *>(glfwGetWindowUserPointer(window));
if (wm) {
wm->SetSize(width, height);
}
}
2025-05-18 17:42:48 +00:00
void WindowManager::SetSize(int width, int height)
{
OX_PROFILE_FUNCTION();
2025-05-18 17:42:48 +00:00
m_width = width;
m_height = height;
glViewport(0, 0, width, height);
}
2025-05-18 17:42:48 +00:00
void WindowManager::PollEvents() const
{
OX_PROFILE_FUNCTION();
2025-05-18 17:42:48 +00:00
glfwPollEvents();
}
bool WindowManager::ShouldClose() const
{
return glfwWindowShouldClose(m_window);
}
2025-05-18 17:42:48 +00:00
void WindowManager::BeginFrame() const
{
OX_PROFILE_FUNCTION();
PollEvents();
glViewport(0, 0, m_width, m_height);
glClearColor(0.07f, 0.07f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
2025-05-18 18:29:55 +00:00
void WindowManager::EndFrame() const
{
OX_PROFILE_FUNCTION();
2025-05-18 17:42:48 +00:00
glfwSwapBuffers(m_window);
}
2025-05-18 17:42:48 +00:00
void WindowManager::Shutdown()
{
if (m_window) {
glfwDestroyWindow(m_window);
m_window = nullptr;
}
glfwTerminate();
2025-05-18 17:42:48 +00:00
}
WindowManager::~WindowManager()
{
Shutdown();
}
2025-05-18 17:42:48 +00:00
}