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