Spectre/Window/Window: refactor out GLContext specific code to its own class (GLWindow)
This commit is contained in:
parent
2de3bd93f7
commit
b1ccea1397
7 changed files with 120 additions and 55 deletions
|
|
@ -44,6 +44,7 @@ set( ENGINE_SRC
|
||||||
source/Window/Window.cpp
|
source/Window/Window.cpp
|
||||||
source/Window/WindowDescription.cpp
|
source/Window/WindowDescription.cpp
|
||||||
source/Window/DisplayMode.cpp
|
source/Window/DisplayMode.cpp
|
||||||
|
source/Window/GLWindow.cpp
|
||||||
source/Window/GLContext.cpp
|
source/Window/GLContext.cpp
|
||||||
|
|
||||||
# GfxDriver
|
# GfxDriver
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@
|
||||||
#define GRAPHICS_H
|
#define GRAPHICS_H
|
||||||
|
|
||||||
#include <Spectre/GfxDriver/GfxDriver.h>
|
#include <Spectre/GfxDriver/GfxDriver.h>
|
||||||
#include <Spectre/Window/Window.h>
|
#include <Spectre/Window/GLWindow.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
|
|
@ -45,14 +45,14 @@ public :
|
||||||
|
|
||||||
GfxDriver* getDriver();
|
GfxDriver* getDriver();
|
||||||
|
|
||||||
Window* getWindow();
|
GLWindow* getWindow();
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
|
|
||||||
int m_width;
|
int m_width;
|
||||||
int m_height;
|
int m_height;
|
||||||
|
|
||||||
Window *m_window;
|
GLWindow *m_window;
|
||||||
|
|
||||||
// Graphics Driver. OpenGL/Vulcan/DirectX etc.
|
// Graphics Driver. OpenGL/Vulcan/DirectX etc.
|
||||||
GfxDriver *m_gfxdrv;
|
GfxDriver *m_gfxdrv;
|
||||||
|
|
|
||||||
41
include/Spectre/Window/GLWindow.h
Normal file
41
include/Spectre/Window/GLWindow.h
Normal 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 */
|
||||||
|
|
@ -5,7 +5,6 @@
|
||||||
#include "DisplayMode.h"
|
#include "DisplayMode.h"
|
||||||
#include "WindowDescription.h"
|
#include "WindowDescription.h"
|
||||||
#include <Spectre/Math/Vector2.h>
|
#include <Spectre/Math/Vector2.h>
|
||||||
#include <Spectre/Window/GLContext.h>
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
|
@ -28,9 +27,9 @@ public :
|
||||||
Window();
|
Window();
|
||||||
virtual ~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);
|
void setSize(unsigned int width, unsigned int height);
|
||||||
|
|
||||||
|
|
@ -58,18 +57,11 @@ public :
|
||||||
|
|
||||||
void grabCursor(bool value);
|
void grabCursor(bool value);
|
||||||
|
|
||||||
bool activate(bool value);
|
|
||||||
|
|
||||||
// Enable/Disable Vertical Sync.
|
|
||||||
bool enableVSync(bool value);
|
|
||||||
|
|
||||||
void swapBuffers();
|
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
void onReshape(int width, int height);
|
virtual void onReshape(int width, int height);
|
||||||
|
|
||||||
protected :
|
protected :
|
||||||
enum Mode m_fmode;
|
enum Mode m_fmode;
|
||||||
|
|
@ -84,8 +76,6 @@ protected :
|
||||||
std::string m_caption;
|
std::string m_caption;
|
||||||
|
|
||||||
PlatformWindow* m_impl;
|
PlatformWindow* m_impl;
|
||||||
|
|
||||||
GLContext* m_context;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namepsace sp
|
} // namepsace sp
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ Graphics::Graphics(PlatformApplication *platform)
|
||||||
m_width = 800;
|
m_width = 800;
|
||||||
m_height = 600;
|
m_height = 600;
|
||||||
|
|
||||||
m_window = new Window();
|
m_window = new GLWindow();
|
||||||
|
|
||||||
// Only have OpenGL atm.
|
// Only have OpenGL atm.
|
||||||
m_gfxdrv = new OpenGLDrv();
|
m_gfxdrv = new OpenGLDrv();
|
||||||
|
|
@ -84,7 +84,7 @@ GfxDriver* Graphics::getDriver()
|
||||||
return m_gfxdrv;
|
return m_gfxdrv;
|
||||||
}
|
}
|
||||||
|
|
||||||
Window* Graphics::getWindow()
|
GLWindow* Graphics::getWindow()
|
||||||
{
|
{
|
||||||
return m_window;
|
return m_window;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
70
source/Window/GLWindow.cpp
Normal file
70
source/Window/GLWindow.cpp
Normal 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
|
||||||
|
|
@ -21,13 +21,11 @@ Window::Window()
|
||||||
m_caption = CAPTION_DEFAULT;
|
m_caption = CAPTION_DEFAULT;
|
||||||
m_fmode = WINDOWED;
|
m_fmode = WINDOWED;
|
||||||
|
|
||||||
m_context = GLContext::create();
|
|
||||||
m_impl = PlatformApplication::get()->createWindow(this);
|
m_impl = PlatformApplication::get()->createWindow(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Window::~Window()
|
Window::~Window()
|
||||||
{
|
{
|
||||||
delete m_context;
|
|
||||||
delete m_impl;
|
delete m_impl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,10 +37,6 @@ bool Window::create(WindowDescription description)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_context->create(m_impl)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
m_description = description;
|
m_description = description;
|
||||||
|
|
@ -56,16 +50,11 @@ void Window::init()
|
||||||
|
|
||||||
setIcon(ICON_DEFAULT);
|
setIcon(ICON_DEFAULT);
|
||||||
|
|
||||||
activate(true);
|
|
||||||
|
|
||||||
enableVSync(false);
|
|
||||||
showCursor(true);
|
showCursor(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::destroy()
|
void Window::destroy()
|
||||||
{
|
{
|
||||||
m_context->destroy();
|
|
||||||
|
|
||||||
m_impl->destroy();
|
m_impl->destroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -166,34 +155,8 @@ void Window::grabCursor(bool value)
|
||||||
m_impl->grabCursor(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)
|
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
|
} // namespace sp
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue