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
28 #include "video/drawing_context.hpp"
30 #include "lisp/lisp.hpp"
31 #include "lisp/parser.hpp"
32 #include "lisp/list_iterator.hpp"
34 #include "tile_manager.hpp"
35 #include "resources.hpp"
37 TileManager::TileManager(const std::string& filename)
40 Uint32 ticks = SDL_GetTicks();
42 load_tileset(filename);
44 log_debug << "Tiles loaded in " << (SDL_GetTicks() - ticks) / 1000.0 << " seconds" << std::endl;
48 TileManager::~TileManager()
50 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
54 void TileManager::load_tileset(std::string filename)
57 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
61 std::string::size_type t = filename.rfind('/');
62 if(t == std::string::npos) {
65 tiles_path = filename.substr(0, t+1);
69 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
71 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
73 throw std::runtime_error("file is not a supertux tiles file.");
75 lisp::ListIterator iter(tiles_lisp);
77 if(iter.item() == "tile") {
78 Tile* tile = new Tile();
79 tile->parse(*(iter.lisp()));
80 while(tile->id >= tiles.size()) {
83 if(tiles[tile->id] != 0) {
84 log_warning << "Tile with ID " << tile->id << " redefined" << std::endl;
86 tiles[tile->id] = tile;
87 } else if(iter.item() == "tilegroup") {
89 const lisp::Lisp* tilegroup_lisp = iter.lisp();
90 tilegroup_lisp->get("name", tilegroup.name);
91 tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
92 tilegroups.insert(tilegroup);
93 } else if (iter.item() == "tiles") {
94 // List of ids (use 0 if the tile should be ignored)
95 std::vector<unsigned int> ids;
96 // List of attributes of the tile
97 std::vector<unsigned int> attributes;
100 // width and height of the image in tile units, this is used for two
102 // a) so we don't have to load the image here to know its dimensions
103 // b) so that the resulting 'tiles' entry is more robust,
104 // ie. enlarging the image won't break the tile id mapping
105 // FIXME: height is actually not used, since width might be enough for
106 // all purposes, still feels somewhat more natural this way
107 unsigned int width = 0;
108 unsigned int height = 0;
110 iter.lisp()->get_vector("ids", ids);
111 iter.lisp()->get_vector("attributes", attributes);
112 iter.lisp()->get("image", image);
113 iter.lisp()->get("width", width);
114 iter.lisp()->get("height", height);
116 if (ids.size() != attributes.size())
118 std::ostringstream err;
119 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
120 << ") missmatch for image '" << image << "', but must be equal";
121 throw std::runtime_error(err.str());
124 for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
128 if(ids[i] >= tiles.size())
129 tiles.resize(ids[i]+1, 0);
131 int x = 32*(i % width);
132 int y = 32*(i / width);
133 Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
134 tiles[ids[i]] = tile;
138 } else if(iter.item() == "properties") {
141 log_warning << "Unknown symbol '" << iter.item() << "' tile defintion file" << std::endl;