]> git.tdb.fi Git - libs/gltk.git/blob - source/resources.cpp
Use std::unique_ptr for managing memory
[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 = make_unique<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 const GL::Font &Resources::get_default_font() const
33 {
34         if(!default_font)
35                 throw logic_error("!default_font");
36
37         return *default_font;
38 }
39
40 GL::Module *Resources::create_module(const string &name)
41 {
42         if(name!="ui.glsl")
43                 return nullptr;
44
45         unique_ptr<GL::Module> mod;
46         if(GL::get_backend_api()==GL::VULKAN)
47                 mod = make_unique<GL::SpirVModule>();
48         else
49                 mod = make_unique<GL::GlslModule>();
50
51         mod->set_source("import msp_interface; import common;\n"
52                 "uniform sampler2D ui_tex;\n"
53                 "#pragma MSP stage(fragment)\n"
54                 "void main() { frag_color = texture(ui_tex, texcoord.xy)*color; }\n");
55
56         return mod.release();
57 }
58
59 GL::Program *Resources::create_program(const string &name)
60 {
61         if(name!="ui.shader")
62                 return nullptr;
63
64         return new GL::Program(get<GL::Module>("ui.glsl"));
65 }
66
67 GL::Sampler *Resources::create_sampler(const string &name)
68 {
69         static const Regex r_name("^(linear|nearest)_clamp.samp$");
70         if(RegMatch m = r_name.match(name))
71         {
72                 unique_ptr<GL::Sampler> sampler = make_unique<GL::Sampler>();
73                 sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
74                 sampler->set_wrap(GL::CLAMP_TO_EDGE);
75                 return sampler.release();
76         }
77
78         return nullptr;
79 }
80
81 GL::Texture2D *Resources::create_texture(const string &name)
82 {
83         string ext = FS::extpart(name);
84         if(ext==".png" || ext==".jpg")
85                 if(auto io = unique_ptr<IO::Seekable>(open_raw(name)))
86                 {
87                         Graphics::Image image;
88                         image.load_io(*io);
89                         io.reset();
90
91                         unique_ptr<GL::Texture2D> tex = make_unique<GL::Texture2D>();
92                         tex->image(image);
93                         return tex.release();
94                 }
95
96         return nullptr;
97 }
98
99
100 Resources::Loader::Loader(Resources &r):
101         Collection::Loader(r),
102         res(r)
103 {
104         add("default_font", &Loader::default_font);
105         add("font", &Loader::font);
106 }
107
108 void Resources::Loader::default_font(const string &name)
109 {
110         res.default_font = &res.get<GL::Font>(name);
111 }
112
113 void Resources::Loader::font(const string &name)
114 {
115         unique_ptr<GL::Font> fnt = make_unique<GL::Font>();
116         load_sub(*fnt, res);
117         res.add(name, fnt.get());
118         if(!res.default_font)
119                 res.default_font = fnt.get();
120         fnt.release();
121 }
122
123 } // namespace GLtk
124 } // namespace Msp