]> git.tdb.fi Git - libs/gl.git/blobdiff - source/view.cpp
Add a new View class to tie up some presentation tasks
[libs/gl.git] / source / view.cpp
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