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