2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "object/tilemap.hpp"
20 #include "scripting/squirrel_util.hpp"
21 #include "scripting/tilemap.hpp"
22 #include "supertux/main.hpp"
23 #include "supertux/object_factory.hpp"
24 #include "supertux/tile_manager.hpp"
25 #include "supertux/tile_set.hpp"
26 #include "util/reader.hpp"
28 TileMap::TileMap(const TileSet *new_tileset) :
40 drawing_effect(NO_EFFECT),
43 remaining_fade_time(0),
46 draw_target(DrawingContext::NORMAL)
50 TileMap::TileMap(const Reader& reader) :
61 movement(Vector(0,0)),
62 drawing_effect(NO_EFFECT),
65 remaining_fade_time(0),
68 draw_target(DrawingContext::NORMAL)
70 tileset = current_tileset;
71 assert(tileset != NULL);
73 reader.get("name", name);
74 reader.get("z-pos", z_pos);
75 reader.get("solid", solid);
76 reader.get("speed", speed_x);
77 reader.get("speed-y", speed_y);
79 if(solid && ((speed_x != 1) || (speed_y != 1))) {
80 log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
85 const lisp::Lisp* pathLisp = reader.get_lisp("path");
87 path.reset(new Path());
88 path->read(*pathLisp);
89 walker.reset(new PathWalker(path.get(), /*running*/false));
90 Vector v = path->get_base();
95 std::string draw_target_s = "normal";
96 reader.get("draw-target", draw_target_s);
97 if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
98 if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
100 if (reader.get("alpha", alpha)) {
101 current_alpha = alpha;
104 reader.get("width", width);
105 reader.get("height", height);
106 if(width < 0 || height < 0)
107 throw std::runtime_error("Invalid/No width/height specified in tilemap.");
109 if(!reader.get("tiles", tiles))
110 throw std::runtime_error("No tiles in tilemap.");
112 if(int(tiles.size()) != width*height) {
113 throw std::runtime_error("wrong number of tiles in tilemap.");
118 // make sure all tiles used on the tilemap are loaded and tilemap isn't empty
119 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) {
128 log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
131 TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
132 bool solid, size_t width, size_t height) :
133 tileset(new_tileset),
143 movement(Vector(0,0)),
144 drawing_effect(NO_EFFECT),
147 remaining_fade_time(0),
150 draw_target(DrawingContext::NORMAL)
154 resize(width, height);
162 TileMap::update(float elapsed_time)
164 // handle tilemap fading
165 if (current_alpha != alpha) {
166 remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
167 if (remaining_fade_time == 0.0f) {
168 current_alpha = alpha;
170 float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
171 if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
172 if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
174 if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
175 if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
178 movement = Vector(0,0);
179 // if we have a path to follow, follow it
181 Vector v = walker->advance(elapsed_time);
182 movement = Vector(v.x-get_x_offset(), std::max(0.0f,v.y-get_y_offset()));
189 TileMap::draw(DrawingContext& context)
191 // skip draw if current opacity is set to 0.0
192 if (current_alpha == 0.0) return;
194 context.push_transform();
195 context.push_target();
196 context.set_target(draw_target);
198 if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
199 if(current_alpha != 1.0) context.set_alpha(current_alpha);
201 float trans_x = roundf(context.get_translation().x);
202 float trans_y = roundf(context.get_translation().y);
203 context.set_translation(Vector(int(trans_x * speed_x),
204 int(trans_y * speed_y)));
206 /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
207 * I have no idea why */
208 float start_x = int((roundf(context.get_translation().x) - roundf(x_offset)) / 32) * 32 + roundf(x_offset);
209 float start_y = int((roundf(context.get_translation().y) - roundf(y_offset)) / 32) * 32 + roundf(y_offset);
210 float end_x = std::min(start_x + SCREEN_WIDTH + 32, float(width * 32 + roundf(x_offset)));
211 float end_y = std::min(start_y + SCREEN_HEIGHT + 32, float(height * 32 + roundf(y_offset)));
212 int tsx = int((start_x - roundf(x_offset)) / 32); // tilestartindex x
213 int tsy = int((start_y - roundf(y_offset)) / 32); // tilestartindex y
217 for(pos.x = start_x, tx = tsx; pos.x < end_x; pos.x += 32, ++tx) {
218 for(pos.y = start_y, ty = tsy; pos.y < end_y; pos.y += 32, ++ty) {
219 if ((tx < 0) || (ty < 0)) continue;
220 const Tile* tile = tileset->get(tiles[ty*width + tx]);
222 tile->draw(context, pos, z_pos);
226 context.pop_target();
227 context.pop_transform();
231 TileMap::goto_node(int node_no)
233 if (!walker.get()) return;
234 walker->goto_node(node_no);
238 TileMap::start_moving()
240 if (!walker.get()) return;
241 walker->start_moving();
245 TileMap::stop_moving()
247 if (!walker.get()) return;
248 walker->stop_moving();
252 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
254 if (name.empty()) return;
255 Scripting::TileMap* interface = new Scripting::TileMap(this);
256 expose_object(vm, table_idx, interface, name, true);
260 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
262 if (name.empty()) return;
263 Scripting::unexpose_object(vm, table_idx, name);
267 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
268 int new_z_pos, bool newsolid)
270 if(int(newt.size()) != newwidth * newheight)
271 throw std::runtime_error("Wrong tilecount count.");
276 tiles.resize(newt.size());
282 // make sure all tiles are loaded
283 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
288 TileMap::resize(int new_width, int new_height, int fill_id)
290 if(new_width < width) {
291 // remap tiles for new width
292 for(int y = 0; y < height && y < new_height; ++y) {
293 for(int x = 0; x < new_width; ++x) {
294 tiles[y * new_width + x] = tiles[y * width + x];
299 tiles.resize(new_width * new_height, fill_id);
301 if(new_width > width) {
303 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
304 for(int x = new_width-1; x >= 0; --x) {
306 tiles[y * new_width + x] = fill_id;
310 tiles[y * new_width + x] = tiles[y * width + x];
320 TileMap::set_solid(bool solid)
326 TileMap::get_tile_id(int x, int y) const
328 if(x < 0 || x >= width || y < 0 || y >= height) {
329 //log_warning << "tile outside tilemap requested" << std::endl;
333 return tiles[y*width + x];
337 TileMap::get_tile(int x, int y) const
339 uint32_t id = get_tile_id(x, y);
340 return tileset->get(id);
344 TileMap::get_tile_id_at(const Vector& pos) const
346 return get_tile_id(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32);
350 TileMap::get_tile_at(const Vector& pos) const
352 uint32_t id = get_tile_id_at(pos);
353 return tileset->get(id);
357 TileMap::change(int x, int y, uint32_t newtile)
359 assert(x >= 0 && x < width && y >= 0 && y < height);
360 tiles[y*width + x] = newtile;
364 TileMap::change_at(const Vector& pos, uint32_t newtile)
366 change(int(pos.x - x_offset)/32, int(pos.y - y_offset)/32, newtile);
370 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
372 for (size_t x = 0; x < get_width(); x++) {
373 for (size_t y = 0; y < get_height(); y++) {
374 if (get_tile_id(x,y) != oldtile)
383 TileMap::fade(float alpha, float seconds)
386 this->remaining_fade_time = seconds;
390 TileMap::set_alpha(float alpha)
393 this->current_alpha = alpha;
394 this->remaining_fade_time = 0;
395 if (current_alpha < 0.25) set_solid(false);
396 if (current_alpha > 0.75) set_solid(true);
402 return this->current_alpha;
405 IMPLEMENT_FACTORY(TileMap, "tilemap");