]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Restore ability to handle raw image files
[libs/gltk.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include "resources.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 Resources::Resources():
10         default_font(0)
11 {
12         add_type<Graphic>().keyword("graphic");
13         add_type<GL::Texture2D>().keyword("texture").creator(&Resources::create_texture);
14         add_type<GL::Font>().keyword("font");
15         add_type<Style>().keyword("style");
16 }
17
18 const GL::Font &Resources::get_default_font() const
19 {
20         if(!default_font)
21                 throw logic_error("!default_font");
22
23         return *default_font;
24 }
25
26 GL::Texture2D *Resources::create_texture(const string &name)
27 {
28         string ext = FS::extpart(name);
29         if(ext==".png" || ext==".jpg")
30         {
31                 IO::Seekable *io = open_from_sources(name);
32                 Graphics::Image image;
33                 image.load_io(*io);
34                 GL::Texture2D *tex = new GL::Texture2D;
35                 tex->set_min_filter(GL::LINEAR);
36                 tex->image(image);
37                 return tex;
38         }
39         else
40                 return 0;
41 }
42
43
44 Resources::Loader::Loader(Resources &r):
45         Collection::Loader(r),
46         res(r)
47 {
48         add("default_font", &Loader::default_font);
49         add("font", &Loader::font);
50 }
51
52 void Resources::Loader::default_font(const string &name)
53 {
54         res.default_font = &res.get<GL::Font>(name);
55 }
56
57 void Resources::Loader::font(const string &name)
58 {
59         RefPtr<GL::Font> fnt = new GL::Font;
60         load_sub(*fnt, res);
61         res.add(name, fnt.get());
62         if(!res.default_font)
63                 res.default_font = fnt.get();
64         fnt.release();
65 }
66
67 } // namespace GLtk
68 } // namespace Msp