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