]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Enable loading of entry widgets from datafiles
[libs/gltk.git] / source / resources.cpp
1 /* $Id$
2
3 This file is part of libmspgltk
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/core/except.h>
9 #include "resources.h"
10
11 using namespace std;
12
13 namespace Msp {
14 namespace GLtk {
15
16 Resources::Resources():
17         path("."),
18         default_font(0)
19 {
20         add_keyword<Graphic>("graphic");
21
22         add_creator(&Resources::create_font);
23         add_creator(&Resources::create_texture);
24 }
25
26 void Resources::set_path(const Path &p)
27 {
28         path=p;
29 }
30
31 const GL::Font &Resources::get_default_font() const
32 {
33         if(!default_font)
34                 throw InvalidState("No default font");
35
36         return *default_font;
37 }
38
39 GL::Font *Resources::create_font(const string &name)
40 {
41         RefPtr<GL::Font> fnt=new GL::Font;
42         DataFile::load<GL::Font, Resources &>(*fnt, (path/name).str(), *this);
43         if(!default_font)
44                 default_font=fnt.get();
45         return fnt.release();
46 }
47
48 GL::Texture2D *Resources::create_texture(const string &name)
49 {
50         RefPtr<GL::Texture2D> tex=new GL::Texture2D;
51         tex->load_image((path/name).str());
52         tex->set_min_filter(GL::LINEAR);
53         return tex.release();
54 }
55
56
57 Resources::Loader::Loader(Resources &r):
58         Collection::Loader(r),
59         res(r)
60 {
61         add("default_font", &Loader::default_font);
62         add("font", &Loader::font);
63         add("style", &Loader::style);
64 }
65
66 void Resources::Loader::default_font(const string &name)
67 {
68         res.default_font=res.get<GL::Font>(name);
69 }
70
71 void Resources::Loader::font(const string &name)
72 {
73         RefPtr<GL::Font> fnt=new GL::Font;
74         load_sub(*fnt);
75         res.add(name, fnt.get());
76         if(!res.default_font)
77                 res.default_font=fnt.get();
78         fnt.release();
79 }
80
81 void Resources::Loader::style(const string &name)
82 {
83         RefPtr<Style> stl=new Style(res);
84         load_sub(*stl, res);
85         res.add(name, stl.get());
86         stl.release();
87 }
88
89 } // namespace GLtk
90 } // namespace Msp