From e07b25a160c5d3231282df948017460ac581b4d7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 19 Dec 2017 11:21:55 +0200 Subject: [PATCH] Split the Window-specific parts out of View This should allow other types of views, such as VR::StereoView, to derive from GL::View and present a uniform API. --- source/view.cpp | 27 ++++----------------------- source/view.h | 24 +++++++----------------- source/windowview.cpp | 34 ++++++++++++++++++++++++++++++++++ source/windowview.h | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 40 deletions(-) create mode 100644 source/windowview.cpp create mode 100644 source/windowview.h diff --git a/source/view.cpp b/source/view.cpp index b4dcca99..f531905f 100644 --- a/source/view.cpp +++ b/source/view.cpp @@ -1,30 +1,22 @@ #include "camera.h" -#include "framebuffer.h" #include "renderable.h" #include "renderer.h" #include "view.h" -using namespace std; - namespace Msp { namespace GL { -View::View(Graphics::Window &w, Graphics::GLContext &c): - window(w), - context(c), +View::View(): target(Framebuffer::system()), camera(0), content(0) -{ - window.signal_resize.connect(sigc::mem_fun(this, &View::window_resized)); - window_resized(window.get_width(), window.get_height()); -} +{ } void View::set_camera(Camera *c) { camera = c; if(camera) - camera->set_aspect(static_cast(window.get_width())/window.get_height()); + camera->set_aspect(get_aspect()); } void View::set_content(Renderable *r) @@ -35,7 +27,7 @@ void View::set_content(Renderable *r) void View::synchronize_camera_aspect(Camera &c) { synced_cameras.push_back(&c); - c.set_aspect(static_cast(window.get_width())/window.get_height()); + c.set_aspect(get_aspect()); } void View::render() @@ -48,17 +40,6 @@ void View::render() content->render(renderer); content->finish_frame(); } - context.swap_buffers(); -} - -void View::window_resized(unsigned w, unsigned h) -{ - target.viewport(0, 0, w, h); - float aspect = static_cast(w)/h; - if(camera) - camera->set_aspect(aspect); - for(list::iterator i=synced_cameras.begin(); i!=synced_cameras.end(); ++i) - (*i)->set_aspect(aspect); } } // namespace GL diff --git a/source/view.h b/source/view.h index f7f47fc7..08629fa0 100644 --- a/source/view.h +++ b/source/view.h @@ -2,15 +2,12 @@ #define MSP_GL_VIEW_H_ #include -#include -#include -#include +#include "framebuffer.h" namespace Msp { namespace GL { class Camera; -class Framebuffer; class Renderable; /** @@ -18,21 +15,17 @@ Manages the presentation of rendering results on the screen. */ class View: public sigc::trackable { -private: - Graphics::Window &window; - Graphics::GLContext &context; +protected: Framebuffer ⌖ Camera *camera; Renderable *content; std::list synced_cameras; -public: - View(Graphics::Window &, Graphics::GLContext &); + View(); - Graphics::Window &get_window() { return window; } - Graphics::GLContext &get_context() { return context; } - unsigned get_width() const { return window.get_width(); } - unsigned get_height() const { return window.get_height(); } +public: + virtual unsigned get_width() const { return target.get_width(); } + virtual unsigned get_height() const { return target.get_height(); } float get_aspect() const { return static_cast(get_width())/get_height(); } void set_camera(Camera *); @@ -41,10 +34,7 @@ public: // Deprecated void synchronize_camera_aspect(Camera &); - void render(); - -private: - void window_resized(unsigned, unsigned); + virtual void render(); }; } // namespace GL diff --git a/source/windowview.cpp b/source/windowview.cpp new file mode 100644 index 00000000..61027acc --- /dev/null +++ b/source/windowview.cpp @@ -0,0 +1,34 @@ +#include "camera.h" +#include "windowview.h" + +using namespace std; + +namespace Msp { +namespace GL { + +WindowView::WindowView(Graphics::Window &w, Graphics::GLContext &c): + window(w), + context(c) +{ + window.signal_resize.connect(sigc::mem_fun(this, &WindowView::window_resized)); + window_resized(window.get_width(), window.get_height()); +} + +void WindowView::render() +{ + View::render(); + context.swap_buffers(); +} + +void WindowView::window_resized(unsigned w, unsigned h) +{ + target.viewport(0, 0, w, h); + float aspect = static_cast(w)/h; + if(camera) + camera->set_aspect(aspect); + for(list::iterator i=synced_cameras.begin(); i!=synced_cameras.end(); ++i) + (*i)->set_aspect(aspect); +} + +} // namespace GL +} // namespace Msp diff --git a/source/windowview.h b/source/windowview.h new file mode 100644 index 00000000..825601f1 --- /dev/null +++ b/source/windowview.h @@ -0,0 +1,35 @@ +#ifndef MSP_GL_WINDOWVIEW_H_ +#define MSP_GL_WINDOWVIEW_H_ + +#include +#include +#include +#include "view.h" + +namespace Msp { +namespace GL { + +class WindowView: public View +{ +private: + Graphics::Window &window; + Graphics::GLContext &context; + +public: + WindowView(Graphics::Window &, Graphics::GLContext &); + + Graphics::Window &get_window() { return window; } + Graphics::GLContext &get_context() { return context; } + unsigned get_width() const { return window.get_width(); } + unsigned get_height() const { return window.get_height(); } + + virtual void render(); + +private: + void window_resized(unsigned, unsigned); +}; + +} // namespace GL +} // namespace Msp + +#endif -- 2.45.2