64cf7c06d76b4c8c0a223037830b7d3c85adf599
[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 <assert.h>
23 #include "video/drawing_context.h"
24 #include "app/setup.h"
25 #include "app/globals.h"
26 #include "utils/lispreader.h"
27 #include "tile.h"
28 #include "tile_manager.h"
29 #include "scene.h"
30
31 TileManager* TileManager::instance_  = 0;
32
33 TileManager::TileManager()
34 {
35   std::string filename = datadir + "/images/tilesets/supertux.stgt";
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   lisp_object_t* root_obj = lisp_read_from_file(filename);
53
54   if (!root_obj)
55     Termination::abort("Couldn't load file", filename);
56
57   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") != 0)
58     assert(false);
59
60   lisp_object_t* cur = lisp_cdr(root_obj);
61   int tileset_id = 0;
62
63   while(!lisp_nil_p(cur)) {
64     lisp_object_t* element = lisp_car(cur);
65
66     if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
67       {
68         LispReader reader(lisp_cdr(element));
69
70         Tile* tile = new Tile;
71         tile->parse(reader);
72
73         while(tile->id >= tiles.size()) {
74             tiles.push_back(0);
75         }
76         tiles[tile->id] = tile;
77       }
78     else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
79       {
80         LispReader reader(lisp_cdr(element));
81         std::string filename;
82         reader.read_string("file", filename);
83         filename = datadir + "/images/tilesets/" + filename;
84         load_tileset(filename);
85       }
86     else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
87       {
88         TileGroup new_;
89         LispReader reader(lisp_cdr(element));
90         reader.read_string("name", new_.name);
91         reader.read_int_vector("tiles", new_.tiles);          
92         tilegroups.insert(new_).first;
93       }
94     else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
95       {
96         LispReader reader(lisp_cdr(element));
97         reader.read_int("id", tileset_id);
98         tileset_id *= 1000;
99       }
100     else
101       {
102         std::cerr << "Unknown symbol: " << 
103           lisp_symbol(lisp_car(element)) << "\n";
104       }
105
106     cur = lisp_cdr(cur);
107   }
108
109   lisp_free(root_obj);
110 }
111