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/parser.hpp"
24 #include "lisp/list_iterator.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "util/file_system.hpp"
27 #include "util/reader.hpp"
29 TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) :
36 TileSetParser::parse()
38 m_tileset.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);
52 if(iter.item() == "tile") {
53 std::auto_ptr<Tile> tile(new Tile(m_tileset));
54 uint32_t id = tile->parse(*(iter.lisp()));
56 if(id >= m_tileset.tiles.size())
57 m_tileset.tiles.resize(id+1, 0);
59 if(m_tileset.tiles[id] != 0) {
60 log_warning << "Tile with ID " << id << " redefined" << std::endl;
62 m_tileset.tiles[id] = tile.release();
64 } else if(iter.item() == "tilegroup") {
65 /* tilegroups are only interesting for the editor */
66 } else if (iter.item() == "tiles") {
67 // List of ids (use 0 if the tile should be ignored)
68 std::vector<uint32_t> ids;
69 // List of attributes of the tile
70 std::vector<uint32_t> attributes;
71 // List of data for the tiles
72 std::vector<uint32_t> datas;
73 //List of frames that the tiles come in
74 std::vector<std::string> images;
76 // width and height of the image in tile units, this is used for two
78 // a) so we don't have to load the image here to know its dimensions
79 // b) so that the resulting 'tiles' entry is more robust,
80 // ie. enlarging the image won't break the tile id mapping
81 // FIXME: height is actually not used, since width might be enough for
82 // all purposes, still feels somewhat more natural this way
83 unsigned int width = 0;
84 unsigned int height = 0;
86 iter.lisp()->get("ids", ids);
87 bool has_attributes = iter.lisp()->get("attributes", attributes);
88 bool has_datas = iter.lisp()->get("datas", datas);
90 if(!iter.lisp()->get("image", images))
91 iter.lisp()->get( "images", images);
93 iter.lisp()->get("width", width);
94 iter.lisp()->get("height", height);
97 iter.lisp()->get("anim-fps", animfps);
99 if(images.size() <= 0) {
100 throw std::runtime_error("No images in tile.");
103 throw std::runtime_error("Negative fps.");
105 if (ids.size() != width*height) {
106 std::ostringstream err;
107 err << "Number of ids (" << ids.size() << ") and size of image (" << width*height
108 << ") mismatch for image '" << images[0] << "', but must be equal";
109 throw std::runtime_error(err.str());
112 if (has_attributes && ids.size() != attributes.size()) {
113 std::ostringstream err;
114 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
115 << ") mismatch for image '" << images[0] << "', but must be equal";
116 throw std::runtime_error(err.str());
119 if (has_datas && ids.size() != datas.size()) {
120 std::ostringstream err;
121 err << "Number of ids (" << ids.size() << ") and datas (" << datas.size()
122 << ") mismatch for image '" << images[0] << "', but must be equal";
123 throw std::runtime_error(err.str());
126 for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
130 if(ids[i] >= m_tileset.tiles.size())
131 m_tileset.tiles.resize(ids[i]+1, 0);
133 int x = 32*(i % width);
134 int y = 32*(i / width);
135 std::auto_ptr<Tile> tile(new Tile(m_tileset, images, Rect(x, y, x + 32, y + 32),
136 (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps));
137 if (m_tileset.tiles[ids[i]] == 0) {
138 m_tileset.tiles[ids[i]] = tile.release();
140 log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
143 } else if(iter.item() == "properties") {
146 log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;