// $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 <assert.h>
-#include "screen/drawing_context.h"
-#include "setup.h"
-#include "globals.h"
-#include "lispreader.h"
-#include "tile.h"
-#include "tile_manager.h"
-
-TileManager* TileManager::instance_ = 0;
-std::set<TileGroup>* TileManager::tilegroups_ = 0;
+#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 *tile_manager = NULL;
+TileSet *current_tileset = NULL;
TileManager::TileManager()
{
- std::string filename = datadir + "/images/tilesets/supertux.stgt";
- load_tileset(filename);
}
TileManager::~TileManager()
{
- for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
- delete *i;
- }
-
- delete tilegroups_;
}
-void TileManager::load_tileset(std::string filename)
+TileSet* TileManager::get_tileset(const std::string &filename)
{
- if(filename == current_tileset)
- return;
-
- // free old tiles
- for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
- delete *i;
- }
- tiles.clear();
-
- lisp_object_t* root_obj = lisp_read_from_file(filename);
-
- if (!root_obj)
- st_abort("Couldn't load file", filename);
-
- if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
- {
- lisp_object_t* cur = lisp_cdr(root_obj);
- int tileset_id = 0;
-
- while(!lisp_nil_p(cur))
- {
- lisp_object_t* element = lisp_car(cur);
-
- if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
- {
- LispReader reader(lisp_cdr(element));
+ TileSets::const_iterator i = tilesets.find(filename);
+ if(i != tilesets.end())
+ return i->second;
- Tile* tile = new Tile;
- int tile_id = tile->read(reader);
- if(tile_id < 0) {
- std::cerr
- << "Warning: parse error when reading a tile, skipping.\n";
- continue;
- }
+ std::auto_ptr<TileSet> tileset (new TileSet(filename));
+ tilesets.insert(std::make_pair(filename, tileset.get()));
- tile_id += tileset_id;
-
- if(tile_id >= int(tiles.size()))
- tiles.resize(tile_id+1);
- 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);
- if(!tilegroups_)
- tilegroups_ = new std::set<TileGroup>;
- 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";
- }
-
- cur = lisp_cdr(cur);
- }
- }
- else
- {
- assert(0);
- }
-
- lisp_free(root_obj);
- current_tileset = filename;
+ return tileset.release();
}
-void
-TileManager::draw_tile(DrawingContext& context, unsigned int c,
- const Vector& pos, int layer)
+TileSet* TileManager::parse_tileset_definition(const lisp::Lisp& reader)
{
- if(c == 0)
- return;
+ std::auto_ptr<TileSet> result(new TileSet());
+
+ 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();
- Tile& tile = get(c);
+ std::string file;
+ if (!tileset_reader->get("file", file)) {
+ log_warning << "Skipping tileset import without file name" << std::endl;
+ continue;
+ }
- if(!tile.images.size())
- return;
+ const TileSet *tileset = get_tileset(file);
- if(tile.images.size() > 1)
- {
- size_t frame
- = ((global_frame_counter*25) / tile.anim_speed) % tile.images.size();
- context.draw_surface(tile.images[frame], pos, layer);
- }
- else if (tile.images.size() == 1)
- {
- context.draw_surface(tile.images[0], pos, layer);
+ 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);
+
+ result->merge(tileset, start, end, offset);
}
+
+ return result.release();
}
-/* EOF */