]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
3b0f4f9154648b88d7254cb6f575854de341193b
[libs/gltk.git] / source / resources.cpp
1 #include <msp/core/except.h>
2 #include "resources.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 Resources::Resources():
10         path("."),
11         default_font(0)
12 {
13         add_keyword<GL::Font>("font");
14         add_keyword<Graphic>("graphic");
15         add_keyword<Style>("style");
16
17         add_creator(&Resources::create_font);
18         add_creator(&Resources::create_texture);
19 }
20
21 void Resources::set_path(const Path &p)
22 {
23         path=p;
24 }
25
26 const GL::Font &Resources::get_default_font() const
27 {
28         if(!default_font)
29                 throw InvalidState("No default font");
30
31         return *default_font;
32 }
33
34 GL::Font *Resources::create_font(const string &name)
35 {
36         RefPtr<GL::Font> fnt=new GL::Font;
37         DataFile::load<GL::Font, Resources &>(*fnt, (path/(name+".font")).str(), *this);
38         if(!default_font)
39                 default_font=fnt.get();
40         return fnt.release();
41 }
42
43 GL::Texture2D *Resources::create_texture(const string &name)
44 {
45         RefPtr<GL::Texture2D> tex=new GL::Texture2D;
46         tex->load_image((path/(name+".png")).str());
47         tex->set_min_filter(GL::LINEAR);
48         return tex.release();
49 }
50
51
52 Resources::Loader::Loader(Resources &r):
53         Collection::Loader(r),
54         res(r)
55 {
56         add("default_font", &Loader::default_font);
57         add("font", &Loader::font);
58 }
59
60 void Resources::Loader::default_font(const string &name)
61 {
62         res.default_font=res.get<GL::Font>(name);
63 }
64
65 void Resources::Loader::font(const string &name)
66 {
67         RefPtr<GL::Font> fnt=new GL::Font;
68         load_sub(*fnt);
69         res.add(name, fnt.get());
70         if(!res.default_font)
71                 res.default_font=fnt.get();
72         fnt.release();
73 }
74
75 } // namespace GLtk
76 } // namespace Msp