From f47014ecf483f4589651397f34cdb738892ff582 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Mon, 17 Oct 2005 16:37:28 +0000 Subject: [PATCH] - added a more compact form to define tiles SVN-Revision: 2861 --- data/images/tiles.strf | 33 ++++++++--------------------- src/tile.cpp | 6 ++++++ src/tile.hpp | 8 +++++--- src/tile_manager.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 71 insertions(+), 32 deletions(-) diff --git a/data/images/tiles.strf b/data/images/tiles.strf index 68c974a32..6d365349e 100644 --- a/data/images/tiles.strf +++ b/data/images/tiles.strf @@ -1056,30 +1056,15 @@ "tiles/doodads/nolok2.png" ) ) - (tile - (id 136) - (images - "tiles/signs/run1.png" - ) - ) - (tile - (id 137) - (images - "tiles/signs/run2.png" - ) - ) - (tile - (id 138) - (images - "tiles/signs/run3.png" - ) - ) - (tile - (id 139) - (images - "tiles/signs/run4.png" - ) - ) + + (tiles + (width 2) + (height 2) + (ids 136 137 + 138 139) + (attributes 0 0 0 0) + (image "tiles/signs/run.png")) + (tile (id 140) (images diff --git a/src/tile.cpp b/src/tile.cpp index 5f74a99af..68e271e12 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -36,6 +36,12 @@ Tile::Tile() { } +Tile::Tile(unsigned int id_, Uint32 attributes_, const ImageSpec& imagespec) + : id(id_), editor_image(0), attributes(attributes_), data(0), anim_fps(1) +{ + imagespecs.push_back(imagespec); +} + Tile::~Tile() { for(std::vector::iterator i = images.begin(); i != images.end(); diff --git a/src/tile.hpp b/src/tile.hpp index a551d309b..319702194 100644 --- a/src/tile.hpp +++ b/src/tile.hpp @@ -71,9 +71,6 @@ public: WORLDMAP_STOP = 0x0010 }; -private: - unsigned int id; - struct ImageSpec { ImageSpec(const std::string& newfile, const Rect& newrect) : file(newfile), rect(newrect) @@ -82,6 +79,10 @@ private: std::string file; Rect rect; }; + +private: + unsigned int id; + std::vector imagespecs; std::vector images; @@ -132,6 +133,7 @@ public: protected: friend class TileManager; Tile(); + Tile(unsigned int id, Uint32 attributes, const ImageSpec& imagespec); void load_images(const std::string& tilesetpath); diff --git a/src/tile_manager.cpp b/src/tile_manager.cpp index 6ade3d9bd..e6a3a155b 100644 --- a/src/tile_manager.cpp +++ b/src/tile_manager.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include #include "video/drawing_context.hpp" @@ -73,11 +74,56 @@ void TileManager::load_tileset(std::string filename) } tiles[tile->id] = tile; } else if(iter.item() == "tilegroup") { - TileGroup tilegroup; - const lisp::Lisp* tilegroup_lisp = iter.lisp(); - tilegroup_lisp->get("name", tilegroup.name); - tilegroup_lisp->get_vector("tiles", tilegroup.tiles); - tilegroups.insert(tilegroup); + TileGroup tilegroup; + const lisp::Lisp* tilegroup_lisp = iter.lisp(); + tilegroup_lisp->get("name", tilegroup.name); + tilegroup_lisp->get_vector("tiles", tilegroup.tiles); + tilegroups.insert(tilegroup); + } else if (iter.item() == "tiles") { + // List of ids (use 0 if the tile should be ignored) + std::vector ids; + // List of attributes of the tile + std::vector attributes; + std::string image; + + // width and height of the image in tile units, this is used for two + // purposes: + // a) so we don't have to load the image here to know its dimensions + // b) so that the resulting 'tiles' entry is more robust, + // ie. enlarging the image won't break the tile id mapping + // FIXME: height is actually not used, since width might be enough for + // all purposes, still feels somewhat more natural this way + unsigned int width = 0; + unsigned int height = 0; + + iter.lisp()->get_vector("ids", ids); + iter.lisp()->get_vector("attributes", attributes); + iter.lisp()->get("image", image); + iter.lisp()->get("width", width); + iter.lisp()->get("height", height); + + if (ids.size() != attributes.size()) + { + std::ostringstream err; + err << "Number of ids (" << ids.size() << ") and attributes (" << attributes.size() + << ") missmatch for image '" << image << "', but must be equal"; + throw std::runtime_error(err.str()); + } + + for(std::vector::size_type i = 0; i < ids.size() && i < width*height; ++i) + { + if (ids[i]) + { + if(ids[i] >= tiles.size()) + tiles.resize(ids[i]+1, 0); + + int x = 32*(i % width); + int y = 32*(i / width); + Tile* tile = new Tile(ids[i], attributes[i], Tile::ImageSpec(image, Rect(x, y, x + 32, y + 32))); + tiles[ids[i]] = tile; + } + } + } else if(iter.item() == "properties") { // deprecated } else { -- 2.11.0