The BIG COMMIT(tm)
[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
21 #include <config.h>
22
23 #include <assert.h>
24 #include "video/drawing_context.h"
25 #include "app/setup.h"
26 #include "app/globals.h"
27 #include "utils/lispreader.h"
28 #include "tile.h"
29 #include "tile_manager.h"
30 #include "scene.h"
31
32 TileManager* TileManager::instance_  = 0;
33
34 TileManager::TileManager()
35 {
36   std::string filename = datadir + "/images/tilesets/supertux.stgt";
37   load_tileset(filename);
38 }
39
40 TileManager::~TileManager()
41 {
42   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
43     delete *i;
44 }
45
46 void TileManager::load_tileset(std::string filename)
47 {
48   if(filename == current_tileset)
49     return;
50   
51   // free old tiles
52   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
53     delete *i;
54   tiles.clear();
55  
56   lisp_object_t* root_obj = lisp_read_from_file(filename);
57
58   if (!root_obj)
59     Termination::abort("Couldn't load file", filename);
60
61   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") != 0)
62     assert(false);
63
64   lisp_object_t* cur = lisp_cdr(root_obj);
65   int tileset_id = 0;
66
67   while(!lisp_nil_p(cur)) {
68     lisp_object_t* element = lisp_car(cur);
69
70     if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
71       {
72         LispReader reader(lisp_cdr(element));
73
74         Tile* tile = new Tile;
75         tile->read(reader);
76
77         while(tile->id >= tiles.size()) {
78             tiles.push_back(0);
79         }
80         tiles[tile->id] = tile;
81       }
82     else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
83       {
84         LispReader reader(lisp_cdr(element));
85         std::string filename;
86         reader.read_string("file", filename);
87         filename = datadir + "/images/tilesets/" + filename;
88         load_tileset(filename);
89       }
90     else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
91       {
92         TileGroup new_;
93         LispReader reader(lisp_cdr(element));
94         reader.read_string("name", new_.name);
95         reader.read_int_vector("tiles", new_.tiles);          
96         tilegroups.insert(new_).first;
97       }
98     else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
99       {
100         LispReader reader(lisp_cdr(element));
101         reader.read_int("id", tileset_id);
102         tileset_id *= 1000;
103       }
104     else
105       {
106         std::cerr << "Unknown symbol: " << 
107           lisp_symbol(lisp_car(element)) << "\n";
108       }
109
110     cur = lisp_cdr(cur);
111   }
112
113   lisp_free(root_obj);
114   current_tileset = filename;
115 }
116