4 // Copyright (C) 2008 Matthias Braun <matze@braunis.de>
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.
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.
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
22 #include "tile_set.hpp"
24 #include "file_system.hpp"
25 #include "lisp/parser.hpp"
26 #include "lisp/lisp.hpp"
27 #include "lisp/list_iterator.hpp"
30 : tiles_path(""), tiles_loaded(false)
33 tiles[0] = new Tile(this);
36 TileSet::TileSet(const std::string& filename)
37 : tiles_path(""), tiles_loaded(true)
39 tiles_path = FileSystem::dirname(filename);
42 tiles[0] = new Tile(this);
45 const lisp::Lisp* root = parser.parse(filename);
47 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
49 throw std::runtime_error("file is not a supertux tiles file.");
51 lisp::ListIterator iter(tiles_lisp);
53 if(iter.item() == "tile") {
54 Tile* tile = new Tile(this);
55 uint32_t id = tile->parse(*(iter.lisp()));
57 if(id >= tiles.size())
58 tiles.resize(id+1, 0);
61 log_warning << "Tile with ID " << id << " redefined" << std::endl;
66 } else if(iter.item() == "tilegroup") {
67 /* tilegroups are only interesting for the editor */
68 } else if (iter.item() == "tiles") {
69 // List of ids (use 0 if the tile should be ignored)
70 std::vector<uint32_t> ids;
71 // List of attributes of the tile
72 std::vector<uint32_t> attributes;
75 // width and height of the image in tile units, this is used for two
77 // a) so we don't have to load the image here to know its dimensions
78 // b) so that the resulting 'tiles' entry is more robust,
79 // ie. enlarging the image won't break the tile id mapping
80 // FIXME: height is actually not used, since width might be enough for
81 // all purposes, still feels somewhat more natural this way
82 unsigned int width = 0;
83 unsigned int height = 0;
85 iter.lisp()->get_vector("ids", ids);
86 iter.lisp()->get_vector("attributes", attributes);
87 iter.lisp()->get("image", image);
88 iter.lisp()->get("width", width);
89 iter.lisp()->get("height", height);
91 if (ids.size() != attributes.size()) {
92 std::ostringstream err;
93 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
94 << ") mismatch for image '" << image << "', but must be equal";
95 throw std::runtime_error(err.str());
98 for(std::vector<uint32_t>::size_type i = 0; i < ids.size() && i < width*height; ++i) {
102 if(ids[i] >= tiles.size())
103 tiles.resize(ids[i]+1, 0);
105 int x = 32*(i % width);
106 int y = 32*(i / width);
107 Tile* tile = new Tile(this, attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
108 if (tiles[ids[i]] == 0) {
109 tiles[ids[i]] = tile;
111 log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl;
115 } else if(iter.item() == "properties") {
118 log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl;
126 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
131 void TileSet::merge(const TileSet *tileset, uint32_t start, uint32_t end,
134 for(uint32_t id = start; id <= end && id < tileset->tiles.size(); ++id) {
135 uint32_t dest_id = id - start + offset;
137 if(dest_id >= tiles.size())
138 tiles.resize(dest_id + 1, 0);
143 Tile *tile = tileset->tiles[id];
147 if(tiles[dest_id] != NULL) {
148 log_warning << "tileset merge resulted in multiple definitions for id "
149 << dest_id << "(originally " << id << ")" << std::endl;
151 tiles[dest_id] = tile;