Removed friendship between TileSetParser and Tile, use proper constructor instead
[supertux.git] / src / supertux / tile.cpp
index 01cdff0..3a17375 100644 (file)
 #include "supertux/tile.hpp"
 
 #include "supertux/tile_set.hpp"
-#include "supertux/timer.hpp"
-#include "util/reader.hpp"
 #include "video/drawing_context.hpp"
 
-Tile::Tile(const TileSet *new_tileset) :
+Tile::Tile(const TileSetnew_tileset) :
   tileset(new_tileset), 
   imagespecs(),
   images(),
@@ -32,18 +30,15 @@ Tile::Tile(const TileSet *new_tileset) :
 {
 }
 
-Tile::Tile(const TileSet *new_tileset, std::vector<std::string> images, Rect rect
+Tile::Tile(const TileSet& new_tileset, const std::vector<ImageSpec>& imagespecs_
            uint32_t attributes, uint32_t data, float animfps) :
   tileset(new_tileset),
-  imagespecs(),
+  imagespecs(imagespecs_),
   images(),
   attributes(attributes), 
   data(data), 
   anim_fps(animfps)
 {
-  for(std::vector<std::string>::iterator i = images.begin(); i != images.end(); ++i) {
-    imagespecs.push_back(ImageSpec(*i, rect));
-  }
   correct_attributes();
 }
 
@@ -55,125 +50,31 @@ Tile::~Tile()
   }
 }
 
-uint32_t
-Tile::parse(const Reader& reader)
-{
-  uint32_t id;
-  if(!reader.get("id", id)) {
-    throw std::runtime_error("Missing tile-id.");
-  }
-
-  bool value = false;
-  if(reader.get("solid", value) && value)
-    attributes |= SOLID;
-  if(reader.get("unisolid", value) && value)
-    attributes |= UNISOLID | SOLID;
-  if(reader.get("brick", value) && value)
-    attributes |= BRICK;
-  if(reader.get("ice", value) && value)
-    attributes |= ICE;
-  if(reader.get("water", value) && value)
-    attributes |= WATER;
-  if(reader.get("hurts", value) && value)
-    attributes |= HURTS;
-  if(reader.get("fire", value) && value)
-    attributes |= FIRE;
-  if(reader.get("fullbox", value) && value)
-    attributes |= FULLBOX;
-  if(reader.get("coin", value) && value)
-    attributes |= COIN;
-  if(reader.get("goal", value) && value)
-    attributes |= GOAL;
-
-  if(reader.get("north", value) && value)
-    data |= WORLDMAP_NORTH;
-  if(reader.get("south", value) && value)
-    data |= WORLDMAP_SOUTH;
-  if(reader.get("west", value) && value)
-    data |= WORLDMAP_WEST;
-  if(reader.get("east", value) && value)
-    data |= WORLDMAP_EAST;
-  if(reader.get("stop", value) && value)
-    data |= WORLDMAP_STOP;
-
-  reader.get("data", data);
-  reader.get("anim-fps", anim_fps);
-
-  if(reader.get("slope-type", data)) {
-    attributes |= SOLID | SLOPE;
-  }
-
-  const lisp::Lisp* images;
-#ifndef NDEBUG
-  images = reader.get_lisp("editor-images");
-  if(images)
-    parse_images(*images);
-  else {
-#endif
-    images = reader.get_lisp("images");
-    if(images)
-      parse_images(*images);
-#ifndef NDEBUG
-  }
-#endif
-
-  correct_attributes();
-  return id;
-}
-
-void
-Tile::parse_images(const Reader& images_lisp)
-{
-  const lisp::Lisp* list = &images_lisp;
-  while(list) {
-    const lisp::Lisp* cur = list->get_car();
-    if(cur->get_type() == lisp::Lisp::TYPE_STRING) {
-      std::string file;
-      cur->get(file);
-      imagespecs.push_back(ImageSpec(file, Rect(0, 0, 0, 0)));
-    } else if(cur->get_type() == lisp::Lisp::TYPE_CONS &&
-              cur->get_car()->get_type() == lisp::Lisp::TYPE_SYMBOL &&
-              cur->get_car()->get_symbol() == "region") {
-      const lisp::Lisp* ptr = cur->get_cdr();
-
-      std::string file;
-      float x = 0, y = 0, w = 0, h = 0;
-      ptr->get_car()->get(file); ptr = ptr->get_cdr();
-      ptr->get_car()->get(x); ptr = ptr->get_cdr();
-      ptr->get_car()->get(y); ptr = ptr->get_cdr();
-      ptr->get_car()->get(w); ptr = ptr->get_cdr();
-      ptr->get_car()->get(h);
-      imagespecs.push_back(ImageSpec(file, Rect(x, y, x+w, y+h)));
-    } else {
-      log_warning << "Expected string or list in images tag" << std::endl;
-      continue;
-    }
-
-    list = list->get_cdr();
-  }
-}
-
 void
 Tile::load_images()
 {
-  const std::string& tiles_path = tileset->tiles_path;
-
-  assert(images.size() == 0);
-  for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i !=
-        imagespecs.end(); ++i) {
-    const ImageSpec& spec = *i;
-    Surface* surface;
-    std::string file = tiles_path + spec.file;
-    if(spec.rect.get_width() <= 0) {
-      surface = new Surface(file);
-    } else {
-      surface = new Surface(file,
-                            (int) spec.rect.p1.x,
-                            (int) spec.rect.p1.y,
-                            (int) spec.rect.get_width(),
-                            (int) spec.rect.get_height());
+  if(images.size() == 0 && imagespecs.size() != 0)
+  {
+    assert(images.size() == 0);
+    for(std::vector<ImageSpec>::iterator i = imagespecs.begin(); i != imagespecs.end(); ++i) 
+    {
+      const ImageSpec& spec = *i;
+
+      Surface* surface;
+      if(spec.rect.get_width() <= 0) 
+      {
+        surface = new Surface(spec.file);
+      }
+      else 
+      {
+        surface = new Surface(spec.file,
+                              (int) spec.rect.p1.x,
+                              (int) spec.rect.p1.y,
+                              (int) spec.rect.get_width(),
+                              (int) spec.rect.get_height());
+      }
+      images.push_back(surface);
     }
-    images.push_back(surface);
   }
 }
 
@@ -188,7 +89,8 @@ Tile::draw(DrawingContext& context, const Vector& pos, int z_pos) const
   }
 }
 
-void Tile::correct_attributes()
+void
+Tile::correct_attributes()
 {
   //Fix little oddities in attributes (not many, currently...)
   if(!(attributes & SOLID) && (attributes & SLOPE || attributes & UNISOLID)) {
@@ -198,4 +100,14 @@ void Tile::correct_attributes()
   }
 }
 
+void
+Tile::print_debug(int id) const
+{
+  log_debug << " Tile: id " << id << ", data " << getData() << ", attributes " << getAttributes() << ":" << std::endl;
+  for(std::vector<Tile::ImageSpec>::const_iterator im = imagespecs.begin(); im != imagespecs.end(); ++im) 
+  {
+    log_debug << "  Imagespec: file " << im->file << "; rect " << im->rect << std::endl;
+  }
+}
+
 /* EOF */