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