From 9813f8711628a0fbe786406e974dc33546dc9cee Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 5 Oct 2021 14:49:25 +0300 Subject: [PATCH] Use DynamicObjectLoader for Material and Scene generic loaders --- source/materials/material.cpp | 31 ++--------------------- source/materials/material.h | 47 ++++++----------------------------- source/render/scene.cpp | 31 ++--------------------- source/render/scene.h | 46 ++++++---------------------------- 4 files changed, 18 insertions(+), 137 deletions(-) diff --git a/source/materials/material.cpp b/source/materials/material.cpp index 71610c7c..6d9c0571 100644 --- a/source/materials/material.cpp +++ b/source/materials/material.cpp @@ -54,9 +54,9 @@ void Material::set_debug_name(const string &name) #endif } -Material::MaterialRegistry &Material::get_material_registry() +Material::GenericLoader::TypeRegistry &Material::get_material_registry() { - static MaterialRegistry registry; + static GenericLoader::TypeRegistry registry; static bool initialized = false; if(!initialized) { @@ -83,32 +83,5 @@ void Material::Loader::sampler(const string &name) obj.sampler = &get_collection().get(name); } - -DataFile::Loader::ActionMap Material::GenericLoader::shared_actions; - -Material::GenericLoader::GenericLoader(DataFile::Collection &c): - coll(c), - material(0), - mat_loader(0) -{ - set_actions(shared_actions); -} - -Material::GenericLoader::~GenericLoader() -{ - delete material; - delete mat_loader; -} - -void Material::GenericLoader::init_actions() -{ - add("type", &GenericLoader::type); -} - -void Material::GenericLoader::type(const DataFile::Symbol &sym) -{ - get_material_registry().invoke(sym.name, *this); -} - } // namespace GL } // namespace Msp diff --git a/source/materials/material.h b/source/materials/material.h index ea02df6b..77e7d44e 100644 --- a/source/materials/material.h +++ b/source/materials/material.h @@ -1,8 +1,8 @@ #ifndef MSP_GL_MATERIAL_H_ #define MSP_GL_MATERIAL_H_ -#include #include +#include #include #include "color.h" #include "programdata.h" @@ -56,37 +56,17 @@ protected: }; public: - class GenericLoader: public DataFile::Loader + class GenericLoader: public DataFile::DynamicObjectLoader { - private: - template - struct CreateMaterial - { - void operator()(const std::string &, GenericLoader &) const; - }; - - DataFile::Collection &coll; - Material *material; - Loader *mat_loader; - - static ActionMap shared_actions; + friend class Material; public: - GenericLoader(DataFile::Collection &); - ~GenericLoader(); + GenericLoader(Collection &c): DynamicObjectLoader(&c) { } - Material *get_object() { Material *m = material; material = 0; return m; } - private: - virtual void init_actions(); - - void type(const DataFile::Symbol &); - - friend class Material; + protected: + virtual const TypeRegistry &get_type_registry() const { return get_material_registry(); } }; -private: - typedef TypeRegistry MaterialRegistry; - protected: const Sampler *sampler; ProgramData shdata; @@ -112,7 +92,7 @@ public: template static void register_type(const std::string &); private: - static MaterialRegistry &get_material_registry(); + static GenericLoader::TypeRegistry &get_material_registry(); }; template @@ -188,19 +168,6 @@ void Material::PropertyLoader::property_texture(void (C::*set_texture)(const (static_cast(obj).*set_texture)(&static_cast(get_collection()).get(name)); } - -template -void Material::GenericLoader::CreateMaterial::operator()(const std::string &, GenericLoader &ldr) const -{ - if(ldr.material) - throw std::logic_error("Material type was already specified"); - - T *mat = new T; - ldr.material = mat; - ldr.mat_loader = new typename T::Loader(*mat, ldr.coll); - ldr.add_auxiliary_loader(*ldr.mat_loader); -} - } // namespace GL } // namespace Msp diff --git a/source/render/scene.cpp b/source/render/scene.cpp index 22958867..75d1859b 100644 --- a/source/render/scene.cpp +++ b/source/render/scene.cpp @@ -73,9 +73,9 @@ bool Scene::frustum_cull(const Renderable &renderable) const return false; } -Scene::SceneRegistry &Scene::get_scene_registry() +Scene::GenericLoader::TypeRegistry &Scene::get_scene_registry() { - static SceneRegistry registry; + static Scene::GenericLoader::TypeRegistry registry; static bool initialized = false; if(!initialized) { @@ -119,32 +119,5 @@ void Scene::Loader::scene(const string &n) obj.add(get_collection().get(n)); } - -DataFile::Loader::ActionMap Scene::GenericLoader::shared_actions; - -Scene::GenericLoader::GenericLoader(DataFile::Collection &c): - coll(c), - scene(0), - scene_loader(0) -{ - set_actions(shared_actions); -} - -Scene::GenericLoader::~GenericLoader() -{ - delete scene; - delete scene_loader; -} - -void Scene::GenericLoader::init_actions() -{ - add("type", &GenericLoader::type); -} - -void Scene::GenericLoader::type(const DataFile::Symbol &sym) -{ - get_scene_registry().invoke(sym.name, *this); -} - } // namespace GL } // namespace Msp diff --git a/source/render/scene.h b/source/render/scene.h index 6fb45d61..e55de1a8 100644 --- a/source/render/scene.h +++ b/source/render/scene.h @@ -2,7 +2,7 @@ #define MSP_GL_SCENE_H_ #include -#include +#include #include #include "matrix.h" #include "renderable.h" @@ -39,37 +39,17 @@ protected: }; public: - class GenericLoader: public DataFile::Loader + class GenericLoader: public DataFile::DynamicObjectLoader { - private: - template - struct CreateScene - { - void operator()(const std::string &, GenericLoader &) const; - }; - - DataFile::Collection &coll; - Scene *scene; - Loader *scene_loader; - - static ActionMap shared_actions; + friend class Scene; public: - GenericLoader(DataFile::Collection &); - ~GenericLoader(); + GenericLoader(DataFile::Collection &c): DynamicObjectLoader(&c) { } - Scene *get_object() { Scene *s = scene; scene = 0; return s; } - private: - virtual void init_actions(); - - void type(const DataFile::Symbol &); - - friend class Scene; + protected: + virtual const TypeRegistry &get_type_registry() const { return get_scene_registry(); } }; -private: - typedef TypeRegistry SceneRegistry; - protected: mutable Matrix culling_matrix; mutable Vector4 frustum_edges[6]; @@ -92,7 +72,7 @@ public: template static void register_type(const std::string &); private: - static SceneRegistry &get_scene_registry(); + static GenericLoader::TypeRegistry &get_scene_registry(); }; template @@ -101,18 +81,6 @@ void Scene::register_type(const std::string &kw) get_scene_registry().register_type(kw); } -template -void Scene::GenericLoader::CreateScene::operator()(const std::string &, GenericLoader &ldr) const -{ - if(ldr.scene) - throw std::logic_error("Scene type was already specified"); - - T *scene = new T; - ldr.scene = scene; - ldr.scene_loader = new typename T::Loader(*scene, ldr.coll); - ldr.add_auxiliary_loader(*ldr.scene_loader); -} - } // namespace GL } // namespace Msp -- 2.43.0