]> git.tdb.fi Git - libs/gl.git/blob - source/resources.cpp
Run a tighter ship with extensions
[libs/gl.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include "animation.h"
3 #include "armature.h"
4 #include "font.h"
5 #include "keyframe.h"
6 #include "material.h"
7 #include "mesh.h"
8 #include "object.h"
9 #include "pose.h"
10 #include "program.h"
11 #include "resourcemanager.h"
12 #include "resources.h"
13 #include "technique.h"
14 #include "texture2d.h"
15 #include "texturecube.h"
16
17 using namespace std;
18
19 namespace Msp {
20 namespace GL {
21
22 Resources::Resources():
23         default_tex_filter(LINEAR_MIPMAP_LINEAR),
24         srgb_conversion(false),
25         resource_manager(0)
26 {
27         add_type<Animation>().suffix(".anim").keyword("animation");
28         add_type<Armature>().suffix(".arma").keyword("armature");
29         add_type<Font>().keyword("font");
30         add_type<KeyFrame>().suffix(".kframe").keyword("keyframe");
31         add_type<Material>().suffix(".mat").keyword("material");
32         add_type<Mesh>().keyword("mesh").creator(&Resources::create_mesh);
33         add_type<Object>().keyword("object");
34         add_type<Pose>().keyword("pose");
35         add_type<Program>().keyword("shader");
36         add_type<Technique>().suffix(".tech").keyword("technique");
37         add_type<Texture2D>().base<Texture>().suffix(".tex2d").suffix(".png").suffix(".jpg").keyword("texture2d").creator(&Resources::create_texture2d);
38         add_type<TextureCube>().base<Texture>().suffix(".texcb").keyword("texture_cube");
39 }
40
41 void Resources::set_default_texture_filter(TextureFilter tf)
42 {
43         default_tex_filter = tf;
44 }
45
46 void Resources::set_srgb_conversion(bool c)
47 {
48         srgb_conversion = c;
49 }
50
51 void Resources::set_resource_manager(ResourceManager *m)
52 {
53         resource_manager = m;
54 }
55
56 Mesh *Resources::create_mesh(const string &name)
57 {
58         if(!resource_manager)
59                 return 0;
60
61         if(RefPtr<IO::Seekable> io = open_from_sources(name))
62         {
63                 RefPtr<GL::Mesh> mesh = new GL::Mesh(resource_manager);
64                 resource_manager->set_resource_location(*mesh, *this, name);
65                 return mesh.release();
66         }
67
68         return 0;
69 }
70
71 Texture2D *Resources::create_texture2d(const string &name)
72 {
73         string ext = FS::extpart(name);
74         if(ext==".tex2d")
75                 return 0;
76
77         if(RefPtr<IO::Seekable> io = open_from_sources(name))
78         {
79                 Graphics::Image image;
80                 if(!resource_manager)
81                         image.load_io(*io);
82
83                 RefPtr<GL::Texture2D> tex = new GL::Texture2D(resource_manager);
84
85                 if(default_tex_filter==NEAREST_MIPMAP_NEAREST || default_tex_filter==NEAREST_MIPMAP_LINEAR ||
86                         default_tex_filter==LINEAR_MIPMAP_NEAREST || default_tex_filter==LINEAR_MIPMAP_LINEAR)
87                 {
88                         tex->set_generate_mipmap(true);
89                         tex->set_mag_filter(LINEAR);
90                 }
91                 else
92                         tex->set_mag_filter(default_tex_filter);
93                 tex->set_min_filter(default_tex_filter);
94
95                 // TODO Somehow pass the srgb flag if a resource manager is in use
96                 if(resource_manager)
97                         resource_manager->set_resource_location(*tex, *this, name);
98                 else
99                         tex->image(image, srgb_conversion);
100                 return tex.release();
101         }
102
103         return 0;
104 }
105
106 } // namespace GL
107 } // namespace Msp