]> git.tdb.fi Git - libs/gl.git/blob - source/builders/sequencetemplate.h
Access builtin resources through a global instance
[libs/gl.git] / source / builders / sequencetemplate.h
1 #ifndef SEQUENCETEMPLATE_H_
2 #define SEQUENCETEMPLATE_H_
3
4 #include <string>
5 #include <vector>
6 #include <msp/core/typeregistry.h>
7 #include <msp/datafile/objectloader.h>
8 #include "blend.h"
9 #include "postprocessor.h"
10 #include "predicate.h"
11
12 namespace Msp {
13 namespace GL {
14
15 class DepthTest;
16 class Lighting;
17 class Renderable;
18
19 class SequenceTemplate
20 {
21 private:
22         class PostProcLoader: virtual public DataFile::Loader
23         {
24         private:
25                 template<typename T>
26                 struct AddPostProc
27                 {
28                         void operator()(const std::string &kw, PostProcLoader &ldr) const { ldr.add(kw, &PostProcLoader::postprocessor<T>); }
29                 };
30
31         protected:
32                 RefPtr<PostProcessor::Template> postproc;
33
34         public:
35                 PostProcLoader();
36
37                 PostProcessor::Template *get_postprocessor_template() { return postproc.release(); }
38
39         protected:
40                 virtual void postprocessor_loaded() { }
41
42         private:
43                 template<typename T>
44                 void postprocessor();
45
46                 friend class SequenceTemplate;
47         };
48
49 public:
50         class Loader: public DataFile::CollectionObjectLoader<SequenceTemplate, Resources>, public PostProcLoader
51         {
52         public:
53                 Loader(SequenceTemplate &, Collection &);
54
55                 virtual void postprocessor_loaded();
56                 void multisample(unsigned);
57                 void multisample_range(unsigned, unsigned);
58                 void postprocessor(const std::string &);
59                 void step(const std::string &);
60                 void step_with_slot(const std::string &, const std::string &);
61         };
62
63         struct Step
64         {
65                 class Loader: public DataFile::CollectionObjectLoader<Step>
66                 {
67                 public:
68                         Loader(Step &);
69                         Loader(Step &, Collection &);
70                 private:
71                         void init();
72
73                         void blend(BlendFactor, BlendFactor);
74                         void blend_predefined(const std::string &);
75                         void depth_test(Predicate);
76                         void depth_test_predefined(const std::string &);
77                         void lighting(const std::string &);
78                         void lighting_inline();
79                         void object(const std::string &);
80                         void scene(const std::string &);
81                 };
82
83                 std::string tag;
84                 RefPtr<Lighting> lighting;
85                 RefPtr<const DepthTest> depth_test;
86                 RefPtr<const Blend> blend;
87                 std::string slot_name;
88                 Renderable *default_renderable;
89
90                 ~Step();
91         };
92
93         struct PostProcessor
94         {
95                 GL::PostProcessor::Template *postprocessor_template;
96                 std::string slot_name;
97
98                 PostProcessor(GL::PostProcessor::Template * = 0);
99         };
100
101         typedef std::vector<PostProcessor> PostProcessorArray;
102
103 private:
104         typedef TypeRegistry<PostProcLoader::AddPostProc, PostProcLoader &> PostProcessorRegistry;
105
106         bool hdr;
107         bool alpha;
108         unsigned required_multisample;
109         unsigned max_multisample;
110         std::vector<Step> steps;
111         PostProcessorArray postprocessors;
112
113 public:
114         SequenceTemplate();
115         ~SequenceTemplate();
116
117         bool get_hdr() const { return hdr; }
118         bool get_alpha() const { return alpha; }
119         unsigned get_required_multisample() const { return required_multisample; }
120         unsigned get_maximum_multisample() const { return max_multisample; }
121         const std::vector<Step> &get_steps() const { return steps; }
122         const PostProcessorArray &get_postprocessors() const { return postprocessors; }
123
124         template<typename T>
125         static void register_postprocessor(const std::string &);
126 private:
127         static PostProcessorRegistry &get_postprocessor_registry();
128 };
129
130 template<typename T>
131 void SequenceTemplate::register_postprocessor(const std::string &kw)
132 {
133         get_postprocessor_registry().register_type<T>(kw);
134 }
135
136 template<typename T>
137 void SequenceTemplate::PostProcLoader::postprocessor()
138 {
139         if(postproc)
140                 throw std::logic_error("Only one postprocessor allowed per slot");
141         RefPtr<typename T::Template> pp = new typename T::Template;
142         load_sub(*pp);
143         postproc = pp;
144         pp = 0;
145         postprocessor_loaded();
146 }
147
148 } // namespace GL
149 } // namespace Msp
150
151 #endif