#include "error.h"
#include "resourcemanager.h"
#include "texture.h"
+#include "texture1d.h"
+#include "texture2d.h"
+#include "texture2darray.h"
+#include "texture3d.h"
+#include "texturecube.h"
using namespace std;
image(img, lv);
}
+Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
+{
+ static GenericLoader::TypeRegistry registry;
+ static bool initialized = false;
+ if(!initialized)
+ {
+ initialized = true;
+ registry.register_type<Texture1D>("1d");
+ registry.register_type<Texture2D>("2d");
+ registry.register_type<Texture3D>("3d");
+ registry.register_type<Texture2DArray>("2d_array");
+ registry.register_type<TextureCube>("cube");
+ }
+ return registry;
+}
+
Texture::Loader::Loader(Texture &t, Collection *c):
CollectionObjectLoader<Texture>(t, c),
.creator([this](const string &n) -> SimpleScene * { create_generic<Scene>(n); return 0; });
add_type<Technique>().suffix(".tech").keyword("technique")
.notify(&set_debug_name<Technique>);
- add_type<Texture1D>().base<Texture>().suffix(".tex1d").keyword("texture1d")
+ add_type<Texture1D>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture1D * { create_texture(n); return 0; })
.notify(&set_debug_name<Texture1D>);
- add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d")
- .creator([this](const string &n){ return create_texture2d(n); })
+ add_type<Texture2D>().base<Texture>().suffix(".tex").suffix(".png").suffix(".jpg")
+ .creator([this](const string &n) -> Texture2D * { create_texture(n); return 0; })
.notify(&set_debug_name<Texture2D>);
- add_type<Texture3D>().base<Texture>().suffix(".tex3d").keyword("texture3d")
+ add_type<Texture3D>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture3D * { create_texture(n); return 0; })
.notify(&set_debug_name<Texture3D>);
- add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube")
+ add_type<TextureCube>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> TextureCube * { create_texture(n); return 0; })
.notify(&set_debug_name<TextureCube>);
- add_type<Texture2DArray>().base<Texture>().suffix(".tex2da").keyword("texture2d_array")
+ add_type<Texture2DArray>().base<Texture>().suffix(".tex")
+ .creator([this](const string &n) -> Texture2DArray * { create_texture(n); return 0; })
.notify(&set_debug_name<Texture2DArray>);
add_type<UnlitMaterial>().base<Material>().suffix(".mat")
.creator([this](const string &n) -> UnlitMaterial * { create_generic<Material>(n); return 0; })
resource_manager = m;
}
-template<typename T>
+template<typename T, typename L>
T *Resources::create_generic(const string &name)
{
if(RefPtr<IO::Seekable> io = open_raw(name))
{
DataFile::Parser parser(*io, name);
- typename T::GenericLoader ldr(*this);
+ L ldr(*this);
ldr.load(parser);
ldr.store_object(*this, name);
}
return 0;
}
-Texture2D *Resources::create_texture2d(const string &name)
+Texture *Resources::create_texture(const string &name)
{
string ext = FS::extpart(name);
- if(ext==".tex2d" && !resource_manager)
- return 0;
+ if(ext==".tex")
+ return create_generic<Texture, GenericResourceLoader<Texture>>(name);
if(RefPtr<IO::Seekable> io = open_raw(name))
{
RefPtr<Texture2D> tex;
- if(ext==".tex2d")
- {
- tex = new Texture2D;
- tex->set_manager(resource_manager);
- DataFile::Parser parser(*io, name);
- Texture2D::Loader ldr(*tex, *this);
- ldr.load(parser);
- }
- else
- {
- // Verify that the image is loadable
- Graphics::Image image;
- if(!resource_manager)
- image.load_io(*io);
+ // Verify that the image is loadable
+ Graphics::Image image;
+ if(!resource_manager)
+ image.load_io(*io);
- tex = new Texture2D(resource_manager);
+ tex = new Texture2D;
+ tex->set_manager(resource_manager);
- if(resource_manager)
- resource_manager->set_resource_location(*tex, *this, name);
- else
- tex->image(image);
- }
+ if(resource_manager)
+ resource_manager->set_resource_location(*tex, *this, name);
+ else
+ tex->image(image);
- return tex.release();
+ add(name, tex.get());
+ tex.release();
}
return 0;
add("light", &Loader::generic<Light>);
add("material", &Loader::generic<Material>);
add("scene", &Loader::generic<Scene>);
+ add("texture", &Loader::generic<Texture, GenericResourceLoader<Texture>>);
}
-template<typename T>
+template<typename T, typename L>
void Resources::Loader::generic(const string &name)
{
- typename T::GenericLoader ldr(obj);
+ L ldr(obj);
load_sub_with(ldr);
ldr.store_object(obj, name);
}
+
+template<typename T>
+void Resources::GenericResourceLoader<T>::type(const DataFile::Symbol &t)
+{
+ T::GenericLoader::type(t);
+ this->object->set_manager(resources.resource_manager);
+}
+
} // namespace GL
} // namespace Msp
Loader(Resources &);
private:
- template<typename T>
+ template<typename T, typename L = typename T::GenericLoader>
void generic(const std::string &);
};
private:
+ template<typename T>
+ class GenericResourceLoader: public T::GenericLoader
+ {
+ private:
+ Resources &resources;
+
+ public:
+ GenericResourceLoader(Resources &r): T::GenericLoader(r), resources(r) { }
+
+ protected:
+ virtual void type(const DataFile::Symbol &);
+ };
+
bool srgb_conversion;
ResourceManager *resource_manager;
void set_resource_manager(ResourceManager *);
protected:
- template<typename T>
+ template<typename T, typename L = typename T::GenericLoader>
T *create_generic(const std::string &);
Mesh *create_mesh(const std::string &);
- Texture2D *create_texture2d(const std::string &);
+ Texture *create_texture(const std::string &);
Module *create_module(const std::string &);
Program *create_program(const std::string &);