X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fresources.cpp;h=e83c1bb178e5adea2acc6525acfdc01c0b8b87f1;hb=e5f9004b672b038e43cf2b20c3fc9327f2b55ffd;hp=886b41654b571571e1cf545ac7d3d766f3f750cc;hpb=34724c2c2f2d30cf66e74ef3e848547171c7fee9;p=libs%2Fgltk.git diff --git a/source/resources.cpp b/source/resources.cpp index 886b416..e83c1bb 100644 --- a/source/resources.cpp +++ b/source/resources.cpp @@ -1,11 +1,4 @@ -/* $Id$ - -This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions -Distributed under the LGPL -*/ - -#include +#include #include "resources.h" using namespace std; @@ -17,38 +10,52 @@ Resources::Resources(): path("."), default_font(0) { - add_keyword("graphic"); - add_keyword("texture"); + init(); +} - add_creator(&Resources::create_font); - add_creator(&Resources::create_texture); +Resources::Resources(const FS::Path &fn): + path(FS::dirname(fn)), + default_font(0) +{ + init(); + + DataFile::load(*this, fn.str()); +} + +void Resources::init() +{ + add_type().keyword("graphic"); + add_type().keyword("texture").creator(&Resources::create_texture); + add_type().creator(&Resources::create_font); } void Resources::set_path(const FS::Path &p) { - path=p; + /* XXX bad, should change Collection API to allow creators to form paths + relative to the datafile location */ + path = p; } 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) { - RefPtr fnt=new GL::Font; - DataFile::load(*fnt, (path/name).str(), *this); + RefPtr fnt = new GL::Font; + DataFile::load(*fnt, (path/name).str(), *this); if(!default_font) - default_font=fnt.get(); + default_font = fnt.get(); return fnt.release(); } GL::Texture2D *Resources::create_texture(const string &name) { - RefPtr tex=new GL::Texture2D; + RefPtr tex = new GL::Texture2D; tex->load_image((path/name).str()); tex->set_min_filter(GL::LINEAR); return tex.release(); @@ -66,22 +73,22 @@ Resources::Loader::Loader(Resources &r): void Resources::Loader::default_font(const string &name) { - res.default_font=res.get(name); + res.default_font = &res.get(name); } void Resources::Loader::font(const string &name) { - RefPtr fnt=new GL::Font; + RefPtr fnt = new 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