]> git.tdb.fi Git - libs/gl.git/commitdiff
Unify the loader wrappers for Material and Scene
authorMikko Rasa <tdb@tdb.fi>
Mon, 4 Oct 2021 17:10:19 +0000 (20:10 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 4 Oct 2021 17:42:42 +0000 (20:42 +0300)
source/materials/material.cpp
source/materials/material.h
source/materials/rendermethod.cpp
source/render/scene.h
source/resources/resources.cpp
source/resources/resources.h
tools/viewer.cpp

index d8665537bcbe6097187628a23363b1e975f6f342..b7dd767eaff72dd309f5cb6adc4f4d211cfed4e5 100644 (file)
@@ -90,14 +90,20 @@ void Material::Loader::sampler(const string &name)
 
 DataFile::Loader::ActionMap Material::GenericLoader::shared_actions;
 
-Material::GenericLoader::GenericLoader(DataFile::Collection *c):
-       coll(c),
+Material::GenericLoader::GenericLoader():
+       coll(0),
        material(0),
        mat_loader(0)
 {
        set_actions(shared_actions);
 }
 
+Material::GenericLoader::GenericLoader(DataFile::Collection &c):
+       GenericLoader()
+{
+       coll = &c;
+}
+
 Material::GenericLoader::~GenericLoader()
 {
        delete material;
index 40d2b7ee96193627e148c2e4bf7189513cfbfc01..4f8e23ccc972001c892bb6e37a65f9448e3f1c9d 100644 (file)
@@ -74,10 +74,11 @@ public:
                static ActionMap shared_actions;
 
        public:
-               GenericLoader(DataFile::Collection * = 0);
+               GenericLoader();
+               GenericLoader(DataFile::Collection &);
                ~GenericLoader();
 
-               Material *get_material() { Material *m = material; material = 0; return m; }
+               Material *get_object() { Material *m = material; material = 0; return m; }
        private:
                virtual void init_actions();
 
index a6a843ee59f1770dbe17b9d9d2286ad3c79a8dcf..2c36cd6fa1e132e9eeded8a3066320cac582a0b9 100644 (file)
@@ -164,9 +164,9 @@ void RenderMethod::Loader::finish()
 
 void RenderMethod::Loader::material_inline()
 {
-       Material::GenericLoader ldr(coll);
+       Material::GenericLoader ldr(get_collection());
        load_sub_with(ldr);
-       RefPtr<Material> mat = ldr.get_material();
+       RefPtr<Material> mat = ldr.get_object();
        get_collection().add(inline_base_name+".mat", mat.get());
        obj.material = mat.release();
        obj.set_material_textures();
index e4f5ecdfd070ff81b499696a8f643a5ce61adb00..6fb45d6102b9fc422f71ba360da33b2e5a87c926 100644 (file)
@@ -58,7 +58,7 @@ public:
                GenericLoader(DataFile::Collection &);
                ~GenericLoader();
 
-               Scene *get_scene() { Scene *s = scene; scene = 0; return s; }
+               Scene *get_object() { Scene *s = scene; scene = 0; return s; }
        private:
                virtual void init_actions();
 
index 62bdabf60594e796439833b110e512bcd3544b24..80a19e7c9bdd6aa3ed30f037930613defc39182b 100644 (file)
@@ -50,7 +50,7 @@ Resources::Resources(bool set_as_global):
        add_type<Lighting>().suffix(".lightn").keyword("lighting")
                .notify(&Resources::set_debug_name<Lighting>);
        add_type<Material>().suffix(".mat")
-               .creator(&Resources::create_material).notify(&Resources::set_debug_name<Material>);
+               .creator(&Resources::create_generic<Material>).notify(&Resources::set_debug_name<Material>);
        add_type<Mesh>().keyword("mesh")
                .creator(&Resources::create_mesh).notify(&Resources::set_debug_name<Mesh>);
        add_type<Module>().suffix(".glsl").suffix(".spv")
@@ -63,7 +63,7 @@ Resources::Resources(bool set_as_global):
        add_type<Sampler>().suffix(".samp").keyword("sampler")
                .notify(&Resources::set_debug_name<Sampler>);
        add_type<Scene>().suffix(".scene")
-               .creator(&Resources::create_scene);
+               .creator(&Resources::create_generic<Scene>);
        add_type<Technique>().suffix(".tech").keyword("technique")
                .notify(&Resources::set_debug_name<Technique>);
        add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
@@ -121,14 +121,15 @@ void Resources::set_resource_manager(ResourceManager *m)
        resource_manager = m;
 }
 
-Material *Resources::create_material(const string &name)
+template<typename T>
+T *Resources::create_generic(const string &name)
 {
        if(RefPtr<IO::Seekable> io = open_raw(name))
        {
                DataFile::Parser parser(*io, name);
-               Material::GenericLoader ldr(this);
+               typename T::GenericLoader ldr(*this);
                ldr.load(parser);
-               return ldr.get_material();
+               return ldr.get_object();
        }
 
        return 0;
@@ -149,19 +150,6 @@ Mesh *Resources::create_mesh(const string &name)
        return 0;
 }
 
-Scene *Resources::create_scene(const string &name)
-{
-       if(RefPtr<IO::Seekable> io = open_raw(name))
-       {
-               DataFile::Parser parser(*io, name);
-               Scene::GenericLoader ldr(*this);
-               ldr.load(parser);
-               return ldr.get_scene();
-       }
-
-       return 0;
-}
-
 Texture2D *Resources::create_texture2d(const string &name)
 {
        string ext = FS::extpart(name);
@@ -269,7 +257,7 @@ void Resources::Loader::scene(const string &name)
 {
        Scene::GenericLoader ldr(obj);
        load_sub_with(ldr);
-       obj.add(name, ldr.get_scene());
+       obj.add(name, ldr.get_object());
 }
 
 } // namespace GL
index 37de5d8cfc80482badc99343ff617dd21d0ff692..293670f9cc06f78563c07a73d5cee3964bf5d781 100644 (file)
@@ -53,9 +53,10 @@ public:
        void set_resource_manager(ResourceManager *);
 
 protected:
-       Material *create_material(const std::string &);
+       template<typename T>
+       T *create_generic(const std::string &);
+
        Mesh *create_mesh(const std::string &);
-       Scene *create_scene(const std::string &);
        Texture2D *create_texture2d(const std::string &);
        Module *create_module(const std::string &);
        Program *create_program(const std::string &);
index a1703ede17d5621b98954db1deef56e57132ab86..b0e3420dab0395394f02ec0aa8525a8b04bff089 100644 (file)
@@ -195,7 +195,7 @@ Viewer::Viewer(int argc, char **argv):
                        IO::BufferedFile in(opts.renderable_name);
                        DataFile::Parser parser(in, opts.renderable_name);
                        ldr.load(parser);
-                       renderable = ldr.get_scene();
+                       renderable = ldr.get_object();
                }
                else
                        renderable = &resources.get<GL::Scene>(opts.renderable_name);