X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fsupertux%2Ftile_set.cpp;h=f7450ec867e29bfec27b66f882988447dffd686b;hb=e621c95d8cbb782c7febc17c5422a307d2a41f7d;hp=74a8fd67e71b85033ea5034d3b81f585a13556f7;hpb=3aa6936791cf24344e27d452ab2fd8c33fb5a2e8;p=supertux.git diff --git a/src/supertux/tile_set.cpp b/src/supertux/tile_set.cpp index 74a8fd67e..f7450ec86 100644 --- a/src/supertux/tile_set.cpp +++ b/src/supertux/tile_set.cpp @@ -16,140 +16,23 @@ #include "supertux/tile_set.hpp" -#include -#include - -#include "lisp/list_iterator.hpp" -#include "lisp/parser.hpp" -#include "util/file_system.hpp" +#include "supertux/tile_set_parser.hpp" TileSet::TileSet() : tiles(), - tiles_path(), tiles_loaded(false) { tiles.resize(1, 0); - tiles[0] = new Tile(this); + tiles[0] = new Tile(); } TileSet::TileSet(const std::string& filename) : tiles(), - tiles_path(), tiles_loaded(true) { - tiles_path = FileSystem::dirname(filename); - - tiles.resize(1, 0); - tiles[0] = new Tile(this); - - lisp::Parser parser; - const lisp::Lisp* root = parser.parse(filename); + TileSetParser parser(*this, filename); + parser.parse(); - const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles"); - if(!tiles_lisp) - throw std::runtime_error("file is not a supertux tiles file."); - - lisp::ListIterator iter(tiles_lisp); - while(iter.next()) { - if(iter.item() == "tile") { - Tile* tile = new Tile(this); - uint32_t id = tile->parse(*(iter.lisp())); - - if(id >= tiles.size()) - tiles.resize(id+1, 0); - - if(tiles[id] != 0) { - log_warning << "Tile with ID " << id << " redefined" << std::endl; - delete tile; - } else { - tiles[id] = tile; - } - } else if(iter.item() == "tilegroup") { - /* tilegroups are only interesting for the editor */ - } else if (iter.item() == "tiles") { - // List of ids (use 0 if the tile should be ignored) - std::vector ids; - // List of attributes of the tile - std::vector attributes; - // List of data for the tiles - std::vector datas; - //List of frames that the tiles come in - std::vector images; - - // width and height of the image in tile units, this is used for two - // purposes: - // a) so we don't have to load the image here to know its dimensions - // b) so that the resulting 'tiles' entry is more robust, - // ie. enlarging the image won't break the tile id mapping - // FIXME: height is actually not used, since width might be enough for - // all purposes, still feels somewhat more natural this way - unsigned int width = 0; - unsigned int height = 0; - - iter.lisp()->get("ids", ids); - bool has_attributes = iter.lisp()->get("attributes", attributes); - bool has_datas = iter.lisp()->get("datas", datas); - - if(!iter.lisp()->get("image", images)) - iter.lisp()->get( "images", images); - - iter.lisp()->get("width", width); - iter.lisp()->get("height", height); - - float animfps = 10; - iter.lisp()->get("anim-fps", animfps); - - if(images.size() <= 0) { - throw std::runtime_error("No images in tile."); - } - if(animfps < 0) { - throw std::runtime_error("Negative fps."); - } - if (ids.size() != width*height) { - std::ostringstream err; - err << "Number of ids (" << ids.size() << ") and size of image (" << width*height - << ") mismatch for image '" << images[0] << "', but must be equal"; - throw std::runtime_error(err.str()); - } - - if (has_attributes && ids.size() != attributes.size()) { - std::ostringstream err; - err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size() - << ") mismatch for image '" << images[0] << "', but must be equal"; - throw std::runtime_error(err.str()); - } - - if (has_datas && ids.size() != datas.size()) { - std::ostringstream err; - err << "Number of ids (" << ids.size() << ") and datas (" << datas.size() - << ") mismatch for image '" << images[0] << "', but must be equal"; - throw std::runtime_error(err.str()); - } - - for(std::vector::size_type i = 0; i < ids.size() && i < width*height; ++i) { - if (ids[i] == 0) - continue; - - if(ids[i] >= tiles.size()) - tiles.resize(ids[i]+1, 0); - - int x = 32*(i % width); - int y = 32*(i / width); - Tile* tile = new Tile(this, images, Rect(x, y, x + 32, y + 32), - (has_attributes ? attributes[i] : 0), (has_datas ? datas[i] : 0), animfps); - if (tiles[ids[i]] == 0) { - tiles[ids[i]] = tile; - } else { - log_warning << "Tile with ID " << ids[i] << " redefined" << std::endl; - delete tile; - } - } - } else if(iter.item() == "properties") { - // deprecated - } else { - log_warning << "Unknown symbol '" << iter.item() << "' in tileset file" << std::endl; - } - } if (0) { // enable this if you want to see a list of free tiles log_info << "Last Tile ID is " << tiles.size()-1 << std::endl; @@ -167,19 +50,16 @@ TileSet::TileSet(const std::string& filename) : } } } + if (0) { // enable this to dump the (large) list of tiles to log_debug // Two dumps are identical iff the tilesets specify identical tiles log_debug << "Tileset in " << filename << std::endl; for(int i = 0; i < int(tiles.size()); ++i) { - if(tiles[i] == 0) - continue; - Tile* t = tiles[i]; - log_debug << " Tile: id " << i << ", data " << t->data << ", attributes " << t->attributes << ":" << std::endl; - for(std::vector::iterator im = t->imagespecs.begin(); im != - t->imagespecs.end(); ++im) { - log_debug << " Imagespec: file " << im->file << "; rect " << im->rect << std::endl; + if(tiles[i] != 0) + { + tiles[i]->print_debug(i); } } }