]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Rework exceptions and use maputils
[libs/gltk.git] / source / resources.cpp
index a61566ae2e8556e0bb5327a498fa08dbef183dae..869885fecd97e77ae3719399c8fd53d6690baef7 100644 (file)
@@ -1,4 +1,4 @@
-#include <msp/core/error.h>
+#include <msp/fs/utils.h>
 #include "resources.h"
 
 using namespace std;
@@ -7,115 +7,93 @@ namespace Msp {
 namespace GLtk {
 
 Resources::Resources():
+       path("."),
        default_font(0)
-{ }
-
-Resources::~Resources()
 {
-       for(FontMap::iterator i=fonts.begin(); i!=fonts.end(); ++i)
-               delete i->second;
-       for(TextureMap::iterator i=textures.begin(); i!=textures.end(); ++i)
-               delete i->second;
+       init();
 }
 
-const GL::Font &Resources::get_font(const string &name) const
+Resources::Resources(const FS::Path &fn):
+       path(FS::dirname(fn)),
+       default_font(0)
 {
-       FontMap::const_iterator i=fonts.find(name);
-       if(i==fonts.end())
-               throw KeyError("Unknown font "+name);
+       init();
 
-       return *i->second;
+       DataFile::load(*this, fn.str());
 }
 
-const GL::Font &Resources::get_default_font() const
+void Resources::init()
 {
-       if(!default_font)
-               throw InvalidState("No default font");
+       add_keyword<Graphic>("graphic");
+       add_keyword<GL::Texture2D>("texture");
 
-       return *default_font;
+       add_creator(&Resources::create_font);
+       add_creator(&Resources::create_texture);
 }
 
-const GL::Texture2D &Resources::get_texture(const string &name) const
+void Resources::set_path(const FS::Path &p)
 {
-       TextureMap::const_iterator i=textures.find(name);
-       if(i==textures.end())
-               throw KeyError("Unknown texture "+name);
-
-       return *i->second;
+       /* XXX bad, should change Collection API to allow creators to form paths
+       relative to the datafile location */
+       path = p;
 }
 
-const Graphic &Resources::get_graphic(const string &name) const
+const GL::Font &Resources::get_default_font() const
 {
-       GraphicMap::const_iterator i=graphics.find(name);
-       if(i==graphics.end())
-               throw KeyError("Unknown graphic "+name);
+       if(!default_font)
+               throw logic_error("!default_font");
 
-       return i->second;
+       return *default_font;
 }
 
-const Style &Resources::get_style(const string &wdg, const string &name) const
+GL::Font *Resources::create_font(const string &name)
 {
-       StyleMap::const_iterator i=styles.find(StyleId(wdg, name));
-       if(i==styles.end())
-               throw KeyError("Unknown style "+name+" for widget "+wdg);
-
-       return i->second;
+       RefPtr<GL::Font> fnt = new GL::Font;
+       DataFile::load(*fnt, (path/name).str(), *this);
+       if(!default_font)
+               default_font = fnt.get();
+       return fnt.release();
 }
 
-
-Resources::Loader::Loader(Resources &r):
-       res(r)
+GL::Texture2D *Resources::create_texture(const string &name)
 {
-       add("font",    &Loader::font);
-       add("texture", &Loader::texture);
-       add("graphic", &Loader::graphic);
-       add("style",   &Loader::style);
+       RefPtr<GL::Texture2D> tex = new GL::Texture2D;
+       tex->load_image((path/name).str());
+       tex->set_min_filter(GL::LINEAR);
+       return tex.release();
 }
 
-void Resources::Loader::font(const string &fn)
-{
-       RefPtr<GL::Font> fnt=new GL::Font;
-       Parser::load(*fnt, fn);
 
-       res.fonts.insert(FontMap::value_type(fn.substr(0, fn.rfind('.')), fnt.get()));
-       if(!res.default_font)
-               res.default_font=fnt.get();
-       fnt.release();
-}
-
-void Resources::Loader::texture(const string &fn)
+Resources::Loader::Loader(Resources &r):
+       Collection::Loader(r),
+       res(r)
 {
-       RefPtr<GL::Texture2D> tex=new GL::Texture2D;
-       tex->image(fn);
-       tex->set_min_filter(GL::LINEAR);
-
-       res.textures.insert(TextureMap::value_type(fn.substr(0, fn.rfind('.')), tex.release()));
+       add("default_font", &Loader::default_font);
+       add("font", &Loader::font);
+       add("style", &Loader::style);
 }
 
-void Resources::Loader::graphic(const std::string &n)
+void Resources::Loader::default_font(const string &name)
 {
-       Graphic graph(res, n);
-       load_sub(graph);
-       if(!graph.get_texture())
-               throw Exception("Graphic without texture");
-
-       res.graphics.insert(GraphicMap::value_type(n, graph));
+       res.default_font = res.get<GL::Font>(name);
 }
 
-void Resources::Loader::style(const string &w, const string &n)
+void Resources::Loader::font(const string &name)
 {
-       Style stl(res, w, n);
-       load_sub(stl);
-
-       res.styles.insert(StyleMap::value_type(StyleId(w, n), stl));
+       RefPtr<GL::Font> fnt = new GL::Font;
+       load_sub(*fnt, res);
+       res.add(name, fnt.get());
+       if(!res.default_font)
+               res.default_font = fnt.get();
+       fnt.release();
 }
 
-
-bool Resources::StyleId::operator<(const StyleId &other) const
+void Resources::Loader::style(const string &name)
 {
-       if(widget<other.widget)
-               return true;
-       return widget==other.widget && name<other.name;
+       RefPtr<Style> stl = new Style(res);
+       load_sub(*stl, res);
+       res.add(name, stl.get());
+       stl.release();
 }
 
 } // namespace GLtk