add RTL info
[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 #include <sstream>
25 #include <stdexcept>
26
27
28 SpriteManager::SpriteManager() :
29   sprites()
30 {
31 }
32
33 SpriteManager::~SpriteManager()
34 {
35   for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
36     delete i->second;
37   }
38 }
39
40 SpritePtr
41 SpriteManager::create(const std::string& name)
42 {
43   Sprites::iterator i = sprites.find(name);
44   SpriteData* data;
45   if(i == sprites.end()) {
46     // try loading the spritefile
47     data = load(name);
48     if(data == NULL) {
49       std::stringstream msg;
50       msg << "Sprite '" << name << "' not found.";
51       throw std::runtime_error(msg.str());
52     }
53   } else {
54     data = i->second;
55   }
56
57   return SpritePtr(new Sprite(*data));
58 }
59
60 SpriteData*
61 SpriteManager::load(const std::string& filename)
62 {
63   lisp::Parser parser;
64   const lisp::Lisp* root;
65
66   try {
67     if(filename.size() >= 7 && filename.compare(filename.size() - 7, 7, ".sprite") == 0) {
68         // Sprite file
69         root = parser.parse(filename);
70     } else {
71       // Load image file directly
72       std::stringstream lisptext;
73       lisptext << "(supertux-sprite (action "
74                <<    "(name \"default\") "
75                <<    "(images \"" << FileSystem::basename(filename) << "\")))";
76
77       root = parser.parse(lisptext, "SpriteManager::load");
78     }
79   } catch(const std::exception& e) {
80     std::ostringstream msg;
81     msg << "Parse error when trying to load sprite '" << filename
82         << "': " << e.what() << "\n";
83     throw std::runtime_error(msg.str());
84   }
85
86   const lisp::Lisp* sprite = root->get_lisp("supertux-sprite");
87   if(!sprite) {
88     std::ostringstream msg;
89     msg << "'" << filename << "' is not a supertux-sprite file";
90     throw std::runtime_error(msg.str());
91   }
92
93   std::unique_ptr<SpriteData> data (
94     new SpriteData(*sprite, FileSystem::dirname(filename)) );
95   sprites[filename] = data.release();
96
97   return sprites[filename];
98 }
99
100 /* EOF */