// $Id$
-//
+//
// SuperTux
// Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
+// Copyright (C) 2006 Matthias Braun <matze@braunis.de>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// 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 <config.h>
+#include <memory>
+#include <stdexcept>
+#include <sstream>
+#include <iostream>
+#include <limits>
#include <assert.h>
-#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 <SDL.h>
+#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> 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<TileSet> 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<uint32_t>::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();
}