-Worldmap cleanups (use DrawingContext transformstack)
[supertux.git] / src / object / tilemap.cpp
index 0878441..59557d1 100644 (file)
 #include "video/drawing_context.h"
 #include "level.h"
 #include "tile.h"
+#include "resources.h"
 #include "tile_manager.h"
 #include "app/globals.h"
-#include "utils/lispreader.h"
-#include "utils/lispwriter.h"
+#include "lisp/lisp.h"
+#include "lisp/writer.h"
+#include "object_factory.h"
 
 TileMap::TileMap()
   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES),
-    vertical_flip(false)
+    drawing_effect(0)
 {
-  tilemanager = TileManager::instance();
+  tilemanager = tile_manager;
 
   if(solid)
     flags |= FLAG_SOLID;
 }
 
-TileMap::TileMap(LispReader& reader)
+TileMap::TileMap(const lisp::Lisp& reader)
   : solid(false), speed(1), width(0), height(0), layer(LAYER_TILES),
-    vertical_flip(false)
+    drawing_effect(0)
 {
-  tilemanager = TileManager::instance();
+  tilemanager = tile_manager;
 
   std::string layer_str;
-  if(reader.read_string("layer", layer_str)) {
+  if(reader.get("layer", layer_str)) {
     if(layer_str == "background")
       layer = LAYER_BACKGROUNDTILES;
     else if(layer_str == "interactive")
@@ -61,8 +63,8 @@ TileMap::TileMap(LispReader& reader)
       std::cerr << "Unknown layer '" << layer_str << "' in tilemap.\n";
   }
 
-  reader.read_bool("solid", solid);
-  reader.read_float("speed", speed);
+  reader.get("solid", solid);
+  reader.get("speed", speed);
 
   if(solid && speed != 1) {
     std::cout << "Speed of solid tilemap is not 1. fixing.\n";
@@ -71,22 +73,23 @@ TileMap::TileMap(LispReader& reader)
   if(solid)
     flags |= FLAG_SOLID;
   
-  if(!reader.read_int("width", width) ||
-     !reader.read_int("height", height))
+  if(!reader.get("width", width) ||
+     !reader.get("height", height))
     throw std::runtime_error("No width or height specified in tilemap.");
 
-  if(!reader.read_int_vector("tiles", tiles))
+  if(!reader.get_vector("tiles", tiles))
     throw std::runtime_error("No tiles in tilemap.");
 
-  if(int(tiles.size()) != width*height)
+  if(int(tiles.size()) != width*height) {
     throw std::runtime_error("wrong number of tiles in tilemap.");
+  }
 }
 
 TileMap::TileMap(int layer_, bool solid_, size_t width_, size_t height_)
   : solid(solid_), speed(1), width(0), height(0), layer(layer_),
-    vertical_flip(false)
+    drawing_effect(0)
 {
-  tilemanager = TileManager::instance();
+  tilemanager = tile_manager;
   
   resize(width_, height_);
 
@@ -99,7 +102,7 @@ TileMap::~TileMap()
 }
 
 void
-TileMap::write(LispWriter& writer)
+TileMap::write(lisp::Writer& writer)
 {
   writer.start_list("tilemap");
 
@@ -133,8 +136,8 @@ TileMap::draw(DrawingContext& context)
 {
   context.push_transform();
 
-  if(vertical_flip)
-    context.set_drawing_effect(VERTICAL_FLIP); 
+  if(drawing_effect != 0)
+    context.set_drawing_effect(drawing_effect); 
   float trans_x = roundf(context.get_translation().x);
   float trans_y = roundf(context.get_translation().y);
   context.set_translation(Vector(trans_x * speed, trans_y * speed));
@@ -142,7 +145,9 @@ TileMap::draw(DrawingContext& context)
   /** if we don't round here, we'll have a 1 pixel gap on screen sometimes.
    * I have no idea why */
   float start_x = roundf(context.get_translation().x);
+  if(start_x < 0) start_x = 0;
   float start_y = roundf(context.get_translation().y);
+  if(start_y < 0) start_y = 0;
   float end_x = std::min(start_x + screen->w, float(width * 32));
   float end_y = std::min(start_y + screen->h, float(height * 32));
   start_x -= int(start_x) % 32;
@@ -182,7 +187,8 @@ void
 TileMap::set(int newwidth, int newheight, const std::vector<unsigned int>&newt,
     int newlayer, bool newsolid)
 {
-  assert(int(newt.size()) == newwidth * newheight);
+  if(int(newt.size()) != newwidth * newheight)
+    throw std::runtime_error("Wrong tilecount count.");
 
   width  = newwidth;
   height = newheight;
@@ -228,19 +234,6 @@ TileMap::resize(int new_width, int new_height)
   width = new_width;
 }
 
-void
-TileMap::do_vertical_flip()
-{
-  // remap tiles vertically flipped
-  for(int y = 0; y < height / 2; ++y) {
-    for(int x = 0; x < width; ++x) {
-      std::swap(tiles[y*width + x], tiles[(((height-1)*width) - (y*width)) + x]);
-    }
-  }
-
-  vertical_flip = true;
-}
-
 const Tile*
 TileMap::get_tile(int x, int y) const
 {
@@ -272,3 +265,5 @@ TileMap::change_at(const Vector& pos, uint32_t newtile)
 {
   change(int(pos.x)/32, int(pos.y)/32, newtile);
 }
+
+IMPLEMENT_FACTORY(TileMap, "tilemap");