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
25 #include "screen/drawing_context.h"
27 TileManager* TileManager::instance_ = 0;
28 std::set<TileGroup>* TileManager::tilegroups_ = 0;
31 : id(-1), attributes(0), data(0), next_tile(0), anim_speed(25)
37 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
41 for(std::vector<Surface*>::iterator i = editor_images.begin();
42 i != editor_images.end(); ++i) {
48 Tile::read(LispReader& reader)
50 if(!reader.read_int("id", id)) {
51 std::cerr << "Missing tile-id.\n";
56 if(reader.read_bool("solid", value) && value)
58 if(reader.read_bool("unisolid", value) && value)
60 if(reader.read_bool("brick", value) && value)
62 if(reader.read_bool("ice", value) && value)
64 if(reader.read_bool("water", value) && value)
66 if(reader.read_bool("spike", value) && value)
68 if(reader.read_bool("fullbox", value) && value)
69 attributes |= FULLBOX;
70 if(reader.read_bool("distro", value) && value)
72 if(reader.read_bool("coin", value) && value)
74 if(reader.read_bool("goal", value) && value)
77 reader.read_int("data", data);
78 reader.read_int("anim-speed", anim_speed);
79 reader.read_int("next-tile", next_tile);
81 std::vector<std::string> filenames;
82 reader.read_string_vector("images", filenames);
83 std::vector<std::string> editor_filenames;
84 reader.read_string_vector("editor-images", editor_filenames);
87 for(std::vector<std::string>::iterator i = filenames.begin();
88 i != filenames.end(); ++i) {
90 = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
91 images.push_back(surface);
93 for(std::vector<std::string>::iterator i = editor_filenames.begin();
94 i != editor_filenames.end(); ++i) {
96 = new Surface(datadir + "/images/tilesets/" + *i, USE_ALPHA);
97 editor_images.push_back(surface);
103 //---------------------------------------------------------------------------
105 TileManager::TileManager()
107 std::string filename = datadir + "/images/tilesets/supertux.stgt";
108 load_tileset(filename);
111 TileManager::~TileManager()
113 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
118 void TileManager::load_tileset(std::string filename)
120 if(filename == current_tileset)
124 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
129 lisp_object_t* root_obj = lisp_read_from_file(filename);
132 st_abort("Couldn't load file", filename);
134 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
136 lisp_object_t* cur = lisp_cdr(root_obj);
139 while(!lisp_nil_p(cur))
141 lisp_object_t* element = lisp_car(cur);
143 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
145 LispReader reader(lisp_cdr(element));
147 Tile* tile = new Tile;
148 int tile_id = tile->read(reader);
151 << "Warning: parse error when reading a tile, skipping.\n";
155 tile_id += tileset_id;
157 if(tile_id >= int(tiles.size()))
158 tiles.resize(tile_id+1);
159 tiles[tile_id] = tile;
161 else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
163 LispReader reader(lisp_cdr(element));
164 std::string filename;
165 reader.read_string("file", filename);
166 filename = datadir + "/images/tilesets/" + filename;
167 load_tileset(filename);
169 else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
172 LispReader reader(lisp_cdr(element));
173 reader.read_string("name", new_.name);
174 reader.read_int_vector("tiles", new_.tiles);
176 tilegroups_ = new std::set<TileGroup>;
177 tilegroups_->insert(new_).first;
179 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
181 LispReader reader(lisp_cdr(element));
182 reader.read_int("id", tileset_id);
187 std::cerr << "Unknown symbol: " <<
188 lisp_symbol(lisp_car(element)) << "\n";
200 current_tileset = filename;
204 TileManager::draw_tile(DrawingContext& context, unsigned int c,
205 const Vector& pos, int layer)
214 if(!tile->images.size())
217 if(tile->images.size() > 1)
220 = ((global_frame_counter*25) / tile->anim_speed) % tile->images.size();
221 context.draw_surface(tile->images[frame], pos, layer);
223 else if (tile->images.size() == 1)
225 context.draw_surface(tile->images[0], pos, layer);