From: Mikko Rasa Date: Sun, 20 Apr 2014 09:44:05 +0000 (+0300) Subject: Add a resource collection class X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=b30209f7d9c984ee03df779446fa4ad5f9334865;p=libs%2Fgl.git Add a resource collection class It's annoying to have to implement the same collection everywhere, especially loading textures from image files. --- diff --git a/source/resources.cpp b/source/resources.cpp new file mode 100644 index 00000000..8e872d5d --- /dev/null +++ b/source/resources.cpp @@ -0,0 +1,74 @@ +#include +#include "animation.h" +#include "armature.h" +#include "font.h" +#include "keyframe.h" +#include "material.h" +#include "mesh.h" +#include "object.h" +#include "pose.h" +#include "program.h" +#include "resources.h" +#include "technique.h" +#include "texture2d.h" +#include "texturecube.h" + +using namespace std; + +namespace Msp { +namespace GL { + +Resources::Resources(): + default_tex_filter(LINEAR_MIPMAP_LINEAR) +{ + add_type().suffix(".anim").keyword("animation"); + add_type().suffix(".arma").keyword("armature"); + add_type().keyword("font"); + add_type().suffix(".kframe").keyword("keyframe"); + add_type().suffix(".mat").keyword("material"); + add_type().keyword("mesh"); + add_type().keyword("object"); + add_type().keyword("pose"); + add_type().keyword("shader"); + add_type().suffix(".tech").keyword("technique"); + add_type().base().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d); + add_type().base().suffix(".texcb").keyword("texture_cube"); +} + +void Resources::set_default_texture_filter(TextureFilter tf) +{ + default_tex_filter = tf; +} + +Texture2D *Resources::create_texture2d(const string &name) +{ + string ext = FS::extpart(name); + if(ext==".tex2d") + return 0; + + if(RefPtr io = open_from_sources(name)) + { + Graphics::Image image; + image.load_io(*io); + + RefPtr tex = new GL::Texture2D; + + if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR || + default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR) + { + tex->set_generate_mipmap(true); + tex->set_mag_filter(LINEAR); + } + else + tex->set_mag_filter(default_tex_filter); + tex->set_min_filter(default_tex_filter); + + tex->image(image); + return tex.release(); + } + + return 0; +} + +} // namespace GL +} // namespace Msp diff --git a/source/resources.h b/source/resources.h new file mode 100644 index 00000000..8871c16e --- /dev/null +++ b/source/resources.h @@ -0,0 +1,29 @@ +#ifndef MSP_GL_RESOURCES_H_ +#define MSP_GL_RESOURCES_H_ + +#include +#include "texture.h" + +namespace Msp { +namespace GL { + +class Texture2D; + +class Resources: virtual public DataFile::Collection +{ +private: + TextureFilter default_tex_filter; + +public: + Resources(); + + void set_default_texture_filter(TextureFilter); + +protected: + Texture2D *create_texture2d(const std::string &); +}; + +} // namespace GL +} // namespace Msp + +#endif