move over rewritten lispreader from tuxkart (with additional fixes), generalized...
[supertux.git] / lib / special / 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.h"
26 #include "sprite_data.h"
27 #include "sprite.h"
28 #include "lisp/lisp.h"
29 #include "lisp/parser.h"
30 #include "lisp/list_iterator.h"
31
32 namespace SuperTux
33 {
34
35 SpriteManager::SpriteManager(const std::string& filename)
36 {
37   load_resfile(filename);
38 }
39
40 SpriteManager::~SpriteManager()
41 {
42   for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
43     delete i->second;
44   }
45 }
46
47 void
48 SpriteManager::load_resfile(const std::string& filename)
49 {
50   lisp::Parser parser;
51   try {
52     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
53
54     const lisp::Lisp* resources = root->get_lisp("supertux-resources");
55     if(!resources)
56       throw std::runtime_error("file is not a supertux-resources files");
57
58     lisp::ListIterator iter(resources);
59     while(iter.next()) {
60       if(iter.item() == "sprite") {
61         SpriteData* spritedata = new SpriteData(iter.lisp());
62
63         printf("Spr: %s.\n", spritedata->get_name().c_str());
64         Sprites::iterator i = sprites.find(spritedata->get_name());
65         if (i == sprites.end()) {
66           sprites[spritedata->get_name()] = spritedata;
67         } else {
68           delete i->second;
69           i->second = spritedata;
70           std::cout << "Warning: dulpicate entry: '" << spritedata->get_name()
71             << "' in spritefile." << std::endl;
72         }
73       } else {
74         std::cout << "SpriteManager: Unknown tag '" << iter.item() 
75           << "' in spritefile.\n";
76       }
77     }
78   } catch(std::exception& e) {
79     std::stringstream msg;
80     msg << "Couldn't load file '" << filename << "': " << e.what() << "\n";
81     throw std::runtime_error(msg.str());
82   }
83 }
84
85 Sprite*
86 SpriteManager::create(const std::string& name)
87 {
88   Sprites::iterator i = sprites.find(name);
89   if(i == sprites.end()) {
90     std::stringstream msg;
91     msg << "Sprite '" << name << "' not found.";
92     throw std::runtime_error(msg.str());
93   }
94   return new Sprite(*i->second);
95 }
96
97 }
98