]> git.tdb.fi Git - libs/demoscene.git/blob - source/stage.cpp
Create Stage pipeline when added to a view
[libs/demoscene.git] / source / stage.cpp
1 #include <msp/gl/view.h>
2 #include "demo.h"
3 #include "stage.h"
4
5 using namespace std;
6 using namespace Msp;
7
8 Stage::Stage():
9         pipeline(0),
10         last_view(0)
11 { }
12
13 Stage::~Stage()
14 {
15         delete pipeline;
16 }
17
18 void Stage::add_things(Demo::ThingMap &things, const string &prefix)
19 {
20         things[prefix+"camera"] = static_cast<GL::Placeable *>(&camera);
21 }
22
23 void Stage::set_camera(const GL::Camera &c)
24 {
25         camera.set_object_matrix(c.get_object_matrix());
26         camera.set_up_direction(c.get_up_direction());
27         camera.set_field_of_view(c.get_field_of_view());
28         camera.set_depth_clip(c.get_near_clip(), c.get_far_clip());
29 }
30
31
32 Stage::UseInView::UseInView():
33         view(0),
34         stage(0)
35 { }
36
37 Stage::UseInView::UseInView(GL::View &v, Stage &s):
38         view(&v),
39         stage(&s)
40 { }
41
42 void Stage::UseInView::validate() const
43 {
44         if(!view)
45                 throw logic_error("null view");
46         if(!stage)
47                 throw logic_error("null stage");
48 }
49
50 void Stage::UseInView::start(float, float)
51 {
52         if(!stage->pipeline || view!=stage->last_view)
53         {
54                 stage->create_pipeline(*view);
55                 if(!stage->pipeline)
56                         throw logic_error("null pipeline");
57                 stage->last_view = view;
58         }
59
60         view->set_camera(&stage->camera);
61         view->set_content(stage->pipeline);
62 }
63
64
65 Stage::SetCamera::SetCamera():
66         stage(0),
67         camera(0)
68 { }
69
70 Stage::SetCamera::SetCamera(Stage &s, const GL::Camera &c):
71         stage(&s),
72         camera(&c)
73 { }
74
75 void Stage::SetCamera::validate() const
76 {
77         if(!stage)
78                 throw logic_error("null stage");
79         if(!camera)
80                 throw logic_error("null camera");
81 }
82
83 void Stage::SetCamera::start(float, float)
84 {
85         stage->set_camera(*camera);
86 }
87
88
89 Stage::UseInView::Loader::Loader(UseInView &u, Demo &d):
90         DataFile::DerivedObjectLoader<UseInView, Action::Loader>(u, d)
91 {
92         add("stage", &Loader::stage);
93         add("view", &Loader::view);
94 }
95
96 void Stage::UseInView::Loader::stage(const string &n)
97 {
98         obj.stage = &demo.get_thing<Stage>(n);
99 }
100
101 void Stage::UseInView::Loader::view(const string &n)
102 {
103         obj.view = &demo.get_thing<GL::View>(n);
104 }
105
106
107 Stage::SetCamera::Loader::Loader(SetCamera &s, Demo &d):
108         DataFile::DerivedObjectLoader<SetCamera, Action::Loader>(s, d)
109 {
110         add("camera", &Loader::camera);
111         add("stage", &Loader::stage);
112 }
113
114 void Stage::SetCamera::Loader::stage(const string &n)
115 {
116         obj.stage = &demo.get_thing<Stage>(n);
117 }
118
119 void Stage::SetCamera::Loader::camera(const string &n)
120 {
121         obj.camera = &demo.get_resources().get<GL::Camera>(n);
122 }