sprites.strf is gone, woohoo, thanks to Christoph
[supertux.git] / src / sprite / sprite_manager.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <iostream>
22 #include <sstream>
23 #include <stdexcept>
24
25 #include "sprite_manager.hpp"
26 #include "sprite_data.hpp"
27 #include "sprite.hpp"
28 #include "lisp/lisp.hpp"
29 #include "lisp/parser.hpp"
30 #include "lisp/list_iterator.hpp"
31 #include "file_system.hpp"
32 #include "msg.hpp"
33
34 SpriteManager::SpriteManager()
35 {
36 }
37
38 SpriteManager::~SpriteManager()
39 {
40   for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
41     delete i->second;
42   }
43 }
44
45 Sprite*
46 SpriteManager::create(const std::string& name)
47 {
48   Sprites::iterator i = sprites.find(name);
49   SpriteData* data;
50   if(i == sprites.end()) {
51     // try loading the spritefile
52     data = load(name);
53     if(data == NULL) {
54       std::stringstream msg;
55       msg << "Sprite '" << name << "' not found.";
56       throw std::runtime_error(msg.str());
57     }
58   } else {
59     data = i->second;
60   }
61   
62   return new Sprite(*data);
63 }
64
65 SpriteData*
66 SpriteManager::load(const std::string& filename)
67 {
68   lisp::Parser parser;
69   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
70
71   const lisp::Lisp* sprite = root->get_lisp("supertux-sprite");
72   if(!sprite) {
73     std::ostringstream msg;
74     msg << "'" << filename << "' is not a supertux-sprite file";
75     throw std::runtime_error(msg.str());
76   }
77
78   std::auto_ptr<SpriteData> data (
79       new SpriteData(sprite, FileSystem::dirname(filename)) );
80   sprites[filename] = data.release();
81   
82   return sprites[filename];
83 }
84