]> git.tdb.fi Git - libs/gl.git/blob - source/render/scene.h
Add inline data items to the collection
[libs/gl.git] / source / render / scene.h
1 #ifndef MSP_GL_SCENE_H_
2 #define MSP_GL_SCENE_H_
3
4 #include <vector>
5 #include <msp/core/typeregistry.h>
6 #include <msp/datafile/objectloader.h>
7 #include "matrix.h"
8 #include "renderable.h"
9 #include "vector.h"
10
11 namespace Msp {
12 namespace GL {
13
14 /**
15 Scenes are containers for other Renderables.  This is a base class that can't
16 be instantiated.  Examples of available Scene types are SimpleScene,
17 InstancedScene and OrderedScene.
18 */
19 class Scene: public Renderable
20 {
21 protected:
22         class Loader: public DataFile::CollectionObjectLoader<Scene>
23         {
24         public:
25                 typedef std::map<std::string, Renderable *> ContentMap;
26
27         private:
28                 ContentMap *content;
29                 unsigned inst_counter;
30
31         public:
32                 Loader(Scene &, Collection &);
33                 Loader(Scene &, Collection &, ContentMap &);
34         private:
35                 void init();
36
37                 void object(const std::string &);
38                 void object_tagged(const std::string &, const std::string &);
39                 void scene(const std::string &);
40         };
41
42 public:
43         class GenericLoader: public DataFile::Loader
44         {
45         private:
46                 template<typename T>
47                 struct CreateScene
48                 {
49                         void operator()(const std::string &, GenericLoader &) const;
50                 };
51
52                 DataFile::Collection &coll;
53                 Scene *scene;
54                 Loader *scene_loader;
55
56                 static ActionMap shared_actions;
57
58         public:
59                 GenericLoader(DataFile::Collection &);
60                 ~GenericLoader();
61
62                 Scene *get_scene() { Scene *s = scene; scene = 0; return s; }
63         private:
64                 virtual void init_actions();
65
66                 void type(const DataFile::Symbol &);
67
68                 friend class Scene;
69         };
70
71 private:
72         typedef TypeRegistry<GenericLoader::CreateScene, GenericLoader &> SceneRegistry;
73
74 protected:
75         mutable Matrix culling_matrix;
76         mutable Vector4 frustum_edges[6];
77
78         Scene() { }
79 private:
80         Scene(const Scene &);
81         Scene &operator=(const Scene &);
82 public:
83         virtual ~Scene() { }
84
85         virtual void add(Renderable &) = 0;
86         virtual void remove(Renderable &) = 0;
87
88 protected:
89         bool setup_frustum(const Renderer &) const;
90         bool frustum_cull(const Renderable &) const;
91
92 public:
93         template<typename T>
94         static void register_type(const std::string &);
95 private:
96         static SceneRegistry &get_scene_registry();
97 };
98
99 template<typename T>
100 void Scene::register_type(const std::string &kw)
101 {
102         get_scene_registry().register_type<T>(kw);
103 }
104
105 template<typename T>
106 void Scene::GenericLoader::CreateScene<T>::operator()(const std::string &, GenericLoader &ldr) const
107 {
108         if(ldr.scene)
109                 throw std::logic_error("Scene type was already specified");
110
111         T *scene = new T;
112         ldr.scene = scene;
113         ldr.scene_loader = new typename T::Loader(*scene, ldr.coll);
114         ldr.add_auxiliary_loader(*ldr.scene_loader);
115 }
116
117 } // namespace GL
118 } // namespace Msp
119
120 #endif