* Make it compile again
[supertux.git] / src / sprite / sprite_manager.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include "sprite/sprite_manager.hpp"
18
19 #include "lisp/parser.hpp"
20 #include "sprite/sprite.hpp"
21 #include "util/file_system.hpp"
22 #include "util/reader.hpp"
23
24 SpriteManager::SpriteManager() :
25   sprites()
26 {
27 }
28
29 SpriteManager::~SpriteManager()
30 {
31   for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
32     delete i->second;
33   }
34 }
35
36 std::auto_ptr<Sprite>
37 SpriteManager::create(const std::string& name)
38 {
39   Sprites::iterator i = sprites.find(name);
40   SpriteData* data;
41   if(i == sprites.end()) {
42     // try loading the spritefile
43     data = load(name);
44     if(data == NULL) {
45       std::stringstream msg;
46       msg << "Sprite '" << name << "' not found.";
47       throw std::runtime_error(msg.str());
48     }
49   } else {
50     data = i->second;
51   }
52
53   return std::auto_ptr<Sprite>(new Sprite(*data));
54 }
55
56 SpriteData*
57 SpriteManager::load(const std::string& filename)
58 {
59   lisp::Parser parser;
60   const lisp::Lisp* root;
61
62   try {
63     if(filename.size() >= 7 && filename.compare(filename.size() - 7, 7, ".sprite") == 0) {
64         // Sprite file
65         root = parser.parse(filename);
66     } else {
67       // Load image file directly
68       std::stringstream lisptext;
69       lisptext << "(supertux-sprite (action "
70                <<    "(name \"default\") "
71                <<    "(images \"" << FileSystem::basename(filename) << "\")))";
72
73       root = parser.parse(lisptext, "SpriteManager::load");
74     }
75   } catch(const std::exception& e) {
76     std::ostringstream msg;
77     msg << "Parse error when trying to load sprite '" << filename
78         << "': " << e.what() << "\n";
79     throw std::runtime_error(msg.str());
80   }
81
82   const lisp::Lisp* sprite = root->get_lisp("supertux-sprite");
83   if(!sprite) {
84     std::ostringstream msg;
85     msg << "'" << filename << "' is not a supertux-sprite file";
86     throw std::runtime_error(msg.str());
87   }
88
89   std::auto_ptr<SpriteData> data (
90     new SpriteData(*sprite, FileSystem::dirname(filename)) );
91   sprites[filename] = data.release();
92
93   return sprites[filename];
94 }
95
96 /* EOF */