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