]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
650140ba4552f59b066f00cc5677b5d34dfd9734
[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(*fnt, (path/(name+".font")).str());
38         return fnt.release();
39 }
40
41 GL::Texture2D *Resources::create_texture(const string &name)
42 {
43         RefPtr<GL::Texture2D> tex=new GL::Texture2D;
44         tex->image((path/(name+".png")).str());
45         tex->set_min_filter(GL::LINEAR);
46         return tex.release();
47 }
48
49
50 Resources::Loader::Loader(Resources &r):
51         Collection::Loader(r),
52         res(r)
53 {
54         add("font", &Loader::font);
55 }
56
57 void Resources::Loader::font(const string &name)
58 {
59         RefPtr<GL::Font> fnt=new GL::Font;
60         load_sub(*fnt);
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