1 #include <msp/fs/utils.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
11 Resources::Resources()
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");
22 Resources::Resources(const FS::Path &fn):
25 dir_src = make_unique<DataFile::DirectorySource>();
26 dir_src->add_directory(FS::dirname(fn));
29 DataFile::load(*this, fn.str());
32 const GL::Font &Resources::get_default_font() const
35 throw logic_error("!default_font");
40 GL::Module *Resources::create_module(const string &name)
45 unique_ptr<GL::Module> mod;
46 if(GL::get_backend_api()==GL::VULKAN)
47 mod = make_unique<GL::SpirVModule>();
49 mod = make_unique<GL::GlslModule>();
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");
59 GL::Program *Resources::create_program(const string &name)
64 return new GL::Program(get<GL::Module>("ui.glsl"));
67 GL::Sampler *Resources::create_sampler(const string &name)
69 static const Regex r_name("^(linear|nearest)_clamp.samp$");
70 if(RegMatch m = r_name.match(name))
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();
81 GL::Texture2D *Resources::create_texture(const string &name)
83 string ext = FS::extpart(name);
84 if(ext==".png" || ext==".jpg")
85 if(auto io = unique_ptr<IO::Seekable>(open_raw(name)))
87 Graphics::Image image;
91 unique_ptr<GL::Texture2D> tex = make_unique<GL::Texture2D>();
100 Resources::Loader::Loader(Resources &r):
101 Collection::Loader(r),
104 add("default_font", &Loader::default_font);
105 add("font", &Loader::font);
108 void Resources::Loader::default_font(const string &name)
110 res.default_font = &res.get<GL::Font>(name);
113 void Resources::Loader::font(const string &name)
115 unique_ptr<GL::Font> fnt = make_unique<GL::Font>();
117 res.add(name, fnt.get());
118 if(!res.default_font)
119 res.default_font = fnt.get();