From: Mikko Rasa Date: Mon, 4 Oct 2021 17:10:19 +0000 (+0300) Subject: Unify the loader wrappers for Material and Scene X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=23d4100160bfa33359ce297b6b36244abcaa5f82;p=libs%2Fgl.git Unify the loader wrappers for Material and Scene --- diff --git a/source/materials/material.cpp b/source/materials/material.cpp index d8665537..b7dd767e 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -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; diff --git a/source/materials/material.h b/source/materials/material.h index 40d2b7ee..4f8e23cc 100644 --- a/source/materials/material.h +++ b/source/materials/material.h @@ -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(); diff --git a/source/materials/rendermethod.cpp b/source/materials/rendermethod.cpp index a6a843ee..2c36cd6f 100644 --- a/source/materials/rendermethod.cpp +++ b/source/materials/rendermethod.cpp @@ -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 mat = ldr.get_material(); + RefPtr mat = ldr.get_object(); get_collection().add(inline_base_name+".mat", mat.get()); obj.material = mat.release(); obj.set_material_textures(); diff --git a/source/render/scene.h b/source/render/scene.h index e4f5ecdf..6fb45d61 100644 --- a/source/render/scene.h +++ b/source/render/scene.h @@ -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(); diff --git a/source/resources/resources.cpp b/source/resources/resources.cpp index 62bdabf6..80a19e7c 100644 --- a/source/resources/resources.cpp +++ b/source/resources/resources.cpp @@ -50,7 +50,7 @@ Resources::Resources(bool set_as_global): add_type().suffix(".lightn").keyword("lighting") .notify(&Resources::set_debug_name); add_type().suffix(".mat") - .creator(&Resources::create_material).notify(&Resources::set_debug_name); + .creator(&Resources::create_generic).notify(&Resources::set_debug_name); add_type().keyword("mesh") .creator(&Resources::create_mesh).notify(&Resources::set_debug_name); add_type().suffix(".glsl").suffix(".spv") @@ -63,7 +63,7 @@ Resources::Resources(bool set_as_global): add_type().suffix(".samp").keyword("sampler") .notify(&Resources::set_debug_name); add_type().suffix(".scene") - .creator(&Resources::create_scene); + .creator(&Resources::create_generic); add_type().suffix(".tech").keyword("technique") .notify(&Resources::set_debug_name); add_type().base().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 +T *Resources::create_generic(const string &name) { if(RefPtr 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 = 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 diff --git a/source/resources/resources.h b/source/resources/resources.h index 37de5d8c..293670f9 100644 --- a/source/resources/resources.h +++ b/source/resources/resources.h @@ -53,9 +53,10 @@ public: void set_resource_manager(ResourceManager *); protected: - Material *create_material(const std::string &); + template + 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 &); diff --git a/tools/viewer.cpp b/tools/viewer.cpp index a1703ede..b0e3420d 100644 --- a/tools/viewer.cpp +++ b/tools/viewer.cpp @@ -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(opts.renderable_name);