1 #include <msp/fs/utils.h>
2 #include <msp/strings/regex.h>
3 #include <msp/strings/utils.h>
11 Resources::Resources()
16 Resources::Resources(const FS::Path &fn)
20 dir_src = new DataFile::DirectorySource;
21 dir_src->add_directory(FS::dirname(fn));
24 DataFile::load(*this, fn.str());
27 void Resources::init()
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");
40 Resources::~Resources()
45 const GL::Font &Resources::get_default_font() const
48 throw logic_error("!default_font");
53 GL::Module *Resources::create_module(const string &name)
59 if(GL::get_backend_api()==GL::VULKAN)
60 mod = new GL::SpirVModule;
62 mod = new GL::GlslModule;
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");
72 GL::Program *Resources::create_program(const string &name)
77 return new GL::Program(get<GL::Module>("ui.glsl"));
80 GL::Sampler *Resources::create_sampler(const string &name)
82 static const Regex r_name("^(linear|nearest)_clamp.samp$");
83 if(RegMatch m = r_name.match(name))
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);
94 GL::Texture2D *Resources::create_texture(const string &name)
96 string ext = FS::extpart(name);
97 if(ext==".png" || ext==".jpg")
98 if(IO::Seekable *io = open_raw(name))
100 Graphics::Image image;
104 GL::Texture2D *tex = new GL::Texture2D;
113 Resources::Loader::Loader(Resources &r):
114 Collection::Loader(r),
117 add("default_font", &Loader::default_font);
118 add("font", &Loader::font);
121 void Resources::Loader::default_font(const string &name)
123 res.default_font = &res.get<GL::Font>(name);
126 void Resources::Loader::font(const string &name)
128 RefPtr<GL::Font> fnt = new GL::Font;
130 res.add(name, fnt.get());
131 if(!res.default_font)
132 res.default_font = fnt.get();