- memleak fix and menu fix from MatzeB
[supertux.git] / src / 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 #include "lispreader.h"
22 #include "sprite_manager.h"
23
24 SpriteManager::SpriteManager(const std::string& filename)
25 {
26   load_resfile(filename);
27 }
28
29 void
30 SpriteManager::load_resfile(const std::string& filename)
31 {
32   lisp_object_t* root_obj = lisp_read_from_file(filename);
33   lisp_object_t* cur = root_obj;
34
35   if (strcmp(lisp_symbol(lisp_car(cur)), "supertux-resources") != 0)
36     return;
37   cur = lisp_cdr(cur);
38
39   while(cur)
40     {
41       lisp_object_t* el = lisp_car(cur);
42
43       if (strcmp(lisp_symbol(lisp_car(el)), "sprite") == 0)
44         {
45           Sprite* sprite = new Sprite(lisp_cdr(el));
46
47           Sprites::iterator i = sprites.find(sprite->get_name());
48           if (i == sprites.end())
49             {
50               sprites[sprite->get_name()] = sprite;
51             }
52           else
53             {
54               delete i->second;
55               i->second = sprite;
56               std::cout << "Warning: dulpicate entry: '" << sprite->get_name() << "'" << std::endl;
57             }
58         }
59       else
60         {
61           std::cout << "SpriteManager: Unknown tag" << std::endl;
62         }
63
64       cur = lisp_cdr(cur);
65     }
66
67   lisp_free(root_obj);
68 }
69
70 Sprite*
71 SpriteManager::load(const std::string& name)
72 {
73   Sprites::iterator i = sprites.find(name);
74   if (i != sprites.end())
75     {
76       return i->second;
77     }
78   else
79     {
80       std::cout << "SpriteManager: Sprite '" << name << "' not found" << std::endl;
81       return 0;
82     }
83 }
84
85 /* EOF */