7dce250abd6d02bf8c5d039f24263a1702ab2aa9
[supertux.git] / src / object / tilemap.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
17 #include <math.h>
18
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"
27
28 TileMap::TileMap(const TileSet *new_tileset) :
29   tileset(new_tileset), 
30   tiles(),
31   solid(false), 
32   speed_x(1), 
33   speed_y(1), 
34   width(0),
35   height(0), 
36   z_pos(0), 
37   offset(Vector(0,0)),
38   movement(0,0),
39   drawing_effect(NO_EFFECT),
40   alpha(1.0), 
41   current_alpha(1.0),
42   remaining_fade_time(0),
43   path(),
44   walker(),
45   draw_target(DrawingContext::NORMAL)
46 {
47 }
48
49 TileMap::TileMap(const Reader& reader) :
50   tileset(),
51   tiles(),
52   solid(false), 
53   speed_x(1), 
54   speed_y(1), 
55   width(-1),
56   height(-1), 
57   z_pos(0), 
58   offset(Vector(0,0)),
59   movement(Vector(0,0)), 
60   drawing_effect(NO_EFFECT),
61   alpha(1.0), 
62   current_alpha(1.0), 
63   remaining_fade_time(0),
64   path(),
65   walker(),
66   draw_target(DrawingContext::NORMAL)
67 {
68   tileset = current_tileset;
69   assert(tileset != NULL);
70
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);
76   
77   if(solid && ((speed_x != 1) || (speed_y != 1))) {
78     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
79     speed_x = 1;
80     speed_y = 1;
81   }
82
83   const lisp::Lisp* pathLisp = reader.get_lisp("path");
84   if (pathLisp) {
85     path.reset(new Path());
86     path->read(*pathLisp);
87     walker.reset(new PathWalker(path.get(), /*running*/false));
88     Vector v = path->get_base();
89     set_offset(v);
90   }
91
92   std::string draw_target_s = "normal";
93   reader.get("draw-target", draw_target_s);
94   if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
95   if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
96
97   if (reader.get("alpha", alpha)) {
98     current_alpha = alpha;
99   }
100
101   reader.get("width", width);
102   reader.get("height", height);
103   if(width < 0 || height < 0)
104     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
105
106   if(!reader.get("tiles", tiles))
107     throw std::runtime_error("No tiles in tilemap.");
108
109   if(int(tiles.size()) != width*height) {
110     throw std::runtime_error("wrong number of tiles in tilemap.");
111   }
112
113   bool empty = true;
114
115   // make sure all tiles used on the tilemap are loaded and tilemap isn't empty
116   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) {
117     if(*i != 0) {
118       empty = false;
119     }
120
121     tileset->get(*i);
122   }
123
124   if(empty)
125     log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
126 }
127
128 TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
129                  bool solid, size_t width, size_t height) :
130   tileset(new_tileset), 
131   tiles(),
132   solid(solid), 
133   speed_x(1), 
134   speed_y(1), 
135   width(0),
136   height(0), 
137   z_pos(z_pos), 
138   offset(Vector(0,0)),
139   movement(Vector(0,0)),
140   drawing_effect(NO_EFFECT), 
141   alpha(1.0), 
142   current_alpha(1.0),
143   remaining_fade_time(0), 
144   path(),
145   walker(),
146   draw_target(DrawingContext::NORMAL)
147 {
148   this->name = name;
149
150   resize(width, height);
151 }
152
153 TileMap::~TileMap()
154 {
155 }
156
157 void
158 TileMap::update(float elapsed_time)
159 {
160   // handle tilemap fading
161   if (current_alpha != alpha) {
162     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
163     if (remaining_fade_time == 0.0f) {
164       current_alpha = alpha;
165     } else {
166       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
167       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
168       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
169     }
170     if ((alpha < 0.25) && (current_alpha < 0.25)) set_solid(false);
171     if ((alpha > 0.75) && (current_alpha > 0.75)) set_solid(true);
172   }
173
174   movement = Vector(0,0);
175   // if we have a path to follow, follow it
176   if (walker.get()) {
177     Vector v = walker->advance(elapsed_time);
178     movement = Vector(v.x-get_offset().x, std::max(0.0f,v.y-get_offset().y));
179     set_offset(v);
180   }
181 }
182
183 void
184 TileMap::draw(DrawingContext& context)
185 {
186   // skip draw if current opacity is 0.0
187   if (current_alpha == 0.0) return;
188
189   context.push_transform();
190   if(draw_target != DrawingContext::NORMAL) {
191     context.push_target();
192     context.set_target(draw_target);
193   }
194
195   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
196   if(current_alpha != 1.0) context.set_alpha(current_alpha);
197
198   /* Force the translation to be an integer so that the tiles appear sharper.
199    * For consistency (i.e., to avoid 1-pixel gaps), this needs to be done even
200    * for solid tilemaps that are guaranteed to have speed 1.
201    * FIXME Force integer translation for all graphics, not just tilemaps. */
202   float trans_x = roundf(context.get_translation().x);
203   float trans_y = roundf(context.get_translation().y);
204   context.set_translation(Vector(int(trans_x * speed_x),
205                                  int(trans_y * speed_y)));
206
207   Rectf draw_rect = Rectf(context.get_translation(),
208         context.get_translation() + Vector(SCREEN_WIDTH, SCREEN_HEIGHT));
209   Rect t_draw_rect = get_tiles_overlapping(draw_rect);
210   Vector start = get_tile_position(t_draw_rect.left, t_draw_rect.top);
211
212   Vector pos;
213   int tx, ty;
214
215   for(pos.x = start.x, tx = t_draw_rect.left; tx < t_draw_rect.right; pos.x += 32, ++tx) {
216     for(pos.y = start.y, ty = t_draw_rect.top; ty < t_draw_rect.bottom; pos.y += 32, ++ty) {
217       int index = ty*width + tx;
218       assert (index >= 0);
219       assert (index < (width * height));
220
221       if (tiles[index] == 0) continue;
222       const Tile* tile = tileset->get(tiles[index]);
223       assert(tile != 0);
224       tile->draw(context, pos, z_pos);
225     } /* for (pos y) */
226   } /* for (pos x) */
227
228   if(draw_target != DrawingContext::NORMAL) {
229     context.pop_target();
230   }
231   context.pop_transform();
232 }
233
234 void
235 TileMap::goto_node(int node_no)
236 {
237   if (!walker.get()) return;
238   walker->goto_node(node_no);
239 }
240
241 void
242 TileMap::start_moving()
243 {
244   if (!walker.get()) return;
245   walker->start_moving();
246 }
247
248 void
249 TileMap::stop_moving()
250 {
251   if (!walker.get()) return;
252   walker->stop_moving();
253 }
254
255 void
256 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
257 {
258   if (name.empty()) return;
259   scripting::TileMap* _this = new scripting::TileMap(this);
260   expose_object(vm, table_idx, _this, name, true);
261 }
262
263 void
264 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
265 {
266   if (name.empty()) return;
267   scripting::unexpose_object(vm, table_idx, name);
268 }
269
270 void
271 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
272              int new_z_pos, bool newsolid)
273 {
274   if(int(newt.size()) != newwidth * newheight)
275     throw std::runtime_error("Wrong tilecount count.");
276
277   width  = newwidth;
278   height = newheight;
279
280   tiles.resize(newt.size());
281   tiles = newt;
282
283   z_pos  = new_z_pos;
284   solid  = newsolid;
285
286   // make sure all tiles are loaded
287   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
288     tileset->get(*i);
289 }
290
291 void
292 TileMap::resize(int new_width, int new_height, int fill_id)
293 {
294   if(new_width < width) {
295     // remap tiles for new width
296     for(int y = 0; y < height && y < new_height; ++y) {
297       for(int x = 0; x < new_width; ++x) {
298         tiles[y * new_width + x] = tiles[y * width + x];
299       }
300     }
301   }
302
303   tiles.resize(new_width * new_height, fill_id);
304
305   if(new_width > width) {
306     // remap tiles
307     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
308       for(int x = new_width-1; x >= 0; --x) {
309         if(x >= width) {
310           tiles[y * new_width + x] = fill_id;
311           continue;
312         }
313
314         tiles[y * new_width + x] = tiles[y * width + x];
315       }
316     }
317   }
318
319   height = new_height;
320   width = new_width;
321 }
322
323 Rect
324 TileMap::get_tiles_overlapping(const Rectf &rect) const
325 {
326   Rectf rect2 = rect;
327   rect2.move(-offset);
328
329   int t_left   = std::max(0     , int(floorf(rect2.get_left  () / 32)));
330   int t_right  = std::min(width , int(ceilf (rect2.get_right () / 32)));
331   int t_top    = std::max(0     , int(floorf(rect2.get_top   () / 32)));
332   int t_bottom = std::min(height, int(ceilf (rect2.get_bottom() / 32)));
333   return Rect(t_left, t_top, t_right, t_bottom);
334 }
335
336 void
337 TileMap::set_solid(bool solid)
338 {
339   this->solid = solid;
340 }
341
342 uint32_t
343 TileMap::get_tile_id(int x, int y) const
344 {
345   if(x < 0 || x >= width || y < 0 || y >= height) {
346     //log_warning << "tile outside tilemap requested" << std::endl;
347     return 0;
348   }
349
350   return tiles[y*width + x];
351 }
352
353 const Tile*
354 TileMap::get_tile(int x, int y) const
355 {
356   uint32_t id = get_tile_id(x, y);
357   return tileset->get(id);
358 }
359
360 uint32_t
361 TileMap::get_tile_id_at(const Vector& pos) const
362 {
363   Vector xy = (pos - offset) / 32;
364   return get_tile_id(int(xy.x), int(xy.y));
365 }
366
367 const Tile*
368 TileMap::get_tile_at(const Vector& pos) const
369 {
370   uint32_t id = get_tile_id_at(pos);
371   return tileset->get(id);
372 }
373
374 void
375 TileMap::change(int x, int y, uint32_t newtile)
376 {
377   assert(x >= 0 && x < width && y >= 0 && y < height);
378   tiles[y*width + x] = newtile;
379 }
380
381 void
382 TileMap::change_at(const Vector& pos, uint32_t newtile)
383 {
384   Vector xy = (pos - offset) / 32;
385   change(int(xy.x), int(xy.y), newtile);
386 }
387
388 void
389 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
390 {
391   for (size_t x = 0; x < get_width(); x++) {
392     for (size_t y = 0; y < get_height(); y++) {
393       if (get_tile_id(x,y) != oldtile)
394         continue;
395
396       change(x,y,newtile);
397     }
398   }
399 }
400
401 void
402 TileMap::fade(float alpha, float seconds)
403 {
404   this->alpha = alpha;
405   this->remaining_fade_time = seconds;
406 }
407
408 void 
409 TileMap::set_alpha(float alpha)
410 {
411   this->alpha = alpha;
412   this->current_alpha = alpha;
413   this->remaining_fade_time = 0;
414   if (current_alpha < 0.25) set_solid(false);
415   if (current_alpha > 0.75) set_solid(true);
416 }
417
418 float 
419 TileMap::get_alpha()
420 {
421   return this->current_alpha;
422 }
423  
424
425 /* EOF */