]> git.tdb.fi Git - libs/gl.git/blob - source/view.cpp
Immediately process window size to set correct viewport size
[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         camera(0),
17         content(0)
18 {
19         window.signal_resize.connect(sigc::mem_fun(this, &View::window_resized));
20         window_resized(window.get_width(), window.get_height());
21 }
22
23 void View::set_camera(Camera *c)
24 {
25         camera = c;
26         if(camera)
27                 camera->set_aspect(static_cast<float>(window.get_width())/window.get_height());
28 }
29
30 void View::set_content(Renderable *r)
31 {
32         content = r;
33 }
34
35 void View::synchronize_camera_aspect(Camera &c)
36 {
37         synced_cameras.push_back(&c);
38         c.set_aspect(static_cast<float>(window.get_width())/window.get_height());
39 }
40
41 void View::render()
42 {
43         target.clear(COLOR_BUFFER_BIT|DEPTH_BUFFER_BIT);
44         if(content)
45         {
46                 Renderer renderer(camera);
47                 content->setup_frame(renderer);
48                 content->render(renderer);
49                 content->finish_frame();
50         }
51         context.swap_buffers();
52 }
53
54 void View::window_resized(unsigned w, unsigned h)
55 {
56         target.viewport(0, 0, w, h);
57         float aspect = static_cast<float>(w)/h;
58         if(camera)
59                 camera->set_aspect(aspect);
60         for(list<Camera *>::iterator i=synced_cameras.begin(); i!=synced_cameras.end(); ++i)
61                 (*i)->set_aspect(aspect);
62 }
63
64 } // namespace GL
65 } // namespace Msp