2 // Copyright (C) 2008 Matthias Braun <matze@braunis.de>
3 // Ingo Ruhnke <grumbel@gmx.de>
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.
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.
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/>.
18 #include "supertux/tile_set_parser.hpp"
23 #include "lisp/list_iterator.hpp"
24 #include "lisp/parser.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "util/file_system.hpp"
28 TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) :
36 TileSetParser::parse()
38 m_tiles_path = FileSystem::dirname(m_filename);
40 m_tileset.tiles.resize(1, 0);
41 m_tileset.tiles[0] = new Tile(m_tileset);
44 const lisp::Lisp* root = parser.parse(m_filename);
46 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
48 throw std::runtime_error("file is not a supertux tiles file.");
50 lisp::ListIterator iter(tiles_lisp);
53 if (iter.item() == "tile")
55 parse_tile(*iter.lisp());
57 else if (iter.item() == "tilegroup")
59 /* tilegroups are only interesting for the editor */
61 else if (iter.item() == "tiles")
63 parse_tiles(*iter.lisp());
67 log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
73 TileSetParser::parse_tile(const Reader& reader)
76 if (!reader.get("id", id))
78 throw std::runtime_error("Missing tile-id.");
81 uint32_t attributes = 0;
83 std::vector<Tile::ImageSpec> imagespecs;
88 if(reader.get("solid", value) && value)
89 attributes |= Tile::SOLID;
90 if(reader.get("unisolid", value) && value)
91 attributes |= Tile::UNISOLID | Tile::SOLID;
92 if(reader.get("brick", value) && value)
93 attributes |= Tile::BRICK;
94 if(reader.get("ice", value) && value)
95 attributes |= Tile::ICE;
96 if(reader.get("water", value) && value)
97 attributes |= Tile::WATER;
98 if(reader.get("hurts", value) && value)
99 attributes |= Tile::HURTS;
100 if(reader.get("fire", value) && value)
101 attributes |= Tile::FIRE;
102 if(reader.get("fullbox", value) && value)
103 attributes |= Tile::FULLBOX;
104 if(reader.get("coin", value) && value)
105 attributes |= Tile::COIN;
106 if(reader.get("goal", value) && value)
107 attributes |= Tile::GOAL;
109 if(reader.get("north", value) && value)
110 data |= Tile::WORLDMAP_NORTH;
111 if(reader.get("south", value) && value)
112 data |= Tile::WORLDMAP_SOUTH;
113 if(reader.get("west", value) && value)
114 data |= Tile::WORLDMAP_WEST;
115 if(reader.get("east", value) && value)
116 data |= Tile::WORLDMAP_EAST;
117 if(reader.get("stop", value) && value)
118 data |= Tile::WORLDMAP_STOP;
120 reader.get("data", data);
121 reader.get("anim-fps", anim_fps);
123 if(reader.get("slope-type", data))
125 attributes |= Tile::SOLID | Tile::SLOPE;
128 const lisp::Lisp* images;
130 images = reader.get_lisp("editor-images");
132 imagespecs = parse_tile_images(*images);
135 images = reader.get_lisp("images");
137 imagespecs = parse_tile_images(*images);
142 std::auto_ptr<Tile> tile(new Tile(m_tileset, imagespecs, attributes, data, anim_fps));
144 if (id >= m_tileset.tiles.size())
145 m_tileset.tiles.resize(id+1, 0);
147 if (m_tileset.tiles[id] != 0)
149 log_warning << "Tile with ID " << id << " redefined" << std::endl;
153 m_tileset.tiles[id] = tile.release();
157 std::vector<Tile::ImageSpec>
158 TileSetParser::parse_tile_images(const Reader& images_lisp)
160 std::vector<Tile::ImageSpec> imagespecs;
162 const lisp::Lisp* list = &images_lisp;
165 const lisp::Lisp* cur = list->get_car();
167 if(cur->get_type() == lisp::Lisp::TYPE_STRING)
171 imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rectf(0, 0, 0, 0)));
173 else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
174 cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
175 cur->get_car()->get_symbol() == "region")
177 const lisp::Lisp* ptr = cur->get_cdr();
184 ptr->get_car()->get(file); ptr = ptr->get_cdr();
185 ptr->get_car()->get(x); ptr = ptr->get_cdr();
186 ptr->get_car()->get(y); ptr = ptr->get_cdr();
187 ptr->get_car()->get(w); ptr = ptr->get_cdr();
188 ptr->get_car()->get(h);
189 imagespecs.push_back(Tile::ImageSpec(m_tiles_path + file, Rectf(x, y, x+w, y+h)));
193 log_warning << "Expected string or list in images tag" << std::endl;
196 list = list->get_cdr();
203 TileSetParser::parse_tiles(const Reader& reader)
205 // List of ids (use 0 if the tile should be ignored)
206 std::vector<uint32_t> ids;
207 // List of attributes of the tile
208 std::vector<uint32_t> attributes;
209 // List of data for the tiles
210 std::vector<uint32_t> datas;
211 //List of frames that the tiles come in
212 std::vector<std::string> images;
214 // width and height of the image in tile units, this is used for two
216 // a) so we don't have to load the image here to know its dimensions
217 // b) so that the resulting 'tiles' entry is more robust,
218 // ie. enlarging the image won't break the tile id mapping
219 // FIXME: height is actually not used, since width might be enough for
220 // all purposes, still feels somewhat more natural this way
221 unsigned int width = 0;
222 unsigned int height = 0;
224 reader.get("ids", ids);
225 bool has_attributes = reader.get("attributes", attributes);
226 bool has_datas = reader.get("datas", datas);
228 reader.get("image", images) || reader.get("images", images);
230 reader.get("width", width);
231 reader.get("height", height);
234 reader.get("anim-fps", animfps);
236 if (images.size() <= 0)
238 throw std::runtime_error("No images in tile.");
240 else if (animfps < 0)
242 throw std::runtime_error("Negative fps.");
244 else if (ids.size() != width*height)
246 std::ostringstream err;
247 err << "Number of ids (" << ids.size() << ") and size of image (" << width*height
248 << ") mismatch for image '" << images[0] << "', but must be equal";
249 throw std::runtime_error(err.str());
251 else if (has_attributes && ids.size() != attributes.size())
253 std::ostringstream err;
254 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
255 << ") mismatch for image '" << images[0] << "', but must be equal";
256 throw std::runtime_error(err.str());
258 else if (has_datas && ids.size() != datas.size())
260 std::ostringstream err;
261 err << "Number of ids (" << ids.size() << ") and datas (" << datas.size()
262 << ") mismatch for image '" << images[0] << "', but must be equal";
263 throw std::runtime_error(err.str());
267 for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i)
271 if (ids[i] >= m_tileset.tiles.size())
272 m_tileset.tiles.resize(ids[i]+1, 0);
274 int x = 32*(i % width);
275 int y = 32*(i / width);
277 std::vector<Tile::ImageSpec> imagespecs;
278 for(std::vector<std::string>::const_iterator j = images.begin(); j != images.end(); ++j)
280 imagespecs.push_back(Tile::ImageSpec(m_tiles_path + *j, Rectf(x, y, x + 32, y + 32)));
283 std::auto_ptr<Tile> tile(new Tile(m_tileset, imagespecs,
284 (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps));
285 if (m_tileset.tiles[ids[i]] == 0) {
286 m_tileset.tiles[ids[i]] = tile.release();
288 log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;