summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorPhilipp Sehmisch <tmw@crushnet.org>2007-12-12 15:45:25 +0000
committerPhilipp Sehmisch <tmw@crushnet.org>2007-12-12 15:45:25 +0000
commit040380b706249fa55fca2778251382f0d4450125 (patch)
tree160bc37a69077501e01ea261ced18625bad3eed5 /tools
parenta98d02eaf287932e3e83a980bac41ecc01f254f6 (diff)
downloadmana-040380b706249fa55fca2778251382f0d4450125.tar.gz
mana-040380b706249fa55fca2778251382f0d4450125.tar.bz2
mana-040380b706249fa55fca2778251382f0d4450125.tar.xz
mana-040380b706249fa55fca2778251382f0d4450125.zip
Added my tmxcopy tool for copying parts of maps to other maps.
Diffstat (limited to 'tools')
-rw-r--r--tools/tmxcopy/base64.cpp149
-rw-r--r--tools/tmxcopy/base64.h37
-rw-r--r--tools/tmxcopy/main.cpp66
-rw-r--r--tools/tmxcopy/map.cpp371
-rw-r--r--tools/tmxcopy/map.hpp81
-rw-r--r--tools/tmxcopy/readme.txt34
-rw-r--r--tools/tmxcopy/tmxcopy.cbp87
-rw-r--r--tools/tmxcopy/tostring.h37
-rw-r--r--tools/tmxcopy/xmlutils.cpp76
-rw-r--r--tools/tmxcopy/xmlutils.h62
-rw-r--r--tools/tmxcopy/zlibutils.cpp123
-rw-r--r--tools/tmxcopy/zlibutils.h11
12 files changed, 1134 insertions, 0 deletions
diff --git a/tools/tmxcopy/base64.cpp b/tools/tmxcopy/base64.cpp
new file mode 100644
index 00000000..9a8f6356
--- /dev/null
+++ b/tools/tmxcopy/base64.cpp
@@ -0,0 +1,149 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997-2000 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | 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 both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Author: Jim Winstead (jimw@php.net) |
+ +----------------------------------------------------------------------+
+ */
+/* $Id$ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include "base64.h"
+
+static char base64_table[] =
+{
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
+ 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
+ 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', '\0'
+};
+static char base64_pad = '=';
+
+unsigned char *php3_base64_encode(const unsigned char *string, int length, int *ret_length) {
+ const unsigned char *current = string;
+ int i = 0;
+ unsigned char *result = (unsigned char *)malloc(((length + 3 - length % 3) * 4 / 3 + 1) * sizeof(char));
+
+ while (length > 2) { /* keep going until we have less than 24 bits */
+ result[i++] = base64_table[current[0] >> 2];
+ result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
+ result[i++] = base64_table[((current[1] & 0x0f) << 2) + (current[2] >> 6)];
+ result[i++] = base64_table[current[2] & 0x3f];
+
+ current += 3;
+ length -= 3; /* we just handle 3 octets of data */
+ }
+
+ /* now deal with the tail end of things */
+ if (length != 0) {
+ result[i++] = base64_table[current[0] >> 2];
+ if (length > 1) {
+ result[i++] = base64_table[((current[0] & 0x03) << 4) + (current[1] >> 4)];
+ result[i++] = base64_table[(current[1] & 0x0f) << 2];
+ result[i++] = base64_pad;
+ }
+ else {
+ result[i++] = base64_table[(current[0] & 0x03) << 4];
+ result[i++] = base64_pad;
+ result[i++] = base64_pad;
+ }
+ }
+ if(ret_length) {
+ *ret_length = i;
+ }
+ result[i] = '\0';
+ return result;
+}
+
+/* as above, but backwards. :) */
+unsigned char *php3_base64_decode(const unsigned char *string, int length, int *ret_length) {
+ const unsigned char *current = string;
+ int ch, i = 0, j = 0, k;
+ char *chp;
+
+ unsigned char *result = (unsigned char *)malloc(length + 1);
+
+ if (result == NULL) {
+ return NULL;
+ }
+
+ /* run through the whole string, converting as we go */
+ while ((ch = *current++) != '\0') {
+ if (ch == base64_pad) break;
+
+ /* When Base64 gets POSTed, all pluses are interpreted as spaces.
+ This line changes them back. It's not exactly the Base64 spec,
+ but it is completely compatible with it (the spec says that
+ spaces are invalid). This will also save many people considerable
+ headache. - Turadg Aleahmad <turadg@wise.berkeley.edu>
+ */
+
+ if (ch == ' ') ch = '+';
+
+ chp = strchr(base64_table, ch);
+ if (chp == NULL) continue;
+ ch = chp - base64_table;
+
+ switch(i % 4) {
+ case 0:
+ result[j] = ch << 2;
+ break;
+ case 1:
+ result[j++] |= ch >> 4;
+ result[j] = (ch & 0x0f) << 4;
+ break;
+ case 2:
+ result[j++] |= ch >>2;
+ result[j] = (ch & 0x03) << 6;
+ break;
+ case 3:
+ result[j++] |= ch;
+ break;
+ }
+ i++;
+ }
+
+ k = j;
+ /* mop things up if we ended on a boundary */
+ if (ch == base64_pad) {
+ switch(i % 4) {
+ case 0:
+ case 1:
+ free(result);
+ return NULL;
+ case 2:
+ k++;
+ case 3:
+ result[k++] = 0;
+ }
+ }
+ if(ret_length) {
+ *ret_length = j;
+ }
+ result[k] = '\0';
+ return result;
+}
diff --git a/tools/tmxcopy/base64.h b/tools/tmxcopy/base64.h
new file mode 100644
index 00000000..ff20ac53
--- /dev/null
+++ b/tools/tmxcopy/base64.h
@@ -0,0 +1,37 @@
+/*
+ +----------------------------------------------------------------------+
+ | PHP HTML Embedded Scripting Language Version 3.0 |
+ +----------------------------------------------------------------------+
+ | Copyright (c) 1997,1998 PHP Development Team (See Credits file) |
+ +----------------------------------------------------------------------+
+ | This program is free software; you can redistribute it and/or modify |
+ | it under the terms of one of the following licenses: |
+ | |
+ | A) the GNU General Public License as published by the Free Software |
+ | Foundation; either version 2 of the License, or (at your option) |
+ | any later version. |
+ | |
+ | B) the PHP License as published by the PHP Development Team and |
+ | included in the distribution in the file: LICENSE |
+ | |
+ | 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 both licenses referred to here. |
+ | If you did not, or have any questions about PHP licensing, please |
+ | contact core@php.net. |
+ +----------------------------------------------------------------------+
+ | Author: Jim Winstead (jimw@php.net) |
+ +----------------------------------------------------------------------+
+ */
+/* $Id$ */
+
+#ifndef _TMW_BASE64_H
+#define _TMW_BASE64_H
+
+extern unsigned char *php3_base64_encode(const unsigned char *, int, int *);
+extern unsigned char *php3_base64_decode(const unsigned char *, int, int *);
+
+#endif /* _TMW_BASE64_H */
diff --git a/tools/tmxcopy/main.cpp b/tools/tmxcopy/main.cpp
new file mode 100644
index 00000000..3d460961
--- /dev/null
+++ b/tools/tmxcopy/main.cpp
@@ -0,0 +1,66 @@
+/*
+ * TMXCopy
+ * Copyright 2007 Philipp Sehmisch
+ *
+ *
+ * TMXCopy 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,
+ * 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
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <iostream>
+#include <string>
+
+#include "map.hpp"
+
+int main(int argc, char * argv[] )
+{
+ // parsing command line options
+ if (argc < 9 || argc > 10)
+ {
+ std::cerr<<"Usage: srcFile x y height width tgtFile x y [outfile]";
+ return -1;
+ }
+
+ std::string srcFile = argv[1];
+ int srcX= atoi(argv[2]);
+ int srcY= atoi(argv[3]);
+ int height= atoi(argv[4]);
+ int width=atoi(argv[5]);
+ std::string tgtFile = argv[6];
+ int destX=atoi(argv[7]);
+ int destY=atoi(argv[8]);
+ std::string outFile = tgtFile;
+ if (argc == 10) outFile = argv[9];
+
+ // plausibility check of command line options
+ if (height < 1 || width < 1 || srcX < 1 || srcY < 1 || destX < 1 || destY < 1)
+ {
+ std::cerr<<"Illegal coordinates!"<<std::endl;
+ std::cerr<<"Usage: sourceFile x y height width targetFile x y [outputFile]"<<std::endl;
+ return -1;
+ }
+
+ try
+ {
+ Map* srcMap = new Map(srcFile);
+ Map* tgtMap = new Map(tgtFile);
+ tgtMap->overwrite(srcMap, srcX, srcY, height, width, destX, destY);
+ tgtMap->save(outFile);
+ }
+ catch (int)
+ {
+ return -1;
+ }
+
+}
diff --git a/tools/tmxcopy/map.cpp b/tools/tmxcopy/map.cpp
new file mode 100644
index 00000000..0674ae1a
--- /dev/null
+++ b/tools/tmxcopy/map.cpp
@@ -0,0 +1,371 @@
+/*
+ * TMXCopy
+ * Copyright 2007 Philipp Sehmisch
+ *
+ *
+ * TMXCopy 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,
+ * 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
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <iostream>
+#include <map>
+#include <list>
+
+#include <zlib.h>
+
+#include "xmlutils.h"
+#include "zlibutils.h"
+#include "base64.h"
+#include "tostring.h"
+
+#include "map.hpp"
+
+Map::Map(std::string filename):
+ mMaxGid(0)
+{
+ std::cout<<"Loading map "<<filename<<std::endl;
+ //load XML tree
+ mXmlDoc = xmlReadFile(filename.c_str(), NULL, 0);
+
+ if (!mXmlDoc)
+ {
+ std::cerr<<"Could not load "<<filename;
+ throw 1;
+ }
+
+ xmlNodePtr rootNode = xmlDocGetRootElement(mXmlDoc);
+
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "map")) {
+ std::cerr<<filename<<"is not a Tiled map file!";
+ throw 1;
+ }
+
+ mWidth = XML::getProperty(rootNode, "width", 0);
+ mHeight = XML::getProperty(rootNode, "height", 0);
+ int layerNum = 0;
+ for_each_xml_child_node(node, rootNode)
+ {
+ if (xmlStrEqual(node->name, BAD_CAST "tileset"))
+ {
+ //add tilesets to vector of used tilesets
+ Tileset* tileset = new Tileset;
+ tileset->name = XML::getProperty(node, "name", "Unnamed");
+ tileset->tilewidth = XML::getProperty(node, "tilewidth", 0);
+ tileset->tileheight = XML::getProperty(node, "tileheight", 0);
+ tileset->firstgid = XML::getProperty(node, "firstgid", 0);
+ for_each_xml_child_node(imageNode, node)
+ {
+ if (xmlStrEqual(imageNode->name, BAD_CAST "image"))
+ tileset->imagefile = XML::getProperty(imageNode, "source", "");
+ }
+
+ //add tileset to tileset list
+ Map::mTilesets.push_back(tileset);
+ }
+ }
+
+ for_each_xml_child_node(node, rootNode)
+ {
+ if (xmlStrEqual(node->name, BAD_CAST "layer"))
+ {
+ Layer* layer = new Layer;
+ layer->resize(mWidth * mHeight);
+ //build layer information
+ for_each_xml_child_node(dataNode, node)
+ {
+ if (!xmlStrEqual(dataNode->name, BAD_CAST "data")) continue;
+
+ std::string encoding = XML::getProperty(dataNode, "encoding", "");
+ std::string compression = XML::getProperty(dataNode, "compression", "");
+
+ if (encoding != "base64")
+ {
+ std::cerr<<"Layers in "<<filename<<" are not base64 encoded!";
+ return;
+ }
+
+ // Read base64 encoded map file
+ xmlNodePtr dataChild = dataNode->xmlChildrenNode;
+ if (!dataChild)
+ continue;
+
+ int len = strlen((const char*)dataChild->content) + 1;
+ unsigned char *charData = new unsigned char[len + 1];
+ const char *charStart = (const char*)dataChild->content;
+ unsigned char *charIndex = charData;
+
+ while (*charStart) {
+ if (*charStart != ' ' && *charStart != '\t' &&
+ *charStart != '\n')
+ {
+ *charIndex = *charStart;
+ charIndex++;
+ }
+ charStart++;
+ }
+ *charIndex = '\0';
+
+ int binLen;
+ unsigned char *binData =
+ php3_base64_decode(charData, strlen((char*)charData), &binLen);
+
+ delete[] charData;
+
+ if (binData) {
+ if (compression == "gzip")
+ {
+ unsigned char *inflated;
+ unsigned int inflatedSize =
+ inflateMemory(binData, binLen, inflated);
+ free(binData);
+ binData = inflated;
+ binLen = inflatedSize;
+ if (inflated == NULL)
+ {
+ std::cerr<<"Error: while decompressing layer "<<layerNum<<" in "<<filename;
+ throw 1;
+ }
+ }
+
+ int c = 0;
+ for (int i = 0; i < binLen - 3; i += 4) {
+ int gid = binData[i] |
+ binData[i + 1] << 8 |
+ binData[i + 2] << 16 |
+ binData[i + 3] << 24;
+
+ if (gid == 0)
+ {
+ layer->at(c).tileset = -1;
+ layer->at(c).index = 0;
+ }
+ else
+ {
+ for (int s = mTilesets.size()-1; s >= 0; s--)
+ {
+ if (mTilesets.at(s)->firstgid < gid)
+ {
+ layer->at(c).tileset = s;
+ layer->at(c).index = gid - mTilesets.at(s)->firstgid;
+ if (mMaxGid < gid) mMaxGid = gid;
+ break;
+ }
+ }
+ }
+
+ c++;
+ }
+ free(binData);
+ } else {
+ std::cerr<<"error processing layer data in "<<filename<<std::endl;
+ }
+ }
+ mLayers.push_back(layer);
+ layerNum++;
+ }
+ }
+
+ std::cout<<"tilesets: "<<mTilesets.size()<<std::endl;
+ std::cout<<"layers:"<<mLayers.size()<<std::endl;
+ std::cout<<"largest GID:"<<mMaxGid<<std::endl<<std::endl;
+}
+
+bool Map::overwrite( Map* srcMap,
+ int srcX, int srcY, int srcWidth, int srcHeight,
+ int destX, int destY)
+{
+ //plausibility check of coordinates
+ bool checkPassed = true;
+ if (srcX + srcWidth > srcMap->getWidth()) {
+ std::cerr<<"Error: Area exceeds right map border of source map!";
+ checkPassed = false;
+ }
+ if (srcY + srcHeight > srcMap->getHeight()) {
+ std::cerr<<"Error: Area exceeds lower map border of source map!";
+ checkPassed = false;
+ }
+ if (destX + srcWidth > mWidth) {
+ std::cerr<<"Error: Area exceeds right map border of target map!";
+ checkPassed = false;
+ }
+ if (destY + srcHeight > mHeight) {
+ std::cerr<<"Error: Area exceeds lower map border of target map!";
+ checkPassed = false;
+ }
+ if (!checkPassed) return false;
+
+ std::map<int, int> translation;
+ translation[-1] = -1;
+ std::vector<Tileset*>* srcTilesets = srcMap->getTilesets();
+
+ //add new tilesets and add redundant tilesets to list of redundand tilesets
+ for (int a = 0; a < srcTilesets->size(); a++)
+ {
+ int b;
+ for (b = 0; b < mTilesets.size(); b++)
+ {
+ if (*srcTilesets->at(a) == *mTilesets.at(b))
+ {
+ break;
+ }
+ }
+ if (b == mTilesets.size())
+ {
+ mMaxGid += srcTilesets->at(a)->firstgid;
+ srcTilesets->at(a)->firstgid = mMaxGid;//it is possible to get some holes in the gid index this way but who cares, we got 32bit.
+ mTilesets.push_back(srcTilesets->at(a));
+ }
+ translation[a] = b;
+ }
+
+ //combining layer information
+ for (int i = 0; i < srcMap->getNumberOfLayers(); i++)
+ {
+ Layer* srcLayer = srcMap->getLayer(i);
+ Layer* destLayer = mLayers.at(i);
+
+ for (int y=0; y<srcWidth; y++)
+ {
+
+ for (int x=0; x<srcHeight; x++)
+ {
+ int srcIndex = srcMap->getWidth() * (y + srcY) + (x + srcX);
+ int tgtIndex = mWidth * (y + destY) + (x + destX);
+ Tile tmpTile = srcLayer->at(srcIndex);
+ tmpTile.tileset = translation[tmpTile.tileset];
+ destLayer->at(tgtIndex) = tmpTile;
+ }
+ }
+ }
+
+ std::clog<<"copying successful!"<<std::endl;
+}
+
+int Map::save(std::string filename)
+{
+ //remove old tileset and layer information in XML tree
+ xmlNodePtr rootNode = xmlDocGetRootElement(mXmlDoc);
+ std::list<xmlNodePtr> toRemove;
+ for_each_xml_child_node(node, rootNode)
+ {
+ if ( xmlStrEqual(node->name, BAD_CAST "tileset")
+ || xmlStrEqual(node->name, BAD_CAST "layer"))
+ {
+ toRemove.push_back(node);
+ }
+ }
+
+ while (!toRemove.empty())
+ {
+ xmlUnlinkNode(toRemove.back());
+ xmlFreeNode(toRemove.back());
+ toRemove.pop_back();
+ }
+
+ //TODO: reorganize GIDs
+
+ //add new tileset information to XML tree
+ for (int i = 0; i< mTilesets.size(); i++)
+ {
+ xmlNodePtr newNode;
+
+ xmlAddChild(rootNode, xmlNewDocText(mXmlDoc, BAD_CAST " "));
+ newNode = xmlNewNode(NULL, BAD_CAST "tileset");
+ xmlNewProp(newNode, BAD_CAST "name", BAD_CAST mTilesets.at(i)->name.c_str());
+ xmlNewProp(newNode, BAD_CAST "firstgid", BAD_CAST toString(mTilesets.at(i)->firstgid).c_str());
+ xmlNewProp(newNode, BAD_CAST "tilewidth", BAD_CAST toString(mTilesets.at(i)->tilewidth).c_str());
+ xmlNewProp(newNode, BAD_CAST "tileheight", BAD_CAST toString(mTilesets.at(i)->tileheight).c_str());
+ xmlAddChild(newNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n "));
+ xmlNodePtr imageNode = xmlNewNode(NULL, BAD_CAST "image");
+ xmlNewProp(imageNode, BAD_CAST "source", BAD_CAST mTilesets.at(i)->imagefile.c_str());
+ xmlAddChild(newNode, imageNode);
+ xmlAddChild(newNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n "));
+ xmlAddChild(rootNode, newNode);
+ xmlAddChild(rootNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n"));
+ }
+
+ //add new layer information to XML tree
+ for (int i = 0; i < mLayers.size(); i++)
+ {
+ //lay out layer information in binary
+ unsigned char* binData = (unsigned char*)malloc(mWidth * mHeight * 4);
+
+ for (int a=0; a < mWidth * mHeight; a++)
+ {
+ Tile tile = mLayers.at(i)->at(a);
+ int ldata;
+ if (tile.tileset != -1)
+ {
+ ldata = tile.index + mTilesets.at(tile.tileset)->firstgid;
+ } else {
+ ldata = 0;
+ }
+
+ binData[a * 4 + 0] = (ldata & 0x000000ff);
+ binData[a * 4 + 1] = (ldata & 0x0000ff00) >> 8;
+ binData[a * 4 + 2] = (ldata & 0x00ff0000) >> 16;
+ binData[a * 4 + 3] = (ldata & 0xff000000) >> 24;
+ }
+
+
+ // GZIP layer information
+ // Doesn't work yet.
+ /*
+ unsigned char* gzipData = (unsigned char*)malloc((mWidth * mHeight * 4) + 128);
+ unsigned int gzipLen;
+ compressMemory (binData, (mWidth * mHeight * 4) + 128, gzipData, gzipLen);
+ free (binData);
+ std::cout<<"GZIP length: "<<gzipLen<<std::endl;
+ */
+
+ //encode layer information in base64
+ unsigned char* base64Data;
+ int base64len;
+ //base64Data = php3_base64_encode(gzipData, gzipLen, &base64len);
+ base64Data = php3_base64_encode(binData, (mWidth * mHeight * 4), &base64len);
+ //free(gzipData);
+
+ xmlNodePtr newNode;
+ xmlAddChild(rootNode, xmlNewDocText(mXmlDoc, BAD_CAST " "));
+ newNode = xmlNewNode(NULL, BAD_CAST "layer");
+ xmlNewProp(newNode, BAD_CAST "name", BAD_CAST ("Layer" + toString(i)).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 "));
+ xmlNodePtr dataNode = xmlNewNode(NULL, BAD_CAST "data");
+ xmlNewProp(dataNode, BAD_CAST "encoding", BAD_CAST "base64");
+ //xmlNewProp(dataNode, BAD_CAST "compression", BAD_CAST "gzip");
+ xmlAddChild(dataNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n "));
+ xmlAddChild(dataNode, xmlNewDocText(mXmlDoc, BAD_CAST base64Data));
+ xmlAddChild(dataNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n "));
+ xmlAddChild(newNode, dataNode);
+ xmlAddChild(newNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n "));
+ xmlAddChild(rootNode, newNode);
+ xmlAddChild(rootNode, xmlNewDocText(mXmlDoc, BAD_CAST "\n"));
+ }
+
+ //save XML tree
+ int retval = xmlSaveFile(filename.c_str(), mXmlDoc);
+
+ if (retval == -1)
+ {
+ std::cerr<<"Could not write outfile "<<filename<<std::endl;
+ return false;
+ }
+ else
+ {
+ std::cout<<"File saved successfully to"<<filename<<std::endl;
+ return true;
+ }
+}
diff --git a/tools/tmxcopy/map.hpp b/tools/tmxcopy/map.hpp
new file mode 100644
index 00000000..89ae1405
--- /dev/null
+++ b/tools/tmxcopy/map.hpp
@@ -0,0 +1,81 @@
+/*
+ * TMXCopy
+ * Copyright 2007 Philipp Sehmisch
+ *
+ *
+ * TMXCopy 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,
+ * 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
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <string>
+#include <vector>
+#include <set>
+#include <libxml/parser.h>
+
+struct Tileset
+{
+ std::string imagefile;
+ int firstgid;
+ std::string name;
+ int tilewidth;
+ int tileheight;
+
+ bool operator== (Tileset const& a)
+ {
+ return (imagefile == a.imagefile &&
+ tilewidth == a.tilewidth &&
+ tileheight == a.tileheight
+ );
+ }
+};
+
+struct Tile
+{
+ int tileset; // number of tileset
+ size_t index; // index in said tileset
+};
+
+typedef std::vector<Tile> Layer;
+
+class Map
+{
+ public:
+ Map(std::string filename);
+
+ bool overwrite( Map* srcMap,
+ int srcX, int srcY, int srcWidth, int srcHeight,
+ int destX, int destY);
+
+ int save(std::string filename);
+
+ int getNumberOfLayers() { return mLayers.size(); }
+
+ Layer* getLayer(size_t num) { return mLayers.at(num); }
+
+ std::vector<Tileset*>* getTilesets() { return &mTilesets; }
+
+ int getWidth() { return mWidth; }
+ int getHeight() { return mHeight; }
+
+ private:
+ std::vector<Layer*> mLayers;
+
+ int mWidth;
+ int mHeight;
+ int mMaxGid;
+
+ std::vector<Tileset*> mTilesets;
+
+ xmlDocPtr mXmlDoc;
+};
diff --git a/tools/tmxcopy/readme.txt b/tools/tmxcopy/readme.txt
new file mode 100644
index 00000000..e4235b94
--- /dev/null
+++ b/tools/tmxcopy/readme.txt
@@ -0,0 +1,34 @@
+Tmxcopy is a little tool that allows to copy parts of one TMX map to another map. This will make it much easier to match the border areas of maps. The program is command line based. The usage is:
+
+tmxcopy sourceFile x y height width targetFile x y [outputFile]
+
+
+Here an example:
+When you want to copy the lower right corner (20x20 tiles) of mapA.tmx to the upper left corner of mapB.txt you would open map A with tiled and check at which coordinates the area you want to copy begins. Let's say mapA is 120x130 tiles. Then the area you want to copy would begin at 100:110 and would be 20x20 tiles large. So the first part of the command is:
+
+ tmxcopy mapA.tmx 100 110 20 20
+
+Then you open the target map to check the coordinates where you want to put the copied map part. We want the upper left corner, so the coordinates are 0:0. That means the next part of the command would be:
+
+ mapB.tmx 0 0
+
+The command is now complete:
+
+ tmxcopy mapA.tmx 100 110 20 20 mapB.tmx 0 0
+
+But when you enter this command the mapB will be overwritten. This could be a problem when you made an error in the command. So it is saver to write the output to a new map file so we can look at the result in Tiled before we replace the original map:
+
+ tmxcopy mapA.tmx 100 110 20 20 mapB.tmx 0 0 temp.tmx
+
+Now we can check temp.tmx to see if the copying worked correctly.
+
+
+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.
+-Compressed maps (tmx.gz) can not be handled yet (but compressed or uncompressed layers work properly)
+-When the target map has an object layer it is moved to the bottom of the layer list of the map (no problem for the game but inconvenient for editing). Objects on the source map are ignored.
+-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
diff --git a/tools/tmxcopy/tmxcopy.cbp b/tools/tmxcopy/tmxcopy.cbp
new file mode 100644
index 00000000..90105493
--- /dev/null
+++ b/tools/tmxcopy/tmxcopy.cbp
@@ -0,0 +1,87 @@
+<?xml version="1.0"?>
+<!DOCTYPE CodeBlocks_project_file>
+<CodeBlocks_project_file>
+ <FileVersion major="1" minor="1"/>
+ <Project>
+ <Option title="tmxcopy"/>
+ <Option makefile="Makefile"/>
+ <Option makefile_is_custom="0"/>
+ <Option active_target="0"/>
+ <Option compiler="0"/>
+ <Build>
+ <Target title="default">
+ <Option output="tmxcopy.exe"/>
+ <Option working_dir="."/>
+ <Option object_output=".objs"/>
+ <Option deps_output=".deps"/>
+ <Option type="1"/>
+ <Option compiler="0"/>
+ <Option projectResourceIncludeDirsRelation="1"/>
+ </Target>
+ </Build>
+ <Linker>
+ <Add library="libxml2"/>
+ <Add library="libz"/>
+ </Linker>
+ <Unit filename="base64.cpp">
+ <Option compilerVar="CPP"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="base64.h">
+ <Option compilerVar=""/>
+ <Option compile="0"/>
+ <Option link="0"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="main.cpp">
+ <Option compilerVar="CPP"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="map.cpp">
+ <Option compilerVar="CPP"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="map.hpp">
+ <Option compilerVar=""/>
+ <Option compile="0"/>
+ <Option link="0"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="tostring.h">
+ <Option compilerVar=""/>
+ <Option compile="0"/>
+ <Option link="0"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="xmlutils.cpp">
+ <Option compilerVar="CPP"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="xmlutils.h">
+ <Option compilerVar=""/>
+ <Option compile="0"/>
+ <Option link="0"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="zlibutils.cpp">
+ <Option compilerVar="CPP"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ <Unit filename="zlibutils.h">
+ <Option compilerVar=""/>
+ <Option compile="0"/>
+ <Option link="0"/>
+ <Option target="default"/>
+ <Option target="release"/>
+ </Unit>
+ </Project>
+</CodeBlocks_project_file>
diff --git a/tools/tmxcopy/tostring.h b/tools/tmxcopy/tostring.h
new file mode 100644
index 00000000..95b8985f
--- /dev/null
+++ b/tools/tmxcopy/tostring.h
@@ -0,0 +1,37 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id$
+ */
+
+#ifndef _TMW_UTILS_TOSTRING_H
+#define _TMW_UTILS_TOSTRING_H
+
+#include <sstream>
+
+template<typename T>
+std::string toString(const T &arg)
+{
+ std::stringstream ss;
+ ss << arg;
+ return ss.str();
+}
+
+#endif
diff --git a/tools/tmxcopy/xmlutils.cpp b/tools/tmxcopy/xmlutils.cpp
new file mode 100644
index 00000000..47bff51a
--- /dev/null
+++ b/tools/tmxcopy/xmlutils.cpp
@@ -0,0 +1,76 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "xmlutils.h"
+
+namespace XML
+{
+ int
+ getProperty(xmlNodePtr node, const char* name, int def)
+ {
+ int &ret = def;
+
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop) {
+ ret = atoi((char*)prop);
+ xmlFree(prop);
+ }
+
+ return ret;
+ }
+
+ double
+ getFloatProperty(xmlNodePtr node, const char* name, double def)
+ {
+ double &ret = def;
+
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop) {
+ ret = atof((char*)prop);
+ xmlFree(prop);
+ }
+
+ return ret;
+ }
+
+ std::string
+ getProperty(xmlNodePtr node, const char *name, const std::string &def)
+ {
+ xmlChar *prop = xmlGetProp(node, BAD_CAST name);
+ if (prop) {
+ std::string val = (char*)prop;
+ xmlFree(prop);
+ return val;
+ }
+
+ return def;
+ }
+
+ xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name)
+ {
+ for_each_xml_child_node(child, parent)
+ if (xmlStrEqual(child->name, BAD_CAST name))
+ return child;
+
+ return NULL;
+ }
+}
diff --git a/tools/tmxcopy/xmlutils.h b/tools/tmxcopy/xmlutils.h
new file mode 100644
index 00000000..32d1a960
--- /dev/null
+++ b/tools/tmxcopy/xmlutils.h
@@ -0,0 +1,62 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#ifndef _XMLUTILS_H
+#define _XMLUTILS_H
+
+#include <libxml/tree.h>
+
+#include <string>
+
+/**
+ * XML helper functions.
+ */
+namespace XML
+{
+ /**
+ * Gets an integer property from an xmlNodePtr.
+ */
+ int
+ getProperty(xmlNodePtr node, const char *name, int def);
+
+ /**
+ * Gets an floating point property from an xmlNodePtr.
+ */
+ double
+ getFloatProperty(xmlNodePtr node, const char *name, double def);
+
+ /**
+ * Gets a string property from an xmlNodePtr.
+ */
+ std::string
+ getProperty(xmlNodePtr node, const char *name, const std::string &def);
+
+ /**
+ * Finds the first child node with the given name
+ */
+ xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name);
+}
+
+#define for_each_xml_child_node(var, parent) \
+ for (xmlNodePtr var = parent->xmlChildrenNode; var; var = var->next)
+
+#endif
diff --git a/tools/tmxcopy/zlibutils.cpp b/tools/tmxcopy/zlibutils.cpp
new file mode 100644
index 00000000..30c762b2
--- /dev/null
+++ b/tools/tmxcopy/zlibutils.cpp
@@ -0,0 +1,123 @@
+
+#include <stdlib.h>
+#include <iostream>
+#include <cassert>
+#include <zlib.h>
+
+/**
+ * Inflates either zlib or gzip deflated memory. The inflated memory is
+ * expected to be freed by the caller.
+ */
+int
+inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength)
+{
+ int bufferSize = 256 * 1024;
+ int ret;
+ z_stream strm;
+
+ out = (unsigned char*) malloc(bufferSize);
+
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.next_in = in;
+ strm.avail_in = inLength;
+ strm.next_out = out;
+ strm.avail_out = bufferSize;
+
+ ret = inflateInit2(&strm, 15 + 32);
+
+ if (ret != Z_OK)
+ return ret;
+
+ do
+ {
+ if (strm.next_out == NULL)
+ {
+ inflateEnd(&strm);
+ return Z_MEM_ERROR;
+ }
+
+ ret = inflate(&strm, Z_NO_FLUSH);
+ assert(ret != Z_STREAM_ERROR);
+
+ switch (ret) {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR;
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ (void) inflateEnd(&strm);
+ return ret;
+ }
+
+ if (ret != Z_STREAM_END)
+ {
+ out = (unsigned char*) realloc(out, bufferSize * 2);
+
+ if (out == NULL)
+ {
+ inflateEnd(&strm);
+ return Z_MEM_ERROR;
+ }
+
+ strm.next_out = out + bufferSize;
+ strm.avail_out = bufferSize;
+ bufferSize *= 2;
+ }
+ }
+ while (ret != Z_STREAM_END);
+ assert(strm.avail_in == 0);
+
+ outLength = bufferSize - strm.avail_out;
+ (void) inflateEnd(&strm);
+ return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+}
+
+int
+inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out)
+{
+ unsigned int outLength = 0;
+ int ret = inflateMemory(in, inLength, out, outLength);
+
+ if (ret != Z_OK || out == NULL)
+ {
+ if (ret == Z_MEM_ERROR)
+ {
+ std::cerr<<"Error: Out of memory while decompressing map data!";
+ }
+ else if (ret == Z_VERSION_ERROR)
+ {
+ std::cerr<<"Error: Incompatible zlib version!";
+ }
+ else if (ret == Z_DATA_ERROR)
+ {
+ std::cerr<<"Error: Incorrect zlib compressed data!";
+ }
+ else
+ {
+ std::cerr<<"Error: Unknown error while decompressing map data!";
+ }
+
+ free(out);
+ out = NULL;
+ outLength = 0;
+ }
+
+ return outLength;
+}
+
+/*
+ * This function doesn't work like it should. The output is not decompressable.
+ */
+int
+compressMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength)
+{
+ uLongf fOutLen = outLength;
+ int ret = compress((Bytef*)out, &fOutLen, (Bytef*)in, inLength);
+ outLength = fOutLen;
+
+ assert (ret == Z_OK);
+}
diff --git a/tools/tmxcopy/zlibutils.h b/tools/tmxcopy/zlibutils.h
new file mode 100644
index 00000000..300c72ad
--- /dev/null
+++ b/tools/tmxcopy/zlibutils.h
@@ -0,0 +1,11 @@
+int
+inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength);
+
+int
+inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out);
+
+int
+compressMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength);