]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Adjust things to conform to changes in other libraries
[libs/gltk.git] / source / resources.cpp
1 #include <msp/fs/utils.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
4 #include "resources.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GLtk {
10
11 Resources::Resources()
12 {
13         init();
14 }
15
16 Resources::Resources(const FS::Path &fn)
17 {
18         init();
19
20         dir_src = new DataFile::DirectorySource;
21         dir_src->add_directory(FS::dirname(fn));
22         add_source(*dir_src);
23
24         DataFile::load(*this, fn.str());
25 }
26
27 void Resources::init()
28 {
29         default_font = 0;
30         dir_src = 0;
31         add_type<Graphic>().keyword("graphic");
32         add_type<GL::Module>().creator([this](const string &n){ return create_module(n); });
33         add_type<GL::Sampler>().creator([this](const string &n){ return create_sampler(n); });
34         add_type<GL::Program>().creator([this](const string &n){ return create_program(n); });
35         add_type<GL::Texture2D>().keyword("texture").creator([this](const string &n){ return create_texture(n); });
36         add_type<GL::Font>().keyword("font");
37         add_type<Style>().keyword("style");
38 }
39
40 Resources::~Resources()
41 {
42         delete dir_src;
43 }
44
45 const GL::Font &Resources::get_default_font() const
46 {
47         if(!default_font)
48                 throw logic_error("!default_font");
49
50         return *default_font;
51 }
52
53 GL::Module *Resources::create_module(const string &name)
54 {
55         if(name!="ui.glsl")
56                 return 0;
57
58         GL::Module *mod = 0;
59         if(GL::get_backend_api()==GL::VULKAN)
60                 mod = new GL::SpirVModule;
61         else
62                 mod = new GL::GlslModule;
63
64         mod->set_source("import msp_interface; import common;\n"
65                 "uniform sampler2D ui_tex;\n"
66                 "#pragma MSP stage(fragment)\n"
67                 "void main() { frag_color = texture(ui_tex, texcoord.xy)*color; }\n");
68
69         return mod;
70 }
71
72 GL::Program *Resources::create_program(const string &name)
73 {
74         if(name!="ui.shader")
75                 return 0;
76
77         return new GL::Program(get<GL::Module>("ui.glsl"));
78 }
79
80 GL::Sampler *Resources::create_sampler(const string &name)
81 {
82         static const Regex r_name("^(linear|nearest)_clamp.samp$");
83         if(RegMatch m = r_name.match(name))
84         {
85                 GL::Sampler *sampler = new GL::Sampler;
86                 sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
87                 sampler->set_wrap(GL::CLAMP_TO_EDGE);
88                 return sampler;
89         }
90
91         return 0;
92 }
93
94 GL::Texture2D *Resources::create_texture(const string &name)
95 {
96         string ext = FS::extpart(name);
97         if(ext==".png" || ext==".jpg")
98                 if(IO::Seekable *io = open_raw(name))
99                 {
100                         Graphics::Image image;
101                         image.load_io(*io);
102                         delete io;
103
104                         GL::Texture2D *tex = new GL::Texture2D;
105                         tex->image(image);
106                         return tex;
107                 }
108
109         return 0;
110 }
111
112
113 Resources::Loader::Loader(Resources &r):
114         Collection::Loader(r),
115         res(r)
116 {
117         add("default_font", &Loader::default_font);
118         add("font", &Loader::font);
119 }
120
121 void Resources::Loader::default_font(const string &name)
122 {
123         res.default_font = &res.get<GL::Font>(name);
124 }
125
126 void Resources::Loader::font(const string &name)
127 {
128         RefPtr<GL::Font> fnt = new GL::Font;
129         load_sub(*fnt, res);
130         res.add(name, fnt.get());
131         if(!res.default_font)
132                 res.default_font = fnt.get();
133         fnt.release();
134 }
135
136 } // namespace GLtk
137 } // namespace Msp