X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Ftile_manager.cpp;h=24f802111873a3617342b0e7f31e36ef3ec5137d;hb=e126dd8c3da286ab759dc36f3f59f3955e16ed09;hp=88ec579863114083827987c235e48f90c5266c53;hpb=6e843b1780f62f45b7021bd8c38181aa211588ee;p=supertux.git diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index 88ec57986..24f802111 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -1,7 +1,8 @@ // $Id$ -// +// // SuperTux // Copyright (C) 2004 Tobias Glaesser +// Copyright (C) 2006 Matthias Braun // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License @@ -12,104 +13,84 @@ // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. -// +// // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA // 02111-1307, USA. #include +#include +#include +#include +#include +#include #include -#include "video/drawing_context.h" -#include "app/setup.h" -#include "app/globals.h" -#include "utils/lispreader.h" -#include "tile.h" -#include "tile_manager.h" -#include "scene.h" +#include +#include "video/drawing_context.hpp" +#include "log.hpp" +#include "lisp/lisp.hpp" +#include "lisp/parser.hpp" +#include "lisp/list_iterator.hpp" +#include "tile.hpp" +#include "tile_set.hpp" +#include "tile_manager.hpp" +#include "resources.hpp" -TileManager* TileManager::instance_ = 0; +TileManager *tile_manager = NULL; +TileSet *current_tileset = NULL; TileManager::TileManager() { - std::string filename = datadir + "/images/tilesets/supertux.stgt"; - load_tileset(filename); } TileManager::~TileManager() { - for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) - delete *i; } -void TileManager::load_tileset(std::string filename) +TileSet* TileManager::get_tileset(const std::string &filename) { - if(filename == current_tileset) - return; - - // free old tiles - for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) - delete *i; - tiles.clear(); - - lisp_object_t* root_obj = lisp_read_from_file(filename); + TileSets::const_iterator i = tilesets.find(filename); + if(i != tilesets.end()) + return i->second; - if (!root_obj) - Termination::abort("Couldn't load file", filename); + std::auto_ptr tileset (new TileSet(filename)); + tilesets.insert(std::make_pair(filename, tileset.get())); - if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") != 0) - assert(false); + return tileset.release(); +} - lisp_object_t* cur = lisp_cdr(root_obj); - int tileset_id = 0; +TileSet* TileManager::parse_tileset_definition(const lisp::Lisp& reader) +{ + std::auto_ptr result(new TileSet()); - while(!lisp_nil_p(cur)) { - lisp_object_t* element = lisp_car(cur); + lisp::ListIterator iter(&reader); + while(iter.next()) { + const std::string& token = iter.item(); + if(token != "tileset") { + log_warning << "Skipping unrecognized token \"" << token << "\" in tileset definition" << std::endl; + continue; + } + const lisp::Lisp* tileset_reader = iter.lisp(); - if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0) - { - LispReader reader(lisp_cdr(element)); + std::string file; + if (!tileset_reader->get("file", file)) { + log_warning << "Skipping tileset import without file name" << std::endl; + continue; + } - Tile* tile = new Tile; - tile->read(reader); + const TileSet *tileset = get_tileset(file); - while(tile->id >= tiles.size()) { - tiles.push_back(0); - } - tiles[tile->id] = tile; - } - else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0) - { - LispReader reader(lisp_cdr(element)); - std::string filename; - reader.read_string("file", filename); - filename = datadir + "/images/tilesets/" + filename; - load_tileset(filename); - } - else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0) - { - TileGroup new_; - LispReader reader(lisp_cdr(element)); - reader.read_string("name", new_.name); - reader.read_int_vector("tiles", new_.tiles); - tilegroups.insert(new_).first; - } - else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0) - { - LispReader reader(lisp_cdr(element)); - reader.read_int("id", tileset_id); - tileset_id *= 1000; - } - else - { - std::cerr << "Unknown symbol: " << - lisp_symbol(lisp_car(element)) << "\n"; - } + uint32_t start = 0; + uint32_t end = std::numeric_limits::max(); + uint32_t offset = 0; + tileset_reader->get("start", start); + tileset_reader->get("end", end); + tileset_reader->get("offset", offset); - cur = lisp_cdr(cur); + result->merge(tileset, start, end, offset); } - lisp_free(root_obj); - current_tileset = filename; + return result.release(); }