src/object/tilemap.cpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
00003 //
00004 //  This program is free software: you can redistribute it and/or modify
00005 //  it under the terms of the GNU General Public License as published by
00006 //  the Free Software Foundation, either version 3 of the License, or
00007 //  (at your option) any later version.
00008 //
00009 //  This program is distributed in the hope that it will be useful,
00010 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //  GNU General Public License for more details.
00013 //
00014 //  You should have received a copy of the GNU General Public License
00015 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017 #include <math.h>
00018 
00019 #include "object/tilemap.hpp"
00020 #include "scripting/squirrel_util.hpp"
00021 #include "scripting/tilemap.hpp"
00022 #include "supertux/globals.hpp"
00023 #include "supertux/object_factory.hpp"
00024 #include "supertux/tile_manager.hpp"
00025 #include "supertux/tile_set.hpp"
00026 #include "util/reader.hpp"
00027 
00028 TileMap::TileMap(const TileSet *new_tileset) :
00029   tileset(new_tileset), 
00030   tiles(),
00031   real_solid(false),
00032   effective_solid(false),
00033   speed_x(1), 
00034   speed_y(1), 
00035   width(0),
00036   height(0), 
00037   z_pos(0), 
00038   offset(Vector(0,0)),
00039   movement(0,0),
00040   drawing_effect(NO_EFFECT),
00041   alpha(1.0), 
00042   current_alpha(1.0),
00043   remaining_fade_time(0),
00044   path(),
00045   walker(),
00046   draw_target(DrawingContext::NORMAL)
00047 {
00048 }
00049 
00050 TileMap::TileMap(const Reader& reader) :
00051   tileset(),
00052   tiles(),
00053   real_solid(false),
00054   effective_solid(false),
00055   speed_x(1), 
00056   speed_y(1), 
00057   width(-1),
00058   height(-1), 
00059   z_pos(0), 
00060   offset(Vector(0,0)),
00061   movement(Vector(0,0)), 
00062   drawing_effect(NO_EFFECT),
00063   alpha(1.0), 
00064   current_alpha(1.0), 
00065   remaining_fade_time(0),
00066   path(),
00067   walker(),
00068   draw_target(DrawingContext::NORMAL)
00069 {
00070   tileset = current_tileset;
00071   assert(tileset != NULL);
00072 
00073   reader.get("name",   name);
00074   reader.get("solid",  real_solid);
00075   reader.get("speed",  speed_x);
00076   reader.get("speed-y", speed_y);
00077 
00078   z_pos = reader_get_layer (reader, /* default = */ 0);
00079   
00080   if(real_solid && ((speed_x != 1) || (speed_y != 1))) {
00081     log_warning << "Speed of solid tilemap is not 1. fixing" << std::endl;
00082     speed_x = 1;
00083     speed_y = 1;
00084   }
00085 
00086   const lisp::Lisp* pathLisp = reader.get_lisp("path");
00087   if (pathLisp) {
00088     path.reset(new Path());
00089     path->read(*pathLisp);
00090     walker.reset(new PathWalker(path.get(), /*running*/false));
00091     Vector v = path->get_base();
00092     set_offset(v);
00093   }
00094 
00095   std::string draw_target_s = "normal";
00096   reader.get("draw-target", draw_target_s);
00097   if (draw_target_s == "normal") draw_target = DrawingContext::NORMAL;
00098   if (draw_target_s == "lightmap") draw_target = DrawingContext::LIGHTMAP;
00099 
00100   if (reader.get("alpha", alpha)) {
00101     current_alpha = alpha;
00102   }
00103 
00104   /* Initialize effective_solid based on real_solid and current_alpha. */
00105   effective_solid = real_solid;
00106   update_effective_solid ();
00107 
00108   reader.get("width", width);
00109   reader.get("height", height);
00110   if(width < 0 || height < 0)
00111     throw std::runtime_error("Invalid/No width/height specified in tilemap.");
00112 
00113   if(!reader.get("tiles", tiles))
00114     throw std::runtime_error("No tiles in tilemap.");
00115 
00116   if(int(tiles.size()) != width*height) {
00117     throw std::runtime_error("wrong number of tiles in tilemap.");
00118   }
00119 
00120   bool empty = true;
00121 
00122   // make sure all tiles used on the tilemap are loaded and tilemap isn't empty
00123   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i) {
00124     if(*i != 0) {
00125       empty = false;
00126     }
00127 
00128     tileset->get(*i);
00129   }
00130 
00131   if(empty)
00132     log_info << "Tilemap '" << name << "', z-pos '" << z_pos << "' is empty." << std::endl;
00133 }
00134 
00135 TileMap::TileMap(const TileSet *new_tileset, std::string name, int z_pos,
00136                  bool solid, size_t width, size_t height) :
00137   tileset(new_tileset), 
00138   tiles(),
00139   real_solid(solid),
00140   effective_solid(solid),
00141   speed_x(1), 
00142   speed_y(1), 
00143   width(0),
00144   height(0), 
00145   z_pos(z_pos), 
00146   offset(Vector(0,0)),
00147   movement(Vector(0,0)),
00148   drawing_effect(NO_EFFECT), 
00149   alpha(1.0), 
00150   current_alpha(1.0),
00151   remaining_fade_time(0), 
00152   path(),
00153   walker(),
00154   draw_target(DrawingContext::NORMAL)
00155 {
00156   this->name = name;
00157 
00158   if (this->z_pos > (LAYER_GUI - 100))
00159     this->z_pos = LAYER_GUI - 100;
00160 
00161   resize(width, height);
00162 }
00163 
00164 TileMap::~TileMap()
00165 {
00166 }
00167 
00168 void
00169 TileMap::update(float elapsed_time)
00170 {
00171   // handle tilemap fading
00172   if (current_alpha != alpha) {
00173     remaining_fade_time = std::max(0.0f, remaining_fade_time - elapsed_time);
00174     if (remaining_fade_time == 0.0f) {
00175       current_alpha = alpha;
00176     } else {
00177       float amt = (alpha - current_alpha) / (remaining_fade_time / elapsed_time);
00178       if (amt > 0) current_alpha = std::min(current_alpha + amt, alpha);
00179       if (amt < 0) current_alpha = std::max(current_alpha + amt, alpha);
00180     }
00181     update_effective_solid ();
00182   }
00183 
00184   movement = Vector(0,0);
00185   // if we have a path to follow, follow it
00186   if (walker.get()) {
00187     Vector v = walker->advance(elapsed_time);
00188     movement = v - get_offset();
00189     set_offset(v);
00190   }
00191 }
00192 
00193 void
00194 TileMap::draw(DrawingContext& context)
00195 {
00196   // skip draw if current opacity is 0.0
00197   if (current_alpha == 0.0) return;
00198 
00199   context.push_transform();
00200   if(draw_target != DrawingContext::NORMAL) {
00201     context.push_target();
00202     context.set_target(draw_target);
00203   }
00204 
00205   if(drawing_effect != 0) context.set_drawing_effect(drawing_effect);
00206   if(current_alpha != 1.0) context.set_alpha(current_alpha);
00207 
00208   /* Force the translation to be an integer so that the tiles appear sharper.
00209    * For consistency (i.e., to avoid 1-pixel gaps), this needs to be done even
00210    * for solid tilemaps that are guaranteed to have speed 1.
00211    * FIXME Force integer translation for all graphics, not just tilemaps. */
00212   float trans_x = roundf(context.get_translation().x);
00213   float trans_y = roundf(context.get_translation().y);
00214   context.set_translation(Vector(int(trans_x * speed_x),
00215                                  int(trans_y * speed_y)));
00216 
00217   Rectf draw_rect = Rectf(context.get_translation(),
00218         context.get_translation() + Vector(SCREEN_WIDTH, SCREEN_HEIGHT));
00219   Rect t_draw_rect = get_tiles_overlapping(draw_rect);
00220   Vector start = get_tile_position(t_draw_rect.left, t_draw_rect.top);
00221 
00222   Vector pos;
00223   int tx, ty;
00224 
00225   for(pos.x = start.x, tx = t_draw_rect.left; tx < t_draw_rect.right; pos.x += 32, ++tx) {
00226     for(pos.y = start.y, ty = t_draw_rect.top; ty < t_draw_rect.bottom; pos.y += 32, ++ty) {
00227       int index = ty*width + tx;
00228       assert (index >= 0);
00229       assert (index < (width * height));
00230 
00231       if (tiles[index] == 0) continue;
00232       const Tile* tile = tileset->get(tiles[index]);
00233       assert(tile != 0);
00234       tile->draw(context, pos, z_pos);
00235     } /* for (pos y) */
00236   } /* for (pos x) */
00237 
00238   if(draw_target != DrawingContext::NORMAL) {
00239     context.pop_target();
00240   }
00241   context.pop_transform();
00242 }
00243 
00244 void
00245 TileMap::goto_node(int node_no)
00246 {
00247   if (!walker.get()) return;
00248   walker->goto_node(node_no);
00249 }
00250 
00251 void
00252 TileMap::start_moving()
00253 {
00254   if (!walker.get()) return;
00255   walker->start_moving();
00256 }
00257 
00258 void
00259 TileMap::stop_moving()
00260 {
00261   if (!walker.get()) return;
00262   walker->stop_moving();
00263 }
00264 
00265 void
00266 TileMap::expose(HSQUIRRELVM vm, SQInteger table_idx)
00267 {
00268   if (name.empty()) return;
00269   scripting::TileMap* _this = new scripting::TileMap(this);
00270   expose_object(vm, table_idx, _this, name, true);
00271 }
00272 
00273 void
00274 TileMap::unexpose(HSQUIRRELVM vm, SQInteger table_idx)
00275 {
00276   if (name.empty()) return;
00277   scripting::unexpose_object(vm, table_idx, name);
00278 }
00279 
00280 void
00281 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
00282              int new_z_pos, bool newsolid)
00283 {
00284   if(int(newt.size()) != newwidth * newheight)
00285     throw std::runtime_error("Wrong tilecount count.");
00286 
00287   width  = newwidth;
00288   height = newheight;
00289 
00290   tiles.resize(newt.size());
00291   tiles = newt;
00292 
00293   if (new_z_pos > (LAYER_GUI - 100))
00294     z_pos = LAYER_GUI - 100;
00295   else
00296     z_pos  = new_z_pos;
00297   real_solid  = newsolid;
00298   update_effective_solid ();
00299 
00300   // make sure all tiles are loaded
00301   for(Tiles::iterator i = tiles.begin(); i != tiles.end(); ++i)
00302     tileset->get(*i);
00303 }
00304 
00305 void
00306 TileMap::resize(int new_width, int new_height, int fill_id)
00307 {
00308   if(new_width < width) {
00309     // remap tiles for new width
00310     for(int y = 0; y < height && y < new_height; ++y) {
00311       for(int x = 0; x < new_width; ++x) {
00312         tiles[y * new_width + x] = tiles[y * width + x];
00313       }
00314     }
00315   }
00316 
00317   tiles.resize(new_width * new_height, fill_id);
00318 
00319   if(new_width > width) {
00320     // remap tiles
00321     for(int y = std::min(height, new_height)-1; y >= 0; --y) {
00322       for(int x = new_width-1; x >= 0; --x) {
00323         if(x >= width) {
00324           tiles[y * new_width + x] = fill_id;
00325           continue;
00326         }
00327 
00328         tiles[y * new_width + x] = tiles[y * width + x];
00329       }
00330     }
00331   }
00332 
00333   height = new_height;
00334   width = new_width;
00335 }
00336 
00337 Rect
00338 TileMap::get_tiles_overlapping(const Rectf &rect) const
00339 {
00340   Rectf rect2 = rect;
00341   rect2.move(-offset);
00342 
00343   int t_left   = std::max(0     , int(floorf(rect2.get_left  () / 32)));
00344   int t_right  = std::min(width , int(ceilf (rect2.get_right () / 32)));
00345   int t_top    = std::max(0     , int(floorf(rect2.get_top   () / 32)));
00346   int t_bottom = std::min(height, int(ceilf (rect2.get_bottom() / 32)));
00347   return Rect(t_left, t_top, t_right, t_bottom);
00348 }
00349 
00350 void
00351 TileMap::set_solid(bool solid)
00352 {
00353   this->real_solid = solid;
00354   update_effective_solid ();
00355 }
00356 
00357 uint32_t
00358 TileMap::get_tile_id(int x, int y) const
00359 {
00360   if(x < 0 || x >= width || y < 0 || y >= height) {
00361     //log_warning << "tile outside tilemap requested" << std::endl;
00362     return 0;
00363   }
00364 
00365   return tiles[y*width + x];
00366 }
00367 
00368 const Tile*
00369 TileMap::get_tile(int x, int y) const
00370 {
00371   uint32_t id = get_tile_id(x, y);
00372   return tileset->get(id);
00373 }
00374 
00375 uint32_t
00376 TileMap::get_tile_id_at(const Vector& pos) const
00377 {
00378   Vector xy = (pos - offset) / 32;
00379   return get_tile_id(int(xy.x), int(xy.y));
00380 }
00381 
00382 const Tile*
00383 TileMap::get_tile_at(const Vector& pos) const
00384 {
00385   uint32_t id = get_tile_id_at(pos);
00386   return tileset->get(id);
00387 }
00388 
00389 void
00390 TileMap::change(int x, int y, uint32_t newtile)
00391 {
00392   assert(x >= 0 && x < width && y >= 0 && y < height);
00393   tiles[y*width + x] = newtile;
00394 }
00395 
00396 void
00397 TileMap::change_at(const Vector& pos, uint32_t newtile)
00398 {
00399   Vector xy = (pos - offset) / 32;
00400   change(int(xy.x), int(xy.y), newtile);
00401 }
00402 
00403 void
00404 TileMap::change_all(uint32_t oldtile, uint32_t newtile)
00405 {
00406   for (size_t x = 0; x < get_width(); x++) {
00407     for (size_t y = 0; y < get_height(); y++) {
00408       if (get_tile_id(x,y) != oldtile)
00409         continue;
00410 
00411       change(x,y,newtile);
00412     }
00413   }
00414 }
00415 
00416 void
00417 TileMap::fade(float alpha, float seconds)
00418 {
00419   this->alpha = alpha;
00420   this->remaining_fade_time = seconds;
00421 }
00422 
00423 void 
00424 TileMap::set_alpha(float alpha)
00425 {
00426   this->alpha = alpha;
00427   this->current_alpha = alpha;
00428   this->remaining_fade_time = 0;
00429   update_effective_solid ();
00430 }
00431 
00432 float 
00433 TileMap::get_alpha()
00434 {
00435   return this->current_alpha;
00436 }
00437  
00438 /*
00439  * Private methods
00440  */
00441 void
00442 TileMap::update_effective_solid (void)
00443 {
00444   if (!real_solid)
00445     effective_solid = false;
00446   else if (effective_solid && (current_alpha < .25))
00447     effective_solid = false;
00448   else if (!effective_solid && (current_alpha >= .75))
00449     effective_solid = true;
00450 }
00451 
00452 /* EOF */

Generated on Mon Apr 21 03:38:16 2014 for SuperTux by  doxygen 1.5.1