feat: window class enhancements

This commit is contained in:
2025-10-05 13:11:45 +02:00
parent 7f08e28a04
commit 9d56515fe5
2 changed files with 49 additions and 24 deletions

View File

@ -14,7 +14,7 @@
class Window : public EventBus {
private:
SDL_Window *m_window;
SDL_Window *m_handle;
SDL_GLContext m_context;
int m_width;
@ -23,7 +23,14 @@ private:
bool m_is_open;
public:
Window();
Window(const char* title, int width, int height);
~Window();
Window(Window&& window) noexcept;
Window& operator=(Window&& window) noexcept;
Window(const Window& window) noexcept = delete;
Window& operator=(const Window& window) noexcept = delete;
public:
[[nodiscard]] inline int GetWidth() const { return m_width; }
[[nodiscard]] inline int GetHeight() const { return m_height; }

View File

@ -8,39 +8,33 @@
#include "renderer/debug.h"
Window::Window() {
Window::Window(const char* title, int width, int height) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
std::cout << "Setting gl context version " << ENGINE_GL_MAJOR_VERSION << "." << ENGINE_GL_MINOR_VERSION << std::endl;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, ENGINE_GL_MAJOR_VERSION);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, ENGINE_GL_MINOR_VERSION);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
std::cout << "Setting gl context multisample " << ENGINE_GL_MULTISAMPLE_BUFFERS
<< "buffers " << ENGINE_GL_MULTISAMPLE_SAMPLES << " samples" << std::endl;
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, ENGINE_GL_MULTISAMPLE_BUFFERS);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, ENGINE_GL_MULTISAMPLE_SAMPLES);
m_width = DEFAULT_WIDTH;
m_height = DEFAULT_HEIGHT;
m_width = width;
m_height = height;
std::cout << "Width: " << m_width << std::endl;
std::cout << "Height: " << m_height << std::endl;
m_handle = SDL_CreateWindow(title, m_width, m_height, SDL_WINDOW_OPENGL|SDL_WINDOW_ALWAYS_ON_TOP|SDL_WINDOW_RESIZABLE);
m_window = SDL_CreateWindow("OpenGL Test", m_width, m_height, SDL_WINDOW_OPENGL|SDL_WINDOW_ALWAYS_ON_TOP|SDL_WINDOW_RESIZABLE);
if (!m_window) {
if (!m_handle) {
std::cerr << "Failed to create window" << std::endl;
std::exit(1);
}
SDL_SetWindowRelativeMouseMode(m_window, true);
SDL_SetWindowRelativeMouseMode(m_handle, true);
m_context = SDL_GL_CreateContext(m_window);
m_context = SDL_GL_CreateContext(m_handle);
if (!SDL_GL_MakeCurrent(m_window, m_context)) {
if (!SDL_GL_MakeCurrent(m_handle, m_context)) {
std::cerr << "SDL_GL_MakeCurrent failed: " << SDL_GetError() << "\n";
SDL_DestroyWindow(m_window);
SDL_DestroyWindow(m_handle);
std::exit(1);
}
@ -48,7 +42,7 @@ Window::Window() {
if (GLEW_OK != glewInit()) {
std::cerr << "Could not initialize GLEW!" << std::endl;
SDL_GL_DestroyContext(m_context);
SDL_DestroyWindow(m_window);
SDL_DestroyWindow(m_handle);
std::exit(1);
}
@ -65,25 +59,49 @@ Window::Window() {
m_is_open = true;
}
Window::Window() : Window("OpenGL Test", DEFAULT_WIDTH, DEFAULT_HEIGHT) {}
Window::Window(Window&& window) noexcept
: m_handle(window.m_handle), m_context(window.m_context), m_width(window.m_width), m_height(window.m_height), m_is_open(window.m_is_open)
{
window.m_handle = nullptr;
window.m_context = (SDL_GLContext)nullptr;
window.m_width = 0;
window.m_height = 0;
window.m_is_open = false;
}
Window& Window::operator=(Window&& window) noexcept
{
if (this == &window) return *this;
// Destroy();
this->m_handle = window.m_handle;
this->m_context = window.m_context;
this->m_width = window.m_width;
this->m_height = window.m_height;
this->m_is_open = window.m_is_open;
return *this;
}
void Window::ProcessEvents() {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
case SDL_EVENT_QUIT:
std::cout << "Close requested" << std::endl;
publish(WindowCloseRequested());
break;
case SDL_EVENT_KEY_DOWN:
if (event.key.scancode == SDL_SCANCODE_ESCAPE) {
std::cout << "Close requested" << std::endl;
publish(WindowCloseRequested());
}
break;
case SDL_EVENT_WINDOW_RESIZED:
int width, height;
if (SDL_GetWindowSizeInPixels(m_window, &width, &height)) {
std::cout << "Window resized: " << width << ", " << height << std::endl;
if (SDL_GetWindowSizeInPixels(m_handle, &width, &height)) {
m_width = width;
m_height = height;
glViewport(
@ -100,7 +118,7 @@ void Window::ProcessEvents() {
}
void Window::SwapBuffers() const {
SDL_GL_SwapWindow(m_window);
SDL_GL_SwapWindow(m_handle);
}
Window::~Window() {
@ -110,7 +128,7 @@ Window::~Window() {
void Window::Destroy() const {
if (m_context)
SDL_GL_DestroyContext(m_context);
if (m_window)
SDL_DestroyWindow(m_window);
if (m_handle)
SDL_DestroyWindow(m_handle);
}