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