Started moving TileSet parsing code into separate class
[supertux.git] / src / supertux / tile.cpp
1 //  SuperTux
2 //  Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
3 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 //
5 //  This program is free software: you can redistribute it and/or modify
6 //  it under the terms of the GNU General Public License as published by
7 //  the Free Software Foundation, either version 3 of the License, or
8 //  (at your option) any later version.
9 //
10 //  This program is distributed in the hope that it will be useful,
11 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 //  GNU General Public License for more details.
14 //
15 //  You should have received a copy of the GNU General Public License
16 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 #include "supertux/tile.hpp"
19
20 #include "supertux/tile_set.hpp"
21 #include "supertux/timer.hpp"
22 #include "util/reader.hpp"
23 #include "video/drawing_context.hpp"
24
25 Tile::Tile(const TileSet& new_tileset) :
26   tileset(new_tileset), 
27   imagespecs(),
28   images(),
29   attributes(0), 
30   data(0), 
31   anim_fps(1)
32 {
33 }
34
35 Tile::Tile(const TileSet& new_tileset, std::vector<std::string> images, Rect rect, 
36            uint32_t attributes, uint32_t data, float animfps) :
37   tileset(new_tileset),
38   imagespecs(),
39   images(),
40   attributes(attributes), 
41   data(data), 
42   anim_fps(animfps)
43 {
44   for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
45     imagespecs.push_back(ImageSpec(*i, rect));
46   }
47   correct_attributes();
48 }
49
50 Tile::~Tile()
51 {
52   for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
53       ++i) {
54     delete *i;
55   }
56 }
57
58 uint32_t
59 Tile::parse(const Reader& reader)
60 {
61   uint32_t id;
62   if(!reader.get("id", id)) {
63     throw std::runtime_error("Missing tile-id.");
64   }
65
66   bool value = false;
67   if(reader.get("solid", value) && value)
68     attributes |= SOLID;
69   if(reader.get("unisolid", value) && value)
70     attributes |= UNISOLID | SOLID;
71   if(reader.get("brick", value) && value)
72     attributes |= BRICK;
73   if(reader.get("ice", value) && value)
74     attributes |= ICE;
75   if(reader.get("water", value) && value)
76     attributes |= WATER;
77   if(reader.get("hurts", value) && value)
78     attributes |= HURTS;
79   if(reader.get("fire", value) && value)
80     attributes |= FIRE;
81   if(reader.get("fullbox", value) && value)
82     attributes |= FULLBOX;
83   if(reader.get("coin", value) && value)
84     attributes |= COIN;
85   if(reader.get("goal", value) && value)
86     attributes |= GOAL;
87
88   if(reader.get("north", value) && value)
89     data |= WORLDMAP_NORTH;
90   if(reader.get("south", value) && value)
91     data |= WORLDMAP_SOUTH;
92   if(reader.get("west", value) && value)
93     data |= WORLDMAP_WEST;
94   if(reader.get("east", value) && value)
95     data |= WORLDMAP_EAST;
96   if(reader.get("stop", value) && value)
97     data |= WORLDMAP_STOP;
98
99   reader.get("data", data);
100   reader.get("anim-fps", anim_fps);
101
102   if(reader.get("slope-type", data)) {
103     attributes |= SOLID | SLOPE;
104   }
105
106   const lisp::Lisp* images;
107 #ifndef NDEBUG
108   images = reader.get_lisp("editor-images");
109   if(images)
110     parse_images(*images);
111   else {
112 #endif
113     images = reader.get_lisp("images");
114     if(images)
115       parse_images(*images);
116 #ifndef NDEBUG
117   }
118 #endif
119
120   correct_attributes();
121   return id;
122 }
123
124 void
125 Tile::parse_images(const Reader& images_lisp)
126 {
127   const lisp::Lisp* list = &images_lisp;
128   while(list) {
129     const lisp::Lisp* cur = list->get_car();
130     if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
131       std::string file;
132       cur->get(file);
133       imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
134     } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
135               cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
136               cur->get_car()->get_symbol() == "region") {
137       const lisp::Lisp* ptr = cur->get_cdr();
138
139       std::string file;
140       float x = 0, y = 0, w = 0, h = 0;
141       ptr->get_car()->get(file); ptr = ptr->get_cdr();
142       ptr->get_car()->get(x); ptr = ptr->get_cdr();
143       ptr->get_car()->get(y); ptr = ptr->get_cdr();
144       ptr->get_car()->get(w); ptr = ptr->get_cdr();
145       ptr->get_car()->get(h);
146       imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
147     } else {
148       log_warning << "Expected string or list in images tag" << std::endl;
149       continue;
150     }
151
152     list = list->get_cdr();
153   }
154 }
155
156 void
157 Tile::load_images()
158 {
159   if(images.size() == 0 && imagespecs.size() != 0)
160   {
161     const std::string& tiles_path = tileset.tiles_path;
162
163     assert(images.size() == 0);
164     for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
165           imagespecs.end(); ++i) {
166       const ImageSpec& spec = *i;
167       Surface* surface;
168       std::string file = tiles_path + spec.file;
169       if(spec.rect.get_width() <= 0) {
170         surface = new Surface(file);
171       } else {
172         surface = new Surface(file,
173                               (int) spec.rect.p1.x,
174                               (int) spec.rect.p1.y,
175                               (int) spec.rect.get_width(),
176                               (int) spec.rect.get_height());
177       }
178       images.push_back(surface);
179     }
180   }
181 }
182
183 void
184 Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
185 {
186   if(images.size() > 1) {
187     size_t frame = size_t(game_time * anim_fps) % images.size();
188     context.draw_surface(images[frame], pos, z_pos);
189   } else if (images.size() == 1) {
190     context.draw_surface(images[0], pos, z_pos);
191   }
192 }
193
194 void
195 Tile::correct_attributes()
196 {
197   //Fix little oddities in attributes (not many, currently...)
198   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
199     attributes |= SOLID;
200     //But still be vocal about it
201     log_warning << "Tile with image " << imagespecs[0].file << " needs solid attribute." << std::endl;
202   }
203 }
204
205 void
206 Tile::print_debug(int id) const
207 {
208   log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
209   for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
210   {
211     log_debug << "  Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
212   }
213 }
214
215 /* EOF */