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