]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Rework exceptions and use maputils
[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         path("."),
11         default_font(0)
12 {
13         init();
14 }
15
16 Resources::Resources(const FS::Path &fn):
17         path(FS::dirname(fn)),
18         default_font(0)
19 {
20         init();
21
22         DataFile::load(*this, fn.str());
23 }
24
25 void Resources::init()
26 {
27         add_keyword<Graphic>("graphic");
28         add_keyword<GL::Texture2D>("texture");
29
30         add_creator(&Resources::create_font);
31         add_creator(&Resources::create_texture);
32 }
33
34 void Resources::set_path(const FS::Path &p)
35 {
36         /* XXX bad, should change Collection API to allow creators to form paths
37         relative to the datafile location */
38         path = p;
39 }
40
41 const GL::Font &Resources::get_default_font() const
42 {
43         if(!default_font)
44                 throw logic_error("!default_font");
45
46         return *default_font;
47 }
48
49 GL::Font *Resources::create_font(const string &name)
50 {
51         RefPtr<GL::Font> fnt = new GL::Font;
52         DataFile::load(*fnt, (path/name).str(), *this);
53         if(!default_font)
54                 default_font = fnt.get();
55         return fnt.release();
56 }
57
58 GL::Texture2D *Resources::create_texture(const string &name)
59 {
60         RefPtr<GL::Texture2D> tex = new GL::Texture2D;
61         tex->load_image((path/name).str());
62         tex->set_min_filter(GL::LINEAR);
63         return tex.release();
64 }
65
66
67 Resources::Loader::Loader(Resources &r):
68         Collection::Loader(r),
69         res(r)
70 {
71         add("default_font", &Loader::default_font);
72         add("font", &Loader::font);
73         add("style", &Loader::style);
74 }
75
76 void Resources::Loader::default_font(const string &name)
77 {
78         res.default_font = res.get<GL::Font>(name);
79 }
80
81 void Resources::Loader::font(const string &name)
82 {
83         RefPtr<GL::Font> fnt = new GL::Font;
84         load_sub(*fnt, res);
85         res.add(name, fnt.get());
86         if(!res.default_font)
87                 res.default_font = fnt.get();
88         fnt.release();
89 }
90
91 void Resources::Loader::style(const string &name)
92 {
93         RefPtr<Style> stl = new Style(res);
94         load_sub(*stl, res);
95         res.add(name, stl.get());
96         stl.release();
97 }
98
99 } // namespace GLtk
100 } // namespace Msp