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/globals.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) :
39 drawing_effect(NO_EFFECT),
42 remaining_fade_time(0),
45 draw_target(DrawingContext::NORMAL)
49 TileMap::TileMap(const Reader& reader) :
59 movement(Vector(0,0)),
60 drawing_effect(NO_EFFECT),
63 remaining_fade_time(0),
66 draw_target(DrawingContext::NORMAL)
68 tileset = current_tileset;
69 assert(tileset != NULL);
71 reader.get("name", name);
72 reader.get("z-pos", z_pos);
73 reader.get("solid", solid);
74 reader.get("speed", speed_x);
75 reader.get("speed-y", speed_y);
77 if(solid && ((speed_x != 1) || (speed_y != 1))) {
78 log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
83 if (z_pos > (LAYER_GUI - 100)) {
84 log_warning << "z-pos of "
85 << ((name == "") ? "unnamed tilemap" : name) << " (" << z_pos << ") "
87 << "Clipping to " << (LAYER_GUI - 100) << "." << std::endl;
88 z_pos = LAYER_GUI - 100;
91 const lisp::Lisp* pathLisp = reader.get_lisp("path");
93 path.reset(new Path());
94 path->read(*pathLisp);
95 walker.reset(new PathWalker(path.get(), /*running*/false));
96 Vector v = path->get_base();
100 std::string draw_target_s = "normal";
101 reader.get("draw-target", draw_target_s);
102 if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
103 if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
105 if (reader.get("alpha", alpha)) {
106 current_alpha = alpha;
109 reader.get("width", width);
110 reader.get("height", height);
111 if(width < 0 || height < 0)
112 throw std::runtime_error("Invalid/No width/height specified in tilemap.");
114 if(!reader.get("tiles", tiles))
115 throw std::runtime_error("No tiles in tilemap.");
117 if(int(tiles.size()) != width*height) {
118 throw std::runtime_error("wrong number of tiles in tilemap.");
123 // make sure all tiles used on the tilemap are loaded and tilemap isn't empty
124 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) {
133 log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
136 TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
137 bool solid, size_t width, size_t height) :
138 tileset(new_tileset),
147 movement(Vector(0,0)),
148 drawing_effect(NO_EFFECT),
151 remaining_fade_time(0),
154 draw_target(DrawingContext::NORMAL)
158 if (this->z_pos > (LAYER_GUI - 100))
159 this->z_pos = LAYER_GUI - 100;
161 resize(width, height);
169 TileMap::update(float elapsed_time)
171 // handle tilemap fading
172 if (current_alpha != alpha) {
173 remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
174 if (remaining_fade_time == 0.0f) {
175 current_alpha = alpha;
177 float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
178 if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
179 if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
181 if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
182 if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
185 movement = Vector(0,0);
186 // if we have a path to follow, follow it
188 Vector v = walker->advance(elapsed_time);
189 movement = Vector(v.x-get_offset().x, std::max(0.0f,v.y-get_offset().y));
195 TileMap::draw(DrawingContext& context)
197 // skip draw if current opacity is 0.0
198 if (current_alpha == 0.0) return;
200 context.push_transform();
201 if(draw_target != DrawingContext::NORMAL) {
202 context.push_target();
203 context.set_target(draw_target);
206 if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
207 if(current_alpha != 1.0) context.set_alpha(current_alpha);
209 /* Force the translation to be an integer so that the tiles appear sharper.
210 * For consistency (i.e., to avoid 1-pixel gaps), this needs to be done even
211 * for solid tilemaps that are guaranteed to have speed 1.
212 * FIXME Force integer translation for all graphics, not just tilemaps. */
213 float trans_x = roundf(context.get_translation().x);
214 float trans_y = roundf(context.get_translation().y);
215 context.set_translation(Vector(int(trans_x * speed_x),
216 int(trans_y * speed_y)));
218 Rectf draw_rect = Rectf(context.get_translation(),
219 context.get_translation() + Vector(SCREEN_WIDTH, SCREEN_HEIGHT));
220 Rect t_draw_rect = get_tiles_overlapping(draw_rect);
221 Vector start = get_tile_position(t_draw_rect.left, t_draw_rect.top);
226 for(pos.x = start.x, tx = t_draw_rect.left; tx < t_draw_rect.right; pos.x += 32, ++tx) {
227 for(pos.y = start.y, ty = t_draw_rect.top; ty < t_draw_rect.bottom; pos.y += 32, ++ty) {
228 int index = ty*width + tx;
230 assert (index < (width * height));
232 if (tiles[index] == 0) continue;
233 const Tile* tile = tileset->get(tiles[index]);
235 tile->draw(context, pos, z_pos);
239 if(draw_target != DrawingContext::NORMAL) {
240 context.pop_target();
242 context.pop_transform();
246 TileMap::goto_node(int node_no)
248 if (!walker.get()) return;
249 walker->goto_node(node_no);
253 TileMap::start_moving()
255 if (!walker.get()) return;
256 walker->start_moving();
260 TileMap::stop_moving()
262 if (!walker.get()) return;
263 walker->stop_moving();
267 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
269 if (name.empty()) return;
270 scripting::TileMap* _this = new scripting::TileMap(this);
271 expose_object(vm, table_idx, _this, name, true);
275 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
277 if (name.empty()) return;
278 scripting::unexpose_object(vm, table_idx, name);
282 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
283 int new_z_pos, bool newsolid)
285 if(int(newt.size()) != newwidth * newheight)
286 throw std::runtime_error("Wrong tilecount count.");
291 tiles.resize(newt.size());
294 if (new_z_pos > (LAYER_GUI - 100))
295 z_pos = LAYER_GUI - 100;
300 // make sure all tiles are loaded
301 for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
306 TileMap::resize(int new_width, int new_height, int fill_id)
308 if(new_width < width) {
309 // remap tiles for new width
310 for(int y = 0; y < height && y < new_height; ++y) {
311 for(int x = 0; x < new_width; ++x) {
312 tiles[y * new_width + x] = tiles[y * width + x];
317 tiles.resize(new_width * new_height, fill_id);
319 if(new_width > width) {
321 for(int y = std::min(height, new_height)-1; y >= 0; --y) {
322 for(int x = new_width-1; x >= 0; --x) {
324 tiles[y * new_width + x] = fill_id;
328 tiles[y * new_width + x] = tiles[y * width + x];
338 TileMap::get_tiles_overlapping(const Rectf &rect) const
343 int t_left = std::max(0 , int(floorf(rect2.get_left () / 32)));
344 int t_right = std::min(width , int(ceilf (rect2.get_right () / 32)));
345 int t_top = std::max(0 , int(floorf(rect2.get_top () / 32)));
346 int t_bottom = std::min(height, int(ceilf (rect2.get_bottom() / 32)));
347 return Rect(t_left, t_top, t_right, t_bottom);
351 TileMap::set_solid(bool solid)
357 TileMap::get_tile_id(int x, int y) const
359 if(x < 0 || x >= width || y < 0 || y >= height) {
360 //log_warning << "tile outside tilemap requested" << std::endl;
364 return tiles[y*width + x];
368 TileMap::get_tile(int x, int y) const
370 uint32_t id = get_tile_id(x, y);
371 return tileset->get(id);
375 TileMap::get_tile_id_at(const Vector& pos) const
377 Vector xy = (pos - offset) / 32;
378 return get_tile_id(int(xy.x), int(xy.y));
382 TileMap::get_tile_at(const Vector& pos) const
384 uint32_t id = get_tile_id_at(pos);
385 return tileset->get(id);
389 TileMap::change(int x, int y, uint32_t newtile)
391 assert(x >= 0 && x < width && y >= 0 && y < height);
392 tiles[y*width + x] = newtile;
396 TileMap::change_at(const Vector& pos, uint32_t newtile)
398 Vector xy = (pos - offset) / 32;
399 change(int(xy.x), int(xy.y), newtile);
403 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
405 for (size_t x = 0; x < get_width(); x++) {
406 for (size_t y = 0; y < get_height(); y++) {
407 if (get_tile_id(x,y) != oldtile)
416 TileMap::fade(float alpha, float seconds)
419 this->remaining_fade_time = seconds;
423 TileMap::set_alpha(float alpha)
426 this->current_alpha = alpha;
427 this->remaining_fade_time = 0;
428 if (current_alpha < 0.25) set_solid(false);
429 if (current_alpha > 0.75) set_solid(true);
435 return this->current_alpha;