]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Add a persistent view size attribute to List
[libs/gltk.git] / source / resources.cpp
index b7586e886ee41df0eb711230c1ebe4bfcba8402c..8cdd0ad8f0b12066adf3b3dfeed9c2029ec304ee 100644 (file)
@@ -6,12 +6,35 @@ using namespace std;
 namespace Msp {
 namespace GLtk {
 
-Resources::Resources():
-       default_font(0)
+Resources::Resources()
 {
+       init();
+}
+
+Resources::Resources(const FS::Path &fn)
+{
+       init();
+
+       dir_src = new DataFile::DirectorySource;
+       dir_src->add_directory(FS::dirname(fn));
+       add_source(*dir_src);
+
+       DataFile::load(*this, fn.str());
+}
+
+void Resources::init()
+{
+       default_font = 0;
+       dir_src = 0;
        add_type<Graphic>().keyword("graphic");
-       add_type<GL::Texture2D>().keyword("texture");
+       add_type<GL::Texture2D>().keyword("texture").creator(&Resources::create_texture);
        add_type<GL::Font>().keyword("font");
+       add_type<Style>().keyword("style");
+}
+
+Resources::~Resources()
+{
+       delete dir_src;
 }
 
 const GL::Font &Resources::get_default_font() const
@@ -22,6 +45,25 @@ const GL::Font &Resources::get_default_font() const
        return *default_font;
 }
 
+GL::Texture2D *Resources::create_texture(const string &name)
+{
+       string ext = FS::extpart(name);
+       if(ext==".png" || ext==".jpg")
+               if(IO::Seekable *io = open_from_sources(name))
+               {
+                       Graphics::Image image;
+                       image.load_io(*io);
+                       delete io;
+
+                       GL::Texture2D *tex = new GL::Texture2D;
+                       tex->set_min_filter(GL::LINEAR);
+                       tex->image(image);
+                       return tex;
+               }
+
+       return 0;
+}
+
 
 Resources::Loader::Loader(Resources &r):
        Collection::Loader(r),
@@ -29,7 +71,6 @@ 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)
@@ -47,13 +88,5 @@ void Resources::Loader::font(const string &name)
        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