]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / resources.cpp
index 01426a20ced22d8911c587ab21338201c8d02c1d..dc678a9480d74aa276039d65b5b0b5cf1c0ed8d5 100644 (file)
@@ -1,11 +1,6 @@
-/* $Id$
-
-This file is part of libmspgltk
-Copyright © 2007  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
-#include <msp/core/except.h>
+#include <msp/fs/utils.h>
+#include <msp/strings/regex.h>
+#include <msp/strings/utils.h>
 #include "resources.h"
 
 using namespace std;
@@ -13,45 +8,92 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Resources::Resources():
-       path("."),
-       default_font(0)
+Resources::Resources()
 {
-       add_keyword<Graphic>("graphic");
-       add_keyword<GL::Texture2D>("texture");
-
-       add_creator(&Resources::create_font);
-       add_creator(&Resources::create_texture);
+       add_type<Graphic>().keyword("graphic");
+       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");
 }
 
-void Resources::set_path(const Path &p)
+Resources::Resources(const FS::Path &fn):
+       Resources()
 {
-       path=p;
+       dir_src = make_unique<DataFile::DirectorySource>();
+       dir_src->add_directory(FS::dirname(fn));
+       add_source(*dir_src);
+
+       DataFile::load(*this, fn.str());
 }
 
 const GL::Font &Resources::get_default_font() const
 {
        if(!default_font)
-               throw InvalidState("No default font");
+               throw logic_error("!default_font");
 
        return *default_font;
 }
 
-GL::Font *Resources::create_font(const string &name)
+GL::Module *Resources::create_module(const string &name)
 {
-       RefPtr<GL::Font> fnt=new GL::Font;
-       DataFile::load<GL::Font, Resources &>(*fnt, (path/name).str(), *this);
-       if(!default_font)
-               default_font=fnt.get();
-       return fnt.release();
+       if(name!="ui.glsl")
+               return nullptr;
+
+       unique_ptr<GL::Module> mod;
+       if(GL::get_backend_api()==GL::VULKAN)
+               mod = make_unique<GL::SpirVModule>();
+       else
+               mod = make_unique<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.release();
+}
+
+GL::Program *Resources::create_program(const string &name)
+{
+       if(name!="ui.shader")
+               return nullptr;
+
+       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))
+       {
+               unique_ptr<GL::Sampler> sampler = make_unique<GL::Sampler>();
+               sampler->set_filter(lexical_cast<GL::TextureFilter>(toupper(m.group(1).str)));
+               sampler->set_wrap(GL::CLAMP_TO_EDGE);
+               return sampler.release();
+       }
+
+       return nullptr;
 }
 
 GL::Texture2D *Resources::create_texture(const string &name)
 {
-       RefPtr<GL::Texture2D> tex=new GL::Texture2D;
-       tex->load_image((path/name).str());
-       tex->set_min_filter(GL::LINEAR);
-       return tex.release();
+       string ext = FS::extpart(name);
+       if(ext==".png" || ext==".jpg")
+               if(auto io = unique_ptr<IO::Seekable>(open_raw(name)))
+               {
+                       Graphics::Image image;
+                       image.load_io(*io);
+                       io.reset();
+
+                       unique_ptr<GL::Texture2D> tex = make_unique<GL::Texture2D>();
+                       tex->image(image);
+                       return tex.release();
+               }
+
+       return nullptr;
 }
 
 
@@ -61,31 +103,22 @@ Resources::Loader::Loader(Resources &r):
 {
        add("default_font", &Loader::default_font);
        add("font", &Loader::font);
-       add("style", &Loader::style);
 }
 
 void Resources::Loader::default_font(const string &name)
 {
-       res.default_font=res.get<GL::Font>(name);
+       res.default_font = &res.get<GL::Font>(name);
 }
 
 void Resources::Loader::font(const string &name)
 {
-       RefPtr<GL::Font> fnt=new GL::Font;
+       unique_ptr<GL::Font> fnt = make_unique<GL::Font>();
        load_sub(*fnt, res);
        res.add(name, fnt.get());
        if(!res.default_font)
-               res.default_font=fnt.get();
+               res.default_font = fnt.get();
        fnt.release();
 }
 
-void Resources::Loader::style(const string &name)
-{
-       RefPtr<Style> stl=new Style(res);
-       load_sub(*stl, res);
-       res.add(name, stl.get());
-       stl.release();
-}
-
 } // namespace GLtk
 } // namespace Msp