X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresources.cpp;h=2cfe022c1a1d74d5035aa855922dcc7ba9e5f0c8;hb=ed9873ba7ee862ad76937f579fe371c1a27d5715;hp=aad671c393729adc4608d3d6024378bcf44bbe35;hpb=131ac8ff2c06f94d40f4bf98d4a6ec0d113cdffc;p=libs%2Fgltk.git diff --git a/source/resources.cpp b/source/resources.cpp index aad671c..2cfe022 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -1,4 +1,11 @@ -#include +/* $Id$ + +This file is part of libmspgltk +Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Distributed under the LGPL +*/ + +#include #include "resources.h" using namespace std; @@ -7,24 +14,18 @@ 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; + add_keyword("graphic"); + + add_creator(&Resources::create_font); + add_creator(&Resources::create_texture); } -const GL::Font &Resources::get_font(const string &name) const +void Resources::set_path(const Path &p) { - FontMap::const_iterator i=fonts.find(name); - if(i==fonts.end()) - throw KeyError("Unknown font "+name); - - return *i->second; + path=p; } const GL::Font &Resources::get_default_font() const @@ -35,87 +36,54 @@ const GL::Font &Resources::get_default_font() const return *default_font; } -const GL::Texture2D &Resources::get_texture(const string &name) const -{ - TextureMap::const_iterator i=textures.find(name); - if(i==textures.end()) - throw KeyError("Unknown texture "+name); - - return *i->second; -} - -const Graphic &Resources::get_graphic(const string &name) const +GL::Font *Resources::create_font(const string &name) { - GraphicMap::const_iterator i=graphics.find(name); - if(i==graphics.end()) - throw KeyError("Unknown graphic "+name); - - return i->second; + RefPtr fnt=new GL::Font; + DataFile::load(*fnt, (path/name).str(), *this); + if(!default_font) + default_font=fnt.get(); + return fnt.release(); } -const Style &Resources::get_style(const string &wdg, const string &name) const +GL::Texture2D *Resources::create_texture(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 tex=new GL::Texture2D; + tex->load_image((path/name).str()); + tex->set_min_filter(GL::LINEAR); + return tex.release(); } Resources::Loader::Loader(Resources &r): + Collection::Loader(r), res(r) { - add("font", &Loader::font); - add("texture", &Loader::texture); - add("graphic", &Loader::graphic); - add("style", &Loader::style); + add("default_font", &Loader::default_font); + add("font", &Loader::font); + add("style", &Loader::style); } -void Resources::Loader::font(const string &fn) +void Resources::Loader::default_font(const string &name) { - RefPtr fnt=new GL::Font; - DataFile::load(*fnt, fn); + res.default_font=res.get(name); +} - res.fonts.insert(FontMap::value_type(fn.substr(0, fn.rfind('.')), fnt.get())); +void Resources::Loader::font(const string &name) +{ + RefPtr fnt=new GL::Font; + load_sub(*fnt); + res.add(name, fnt.get()); if(!res.default_font) res.default_font=fnt.get(); fnt.release(); } -void Resources::Loader::texture(const string &fn) -{ - RefPtr 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())); -} - -void Resources::Loader::graphic(const std::string &n) -{ - Graphic graph(res, n); - load_sub(graph); - if(!graph.get_texture()) - throw Exception("Graphic without texture"); - - res.graphics.insert(GraphicMap::value_type(n, graph)); -} - -void Resources::Loader::style(const string &w, const string &n) -{ - Style stl(res, w, n); - load_sub(stl); - - res.styles.insert(StyleMap::value_type(StyleId(w, n), stl)); -} - - -bool Resources::StyleId::operator<(const StyleId &other) const +void Resources::Loader::style(const string &name) { - if(widget stl=new Style(res); + load_sub(*stl, res); + res.add(name, stl.get()); + stl.release(); } } // namespace GLtk