]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Fix incorrect usage of DataFile::load
[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         add_keyword<GL::Texture2D>("texture");
22
23         add_creator(&Resources::create_font);
24         add_creator(&Resources::create_texture);
25 }
26
27 void Resources::set_path(const FS::Path &p)
28 {
29         path=p;
30 }
31
32 const GL::Font &Resources::get_default_font() const
33 {
34         if(!default_font)
35                 throw InvalidState("No default font");
36
37         return *default_font;
38 }
39
40 GL::Font *Resources::create_font(const string &name)
41 {
42         RefPtr<GL::Font> fnt=new GL::Font;
43         DataFile::load(*fnt, (path/name).str(), *this);
44         if(!default_font)
45                 default_font=fnt.get();
46         return fnt.release();
47 }
48
49 GL::Texture2D *Resources::create_texture(const string &name)
50 {
51         RefPtr<GL::Texture2D> tex=new GL::Texture2D;
52         tex->load_image((path/name).str());
53         tex->set_min_filter(GL::LINEAR);
54         return tex.release();
55 }
56
57
58 Resources::Loader::Loader(Resources &r):
59         Collection::Loader(r),
60         res(r)
61 {
62         add("default_font", &Loader::default_font);
63         add("font", &Loader::font);
64         add("style", &Loader::style);
65 }
66
67 void Resources::Loader::default_font(const string &name)
68 {
69         res.default_font=res.get<GL::Font>(name);
70 }
71
72 void Resources::Loader::font(const string &name)
73 {
74         RefPtr<GL::Font> fnt=new GL::Font;
75         load_sub(*fnt, res);
76         res.add(name, fnt.get());
77         if(!res.default_font)
78                 res.default_font=fnt.get();
79         fnt.release();
80 }
81
82 void Resources::Loader::style(const string &name)
83 {
84         RefPtr<Style> stl=new Style(res);
85         load_sub(*stl, res);
86         res.add(name, stl.get());
87         stl.release();
88 }
89
90 } // namespace GLtk
91 } // namespace Msp