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 TileManager* TileManager::instance_ = 0;
26 std::set<TileGroup>* TileManager::tilegroups_ = 0;
34 for(std::vector<Surface*>::iterator i = images.begin(); i != images.end();
38 for(std::vector<Surface*>::iterator i = editor_images.begin();
39 i != editor_images.end(); ++i) {
44 //---------------------------------------------------------------------------
46 TileManager::TileManager()
48 std::string filename = datadir + "/images/tilesets/supertux.stgt";
49 load_tileset(filename);
52 TileManager::~TileManager()
54 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
59 void TileManager::load_tileset(std::string filename)
61 if(filename == current_tileset)
65 for(std::vector<Tile*>::iterator i = tiles.begin(); i != tiles.end(); ++i) {
70 lisp_object_t* root_obj = lisp_read_from_file(filename);
73 st_abort("Couldn't load file", filename);
75 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-tiles") == 0)
77 lisp_object_t* cur = lisp_cdr(root_obj);
80 while(!lisp_nil_p(cur))
82 lisp_object_t* element = lisp_car(cur);
84 if (strcmp(lisp_symbol(lisp_car(element)), "tile") == 0)
88 Tile* tile = new Tile;
95 tile->fullbox = false;
100 tile->anim_speed = 25;
102 LispReader reader(lisp_cdr(element));
103 assert(reader.read_int("id", &tile->id));
104 reader.read_bool("solid", &tile->solid);
105 reader.read_bool("brick", &tile->brick);
106 reader.read_bool("ice", &tile->ice);
107 reader.read_bool("water", &tile->water);
108 reader.read_bool("spike", &tile->spike);
109 reader.read_bool("fullbox", &tile->fullbox);
110 reader.read_bool("distro", &tile->distro);
111 reader.read_bool("goal", &tile->goal);
112 reader.read_int("data", &tile->data);
113 reader.read_int("anim-speed", &tile->anim_speed);
114 reader.read_int("next-tile", &tile->next_tile);
115 reader.read_string_vector("images", &tile->filenames);
116 reader.read_string_vector("editor-images", &tile->editor_filenames);
118 for(std::vector<std::string>::iterator it = tile->
120 it != tile->filenames.end();
124 tile->images.push_back(cur_image);
125 tile->images[tile->images.size()-1] = new Surface(
126 datadir + "/images/tilesets/" + (*it),
129 for(std::vector<std::string>::iterator it = tile->editor_filenames.begin();
130 it != tile->editor_filenames.end();
134 tile->editor_images.push_back(cur_image);
135 tile->editor_images[tile->editor_images.size()-1] = new Surface(
136 datadir + "/images/tilesets/" + (*it),
140 if (tile->id + tileset_id >= int(tiles.size())
142 tiles.resize(tile->id + tileset_id+1);
144 tiles[tile->id + tileset_id] = tile;
146 else if (strcmp(lisp_symbol(lisp_car(element)), "tileset") == 0)
148 LispReader reader(lisp_cdr(element));
149 std::string filename;
150 reader.read_string("file", &filename);
151 filename = datadir + "/images/tilesets/" + filename;
152 load_tileset(filename);
154 else if (strcmp(lisp_symbol(lisp_car(element)), "tilegroup") == 0)
157 LispReader reader(lisp_cdr(element));
158 reader.read_string("name", &new_.name);
159 reader.read_int_vector("tiles", &new_.tiles);
161 tilegroups_ = new std::set<TileGroup>;
162 tilegroups_->insert(new_).first;
164 else if (strcmp(lisp_symbol(lisp_car(element)), "properties") == 0)
166 LispReader reader(lisp_cdr(element));
167 reader.read_int("id", &tileset_id);
172 puts("Unhandled symbol");
184 current_tileset = filename;
188 Tile::draw(float x, float y, unsigned int c, Uint8 alpha)
192 Tile* ptile = TileManager::instance()->get(c);
195 if(ptile->images.size() > 1)
197 ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))]->draw(x,y, alpha);
199 else if (ptile->images.size() == 1)
201 ptile->images[0]->draw(x,y, alpha);
205 //printf("Tile not dravable %u\n", c);
212 Tile::draw_stretched(float x, float y, int w, int h, unsigned int c, Uint8 alpha)
216 Tile* ptile = TileManager::instance()->get(c);
219 if(ptile->images.size() > 1)
221 ptile->images[( ((global_frame_counter*25) / ptile->anim_speed) % (ptile->images.size()))]->draw_stretched(x,y,w,h, alpha);
223 else if (ptile->images.size() == 1)
225 ptile->images[0]->draw_stretched(x,y, w, h, alpha);
229 //printf("Tile not dravable %u\n", c);