]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/resources.cpp
Rework exceptions and use maputils
[libs/gltk.git] / source / resources.cpp
index 01426a20ced22d8911c587ab21338201c8d02c1d..869885fecd97e77ae3719399c8fd53d6690baef7 100644 (file)
@@ -1,11 +1,4 @@
-/* $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 "resources.h"
 
 using namespace std;
@@ -16,6 +9,20 @@ namespace GLtk {
 Resources::Resources():
        path("."),
        default_font(0)
+{
+       init();
+}
+
+Resources::Resources(const FS::Path &fn):
+       path(FS::dirname(fn)),
+       default_font(0)
+{
+       init();
+
+       DataFile::load(*this, fn.str());
+}
+
+void Resources::init()
 {
        add_keyword<Graphic>("graphic");
        add_keyword<GL::Texture2D>("texture");
@@ -24,31 +31,33 @@ Resources::Resources():
        add_creator(&Resources::create_texture);
 }
 
-void Resources::set_path(const Path &p)
+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<GL::Font> fnt=new GL::Font;
-       DataFile::load<GL::Font, Resources &>(*fnt, (path/name).str(), *this);
+       RefPtr<GL::Font> 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<GL::Texture2D> tex=new GL::Texture2D;
+       RefPtr<GL::Texture2D> tex = new GL::Texture2D;
        tex->load_image((path/name).str());
        tex->set_min_filter(GL::LINEAR);
        return tex.release();
@@ -66,22 +75,22 @@ Resources::Loader::Loader(Resources &r):
 
 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;
+       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();
+               res.default_font = fnt.get();
        fnt.release();
 }
 
 void Resources::Loader::style(const string &name)
 {
-       RefPtr<Style> stl=new Style(res);
+       RefPtr<Style> stl = new Style(res);
        load_sub(*stl, res);
        res.add(name, stl.get());
        stl.release();