]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Adjust things to conform to changes in other libraries
[libs/gltk.git] / source / resources.cpp
index 36be79e7914272d76a30c4c5af904c8fa5dc6cee..06fa44cf0c502867a1ecb255bea5de9251cc6314 100644 (file)
@@ -1,4 +1,6 @@
 #include <msp/fs/utils.h>
+#include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
 #include "resources.h"
 
 using namespace std;
@@ -27,7 +29,10 @@ void Resources::init()
        default_font = 0;
        dir_src = 0;
        add_type<Graphic>().keyword("graphic");
-       add_type<GL::Texture2D>().keyword("texture").creator(&Resources::create_texture);
+       add_type<GL::Module>().creator([this](const string &n){ return create_module(n); });
+       add_type<GL::Sampler>().creator([this](const string &n){ return create_sampler(n); });
+       add_type<GL::Program>().creator([this](const string &n){ return create_program(n); });
+       add_type<GL::Texture2D>().keyword("texture").creator([this](const string &n){ return create_texture(n); });
        add_type<GL::Font>().keyword("font");
        add_type<Style>().keyword("style");
 }
@@ -45,21 +50,63 @@ const GL::Font &Resources::get_default_font() const
        return *default_font;
 }
 
+GL::Module *Resources::create_module(const string &name)
+{
+       if(name!="ui.glsl")
+               return 0;
+
+       GL::Module *mod = 0;
+       if(GL::get_backend_api()==GL::VULKAN)
+               mod = new GL::SpirVModule;
+       else
+               mod = new GL::GlslModule;
+
+       mod->set_source("import msp_interface; import common;\n"
+               "uniform sampler2D ui_tex;\n"
+               "#pragma MSP stage(fragment)\n"
+               "void main() { frag_color = texture(ui_tex, texcoord.xy)*color; }\n");
+
+       return mod;
+}
+
+GL::Program *Resources::create_program(const string &name)
+{
+       if(name!="ui.shader")
+               return 0;
+
+       return new GL::Program(get<GL::Module>("ui.glsl"));
+}
+
+GL::Sampler *Resources::create_sampler(const string &name)
+{
+       static const Regex r_name("^(linear|nearest)_clamp.samp$");
+       if(RegMatch m = r_name.match(name))
+       {
+               GL::Sampler *sampler = new GL::Sampler;
+               sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
+               sampler->set_wrap(GL::CLAMP_TO_EDGE);
+               return sampler;
+       }
+
+       return 0;
+}
+
 GL::Texture2D *Resources::create_texture(const string &name)
 {
        string ext = FS::extpart(name);
        if(ext==".png" || ext==".jpg")
-       {
-               IO::Seekable *io = open_from_sources(name);
-               Graphics::Image image;
-               image.load_io(*io);
-               GL::Texture2D *tex = new GL::Texture2D;
-               tex->set_min_filter(GL::LINEAR);
-               tex->image(image);
-               return tex;
-       }
-       else
-               return 0;
+               if(IO::Seekable *io = open_raw(name))
+               {
+                       Graphics::Image image;
+                       image.load_io(*io);
+                       delete io;
+
+                       GL::Texture2D *tex = new GL::Texture2D;
+                       tex->image(image);
+                       return tex;
+               }
+
+       return 0;
 }