From b1ccea139707a6a4193c5a89b9873da726b3379b Mon Sep 17 00:00:00 2001 From: Henrik Hautakoski Date: Wed, 23 Aug 2023 19:28:24 +0200 Subject: [PATCH] Spectre/Window/Window: refactor out GLContext specific code to its own class (GLWindow) --- engine.cmake | 1 + include/Spectre/Graphics.h | 6 +-- include/Spectre/Window/GLWindow.h | 41 ++++++++++++++++++ include/Spectre/Window/Window.h | 16 ++----- source/Graphics/Graphics.cpp | 4 +- source/Window/GLWindow.cpp | 70 +++++++++++++++++++++++++++++++ source/Window/Window.cpp | 37 ---------------- 7 files changed, 120 insertions(+), 55 deletions(-) create mode 100644 include/Spectre/Window/GLWindow.h create mode 100644 source/Window/GLWindow.cpp diff --git a/engine.cmake b/engine.cmake index bf11d58..3758ad7 100644 --- a/engine.cmake +++ b/engine.cmake @@ -44,6 +44,7 @@ set( ENGINE_SRC source/Window/Window.cpp source/Window/WindowDescription.cpp source/Window/DisplayMode.cpp + source/Window/GLWindow.cpp source/Window/GLContext.cpp # GfxDriver diff --git a/include/Spectre/Graphics.h b/include/Spectre/Graphics.h index 0720285..18ea850 100644 --- a/include/Spectre/Graphics.h +++ b/include/Spectre/Graphics.h @@ -3,7 +3,7 @@ #define GRAPHICS_H #include -#include +#include namespace sp { @@ -45,14 +45,14 @@ public : GfxDriver* getDriver(); - Window* getWindow(); + GLWindow* getWindow(); protected : int m_width; int m_height; - Window *m_window; + GLWindow *m_window; // Graphics Driver. OpenGL/Vulcan/DirectX etc. GfxDriver *m_gfxdrv; diff --git a/include/Spectre/Window/GLWindow.h b/include/Spectre/Window/GLWindow.h new file mode 100644 index 0000000..26d9203 --- /dev/null +++ b/include/Spectre/Window/GLWindow.h @@ -0,0 +1,41 @@ + +#ifndef SPECTRE_WINDOW_GLWINDOW_H +#define SPECTRE_WINDOW_GLWINDOW_H + +#include +#include + +namespace sp { + +class GLContext; + +// GLWindow represents a window that has an OpenGL context attached to it. +class GLWindow : public Window +{ +public: + GLWindow(); + virtual ~GLWindow(); + + virtual bool create(WindowDescription decription); + + virtual void destroy(); + + bool activate(bool value); + + // Enable/Disable Vertical Sync. + bool enableVSync(bool value); + + void swapBuffers(); + +protected: + + virtual void onReshape(int width, int height); + +private: + + GLContext* m_context; +}; + +} // namepsace sp + +#endif /* SPECTRE_WINDOW_GLWINDOW_H */ diff --git a/include/Spectre/Window/Window.h b/include/Spectre/Window/Window.h index 41a7074..f26939f 100644 --- a/include/Spectre/Window/Window.h +++ b/include/Spectre/Window/Window.h @@ -5,7 +5,6 @@ #include "DisplayMode.h" #include "WindowDescription.h" #include -#include #include #include @@ -28,9 +27,9 @@ public : Window(); virtual ~Window(); - bool create(WindowDescription decription); + virtual bool create(WindowDescription decription); - void destroy(); + virtual void destroy(); void setSize(unsigned int width, unsigned int height); @@ -58,18 +57,11 @@ public : void grabCursor(bool value); - bool activate(bool value); - - // Enable/Disable Vertical Sync. - bool enableVSync(bool value); - - void swapBuffers(); - protected : void init(); - void onReshape(int width, int height); + virtual void onReshape(int width, int height); protected : enum Mode m_fmode; @@ -84,8 +76,6 @@ protected : std::string m_caption; PlatformWindow* m_impl; - - GLContext* m_context; }; } // namepsace sp diff --git a/source/Graphics/Graphics.cpp b/source/Graphics/Graphics.cpp index 63cfc4e..8972e4d 100644 --- a/source/Graphics/Graphics.cpp +++ b/source/Graphics/Graphics.cpp @@ -10,7 +10,7 @@ Graphics::Graphics(PlatformApplication *platform) m_width = 800; m_height = 600; - m_window = new Window(); + m_window = new GLWindow(); // Only have OpenGL atm. m_gfxdrv = new OpenGLDrv(); @@ -84,7 +84,7 @@ GfxDriver* Graphics::getDriver() return m_gfxdrv; } -Window* Graphics::getWindow() +GLWindow* Graphics::getWindow() { return m_window; } diff --git a/source/Window/GLWindow.cpp b/source/Window/GLWindow.cpp new file mode 100644 index 0000000..b299440 --- /dev/null +++ b/source/Window/GLWindow.cpp @@ -0,0 +1,70 @@ + +#include +#include +#include + +namespace sp { + +GLWindow::GLWindow() +{ + m_context = GLContext::create(); +} + +GLWindow::~GLWindow() +{ + delete m_context; +} + +bool GLWindow::create(WindowDescription description) +{ + if (!Window::create(description)) { + return false; + } + + if (!m_context->create(m_impl)) { + return false; + } + + enableVSync(false); + + return activate(true); +} + +void GLWindow::destroy() +{ + m_context->destroy(); + + Window::destroy(); +} + +bool GLWindow::activate(bool value) +{ + if (value) { + return m_context->activate(); + } + return m_context->deactivate(); +} + +bool GLWindow::enableVSync(bool value) +{ + return m_context->setSwapInterval(value ? 1 : 0); +} + +void GLWindow::swapBuffers() +{ + if (activate(true)) { + m_context->swapBuffers(); + } +} + +void GLWindow::onReshape(int width, int height) +{ + // TODO: This should even not be here. + // Generic Display should not have any GL calls. + // But it's better to have it here then in the platform specific GLContext + if (activate(true)) { + glViewport(0, 0, width, height); + } +} + +} // namespace \ No newline at end of file diff --git a/source/Window/Window.cpp b/source/Window/Window.cpp index 96d4e96..78bf657 100644 --- a/source/Window/Window.cpp +++ b/source/Window/Window.cpp @@ -21,13 +21,11 @@ Window::Window() m_caption = CAPTION_DEFAULT; m_fmode = WINDOWED; - m_context = GLContext::create(); m_impl = PlatformApplication::get()->createWindow(this); } Window::~Window() { - delete m_context; delete m_impl; } @@ -39,10 +37,6 @@ bool Window::create(WindowDescription description) return false; } - if (!m_context->create(m_impl)) { - return false; - } - init(); m_description = description; @@ -56,16 +50,11 @@ void Window::init() setIcon(ICON_DEFAULT); - activate(true); - - enableVSync(false); showCursor(true); } void Window::destroy() { - m_context->destroy(); - m_impl->destroy(); } @@ -166,34 +155,8 @@ void Window::grabCursor(bool value) m_impl->grabCursor(value); } -bool Window::activate(bool value) -{ - if (value) { - return m_context->activate(); - } - return m_context->deactivate(); -} - -bool Window::enableVSync(bool value) -{ - return m_context->setSwapInterval(value ? 1 : 0); -} - -void Window::swapBuffers() -{ - if (activate(true)) { - m_context->swapBuffers(); - } -} - void Window::onReshape(int width, int height) { - // TODO: This should even not be here. - // Generic Display should not have any GL calls. - // But it's better to have it here then in the platform specific GLContext - if (activate(true)) { - glViewport(0, 0, width, height); - } } } // namespace sp