4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.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
27 #include "video/drawing_context.hpp"
28 #include "lisp/lisp.hpp"
29 #include "lisp/parser.hpp"
30 #include "lisp/list_iterator.hpp"
32 #include "tile_manager.hpp"
33 #include "resources.hpp"
35 TileManager::TileManager(const std::string& filename)
37 load_tileset(filename);
40 TileManager::~TileManager()
42 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
46 void TileManager::load_tileset(std::string filename)
49 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
53 std::string::size_type t = filename.rfind('/');
54 if(t == std::string::npos) {
57 tiles_path = filename.substr(0, t+1);
61 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
63 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
65 throw std::runtime_error("file is not a supertux tiles file.");
67 lisp::ListIterator iter(tiles_lisp);
69 if(iter.item() == "tile") {
70 Tile* tile = new Tile();
71 tile->parse(*(iter.lisp()));
72 while(tile->id >= tiles.size()) {
75 tiles[tile->id] = tile;
76 } else if(iter.item() == "tilegroup") {
78 const lisp::Lisp* tilegroup_lisp = iter.lisp();
79 tilegroup_lisp->get("name", tilegroup.name);
80 tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
81 tilegroups.insert(tilegroup);
82 } else if (iter.item() == "tiles") {
83 // List of ids (use 0 if the tile should be ignored)
84 std::vector<unsigned int> ids;
85 // List of attributes of the tile
86 std::vector<unsigned int> attributes;
89 // width and height of the image in tile units, this is used for two
91 // a) so we don't have to load the image here to know its dimensions
92 // b) so that the resulting 'tiles' entry is more robust,
93 // ie. enlarging the image won't break the tile id mapping
94 // FIXME: height is actually not used, since width might be enough for
95 // all purposes, still feels somewhat more natural this way
96 unsigned int width = 0;
97 unsigned int height = 0;
99 iter.lisp()->get_vector("ids", ids);
100 iter.lisp()->get_vector("attributes", attributes);
101 iter.lisp()->get("image", image);
102 iter.lisp()->get("width", width);
103 iter.lisp()->get("height", height);
105 if (ids.size() != attributes.size())
107 std::ostringstream err;
108 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
109 << ") missmatch for image '" << image << "', but must be equal";
110 throw std::runtime_error(err.str());
113 for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
117 if(ids[i] >= tiles.size())
118 tiles.resize(ids[i]+1, 0);
120 int x = 32*(i % width);
121 int y = 32*(i / width);
122 Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
123 tiles[ids[i]] = tile;
127 } else if(iter.item() == "properties") {
130 std::cerr << "Unknown symbol '" << iter.item() << "' tile defintion file.\n";