]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a resource collection class
authorMikko Rasa <tdb@tdb.fi>
Sun, 20 Apr 2014 09:44:05 +0000 (12:44 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 20 Apr 2014 09:44:05 +0000 (12:44 +0300)
It's annoying to have to implement the same collection everywhere,
especially loading textures from image files.

source/resources.cpp [new file with mode: 0644]
source/resources.h [new file with mode: 0644]

diff --git a/source/resources.cpp b/source/resources.cpp
new file mode 100644 (file)
index 0000000..8e872d5
--- /dev/null
@@ -0,0 +1,74 @@
+#include <msp/fs/utils.h>
+#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<Animation>().suffix(".anim").keyword("animation");
+       add_type<Armature>().suffix(".arma").keyword("armature");
+       add_type<Font>().keyword("font");
+       add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
+       add_type<Material>().suffix(".mat").keyword("material");
+       add_type<Mesh>().keyword("mesh");
+       add_type<Object>().keyword("object");
+       add_type<Pose>().keyword("pose");
+       add_type<Program>().keyword("shader");
+       add_type<Technique>().suffix(".tech").keyword("technique");
+       add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
+       add_type<TextureCube>().base<Texture>().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::Seekable> io = open_from_sources(name))
+       {
+               Graphics::Image image;
+               image.load_io(*io);
+
+               RefPtr<GL::Texture2D> 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 (file)
index 0000000..8871c16
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef MSP_GL_RESOURCES_H_
+#define MSP_GL_RESOURCES_H_
+
+#include <msp/datafile/collection.h>
+#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