From: Mikko Rasa Date: Tue, 9 Nov 2021 10:37:15 +0000 (+0200) Subject: Create a Device class to hold the graphics context X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=f0414f06fc2463e9765c9492dce60e0468dceb3c;p=libs%2Fgl.git Create a Device class to hold the graphics context This is necessary to make the selected backend transparent to applications, and also because Vulkan needs access to the VkDevice. --- diff --git a/demos/desertpillars/source/desertpillars.cpp b/demos/desertpillars/source/desertpillars.cpp index 53f0a86c..532a9499 100644 --- a/demos/desertpillars/source/desertpillars.cpp +++ b/demos/desertpillars/source/desertpillars.cpp @@ -23,10 +23,10 @@ DesertPillars::Options::Options() DesertPillars::DesertPillars(int, char **): window(display, opts.wnd_opts), - gl_ctx(window, opts.gl_opts), + gl_device(window, opts.gl_opts), keyboard(window), resources(&res_mgr), - view(window, gl_ctx), + view(window), camera(resources.get("Camera.camera")), lighting(resources.get("Desert.lightn")), sphere(resources.get("Sphere.object")), diff --git a/demos/desertpillars/source/desertpillars.h b/demos/desertpillars/source/desertpillars.h index 4e478099..352b8c2c 100644 --- a/demos/desertpillars/source/desertpillars.h +++ b/demos/desertpillars/source/desertpillars.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -16,7 +17,6 @@ #include #include #include -#include #include #include #include @@ -69,7 +69,7 @@ private: Msp::Graphics::Display display; Options opts; Msp::Graphics::Window window; - Msp::Graphics::GLContext gl_ctx; + Msp::GL::Device gl_device; Msp::Input::Keyboard keyboard; Msp::GL::ResourceManager res_mgr; Resources resources; diff --git a/source/backends/opengl/device_backend.cpp b/source/backends/opengl/device_backend.cpp new file mode 100644 index 00000000..5429c0c1 --- /dev/null +++ b/source/backends/opengl/device_backend.cpp @@ -0,0 +1,11 @@ +#include "device_backend.h" + +namespace Msp { +namespace GL { + +OpenGLDevice::OpenGLDevice(Graphics::Window &wnd, const Graphics::GLOptions &opts): + context(wnd, opts) +{ } + +} // namespace GL +} // namespace Msp diff --git a/source/backends/opengl/device_backend.h b/source/backends/opengl/device_backend.h new file mode 100644 index 00000000..e64352d3 --- /dev/null +++ b/source/backends/opengl/device_backend.h @@ -0,0 +1,26 @@ +#ifndef MSP_GL_DEVICE_BACKEND_H_ +#define MSP_GL_DEVICE_BACKEND_H_ + +#include +#include + +namespace Msp { +namespace GL { + +class OpenGLDevice: public NonCopyable +{ +protected: + Graphics::GLContext context; + + OpenGLDevice(Graphics::Window &, const Graphics::GLOptions &); + + Graphics::GLContext &get_context() { return context; } +}; + +using DeviceBackend = OpenGLDevice; +using DeviceOptions = Graphics::GLOptions; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/core/device.cpp b/source/core/device.cpp new file mode 100644 index 00000000..9a24a1f2 --- /dev/null +++ b/source/core/device.cpp @@ -0,0 +1,29 @@ +#include "device.h" +#include "error.h" + +namespace Msp { +namespace GL { + +Device *Device::current = 0; + +Device::Device(Graphics::Window &w, const DeviceOptions &o): + DeviceBackend(w, o) +{ + current = this; +} + +Device::~Device() +{ + if(this==current) + current = 0; +} + +Device &Device::get_current() +{ + if(!current) + throw invalid_operation("no current device"); + return *current; +} + +} // namespace GL +} // namespace Msp diff --git a/source/core/device.h b/source/core/device.h new file mode 100644 index 00000000..6a6a7608 --- /dev/null +++ b/source/core/device.h @@ -0,0 +1,32 @@ +#ifndef MSP_GL_DEVICE_H_ +#define MSP_GL_DEVICE_H_ + +#include +#include "device_backend.h" + +namespace Msp { +namespace GL { + +/** +Represents a graphics device. An instance must be created to use the library. +*/ +class Device: public DeviceBackend +{ + friend DeviceBackend; + +private: + static Device *current; + +public: + Device(Graphics::Window &, const DeviceOptions & = DeviceOptions()); + ~Device(); + + using DeviceBackend::get_context; + + static Device &get_current(); +}; + +} // namespace GL +} // namespace Msp + +#endif diff --git a/source/render/windowview.cpp b/source/render/windowview.cpp index b32a0b67..e139436b 100644 --- a/source/render/windowview.cpp +++ b/source/render/windowview.cpp @@ -6,10 +6,10 @@ using namespace std; namespace Msp { namespace GL { -WindowView::WindowView(Graphics::Window &w, Graphics::GLContext &c): +WindowView::WindowView(Graphics::Window &w): View(Framebuffer::system()), window(w), - context(c) + device(Device::get_current()) { window.signal_resize.connect(sigc::mem_fun(this, &WindowView::window_resized)); window_resized(window.get_width(), window.get_height()); @@ -18,7 +18,7 @@ WindowView::WindowView(Graphics::Window &w, Graphics::GLContext &c): void WindowView::render(Renderer &renderer) { View::render(renderer); - context.swap_buffers(); + device.get_context().swap_buffers(); } void WindowView::window_resized(unsigned w, unsigned h) diff --git a/source/render/windowview.h b/source/render/windowview.h index 9646a65d..028e9805 100644 --- a/source/render/windowview.h +++ b/source/render/windowview.h @@ -2,8 +2,8 @@ #define MSP_GL_WINDOWVIEW_H_ #include -#include #include +#include "device.h" #include "view.h" namespace Msp { @@ -21,13 +21,12 @@ class WindowView: public View, public sigc::trackable { private: Graphics::Window &window; - Graphics::GLContext &context; + Device &device; public: - WindowView(Graphics::Window &, Graphics::GLContext &); + WindowView(Graphics::Window &); Graphics::Window &get_window() { return window; } - Graphics::GLContext &get_context() { return context; } virtual unsigned get_width() const { return window.get_width(); } virtual unsigned get_height() const { return window.get_height(); } diff --git a/tools/viewer.cpp b/tools/viewer.cpp index fde5b8d4..b6452a3c 100644 --- a/tools/viewer.cpp +++ b/tools/viewer.cpp @@ -5,12 +5,14 @@ #include #include #include -#include +#include +#include #include #include #include #include #include +#include #include #include #include @@ -63,7 +65,7 @@ private: Options opts; Graphics::Display display; Graphics::Window window; - Graphics::GLContext gl_ctx; + GL::Device gl_device; Input::Mouse mouse; Resources resources; GL::WindowView view; @@ -132,9 +134,9 @@ Viewer::Options::Options(int argc, char **argv) Viewer::Viewer(int argc, char **argv): opts(argc, argv), window(display, opts.wnd_opts), - gl_ctx(window, opts.gl_opts), + gl_device(window, opts.gl_opts), mouse(window), - view(window, gl_ctx), + view(window), sequence(0), renderable(0), anim_object(0),