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"
29 #include "lisp/lisp.hpp"
30 #include "lisp/parser.hpp"
31 #include "lisp/list_iterator.hpp"
33 #include "tile_manager.hpp"
34 #include "resources.hpp"
36 TileManager::TileManager(const std::string& filename)
39 Uint32 ticks = SDL_GetTicks();
41 load_tileset(filename);
43 printf("Tiles loaded in %f seconds\n", (SDL_GetTicks() - ticks) / 1000.0);
47 TileManager::~TileManager()
49 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
53 void TileManager::load_tileset(std::string filename)
56 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
60 std::string::size_type t = filename.rfind('/');
61 if(t == std::string::npos) {
64 tiles_path = filename.substr(0, t+1);
68 std::auto_ptr<lisp::Lisp> root (parser.parse(filename));
70 const lisp::Lisp* tiles_lisp = root->get_lisp("supertux-tiles");
72 throw std::runtime_error("file is not a supertux tiles file.");
74 lisp::ListIterator iter(tiles_lisp);
76 if(iter.item() == "tile") {
77 Tile* tile = new Tile();
78 tile->parse(*(iter.lisp()));
79 while(tile->id >= tiles.size()) {
82 if(tiles[tile->id] != 0) {
83 std::cout << "Warning: Tile with ID " << tile->id << " redefined\n";
85 tiles[tile->id] = tile;
86 } else if(iter.item() == "tilegroup") {
88 const lisp::Lisp* tilegroup_lisp = iter.lisp();
89 tilegroup_lisp->get("name", tilegroup.name);
90 tilegroup_lisp->get_vector("tiles", tilegroup.tiles);
91 tilegroups.insert(tilegroup);
92 } else if (iter.item() == "tiles") {
93 // List of ids (use 0 if the tile should be ignored)
94 std::vector<unsigned int> ids;
95 // List of attributes of the tile
96 std::vector<unsigned int> attributes;
99 // width and height of the image in tile units, this is used for two
101 // a) so we don't have to load the image here to know its dimensions
102 // b) so that the resulting 'tiles' entry is more robust,
103 // ie. enlarging the image won't break the tile id mapping
104 // FIXME: height is actually not used, since width might be enough for
105 // all purposes, still feels somewhat more natural this way
106 unsigned int width = 0;
107 unsigned int height = 0;
109 iter.lisp()->get_vector("ids", ids);
110 iter.lisp()->get_vector("attributes", attributes);
111 iter.lisp()->get("image", image);
112 iter.lisp()->get("width", width);
113 iter.lisp()->get("height", height);
115 if (ids.size() != attributes.size())
117 std::ostringstream err;
118 err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size()
119 << ") missmatch for image '" << image << "', but must be equal";
120 throw std::runtime_error(err.str());
123 for(std::vector<unsigned int>::size_type i = 0; i < ids.size() && i < width*height; ++i)
127 if(ids[i] >= tiles.size())
128 tiles.resize(ids[i]+1, 0);
130 int x = 32*(i % width);
131 int y = 32*(i / width);
132 Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32)));
133 tiles[ids[i]] = tile;
137 } else if(iter.item() == "properties") {
140 std::cerr << "Unknown symbol '" << iter.item() << "' tile defintion file.\n";