4 // Copyright (C) 2004 Tobias Glaesser <tobi.web@gmx.de>
5 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
29 #include "video/drawing_context.hpp"
31 #include "lisp/lisp.hpp"
32 #include "lisp/parser.hpp"
33 #include "lisp/list_iterator.hpp"
35 #include "tile_manager.hpp"
36 #include "resources.hpp"
38 TileManager* tile_manager = NULL;
40 TileManager::TileManager(const std::string& filename)
43 Uint32 ticks = SDL_GetTicks();
45 load_tileset(filename);
47 log_debug << "Tiles loaded in " << (SDL_GetTicks() - ticks) / 1000.0 << " seconds" << std::endl;
51 TileManager::~TileManager()
53 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
57 void TileManager::load_tileset(std::string filename)
60 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
64 std::string::size_type t = filename.rfind('/');
65 if(t == std::string::npos) {
68 tiles_path = filename.substr(0, t+1);
72 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
74 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
76 throw std::runtime_error("file is not a supertux tiles file.");
78 lisp::ListIterator iter(tiles_lisp);
80 if(iter.item() == "tile") {
81 Tile* tile = new Tile();
82 tile->parse(*(iter.lisp()));
83 while(tile->id >= tiles.size()) {
86 if(tiles[tile->id] != 0) {
87 log_warning << "Tile with ID " << tile->id << " redefined" << std::endl;
89 tiles[tile->id] = tile;
90 } else if(iter.item() == "tilegroup") {
92 const lisp::Lisp* tilegroup_lisp = iter.lisp();
93 tilegroup_lisp->get("name", tilegroup.name);
94 tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
95 tilegroups.insert(tilegroup);
96 } else if (iter.item() == "tiles") {
97 // List of ids (use 0 if the tile should be ignored)
98 std::vector<unsigned int> ids;
99 // List of attributes of the tile
100 std::vector<unsigned int> attributes;
103 // width and height of the image in tile units, this is used for two
105 // a) so we don't have to load the image here to know its dimensions
106 // b) so that the resulting 'tiles' entry is more robust,
107 // ie. enlarging the image won't break the tile id mapping
108 // FIXME: height is actually not used, since width might be enough for
109 // all purposes, still feels somewhat more natural this way
110 unsigned int width = 0;
111 unsigned int height = 0;
113 iter.lisp()->get_vector("ids", ids);
114 iter.lisp()->get_vector("attributes", attributes);
115 iter.lisp()->get("image", image);
116 iter.lisp()->get("width", width);
117 iter.lisp()->get("height", height);
119 if (ids.size() != attributes.size())
121 std::ostringstream err;
122 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
123 << ") missmatch for image '" << image << "', but must be equal";
124 throw std::runtime_error(err.str());
127 for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
131 if(ids[i] >= tiles.size())
132 tiles.resize(ids[i]+1, 0);
134 int x = 32*(i % width);
135 int y = 32*(i / width);
136 Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
137 tiles[ids[i]] = tile;
141 } else if(iter.item() == "properties") {
144 log_warning << "Unknown symbol '" << iter.item() << "' tile defintion file" << std::endl;