]> git.tdb.fi Git - libs/gl.git/commitdiff
Create a Device class to hold the graphics context
authorMikko Rasa <tdb@tdb.fi>
Tue, 9 Nov 2021 10:37:15 +0000 (12:37 +0200)
committerMikko Rasa <tdb@tdb.fi>
Tue, 9 Nov 2021 10:37:15 +0000 (12:37 +0200)
This is necessary to make the selected backend transparent to
applications, and also because Vulkan needs access to the VkDevice.

demos/desertpillars/source/desertpillars.cpp
demos/desertpillars/source/desertpillars.h
source/backends/opengl/device_backend.cpp [new file with mode: 0644]
source/backends/opengl/device_backend.h [new file with mode: 0644]
source/core/device.cpp [new file with mode: 0644]
source/core/device.h [new file with mode: 0644]
source/render/windowview.cpp
source/render/windowview.h
tools/viewer.cpp

index 53f0a86c72cee3c957b66c0f174a10036b983eab..532a94990120d98f4eb3fc55edfca8eb899de5d6 100644 (file)
@@ -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<GL::Camera>("Camera.camera")),
        lighting(resources.get<GL::Lighting>("Desert.lightn")),
        sphere(resources.get<GL::Object>("Sphere.object")),
index 4e4780991275243ebcc5abdc94e69c833e55c7c2..352b8c2c0b2b09fdaa496e7191bcf6d1cefc80cb 100644 (file)
@@ -5,6 +5,7 @@
 #include <msp/core/application.h>
 #include <msp/datafile/directorysource.h>
 #include <msp/gl/camera.h>
+#include <msp/gl/device.h>
 #include <msp/gl/environmentmap.h>
 #include <msp/gl/objectinstance.h>
 #include <msp/gl/orderedscene.h>
@@ -16,7 +17,6 @@
 #include <msp/gl/technique.h>
 #include <msp/gl/windowview.h>
 #include <msp/graphics/display.h>
-#include <msp/graphics/glcontext.h>
 #include <msp/graphics/window.h>
 #include <msp/input/keyboard.h>
 #include <msp/time/timestamp.h>
@@ -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 (file)
index 0000000..5429c0c
--- /dev/null
@@ -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 (file)
index 0000000..e64352d
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef MSP_GL_DEVICE_BACKEND_H_
+#define MSP_GL_DEVICE_BACKEND_H_
+
+#include <msp/core/noncopyable.h>
+#include <msp/graphics/glcontext.h>
+
+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 (file)
index 0000000..9a24a1f
--- /dev/null
@@ -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 (file)
index 0000000..6a6a760
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef MSP_GL_DEVICE_H_
+#define MSP_GL_DEVICE_H_
+
+#include <msp/graphics/window.h>
+#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
index b32a0b67a68b826c4cbf9d635e161f59ac1be46b..e139436b4c571ba5e25af37c1243dacb31658751 100644 (file)
@@ -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)
index 9646a65d746ebcef6e7e5aa15db422d93dc5d602..028e9805436797c35adfddf3c75d9ac47b084138 100644 (file)
@@ -2,8 +2,8 @@
 #define MSP_GL_WINDOWVIEW_H_
 
 #include <sigc++/trackable.h>
-#include <msp/graphics/glcontext.h>
 #include <msp/graphics/window.h>
+#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(); }
 
index fde5b8d4e757d1defdf37d7149bf2d7776387769..b6452a3cf454f9bfd8a69d189dceb05973aace5e 100644 (file)
@@ -5,12 +5,14 @@
 #include <msp/datafile/packsource.h>
 #include <msp/fs/stat.h>
 #include <msp/fs/utils.h>
-#include <msp/graphics/simplewindow.h>
+#include <msp/graphics/display.h>
+#include <msp/graphics/window.h>
 #include <msp/gl/animatedobject.h>
 #include <msp/gl/animation.h>
 #include <msp/gl/animationplayer.h>
 #include <msp/gl/blend.h>
 #include <msp/gl/camera.h>
+#include <msp/gl/device.h>
 #include <msp/gl/directionallight.h>
 #include <msp/gl/framebuffer.h>
 #include <msp/gl/lighting.h>
@@ -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),