e0c6b5edb5933217c950b04e685c39758f8db92a
[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
33 SpriteManager::SpriteManager(const std::string& filename)
34 {
35 #ifdef DEBUG
36   Uint32 ticks = SDL_GetTicks();
37 #endif
38   load_resfile(filename);
39 #ifdef DEBUG
40   printf("Loaded sprites in %f seconds\n", (SDL_GetTicks() - ticks) / 1000.0f);
41 #endif
42 }
43
44 SpriteManager::~SpriteManager()
45 {
46   for(Sprites::iterator i = sprites.begin(); i != sprites.end(); ++i) {
47     delete i->second;
48   }
49 }
50
51 void
52 SpriteManager::load_resfile(const std::string& filename)
53 {
54   lisp::Parser parser;
55   try {
56     std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
57
58     const lisp::Lisp* resources = root->get_lisp("supertux-resources");
59     if(!resources)
60       throw std::runtime_error("file is not a supertux-resources files");
61
62     lisp::ListIterator iter(resources);
63     while(iter.next()) {
64       if(iter.item() == "sprite") {
65         SpriteData* spritedata = new SpriteData(iter.lisp(), "images/");
66
67         Sprites::iterator i = sprites.find(spritedata->get_name());
68         if (i == sprites.end()) {
69           sprites[spritedata->get_name()] = spritedata;
70         } else {
71           delete i->second;
72           i->second = spritedata;
73           std::cout << "Warning: dulpicate entry: '" << spritedata->get_name()
74             << "' in spritefile." << std::endl;
75         }
76       } else {
77         std::cout << "SpriteManager: Unknown tag '" << iter.item() 
78           << "' in spritefile.\n";
79       }
80     }
81   } catch(std::exception& e) {
82     std::stringstream msg;
83     msg << "Couldn't load file '" << filename << "': " << e.what() << "\n";
84     throw std::runtime_error(msg.str());
85   }
86 }
87
88 Sprite*
89 SpriteManager::create(const std::string& name)
90 {
91   Sprites::iterator i = sprites.find(name);
92   SpriteData* data;
93   if(i == sprites.end()) {
94     // try loading the spritefile
95     data = load(name);
96     if(data == NULL) {
97       std::stringstream msg;
98       msg << "Sprite '" << name << "' not found.";
99       throw std::runtime_error(msg.str());
100     }
101   } else {
102     data = i->second;
103   }
104   
105   return new Sprite(*data);
106 }
107
108 SpriteData*
109 SpriteManager::load(const std::string& filename)
110 {
111   lisp::Parser parser;
112   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
113
114   const lisp::Lisp* sprite = root->get_lisp("supertux-sprite");
115   if(!sprite) {
116     std::ostringstream msg;
117     msg << "'" << filename << "' is not a supertux-sprite file";
118     throw std::runtime_error(msg.str());
119   }
120
121   std::auto_ptr<SpriteData> data (
122       new SpriteData(sprite, FileSystem::dirname(filename)) );
123   sprites[filename] = data.release();
124   
125   return sprites[filename];
126 }
127