From: Mikko Rasa Date: Thu, 15 Sep 2016 21:09:13 +0000 (+0300) Subject: Add a new View class to tie up some presentation tasks X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=commitdiff_plain;h=9759cae2abf138acc548e3f230967e2c843e967e Add a new View class to tie up some presentation tasks --- diff --git a/source/view.cpp b/source/view.cpp new file mode 100644 index 00000000..47411998 --- /dev/null +++ b/source/view.cpp @@ -0,0 +1,48 @@ +#include "camera.h" +#include "framebuffer.h" +#include "renderable.h" +#include "view.h" + +using namespace std; + +namespace Msp { +namespace GL { + +View::View(Graphics::Window &w, Graphics::GLContext &c): + window(w), + context(c), + target(Framebuffer::system()), + content(0) +{ + window.signal_resize.connect(sigc::mem_fun(this, &View::window_resized)); +} + +void View::set_content(const Renderable *r) +{ + content = r; +} + +void View::synchronize_camera_aspect(Camera &c) +{ + synced_cameras.push_back(&c); + c.set_aspect(static_cast(window.get_width())/window.get_height()); +} + +void View::render() +{ + target.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT); + if(content) + content->render(); + context.swap_buffers(); +} + +void View::window_resized(unsigned w, unsigned h) +{ + target.viewport(0, 0, w, h); + float aspect = static_cast(w)/h; + for(list::iterator i=synced_cameras.begin(); i!=synced_cameras.end(); ++i) + (*i)->set_aspect(aspect); +} + +} // namespace GL +} // namespace Msp diff --git a/source/view.h b/source/view.h new file mode 100644 index 00000000..93eb49d7 --- /dev/null +++ b/source/view.h @@ -0,0 +1,42 @@ +#ifndef MSP_GL_VIEW_H_ +#define MSP_GL_VIEW_H_ + +#include +#include +#include + +namespace Msp { +namespace GL { + +class Camera; +class Framebuffer; +class Renderable; + +/** +Manages the presentation of rendering results on the screen. +*/ +class View +{ +private: + Graphics::Window &window; + Graphics::GLContext &context; + Framebuffer ⌖ + const Renderable *content; + std::list synced_cameras; + +public: + View(Graphics::Window &, Graphics::GLContext &); + + void set_content(const Renderable *); + void synchronize_camera_aspect(Camera &); + + void render(); + +private: + void window_resized(unsigned, unsigned); +}; + +} // namespace GL +} // namespace Msp + +#endif