From c62de9aa47285c1145c5e24dae5638bfc47bbf52 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Thu, 23 Oct 2008 20:07:06 +0000 Subject: Added missing header and print newline at end of usage instructions --- tools/tmxcopy/main.cpp | 2 +- tools/tmxcopy/map.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/tmxcopy/main.cpp b/tools/tmxcopy/main.cpp index ab67afe6..ac18ce04 100644 --- a/tools/tmxcopy/main.cpp +++ b/tools/tmxcopy/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char * argv[] ) // parsing command line options if (argc < 9 || argc > 10) { - std::cerr<<"Usage: srcFile x y width height tgtFile x y [outfile]"; + std::cerr<<"Usage: srcFile x y width height tgtFile x y [outfile]\n"; return -1; } diff --git a/tools/tmxcopy/map.cpp b/tools/tmxcopy/map.cpp index 92e661fd..d9fc8ada 100644 --- a/tools/tmxcopy/map.cpp +++ b/tools/tmxcopy/map.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include "xmlutils.h" -- cgit v1.2.3-70-g09d2 From de6302f27a5454bdc8fca2fcc2e958916826450f Mon Sep 17 00:00:00 2001 From: Steve Cotton Date: Sun, 8 Mar 2009 00:34:12 +0000 Subject: Fix tmxcopy off-by-one error translating tilesets The first tiles of each tileset got shuffled. --- tools/tmxcopy/map.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'tools') diff --git a/tools/tmxcopy/map.cpp b/tools/tmxcopy/map.cpp index d9fc8ada..1c374e25 100644 --- a/tools/tmxcopy/map.cpp +++ b/tools/tmxcopy/map.cpp @@ -155,7 +155,7 @@ Map::Map(std::string filename): { for (int s = mTilesets.size()-1; s >= 0; s--) { - if (mTilesets.at(s)->firstgid < gid) + if (mTilesets.at(s)->firstgid <= gid) { layer->at(c).tileset = s; layer->at(c).index = gid - mTilesets.at(s)->firstgid; @@ -204,6 +204,10 @@ bool Map::overwrite( Map* srcMap, std::cerr<<"Error: Area exceeds lower map border of target map!"<getNumberOfLayers() > mLayers.size()) { + std::cerr<<"Error: Source has more layers than target map"< translation; -- cgit v1.2.3-70-g09d2 From b1c08a56778308897b2a46d6111084044583de0e Mon Sep 17 00:00:00 2001 From: Steve Cotton Date: Mon, 9 Mar 2009 22:11:06 +0000 Subject: Make tmxcopy pair up layers by name --- tools/tmxcopy/main.cpp | 68 ++++++++++++++++++++------ tools/tmxcopy/map.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++---- tools/tmxcopy/map.hpp | 52 ++++++++++++++++++-- tools/tmxcopy/readme.txt | 18 ++++++- 4 files changed, 231 insertions(+), 28 deletions(-) (limited to 'tools') diff --git a/tools/tmxcopy/main.cpp b/tools/tmxcopy/main.cpp index ac18ce04..4926d69f 100644 --- a/tools/tmxcopy/main.cpp +++ b/tools/tmxcopy/main.cpp @@ -1,6 +1,7 @@ /* * TMXCopy * Copyright (C) 2007 Philipp Sehmisch + * Copyright (C) 2009 Steve Cotton * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,34 +20,70 @@ #include #include +#include #include "map.hpp" +void printUsage() +{ + std::cerr<<"Usage: tmxcopy [-c] [-n] srcFile x y width height tgtFile x y [outfile]"< 10) + ConfigurationOptions config = {0}; + int opt; + while ((opt = getopt(argc, argv, "cn")) != -1) + { + switch (opt) + { + case 'c': + config.createMissingLayers = true; + break; + case 'n': + config.copyLayersByOrdinal = true; + break; + case '?': + std::cerr<<"Unrecognized option"< 9) { - std::cerr<<"Usage: srcFile x y width height tgtFile x y [outfile]\n"; + std::cerr<<"Too many args"<overwrite(srcMap, srcX, srcY, width, height, destX, destY)) + if (tgtMap->overwrite(srcMap, srcX, srcY, width, height, destX, destY, config)) { tgtMap->save(outFile); } else { return -1; } + delete srcMap; + delete tgtMap; } catch (int) { return -1; } - } diff --git a/tools/tmxcopy/map.cpp b/tools/tmxcopy/map.cpp index 1c374e25..75cbecbb 100644 --- a/tools/tmxcopy/map.cpp +++ b/tools/tmxcopy/map.cpp @@ -1,6 +1,7 @@ /* * TMXCopy * Copyright (C) 2007 Philipp Sehmisch + * Copyright (C) 2009 Steve Cotton * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,6 +25,7 @@ #include #include +#include #include "xmlutils.h" #include "zlibutils.h" @@ -80,9 +82,19 @@ Map::Map(std::string filename): { if (xmlStrEqual(node->name, BAD_CAST "layer")) { - Layer* layer = new Layer; - layer->resize(mWidth * mHeight); //build layer information + std::string name = XML::getProperty(node, "name", ""); + Layer* layer = new Layer(name, mWidth * mHeight); + if ( + (mWidth != XML::getProperty(node, "width" , 0)) || + (mHeight != XML::getProperty(node, "height", 0)) || + (0 != XML::getProperty(node, "x" , 0)) || + (0 != XML::getProperty(node, "y" , 0))) + { + std::cerr<<"Error: layer size does not match map size for layer \""<name, BAD_CAST "data")) continue; @@ -184,7 +196,8 @@ Map::Map(std::string filename): bool Map::overwrite( Map* srcMap, int srcX, int srcY, int srcWidth, int srcHeight, - int destX, int destY) + int destX, int destY, + const ConfigurationOptions& config) { //plausibility check of coordinates bool checkPassed = true; @@ -204,10 +217,32 @@ bool Map::overwrite( Map* srcMap, std::cerr<<"Error: Area exceeds lower map border of target map!"<getNumberOfLayers() > mLayers.size()) { - std::cerr<<"Error: Source has more layers than target map"<getNumberOfLayers() > mLayers.size()) { + std::cerr<<"Error: Source has more layers than target map"<getNumberOfLayers(); i++) + { + Layer* srcLayer = srcMap->getLayer(i); + Layer* destLayer = getLayer(srcLayer->getName()); + if (!destLayer) + { + std::cerr<<"Error: target map has no layer named \""<getName()<<"\""< translation; @@ -238,7 +273,43 @@ bool Map::overwrite( Map* srcMap, for (int i = 0; i < srcMap->getNumberOfLayers(); i++) { Layer* srcLayer = srcMap->getLayer(i); - Layer* destLayer = mLayers.at(i); + Layer* destLayer = NULL; + if (config.copyLayersByOrdinal) + { + if (i < mLayers.size()) + { + destLayer = mLayers.at(i); + } + } + else + { + destLayer = getLayer(srcLayer->getName()); + } + + if (!destLayer) + { + assert(config.createMissingLayers); /* Tested earlier, in the checkPassed section */ + /* Generate a name for the new layer, which must be + * unique in the target map, and should be unique in + * the source map (to avoid collisions later in the + * copying process). + * Start by trying the name of the source layer. + */ + std::string name = srcLayer->getName(); + if (getLayer(name)) + { + int k=0; + do + { + name = "Layer" + toString(k); + k++; + } while (getLayer(name) || srcMap->getLayer(name)); + } + + destLayer = new Layer(name, mWidth * mHeight); + mLayers.push_back(destLayer); + std::cout<<"Created new layer "<getName()).c_str()); xmlNewProp(newNode, BAD_CAST "width", BAD_CAST toString(mWidth).c_str()); xmlNewProp(newNode, BAD_CAST "height", BAD_CAST toString(mHeight).c_str()); xmlAddChild(newNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n ")); @@ -358,6 +429,9 @@ int Map::save(std::string filename) xmlAddChild(newNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n ")); xmlAddChild(rootNode, newNode); xmlAddChild(rootNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n")); + + free(base64Data); + free(binData); } //save XML tree @@ -374,3 +448,34 @@ int Map::save(std::string filename) return true; } } + +Layer* Map::getLayer(std::string name) +{ + for (std::vector::iterator layer = mLayers.begin(); + layer != mLayers.end(); + layer++) + { + if ((*layer)->getName() == name) + return *layer; + } + return NULL; +} + +Map::~Map() +{ + for (std::vector::iterator layer = mLayers.begin(); + layer != mLayers.end(); + layer++) + { + delete *layer; + } + + for (std::vector::iterator tileset = mTilesets.begin(); + tileset != mTilesets.end(); + tileset++) + { + delete *tileset; + } + + xmlFreeDoc(mXmlDoc); +} diff --git a/tools/tmxcopy/map.hpp b/tools/tmxcopy/map.hpp index 89ae1405..6dd7794a 100644 --- a/tools/tmxcopy/map.hpp +++ b/tools/tmxcopy/map.hpp @@ -1,7 +1,7 @@ /* * TMXCopy * Copyright 2007 Philipp Sehmisch - * + * Copyright 2009 Steve Cotton * * TMXCopy is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,22 @@ #include #include +struct ConfigurationOptions +{ + /* When copying map layers, how to match source layer to + * destination layer. + * + * True: Pair the first layer to the first layer, the second + * to the second, etc. + * + * False: Pair up layers with matching names. + */ + bool copyLayersByOrdinal; + + /* Create extra layers in the target as necessary. */ + bool createMissingLayers; +}; + struct Tileset { std::string imagefile; @@ -46,22 +62,50 @@ struct Tile size_t index; // index in said tileset }; -typedef std::vector Layer; +typedef std::vector LayerTiles; + +/* This represents an empty tile in the layer. + * Note that {0,0} would be the first tile in the first tileset. + */ +const Tile defaultTile = {-1, 0}; + +class Layer +{ + public: + /* name - the name of the layer, as shown in Tiled + * tileCount - total number of tiles (width*height) + */ + Layer(std::string name, LayerTiles::size_type tileCount) + : mTiles(tileCount, defaultTile), + mName (name) + { + } + + std::string getName() { return mName; } + Tile& at(LayerTiles::size_type c) { return mTiles.at(c); } + + private: + LayerTiles mTiles; + std::string mName; +}; class Map { public: Map(std::string filename); + ~Map(); - bool overwrite( Map* srcMap, + bool overwrite(Map* srcMap, int srcX, int srcY, int srcWidth, int srcHeight, - int destX, int destY); + int destX, int destY, + const ConfigurationOptions& config); int save(std::string filename); int getNumberOfLayers() { return mLayers.size(); } Layer* getLayer(size_t num) { return mLayers.at(num); } + Layer* getLayer(std::string name); std::vector* getTilesets() { return &mTilesets; } diff --git a/tools/tmxcopy/readme.txt b/tools/tmxcopy/readme.txt index e4235b94..e8ec830a 100644 --- a/tools/tmxcopy/readme.txt +++ b/tools/tmxcopy/readme.txt @@ -23,6 +23,22 @@ But when you enter this command the mapB will be overwritten. This could be a pr Now we can check temp.tmx to see if the copying worked correctly. +Which layer gets copied to which: +By default layers are copied to layers of the same name. The -n option will make it copy by layer number instead. + + mapA: Ground, Fringe, Over, Collision, Object + mapB: Ground, Fencing, Fringe, Over, Collision, Object + The default copies Ground->Ground, Fringe->Fringe, Over->Over, Collision->Collision (the object layer is not affected) + -n copies Ground->Ground, Fringe->Fencing, Over->Fringe, Collision->Over (mapB's collision and object layers are not affected) + + mapA: Ground, Fringe, Over, Collision, Object + mapC: Ground, Fringe, Overhead, Collision, Object + The default quits with an error + -n copies Over->Overhead + +The -c option creates layers as needed. Using it to copy mapB to mapA will add a Fencing layer to mapA. + + The program works so far but there are still some minor problems: -Only tested for TMW-compilant maps. I don't guarantee that it works with Tiled maps that are made for other games and thus use different features. It is assumed that the target map and the source maps have the same number of layers, for example. @@ -31,4 +47,4 @@ The program works so far but there are still some minor problems: -Layer data of output file isn't gzip-compressed yet -Created TMX file is a bit malformated (but working properly) -The last 2 problems can be solved easily by opening and saving the map in Tiled. \ No newline at end of file +The last 2 problems can be solved easily by opening and saving the map in Tiled. -- cgit v1.2.3-70-g09d2 From 49fa807c986283191e72a403d4fbbad8dd094dee Mon Sep 17 00:00:00 2001 From: Steve Cotton Date: Tue, 10 Mar 2009 03:31:13 +0000 Subject: Copyright header fix As Bjorn did in 0a106989bd16c48525f01cb8515809e74f37a8d8 --- tools/tmxcopy/map.hpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'tools') diff --git a/tools/tmxcopy/map.hpp b/tools/tmxcopy/map.hpp index 6dd7794a..af2f5385 100644 --- a/tools/tmxcopy/map.hpp +++ b/tools/tmxcopy/map.hpp @@ -1,20 +1,20 @@ /* * TMXCopy - * Copyright 2007 Philipp Sehmisch - * Copyright 2009 Steve Cotton + * Copyright (C) 2007 Philipp Sehmisch + * Copyright (C) 2009 Steve Cotton * - * TMXCopy is free software; you can redistribute it and/or modify + * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * any later version. * - * TMXCopy is distributed in the hope that it will be useful, + * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License - * along with TMXCopy; if not, write to the Free Software + * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ -- cgit v1.2.3-70-g09d2