commented out an error-message line in glutil.hpp that caused linking errors
[supertux.git] / src / tile_manager.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@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
19 //  02111-1307, USA.
20 #include <config.h>
21
22 #include <memory>
23 #include <stdexcept>
24 #include <iostream>
25 #include <assert.h>
26 #include "video/drawing_context.hpp"
27 #include "lisp/lisp.hpp"
28 #include "lisp/parser.hpp"
29 #include "lisp/list_iterator.hpp"
30 #include "tile.hpp"
31 #include "tile_manager.hpp"
32 #include "resources.hpp"
33
34 TileManager::TileManager(const std::string& filename)
35 {
36   load_tileset(filename);
37 }
38
39 TileManager::~TileManager()
40 {
41   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
42     delete *i;
43 }
44
45 void TileManager::load_tileset(std::string filename)
46 {
47   // free old tiles
48   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
49     delete *i;
50   tiles.clear();
51
52   std::string::size_type t = filename.rfind('/');
53   if(t == std::string::npos) {
54     tiles_path = "";
55   } else {
56     tiles_path = filename.substr(0, t+1);
57   }
58  
59   lisp::Parser parser;
60   std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
61
62   const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
63   if(!tiles_lisp)
64     throw std::runtime_error("file is not a supertux tiles file.");
65
66   lisp::ListIterator iter(tiles_lisp);
67   while(iter.next()) {
68     if(iter.item() == "tile") {
69       Tile* tile = new Tile();
70       tile->parse(*(iter.lisp()));
71       while(tile->id >= tiles.size()) {
72         tiles.push_back(0);
73       }
74       tiles[tile->id] = tile;
75     } else if(iter.item() == "tilegroup") {
76         TileGroup tilegroup;
77         const lisp::Lisp* tilegroup_lisp = iter.lisp();
78         tilegroup_lisp->get("name", tilegroup.name);
79         tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
80         tilegroups.insert(tilegroup);
81     } else if(iter.item() == "properties") {
82       // deprecated
83     } else {
84       std::cerr << "Unknown symbol '" << iter.item() << "' tile defintion file.\n";
85     }
86   }
87 }
88