2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "sprite/sprite_manager.hpp"
19 #include "lisp/parser.hpp"
20 #include "sprite/sprite.hpp"
21 #include "util/file_system.hpp"
23 SpriteManager* sprite_manager = NULL;
25 SpriteManager::SpriteManager() :
30 SpriteManager::~SpriteManager()
32 for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
38 SpriteManager::create(const std::string& name)
40 Sprites::iterator i = sprites.find(name);
42 if(i == sprites.end()) {
43 // try loading the spritefile
46 std::stringstream msg;
47 msg << "Sprite '" << name << "' not found.";
48 throw std::runtime_error(msg.str());
54 return std::auto_ptr<Sprite>(new Sprite(*data));
58 SpriteManager::load(const std::string& filename)
61 const lisp::Lisp* root;
64 root = parser.parse(filename);
65 } catch(const std::exception& e) {
66 std::ostringstream msg;
67 msg << "Parse error when trying to load sprite '" << filename
68 << "': " << e.what() << "\n";
69 throw std::runtime_error(msg.str());
72 const lisp::Lisp* sprite = root->get_lisp("supertux-sprite");
74 std::ostringstream msg;
75 msg << "'" << filename << "' is not a supertux-sprite file";
76 throw std::runtime_error(msg.str());
79 std::auto_ptr<SpriteData> data (
80 new SpriteData(sprite, FileSystem::dirname(filename)) );
81 sprites[filename] = data.release();
83 return sprites[filename];