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