]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a new View class to tie up some presentation tasks
authorMikko Rasa <tdb@tdb.fi>
Thu, 15 Sep 2016 21:09:13 +0000 (00:09 +0300)
committerMikko Rasa <tdb@tdb.fi>
Thu, 15 Sep 2016 23:31:32 +0000 (02:31 +0300)
source/view.cpp [new file with mode: 0644]
source/view.h [new file with mode: 0644]

diff --git a/source/view.cpp b/source/view.cpp
new file mode 100644 (file)
index 0000000..4741199
--- /dev/null
@@ -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<float>(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<float>(w)/h;
+       for(list<Camera *>::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 (file)
index 0000000..93eb49d
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef MSP_GL_VIEW_H_
+#define MSP_GL_VIEW_H_
+
+#include <list>
+#include <msp/graphics/glcontext.h>
+#include <msp/graphics/window.h>
+
+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 &target;
+       const Renderable *content;
+       std::list<Camera *> 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