]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Change mspparser -> mspdatafile
[libs/gltk.git] / source / resources.cpp
1 #include <msp/core/error.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
13 Resources::~Resources()
14 {
15         for(FontMap::iterator i=fonts.begin(); i!=fonts.end(); ++i)
16                 delete i->second;
17         for(TextureMap::iterator i=textures.begin(); i!=textures.end(); ++i)
18                 delete i->second;
19 }
20
21 const GL::Font &Resources::get_font(const string &name) const
22 {
23         FontMap::const_iterator i=fonts.find(name);
24         if(i==fonts.end())
25                 throw KeyError("Unknown font "+name);
26
27         return *i->second;
28 }
29
30 const GL::Font &Resources::get_default_font() const
31 {
32         if(!default_font)
33                 throw InvalidState("No default font");
34
35         return *default_font;
36 }
37
38 const GL::Texture2D &Resources::get_texture(const string &name) const
39 {
40         TextureMap::const_iterator i=textures.find(name);
41         if(i==textures.end())
42                 throw KeyError("Unknown texture "+name);
43
44         return *i->second;
45 }
46
47 const Graphic &Resources::get_graphic(const string &name) const
48 {
49         GraphicMap::const_iterator i=graphics.find(name);
50         if(i==graphics.end())
51                 throw KeyError("Unknown graphic "+name);
52
53         return i->second;
54 }
55
56 const Style &Resources::get_style(const string &wdg, const string &name) const
57 {
58         StyleMap::const_iterator i=styles.find(StyleId(wdg, name));
59         if(i==styles.end())
60                 throw KeyError("Unknown style "+name+" for widget "+wdg);
61
62         return i->second;
63 }
64
65
66 Resources::Loader::Loader(Resources &r):
67         res(r)
68 {
69         add("font",    &Loader::font);
70         add("texture", &Loader::texture);
71         add("graphic", &Loader::graphic);
72         add("style",   &Loader::style);
73 }
74
75 void Resources::Loader::font(const string &fn)
76 {
77         RefPtr<GL::Font> fnt=new GL::Font;
78         DataFile::load(*fnt, fn);
79
80         res.fonts.insert(FontMap::value_type(fn.substr(0, fn.rfind('.')), fnt.get()));
81         if(!res.default_font)
82                 res.default_font=fnt.get();
83         fnt.release();
84 }
85
86 void Resources::Loader::texture(const string &fn)
87 {
88         RefPtr<GL::Texture2D> tex=new GL::Texture2D;
89         tex->image(fn);
90         tex->set_min_filter(GL::LINEAR);
91
92         res.textures.insert(TextureMap::value_type(fn.substr(0, fn.rfind('.')), tex.release()));
93 }
94
95 void Resources::Loader::graphic(const std::string &n)
96 {
97         Graphic graph(res, n);
98         load_sub(graph);
99         if(!graph.get_texture())
100                 throw Exception("Graphic without texture");
101
102         res.graphics.insert(GraphicMap::value_type(n, graph));
103 }
104
105 void Resources::Loader::style(const string &w, const string &n)
106 {
107         Style stl(res, w, n);
108         load_sub(stl);
109
110         res.styles.insert(StyleMap::value_type(StyleId(w, n), stl));
111 }
112
113
114 bool Resources::StyleId::operator<(const StyleId &other) const
115 {
116         if(widget<other.widget)
117                 return true;
118         return widget==other.widget && name<other.name;
119 }
120
121 } // namespace GLtk
122 } // namespace Msp