]> git.tdb.fi Git - libs/gl.git/commitdiff
Make scene contents retrievable when loaded with GenericLoader
authorMikko Rasa <tdb@tdb.fi>
Tue, 9 Jul 2024 13:32:48 +0000 (16:32 +0300)
committerMikko Rasa <tdb@tdb.fi>
Tue, 9 Jul 2024 13:33:24 +0000 (16:33 +0300)
Content from inline sub-scenes will also be added.

source/render/scene.cpp
source/render/scene.h

index 4d8732aa96a07582297fc3b6237cde8aaf53ef25..f2f3bbf624f083b5c3a2bbf78b0bedc9a6be8f3f 100644 (file)
@@ -16,6 +16,8 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
+thread_local Scene::ContentMap *Scene::named_content = nullptr;
+
 Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry()
 {
        static Scene::GenericLoader::TypeRegistry registry;
@@ -75,5 +77,17 @@ void Scene::Loader::scene_inline()
        obj.add(*dyn_sub<GenericLoader>().into(get_collection(), format("scene %d", ++inline_counter)).load());
 }
 
+
+void Scene::GenericLoader::prepare()
+{
+       saved_content = named_content;
+       named_content = content;
+}
+
+void Scene::GenericLoader::finish(bool)
+{
+       named_content = saved_content;
+}
+
 } // namespace GL
 } // namespace Msp
index 8dfd50d58e6b6baecbf73bc8c261eb0510db6096..babda141a4af60d821ded23860f5106c51ec416a 100644 (file)
@@ -25,17 +25,18 @@ SimpleScene is a good default choice if there are no specific requirements.
 class MSPGL_API Scene: public Renderable
 {
 protected:
+       using ContentMap = std::map<std::string, Renderable *>;
+
        class MSPGL_API Loader: public DataFile::CollectionObjectLoader<Scene>
        {
        public:
-               typedef std::map<std::string, Renderable *> ContentMap;
 
        private:
                ContentMap *content;
                unsigned inline_counter = 0;
 
        public:
-               Loader(Scene &s, Collection &c): Loader(s, c, nullptr) { }
+               Loader(Scene &s, Collection &c): Loader(s, c, named_content) { }
                Loader(Scene &s, Collection &c, ContentMap &m) : Loader(s, c, &m) { }
        private:
                Loader(Scene &, Collection &, ContentMap *);
@@ -52,13 +53,24 @@ public:
        {
                friend class Scene;
 
+       private:
+               ContentMap *content = nullptr;
+               ContentMap *saved_content = nullptr;
+
        public:
-               GenericLoader(DataFile::Collection &c): DynamicObjectLoader<Scene>(&c) { }
+               GenericLoader(DataFile::Collection &c): DynamicObjectLoader(&c), content(named_content) { }
+               GenericLoader(DataFile::Collection &c, ContentMap &m): DynamicObjectLoader(&c), content(&m) { }
 
        protected:
                const TypeRegistry &get_type_registry() const override { return get_scene_registry(); }
+
+               void prepare() override;
+               void finish(bool) override;
        };
 
+private:
+       static thread_local ContentMap *named_content;
+
 protected:
        Scene() = default;
 public: