1
0
Fork 0

Spectre/Window/Window: refactor out GLContext specific code to its own class (GLWindow)

This commit is contained in:
Henrik Hautakoski 2023-08-23 19:28:24 +02:00
parent 2de3bd93f7
commit b1ccea1397
7 changed files with 120 additions and 55 deletions

View file

@ -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

View file

@ -3,7 +3,7 @@
#define GRAPHICS_H
#include <Spectre/GfxDriver/GfxDriver.h>
#include <Spectre/Window/Window.h>
#include <Spectre/Window/GLWindow.h>
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;

View file

@ -0,0 +1,41 @@
#ifndef SPECTRE_WINDOW_GLWINDOW_H
#define SPECTRE_WINDOW_GLWINDOW_H
#include <Spectre/Window/Window.h>
#include <Spectre/Window/GLContext.h>
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 */

View file

@ -5,7 +5,6 @@
#include "DisplayMode.h"
#include "WindowDescription.h"
#include <Spectre/Math/Vector2.h>
#include <Spectre/Window/GLContext.h>
#include <cstdint>
#include <string>
@ -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

View file

@ -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;
}

View file

@ -0,0 +1,70 @@
#include <Spectre/Window/GLContext.h>
#include <Spectre/Window/GLWindow.h>
#include <Graphics/GL/gl.h>
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

View file

@ -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