Had to change the #includes of dependend headers from "dir/header.h" to "../dir...
[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
20 #include <iostream>
21
22 #include "../utils/lispreader.h"
23 #include "../special/sprite_manager.h"
24
25 using namespace SuperTux;
26
27 SpriteManager::SpriteManager(const std::string& filename)
28 {
29   load_resfile(filename);
30 }
31
32 SpriteManager::~SpriteManager()
33 {
34   for(std::map<std::string, Sprite*>::iterator i = sprites.begin();
35       i != sprites.end(); ++i) {
36     delete i->second;
37   }
38 }
39
40 void
41 SpriteManager::load_resfile(const std::string& filename)
42 {
43   lisp_object_t* root_obj = lisp_read_from_file(filename);
44   if (!root_obj)
45     {
46       std::cout << "SpriteManager: Couldn't load: " << filename << std::endl;
47       return;
48     }
49
50   lisp_object_t* cur = root_obj;
51
52   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-resources") != 0)
53     return;
54   cur = lisp_cdr(cur);
55
56   while(cur)
57     {
58       lisp_object_t* el = lisp_car(cur);
59
60       if (strcmp(lisp_symbol(lisp_car(el)), "sprite") == 0)
61         {
62           Sprite* sprite = new Sprite(lisp_cdr(el));
63
64           Sprites::iterator i = sprites.find(sprite->get_name());
65           if (i == sprites.end())
66             {
67               sprites[sprite->get_name()] = sprite;
68             }
69           else
70             {
71               delete i->second;
72               i->second = sprite;
73               std::cout << "Warning: dulpicate entry: '" << sprite->get_name() << "'" << std::endl;
74             }
75         }
76       else
77         {
78           std::cout << "SpriteManager: Unknown tag" << std::endl;
79         }
80
81       cur = lisp_cdr(cur);
82     }
83
84   lisp_free(root_obj);
85 }
86
87 Sprite*
88 SpriteManager::load(const std::string& name)
89 {
90   Sprites::iterator i = sprites.find(name);
91   if (i != sprites.end())
92     {
93       return i->second;
94     }
95   else
96     {
97       std::cout << "SpriteManager: Sprite '" << name << "' not found" << std::endl;
98       return 0;
99     }
100 }
101
102 /* EOF */