]> git.tdb.fi Git - libs/gl.git/blob - source/view.cpp
Add a new View class to tie up some presentation tasks
[libs/gl.git] / source / view.cpp
1 #include "camera.h"
2 #include "framebuffer.h"
3 #include "renderable.h"
4 #include "view.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 View::View(Graphics::Window &w, Graphics::GLContext &c):
12         window(w),
13         context(c),
14         target(Framebuffer::system()),
15         content(0)
16 {
17         window.signal_resize.connect(sigc::mem_fun(this, &View::window_resized));
18 }
19
20 void View::set_content(const Renderable *r)
21 {
22         content = r;
23 }
24
25 void View::synchronize_camera_aspect(Camera &c)
26 {
27         synced_cameras.push_back(&c);
28         c.set_aspect(static_cast<float>(window.get_width())/window.get_height());
29 }
30
31 void View::render()
32 {
33         target.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
34         if(content)
35                 content->render();
36         context.swap_buffers();
37 }
38
39 void View::window_resized(unsigned w, unsigned h)
40 {
41         target.viewport(0, 0, w, h);
42         float aspect = static_cast<float>(w)/h;
43         for(list<Camera *>::iterator i=synced_cameras.begin(); i!=synced_cameras.end(); ++i)
44                 (*i)->set_aspect(aspect);
45 }
46
47 } // namespace GL
48 } // namespace Msp