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 = new DataFile::DirectorySource;
26 dir_src->add_directory(FS::dirname(fn));
29 DataFile::load(*this, fn.str());
32 Resources::~Resources()
37 const GL::Font &Resources::get_default_font() const
40 throw logic_error("!default_font");
45 GL::Module *Resources::create_module(const string &name)
51 if(GL::get_backend_api()==GL::VULKAN)
52 mod = new GL::SpirVModule;
54 mod = new GL::GlslModule;
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");
64 GL::Program *Resources::create_program(const string &name)
69 return new GL::Program(get<GL::Module>("ui.glsl"));
72 GL::Sampler *Resources::create_sampler(const string &name)
74 static const Regex r_name("^(linear|nearest)_clamp.samp$");
75 if(RegMatch m = r_name.match(name))
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);
86 GL::Texture2D *Resources::create_texture(const string &name)
88 string ext = FS::extpart(name);
89 if(ext==".png" || ext==".jpg")
90 if(IO::Seekable *io = open_raw(name))
92 Graphics::Image image;
96 GL::Texture2D *tex = new GL::Texture2D;
105 Resources::Loader::Loader(Resources &r):
106 Collection::Loader(r),
109 add("default_font", &Loader::default_font);
110 add("font", &Loader::font);
113 void Resources::Loader::default_font(const string &name)
115 res.default_font = &res.get<GL::Font>(name);
118 void Resources::Loader::font(const string &name)
120 RefPtr<GL::Font> fnt = new GL::Font;
122 res.add(name, fnt.get());
123 if(!res.default_font)
124 res.default_font = fnt.get();