- moved tilemanager into its own class
[supertux.git] / src / tile.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 <cassert>
22 #include <iostream>
23
24 #include "globals.h"
25 #include "tile.h"
26 #include "scene.h"
27 #include "lispreader.h"
28 #include "vector.h"
29 #include "screen/drawing_context.h"
30
31 Tile::Tile()
32   : id(-1), attributes(0), data(0), next_tile(0), anim_speed(25)
33 {
34 }
35
36 Tile::~Tile()
37 {
38   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
39       ++i) {
40     delete *i;
41   }
42   for(std::vector<Surface*>::iterator i = editor_images.begin();
43       i != editor_images.end(); ++i) {
44     delete *i;                                                                
45   }
46 }
47
48 int
49 Tile::read(LispReader& reader)
50 {
51   if(!reader.read_int("id", id)) {
52     std::cerr << "Missing tile-id.\n";
53     return -1;
54   }
55   
56   bool value;
57   if(reader.read_bool("solid", value) && value)
58     attributes |= SOLID;
59   if(reader.read_bool("unisolid", value) && value)
60     attributes |= GOAL;                            
61   if(reader.read_bool("brick", value) && value)
62     attributes |= BRICK;
63   if(reader.read_bool("ice", value) && value)
64     attributes |= ICE;
65   if(reader.read_bool("water", value) && value)
66     attributes |= WATER;
67   if(reader.read_bool("spike", value) && value)
68     attributes |= SPIKE;
69   if(reader.read_bool("fullbox", value) && value)
70     attributes |= FULLBOX;
71   if(reader.read_bool("distro", value) && value)
72     attributes |= COIN;
73   if(reader.read_bool("coin", value) && value)
74     attributes |= COIN;
75   if(reader.read_bool("goal", value) && value)
76     attributes |= GOAL;
77
78   reader.read_int("data", data);
79   reader.read_int("anim-speed", anim_speed);
80   reader.read_int("next-tile", next_tile);
81
82   std::vector<std::string> filenames;
83   reader.read_string_vector("images", filenames);
84   std::vector<std::string> editor_filenames;
85   reader.read_string_vector("editor-images", editor_filenames);
86
87   std::vector<int> grid;
88   reader.read_int_vector("grid", grid);
89
90   // read images
91   for(std::vector<std::string>::iterator i = filenames.begin();
92       i != filenames.end(); ++i) 
93     {
94       if (grid.size() == 4)
95         {
96           Surface* surface = new Surface(datadir + "/images/tilesets/" + *i,
97                                          grid[0], grid[1], grid[2], grid[3],
98                                          USE_ALPHA);
99           images.push_back(surface);
100         }
101       else
102         {
103           Surface* surface = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
104           images.push_back(surface);
105         }
106     }
107
108   for(std::vector<std::string>::iterator i = editor_filenames.begin();
109       i != editor_filenames.end(); ++i) {
110     Surface* surface 
111       = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
112     editor_images.push_back(surface);
113   }
114
115   return id;
116 }
117
118 /* EOF */
119