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
26 #include "screen/drawing_context.h"
28 TileManager* TileManager::instance_ = 0;
29 std::set<TileGroup>* TileManager::tilegroups_ = 0;
32 : id(-1), attributes(0), data(0), next_tile(0), anim_speed(25)
38 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
42 for(std::vector<Surface*>::iterator i = editor_images.begin();
43 i != editor_images.end(); ++i) {
49 Tile::read(LispReader& reader)
51 if(!reader.read_int("id", id)) {
52 std::cerr << "Missing tile-id.\n";
57 if(reader.read_bool("solid", value) && value)
59 if(reader.read_bool("unisolid", value) && value)
61 if(reader.read_bool("brick", value) && value)
63 if(reader.read_bool("ice", value) && value)
65 if(reader.read_bool("water", value) && value)
67 if(reader.read_bool("spike", value) && value)
69 if(reader.read_bool("fullbox", value) && value)
70 attributes |= FULLBOX;
71 if(reader.read_bool("distro", value) && value)
73 if(reader.read_bool("coin", value) && value)
75 if(reader.read_bool("goal", value) && value)
78 reader.read_int("data", data);
79 reader.read_int("anim-speed", anim_speed);
80 reader.read_int("next-tile", next_tile);
82 std::vector<std::string> filenames;
83 reader.read_string_vector("images", filenames);
84 std::vector<std::string> editor_filenames;
85 reader.read_string_vector("editor-images", editor_filenames);
87 std::vector<int> grid;
88 reader.read_int_vector("grid", grid);
91 for(std::vector<std::string>::iterator i = filenames.begin();
92 i != filenames.end(); ++i)
96 Surface* surface = new Surface(datadir + "/images/tilesets/" + *i,
97 grid[0], grid[1], grid[2], grid[3],
99 images.push_back(surface);
103 Surface* surface = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
104 images.push_back(surface);
108 for(std::vector<std::string>::iterator i = editor_filenames.begin();
109 i != editor_filenames.end(); ++i) {
111 = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
112 editor_images.push_back(surface);
118 //---------------------------------------------------------------------------
120 TileManager::TileManager()
122 std::string filename = datadir + "/images/tilesets/supertux.stgt";
123 load_tileset(filename);
126 TileManager::~TileManager()
128 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
135 void TileManager::load_tileset(std::string filename)
137 if(filename == current_tileset)
141 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
146 lisp_object_t* root_obj = lisp_read_from_file(filename);
149 st_abort("Couldn't load file", filename);
151 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
153 lisp_object_t* cur = lisp_cdr(root_obj);
156 while(!lisp_nil_p(cur))
158 lisp_object_t* element = lisp_car(cur);
160 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
162 LispReader reader(lisp_cdr(element));
164 Tile* tile = new Tile;
165 int tile_id = tile->read(reader);
168 << "Warning: parse error when reading a tile, skipping.\n";
172 tile_id += tileset_id;
174 if(tile_id >= int(tiles.size()))
175 tiles.resize(tile_id+1);
176 tiles[tile_id] = tile;
178 else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
180 LispReader reader(lisp_cdr(element));
181 std::string filename;
182 reader.read_string("file", filename);
183 filename = datadir + "/images/tilesets/" + filename;
184 load_tileset(filename);
186 else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
189 LispReader reader(lisp_cdr(element));
190 reader.read_string("name", new_.name);
191 reader.read_int_vector("tiles", new_.tiles);
193 tilegroups_ = new std::set<TileGroup>;
194 tilegroups_->insert(new_).first;
196 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
198 LispReader reader(lisp_cdr(element));
199 reader.read_int("id", tileset_id);
204 std::cerr << "Unknown symbol: " <<
205 lisp_symbol(lisp_car(element)) << "\n";
217 current_tileset = filename;
221 TileManager::draw_tile(DrawingContext& context, unsigned int c,
222 const Vector& pos, int layer)
229 if(!tile.images.size())
232 if(tile.images.size() > 1)
235 = ((global_frame_counter*25) / tile.anim_speed) % tile.images.size();
236 context.draw_surface(tile.images[frame], pos, layer);
238 else if (tile.images.size() == 1)
240 context.draw_surface(tile.images[0], pos, layer);