[Update] Tmx Parser updated to latest revision.

This commit is contained in:
Tamir Atias 2012-09-02 14:11:42 +03:00
parent b56261e987
commit 1fcb935d79
29 changed files with 2544 additions and 2465 deletions

View File

@ -38,20 +38,6 @@ HEADERS += ../src/Actor/Player.h \
../src/System/ResourceManager.h \ ../src/System/ResourceManager.h \
../src/Texture/Texture.h \ ../src/Texture/Texture.h \
../src/Sound/Music.h \ ../src/Sound/Music.h \
../src/TMXParser/TmxUtil.h \
../src/TMXParser/TmxTileset.h \
../src/TMXParser/TmxTile.h \
../src/TMXParser/TmxPropertySet.h \
../src/TMXParser/TmxPolyline.h \
../src/TMXParser/TmxPolygon.h \
../src/TMXParser/TmxObjectGroup.h \
../src/TMXParser/TmxObject.h \
../src/TMXParser/TmxMapTile.h \
../src/TMXParser/TmxImage.h \
../src/TMXParser/TmxPoint.h \
../src/TMXParser/TmxMap.h \
../src/TMXParser/TmxLayer.h \
../src/TMXParser/Tmx.h \
../src/TMXParser/base64.h \ ../src/TMXParser/base64.h \
../src/Collision/TileCollision.h \ ../src/Collision/TileCollision.h \
../src/Actor/NPC.h \ ../src/Actor/NPC.h \
@ -68,7 +54,21 @@ HEADERS += ../src/Actor/Player.h \
../src/Main/TitleScreen.h \ ../src/Main/TitleScreen.h \
../src/Level/Warp.h \ ../src/Level/Warp.h \
../src/Math/Rect.h \ ../src/Math/Rect.h \
../src/BattleSys/Slot.h ../src/BattleSys/Slot.h \
../src/TMXParser/TmxTile.h \
../src/TMXParser/TmxPropertySet.h \
../src/TMXParser/TmxPolyline.h \
../src/TMXParser/TmxPolygon.h \
../src/TMXParser/TmxPoint.h \
../src/TMXParser/TmxObjectGroup.h \
../src/TMXParser/TmxObject.h \
../src/TMXParser/TmxMapTile.h \
../src/TMXParser/TmxMap.h \
../src/TMXParser/TmxLayer.h \
../src/TMXParser/TmxImage.h \
../src/TMXParser/Tmx.h \
../src/TMXParser/base64/base64.h \
../src/TMXParser/TmxUtil.h
SOURCES += ../src/Actor/Player.cpp \ SOURCES += ../src/Actor/Player.cpp \
../src/Collision/AABB.cpp \ ../src/Collision/AABB.cpp \
@ -87,18 +87,6 @@ SOURCES += ../src/Actor/Player.cpp \
../src/System/Debug.cpp \ ../src/System/Debug.cpp \
../src/Texture/Texture.cpp \ ../src/Texture/Texture.cpp \
../src/Sound/Music.cpp \ ../src/Sound/Music.cpp \
../src/TMXParser/TmxUtil.cpp \
../src/TMXParser/TmxTileset.cpp \
../src/TMXParser/TmxTile.cpp \
../src/TMXParser/TmxPropertySet.cpp \
../src/TMXParser/TmxPolyline.cpp \
../src/TMXParser/TmxPolygon.cpp \
../src/TMXParser/TmxObjectGroup.cpp \
../src/TMXParser/TmxObject.cpp \
../src/TMXParser/TmxMap.cpp \
../src/TMXParser/TmxLayer.cpp \
../src/TMXParser/TmxImage.cpp \
../src/TMXParser/base64.cpp \
../src/Actor/NPC.cpp \ ../src/Actor/NPC.cpp \
../src/Sound/SoundEffect.cpp \ ../src/Sound/SoundEffect.cpp \
../src/Actor/Actor.cpp \ ../src/Actor/Actor.cpp \
@ -111,6 +99,18 @@ SOURCES += ../src/Actor/Player.cpp \
../src/UI/Button.cpp \ ../src/UI/Button.cpp \
../src/Main/TitleScreen.cpp \ ../src/Main/TitleScreen.cpp \
../src/Level/Warp.cpp \ ../src/Level/Warp.cpp \
../src/BattleSys/Slot.cpp ../src/BattleSys/Slot.cpp \
../src/TMXParser/TmxTileset.cpp \
../src/TMXParser/TmxTile.cpp \
../src/TMXParser/TmxPropertySet.cpp \
../src/TMXParser/TmxPolyline.cpp \
../src/TMXParser/TmxPolygon.cpp \
../src/TMXParser/TmxObjectGroup.cpp \
../src/TMXParser/TmxObject.cpp \
../src/TMXParser/TmxMap.cpp \
../src/TMXParser/TmxLayer.cpp \
../src/TMXParser/TmxImage.cpp \
../src/TMXParser/base64/base64.cpp \
../src/TMXParser/TmxUtil.cpp
QMAKE_CLEAN += LibD Debug.log QMAKE_CLEAN += LibD Debug.log

View File

@ -102,7 +102,7 @@ bool Level::Load(const std::string& filename) {
for(int x = 0; x < _width; x++) { for(int x = 0; x < _width; x++) {
for(int y = 0; y < _height; y++) { for(int y = 0; y < _height; y++) {
Tmx::MapTile tile = tmxLayer->GetTile(x, y); Tmx::MapTile tile = tmxLayer->GetTile(x, y);
_collisions[y * _width + x] = tile.gid != 0; _collisions[y * _width + x] = tile.tilesetId > -1;
} }
} }
continue; continue;
@ -119,11 +119,10 @@ bool Level::Load(const std::string& filename) {
for(int y = 0; y < layer->GetHeight(); y++) { for(int y = 0; y < layer->GetHeight(); y++) {
Tmx::MapTile tmxTile = tmxLayer->GetTile(x, y); Tmx::MapTile tmxTile = tmxLayer->GetTile(x, y);
const Tmx::Tileset* tmxTileset = map.FindTileset(tmxTile.gid);
MapTile tile; MapTile tile;
if(tmxTile.gid != 0) { if(tmxTile.tilesetId != -1) {
tile.id = tmxTile.gid - tmxTileset->GetFirstGid(); const Tmx::Tileset* tmxTileset = map.GetTileset(tmxTile.tilesetId);
tile.id = tmxTile.id;
tile.tileset = tilesetMap.find(tmxTileset)->second; tile.tileset = tilesetMap.find(tmxTileset)->second;
} else { } else {
tile.id = 0; tile.id = 0;

View File

@ -1,40 +1,40 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxImage.h // TmxImage.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include "TmxMap.h" #include "TmxMap.h"
#include "TmxTileset.h" #include "TmxTileset.h"
#include "TmxTile.h" #include "TmxTile.h"
#include "TmxImage.h" #include "TmxImage.h"
#include "TmxLayer.h" #include "TmxLayer.h"
#include "TmxObject.h" #include "TmxObject.h"
#include "TmxObjectGroup.h" #include "TmxObjectGroup.h"
#include "TmxPolygon.h" #include "TmxPolygon.h"
#include "TmxPolyline.h" #include "TmxPolyline.h"
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
#include "TmxUtil.h" #include "TmxUtil.h"

View File

@ -1,60 +1,60 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxImage.cpp // TmxImage.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxImage.h" #include "TmxImage.h"
namespace Tmx namespace Tmx
{ {
Image::Image() Image::Image()
: source() : source()
, width() , width()
, height() , height()
, transparent_color() , transparent_color()
{} {}
Image::~Image() Image::~Image()
{} {}
void Image::Parse(const TiXmlNode *imageNode) void Image::Parse(const TiXmlNode *imageNode)
{ {
const TiXmlElement* imageElem = imageNode->ToElement(); const TiXmlElement* imageElem = imageNode->ToElement();
// Read all the attribute into member variables. // Read all the attribute into member variables.
source = imageElem->Attribute("source"); source = imageElem->Attribute("source");
imageElem->Attribute("width", &width); imageElem->Attribute("width", &width);
imageElem->Attribute("height", &height); imageElem->Attribute("height", &height);
const char *trans = imageElem->Attribute("trans"); const char *trans = imageElem->Attribute("trans");
if (trans) if (trans)
{ {
transparent_color = trans; transparent_color = trans;
} }
} }
}; };

View File

@ -1,67 +1,67 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxImage.h // TmxImage.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// An image within a tileset. // An image within a tileset.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Image class Image
{ {
public: public:
Image(); Image();
~Image(); ~Image();
// Parses an image element. // Parses an image element.
void Parse(const TiXmlNode *imageNode); void Parse(const TiXmlNode *imageNode);
// Get the path to the file of the image (relative to the map) // Get the path to the file of the image (relative to the map)
const std::string &GetSource() const { return source; } const std::string &GetSource() const { return source; }
// Get the width of the image. // Get the width of the image.
int GetWidth() const { return width; } int GetWidth() const { return width; }
// Get the height of the image. // Get the height of the image.
int GetHeight() const { return height; } int GetHeight() const { return height; }
// Get the transparent color used in the image. // Get the transparent color used in the image.
const std::string &GetTransparentColor() const const std::string &GetTransparentColor() const
{ return transparent_color; } { return transparent_color; }
private: private:
std::string source; std::string source;
int width; int width;
int height; int height;
std::string transparent_color; std::string transparent_color;
}; };
}; };

View File

@ -1,229 +1,277 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxLayer.cpp // TmxLayer.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <stdlib.h> #include <tinyxml.h>
#include <zlib.h> #include <zlib.h>
#include <tinyxml.h> #include <stdlib.h>
#include <stdio.h>
#include "TmxLayer.h"
#include "TmxUtil.h" #include "TmxLayer.h"
#include "TmxUtil.h"
namespace Tmx #include "TmxMap.h"
{ #include "TmxTileset.h"
Layer::Layer()
: name() namespace Tmx
, width(0) {
, height(0) Layer::Layer(const Map *_map)
, opacity(1.0f) : map(_map)
, visible(true) , name()
, properties() , width(0)
, encoding(TMX_ENCODING_XML) , height(0)
, compression(TMX_COMPRESSION_NONE) , opacity(1.0f)
{ , visible(true)
// Set the map to null to specify that it is not yet allocated. , properties()
tile_map = NULL; , encoding(TMX_ENCODING_XML)
} , compression(TMX_COMPRESSION_NONE)
{
Layer::~Layer() // Set the map to null to specify that it is not yet allocated.
{ tile_map = NULL;
// If the tile map is allocated, delete it from the memory. }
if (tile_map)
{ Layer::~Layer()
delete [] tile_map; {
tile_map = NULL; // If the tile map is allocated, delete it from the memory.
} if (tile_map)
} {
delete [] tile_map;
void Layer::Parse(const TiXmlNode *layerNode) tile_map = NULL;
{ }
const TiXmlElement *layerElem = layerNode->ToElement(); }
// Read the attributes. void Layer::Parse(const TiXmlNode *layerNode)
name = layerElem->Attribute("name"); {
const TiXmlElement *layerElem = layerNode->ToElement();
layerElem->Attribute("width", &width);
layerElem->Attribute("height", &height); // Read the attributes.
name = layerElem->Attribute("name");
const char *opacityStr = layerElem->Attribute("opacity");
if (opacityStr) layerElem->Attribute("width", &width);
{ layerElem->Attribute("height", &height);
opacity = (float)atof(opacityStr);
} const char *opacityStr = layerElem->Attribute("opacity");
if (opacityStr)
const char *visibleStr = layerElem->Attribute("visible"); {
if (visibleStr) opacity = (float)atof(opacityStr);
{ }
visible = atoi(visibleStr) != 0; // to prevent visual c++ from complaining..
} const char *visibleStr = layerElem->Attribute("visible");
if (visibleStr)
// Read the properties. {
const TiXmlNode *propertiesNode = layerNode->FirstChild("properties"); visible = atoi(visibleStr) != 0; // to prevent visual c++ from complaining..
if (propertiesNode) }
{
properties.Parse(propertiesNode); // Read the properties.
} const TiXmlNode *propertiesNode = layerNode->FirstChild("properties");
if (propertiesNode)
// Allocate memory for reading the tiles. {
tile_map = new MapTile[width * height]; properties.Parse(propertiesNode);
}
const TiXmlNode *dataNode = layerNode->FirstChild("data");
const TiXmlElement *dataElem = dataNode->ToElement(); // Allocate memory for reading the tiles.
tile_map = new MapTile[width * height];
const char *encodingStr = dataElem->Attribute("encoding");
const char *compressionStr = dataElem->Attribute("compression"); const TiXmlNode *dataNode = layerNode->FirstChild("data");
const TiXmlElement *dataElem = dataNode->ToElement();
// Check for encoding.
if (encodingStr) const char *encodingStr = dataElem->Attribute("encoding");
{ const char *compressionStr = dataElem->Attribute("compression");
if (!strcmp(encodingStr, "base64"))
{ // Check for encoding.
encoding = TMX_ENCODING_BASE64; if (encodingStr)
} {
else if (!strcmp(encodingStr, "csv")) if (!strcmp(encodingStr, "base64"))
{ {
encoding = TMX_ENCODING_CSV; encoding = TMX_ENCODING_BASE64;
} }
} else if (!strcmp(encodingStr, "csv"))
{
// Check for compression. encoding = TMX_ENCODING_CSV;
if (compressionStr) }
{ }
if (!strcmp(compressionStr, "gzip"))
{ // Check for compression.
compression = TMX_COMPRESSION_GZIP; if (compressionStr)
} {
else if (!strcmp(compressionStr, "zlib")) if (!strcmp(compressionStr, "gzip"))
{ {
compression = TMX_COMPRESSION_ZLIB; compression = TMX_COMPRESSION_GZIP;
} }
} else if (!strcmp(compressionStr, "zlib"))
{
// Decode. compression = TMX_COMPRESSION_ZLIB;
switch (encoding) }
{ }
case TMX_ENCODING_XML:
ParseXML(dataNode); // Decode.
break; switch (encoding)
{
case TMX_ENCODING_BASE64: case TMX_ENCODING_XML:
ParseBase64(dataElem->GetText()); ParseXML(dataNode);
break; break;
case TMX_ENCODING_CSV: case TMX_ENCODING_BASE64:
ParseCSV(dataElem->GetText()); ParseBase64(dataElem->GetText());
break; break;
}
} case TMX_ENCODING_CSV:
ParseCSV(dataElem->GetText());
void Layer::ParseXML(const TiXmlNode *dataNode) break;
{ }
const TiXmlNode *tileNode = dataNode->FirstChild("tile"); }
int tileCount = 0;
void Layer::ParseXML(const TiXmlNode *dataNode)
while (tileNode) {
{ const TiXmlNode *tileNode = dataNode->FirstChild("tile");
const TiXmlElement *tileElem = tileNode->ToElement(); int tileCount = 0;
int gid = 0; while (tileNode)
{
// Read the Global-ID of the tile directly into the array entry. const TiXmlElement *tileElem = tileNode->ToElement();
tileElem->Attribute("gid", &gid);
unsigned gid = 0;
// Convert the gid to a map tile.
tile_map[tileCount++] = MapTile((unsigned)gid); // Read the Global-ID of the tile.
const char* gidText = tileElem->Attribute("gid");
tileNode = dataNode->IterateChildren("tile", tileNode);
} // Convert to an unsigned.
} sscanf(gidText, "%u", &gid);
void Layer::ParseBase64(const std::string &innerText) // Find the tileset index.
{ const int tilesetIndex = map->FindTilesetIndex(gid);
const std::string &text = Util::DecodeBase64(innerText); if (tilesetIndex != -1)
{
// Temporary array of gids to be converted to map tiles. // If valid, set up the map tile with the tileset.
unsigned *out = 0; const Tmx::Tileset* tileset = map->GetTileset(tilesetIndex);
tile_map[tileCount] = MapTile(gid, tileset->GetFirstGid(), tilesetIndex);
if (compression == TMX_COMPRESSION_ZLIB) }
{ else
// Use zlib to uncompress the layer into the temporary array of tiles. {
uLongf outlen = width * height * 4; // Otherwise, make it null.
out = (unsigned *)malloc(outlen); tile_map[tileCount] = MapTile(gid, 0, -1);
uncompress( }
(Bytef*)out, &outlen,
(const Bytef*)text.c_str(), text.size()); tileNode = dataNode->IterateChildren("tile", tileNode);
tileCount++;
} }
else if (compression == TMX_COMPRESSION_GZIP) }
{
// Use the utility class for decompressing (which uses zlib) void Layer::ParseBase64(const std::string &innerText)
out = (unsigned *)Util::DecompressGZIP( {
text.c_str(), const std::string &text = Util::DecodeBase64(innerText);
text.size(),
width * height * 4); // Temporary array of gids to be converted to map tiles.
} unsigned *out = 0;
else
{ if (compression == TMX_COMPRESSION_ZLIB)
out = (unsigned *)malloc(text.size()); {
// Use zlib to uncompress the layer into the temporary array of tiles.
// Copy every gid into the temporary array since uLongf outlen = width * height * 4;
// the decoded string is an array of 32-bit integers. out = (unsigned *)malloc(outlen);
memcpy(out, text.c_str(), text.size()); uncompress(
} (Bytef*)out, &outlen,
(const Bytef*)text.c_str(), text.size());
// Convert the gids to map tiles.
for (int x = 0; x < width; x++) }
{ else if (compression == TMX_COMPRESSION_GZIP)
for (int y = 0; y < height; y++) {
{ // Use the utility class for decompressing (which uses zlib)
tile_map[y * width + x] = MapTile(out[y * width + x]); out = (unsigned *)Util::DecompressGZIP(
} text.c_str(),
} text.size(),
width * height * 4);
// Free the temporary array from memory. }
free(out); else
} {
out = (unsigned *)malloc(text.size());
void Layer::ParseCSV(const std::string &innerText)
{ // Copy every gid into the temporary array since
// Duplicate the string for use with C stdio. // the decoded string is an array of 32-bit integers.
char *csv = strdup(innerText.c_str()); memcpy(out, text.c_str(), text.size());
}
// Iterate through every token of ';' in the CSV string.
char *pch = strtok(csv, ";"); // Convert the gids to map tiles.
int tileCount = 0; for (int x = 0; x < width; x++)
{
while (pch) for (int y = 0; y < height; y++)
{ {
tile_map[tileCount] = MapTile((unsigned)atoi(pch)); unsigned gid = out[y * width + x];
++tileCount; // Find the tileset index.
pch = strtok(NULL, ";"); const int tilesetIndex = map->FindTilesetIndex(gid);
} if (tilesetIndex != -1)
{
free(csv); // If valid, set up the map tile with the tileset.
} const Tmx::Tileset* tileset = map->GetTileset(tilesetIndex);
}; tile_map[y * width + x] = MapTile(gid, tileset->GetFirstGid(), tilesetIndex);
}
else
{
// Otherwise, make it null.
tile_map[y * width + x] = MapTile(gid, 0, -1);
}
}
}
// Free the temporary array from memory.
free(out);
}
void Layer::ParseCSV(const std::string &innerText)
{
// Duplicate the string for use with C stdio.
char *csv = strdup(innerText.c_str());
// Iterate through every token of ';' in the CSV string.
char *pch = strtok(csv, ",");
int tileCount = 0;
while (pch)
{
unsigned gid;
sscanf(pch, "%u", &gid);
// Find the tileset index.
const int tilesetIndex = map->FindTilesetIndex(gid);
if (tilesetIndex != -1)
{
// If valid, set up the map tile with the tileset.
const Tmx::Tileset* tileset = map->GetTileset(tilesetIndex);
tile_map[tileCount] = MapTile(gid, tileset->GetFirstGid(), tilesetIndex);
}
else
{
// Otherwise, make it null.
tile_map[tileCount] = MapTile(gid, 0, -1);
}
pch = strtok(NULL, ",");
tileCount++;
}
free(csv);
}
};

View File

@ -1,133 +1,140 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxLayer.h // TmxLayer.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
#include "TmxMapTile.h" #include "TmxMapTile.h"
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- class Map;
// Type used for the encoding of the layer data.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
enum LayerEncodingType // Type used for the encoding of the layer data.
{ //-------------------------------------------------------------------------
TMX_ENCODING_XML, enum LayerEncodingType
TMX_ENCODING_BASE64, {
TMX_ENCODING_CSV TMX_ENCODING_XML,
}; TMX_ENCODING_BASE64,
TMX_ENCODING_CSV
//------------------------------------------------------------------------- };
// Type used for the compression of the layer data.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
enum LayerCompressionType // Type used for the compression of the layer data.
{ //-------------------------------------------------------------------------
TMX_COMPRESSION_NONE, enum LayerCompressionType
TMX_COMPRESSION_ZLIB, {
TMX_COMPRESSION_GZIP TMX_COMPRESSION_NONE,
}; TMX_COMPRESSION_ZLIB,
TMX_COMPRESSION_GZIP
//------------------------------------------------------------------------- };
// Used for storing information about the tile ids for every layer.
// This class also have a property set. //-------------------------------------------------------------------------
//------------------------------------------------------------------------- // Used for storing information about the tile ids for every layer.
class Layer // This class also have a property set.
{ //-------------------------------------------------------------------------
public: class Layer
Layer(); {
~Layer(); public:
Layer(const Tmx::Map *_map);
// Parse a layer node. ~Layer();
void Parse(const TiXmlNode *layerNode);
// Parse a layer node.
// Get the name of the layer. void Parse(const TiXmlNode *layerNode);
const std::string &GetName() const { return name; }
// Get the name of the layer.
// Get the width of the layer, in tiles. const std::string &GetName() const { return name; }
float GetWidth() const { return width; }
// Get the width of the layer, in tiles.
// Get the height of the layer, in tiles. int GetWidth() const { return width; }
float GetHeight() const { return height; }
// Get the height of the layer, in tiles.
// Get the visibility of the layer int GetHeight() const { return height; }
bool IsVisible() const { return visible; }
// Get the visibility of the layer
// Get the property set. bool IsVisible() const { return visible; }
const PropertySet &GetProperties() const { return properties; }
// Get the property set.
// Pick a specific tile from the list. const Tmx::PropertySet &GetProperties() const { return properties; }
unsigned GetTileGid(int x, int y) const { return tile_map[y * width + x].gid; }
// Pick a specific tile from the list.
// Get whether the tile is flipped horizontally. unsigned GetTileId(int x, int y) const { return tile_map[y * width + x].id; }
bool IsTileFlippedHorizontally(int x, int y) const
{ return tile_map[y * width + x].flippedHorizontally; } // Get the tileset index for a tileset from the list.
int GetTileTilesetIndex(int x, int y) const { return tile_map[y * width + x].tilesetId; }
// Get whether the tile is flipped vertically.
bool IsTileFlippedVertically(int x, int y) const // Get whether a tile is flipped horizontally.
{ return tile_map[y * width + x].flippedVertically; } bool IsTileFlippedHorizontally(int x, int y) const
{ return tile_map[y * width + x].flippedHorizontally; }
// Get whether the tile is flipped diagonally.
bool IsTileFlippedDiagonally(int x, int y) const // Get whether a tile is flipped vertically.
{ return tile_map[y * width + x].flippedDiagonally; } bool IsTileFlippedVertically(int x, int y) const
{ return tile_map[y * width + x].flippedVertically; }
// Get the tile specific to the map.
MapTile GetTile(int x, int y) const { return tile_map[y * width + x]; } // Get whether a tile is flipped diagonally.
bool IsTileFlippedDiagonally(int x, int y) const
// Get the type of encoding that was used for parsing the layer data. { return tile_map[y * width + x].flippedDiagonally; }
// See: LayerEncodingType
LayerEncodingType GetEncoding() const { return encoding; } // Get a tile specific to the map.
const Tmx::MapTile& GetTile(int x, int y) const { return tile_map[y * width + x]; }
// Get the type of compression that was used for parsing the layer data.
// See: LayerCompressionType // Get the type of encoding that was used for parsing the layer data.
LayerCompressionType GetCompression() const { return compression; } // See: LayerEncodingType
Tmx::LayerEncodingType GetEncoding() const { return encoding; }
private:
void ParseXML(const TiXmlNode *dataNode); // Get the type of compression that was used for parsing the layer data.
void ParseBase64(const std::string &innerText); // See: LayerCompressionType
void ParseCSV(const std::string &innerText); Tmx::LayerCompressionType GetCompression() const { return compression; }
std::string name; private:
void ParseXML(const TiXmlNode *dataNode);
int width; void ParseBase64(const std::string &innerText);
int height; void ParseCSV(const std::string &innerText);
float opacity; const Tmx::Map *map;
bool visible;
std::string name;
PropertySet properties;
int width;
MapTile *tile_map; int height;
LayerEncodingType encoding; float opacity;
LayerCompressionType compression; bool visible;
};
}; Tmx::PropertySet properties;
Tmx::MapTile *tile_map;
Tmx::LayerEncodingType encoding;
Tmx::LayerCompressionType compression;
};
};

View File

@ -1,249 +1,266 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxMap.cpp // TmxMap.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <cstdio> #include <tinyxml.h>
#include <tinyxml.h> #include <stdio.h>
#include "TmxMap.h" #include "TmxMap.h"
#include "TmxTileset.h" #include "TmxTileset.h"
#include "TmxLayer.h" #include "TmxLayer.h"
#include "TmxObjectGroup.h" #include "TmxObjectGroup.h"
using std::vector; using std::vector;
using std::string; using std::string;
namespace Tmx namespace Tmx
{ {
Map::Map() Map::Map()
: file_name() : file_name()
, file_path() , file_path()
, version(0.0) , version(0.0)
, orientation(TMX_MO_ORTHOGONAL) , orientation(TMX_MO_ORTHOGONAL)
, width(0) , width(0)
, height(0) , height(0)
, tile_width(0) , tile_width(0)
, tile_height(0) , tile_height(0)
, layers() , layers()
, object_groups() , object_groups()
, tilesets() , tilesets()
, has_error(false) , has_error(false)
, error_code(0) , error_code(0)
, error_text() , error_text()
{} {}
Map::~Map() Map::~Map()
{ {
// Iterate through all of the object groups and delete each of them. // Iterate through all of the object groups and delete each of them.
vector< ObjectGroup* >::iterator ogIter; vector< ObjectGroup* >::iterator ogIter;
for (ogIter = object_groups.begin(); ogIter != object_groups.end(); ++ogIter) for (ogIter = object_groups.begin(); ogIter != object_groups.end(); ++ogIter)
{ {
ObjectGroup *objectGroup = (*ogIter); ObjectGroup *objectGroup = (*ogIter);
if (objectGroup) if (objectGroup)
{ {
delete objectGroup; delete objectGroup;
objectGroup = NULL; objectGroup = NULL;
} }
} }
// Iterate through all of the layers and delete each of them. // Iterate through all of the layers and delete each of them.
vector< Layer* >::iterator lIter; vector< Layer* >::iterator lIter;
for (lIter = layers.begin(); lIter != layers.end(); ++lIter) for (lIter = layers.begin(); lIter != layers.end(); ++lIter)
{ {
Layer *layer = (*lIter); Layer *layer = (*lIter);
if (layer) if (layer)
{ {
delete layer; delete layer;
layer = NULL; layer = NULL;
} }
} }
// Iterate through all of the tilesets and delete each of them. // Iterate through all of the tilesets and delete each of them.
vector< Tileset* >::iterator tsIter; vector< Tileset* >::iterator tsIter;
for (tsIter = tilesets.begin(); tsIter != tilesets.end(); ++tsIter) for (tsIter = tilesets.begin(); tsIter != tilesets.end(); ++tsIter)
{ {
Tileset *tileset = (*tsIter); Tileset *tileset = (*tsIter);
if (tileset) if (tileset)
{ {
delete tileset; delete tileset;
tileset = NULL; tileset = NULL;
} }
} }
} }
void Map::ParseFile(const string &fileName) void Map::ParseFile(const string &fileName)
{ {
file_name = fileName; file_name = fileName;
int lastSlash = fileName.find_last_of("/"); int lastSlash = fileName.find_last_of("/");
// Get the directory of the file using substring. // Get the directory of the file using substring.
if (lastSlash > 0) if (lastSlash > 0)
{ {
file_path = fileName.substr(0, lastSlash + 1); file_path = fileName.substr(0, lastSlash + 1);
} }
else else
{ {
file_path = ""; file_path = "";
} }
char* fileText; char* fileText;
int fileSize; int fileSize;
// Open the file for reading. // Open the file for reading.
FILE *file = fopen(fileName.c_str(), "rb"); FILE *file = fopen(fileName.c_str(), "rb");
// Check if the file could not be opened. // Check if the file could not be opened.
if (!file) if (!file)
{ {
has_error = true; has_error = true;
error_code = TMX_COULDNT_OPEN; error_code = TMX_COULDNT_OPEN;
error_text = "Could not open the file."; error_text = "Could not open the file.";
return; return;
} }
// Find out the file size. // Find out the file size.
fseek(file, 0, SEEK_END); fseek(file, 0, SEEK_END);
fileSize = ftell(file); fileSize = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
// Allocate memory for the file and read it into the memory. // Allocate memory for the file and read it into the memory.
fileText = new char[fileSize]; fileText = new char[fileSize];
fread(fileText, 1, fileSize, file); fread(fileText, 1, fileSize, file);
fclose(file); fclose(file);
// Copy the contents into a C++ string and delete it from memory. // Copy the contents into a C++ string and delete it from memory.
std::string text(fileText, fileText+fileSize); std::string text(fileText, fileText+fileSize);
delete [] fileText; delete [] fileText;
ParseText(text); ParseText(text);
} }
void Map::ParseText(const string &text) void Map::ParseText(const string &text)
{ {
// Create a tiny xml document and use it to parse the text. // Create a tiny xml document and use it to parse the text.
TiXmlDocument doc; TiXmlDocument doc;
doc.Parse(text.c_str()); doc.Parse(text.c_str());
// Check for parsing errors. // Check for parsing errors.
if (doc.Error()) if (doc.Error())
{ {
has_error = true; has_error = true;
error_code = TMX_PARSING_ERROR; error_code = TMX_PARSING_ERROR;
error_text = doc.ErrorDesc(); error_text = doc.ErrorDesc();
return; return;
} }
TiXmlNode *mapNode = doc.FirstChild("map"); TiXmlNode *mapNode = doc.FirstChild("map");
TiXmlElement* mapElem = mapNode->ToElement(); TiXmlElement* mapElem = mapNode->ToElement();
// Read the map attributes. // Read the map attributes.
mapElem->Attribute("version", &version); mapElem->Attribute("version", &version);
mapElem->Attribute("width", &width); mapElem->Attribute("width", &width);
mapElem->Attribute("height", &height); mapElem->Attribute("height", &height);
mapElem->Attribute("tilewidth", &tile_width); mapElem->Attribute("tilewidth", &tile_width);
mapElem->Attribute("tileheight", &tile_height); mapElem->Attribute("tileheight", &tile_height);
// Read the orientation // Read the orientation
std::string orientationStr = mapElem->Attribute("orientation"); std::string orientationStr = mapElem->Attribute("orientation");
if (!orientationStr.compare("orthogonal")) if (!orientationStr.compare("orthogonal"))
{ {
orientation = TMX_MO_ORTHOGONAL; orientation = TMX_MO_ORTHOGONAL;
} }
else if (!orientationStr.compare("isometric")) else if (!orientationStr.compare("isometric"))
{ {
orientation = TMX_MO_ISOMETRIC; orientation = TMX_MO_ISOMETRIC;
} }
// Read the map properties. // Read the map properties.
const TiXmlNode *propertiesNode = mapElem->FirstChild("properties"); const TiXmlNode *propertiesNode = mapElem->FirstChild("properties");
if (propertiesNode) if (propertiesNode)
{ {
properties.Parse(propertiesNode); properties.Parse(propertiesNode);
} }
// Iterate through all of the tileset elements. // Iterate through all of the tileset elements.
const TiXmlNode *tilesetNode = mapNode->FirstChild("tileset"); const TiXmlNode *tilesetNode = mapNode->FirstChild("tileset");
while (tilesetNode) while (tilesetNode)
{ {
// Allocate a new tileset and parse it. // Allocate a new tileset and parse it.
Tileset *tileset = new Tileset(); Tileset *tileset = new Tileset();
tileset->Parse(tilesetNode->ToElement()); tileset->Parse(tilesetNode->ToElement());
// Add the tileset to the list. // Add the tileset to the list.
tilesets.push_back(tileset); tilesets.push_back(tileset);
tilesetNode = mapNode->IterateChildren("tileset", tilesetNode); tilesetNode = mapNode->IterateChildren("tileset", tilesetNode);
} }
// Iterate through all of the layer elements. // Iterate through all of the layer elements.
TiXmlNode *layerNode = mapNode->FirstChild("layer"); TiXmlNode *layerNode = mapNode->FirstChild("layer");
while (layerNode) while (layerNode)
{ {
// Allocate a new layer and parse it. // Allocate a new layer and parse it.
Layer *layer = new Layer(); Layer *layer = new Layer(this);
layer->Parse(layerNode); layer->Parse(layerNode);
// Add the layer to the list. // Add the layer to the list.
layers.push_back(layer); layers.push_back(layer);
layerNode = mapNode->IterateChildren("layer", layerNode); layerNode = mapNode->IterateChildren("layer", layerNode);
} }
// Iterate through all of the objectgroup elements. // Iterate through all of the objectgroup elements.
TiXmlNode *objectGroupNode = mapNode->FirstChild("objectgroup"); TiXmlNode *objectGroupNode = mapNode->FirstChild("objectgroup");
while (objectGroupNode) while (objectGroupNode)
{ {
// Allocate a new object group and parse it. // Allocate a new object group and parse it.
ObjectGroup *objectGroup = new ObjectGroup(); ObjectGroup *objectGroup = new ObjectGroup();
objectGroup->Parse(objectGroupNode); objectGroup->Parse(objectGroupNode);
// Add the object group to the list. // Add the object group to the list.
object_groups.push_back(objectGroup); object_groups.push_back(objectGroup);
objectGroupNode = mapNode->IterateChildren("objectgroup", objectGroupNode); objectGroupNode = mapNode->IterateChildren("objectgroup", objectGroupNode);
} }
} }
const Tileset *Map::FindTileset(int gid) const int Map::FindTilesetIndex(int gid) const
{ {
for (int i = tilesets.size() - 1; i > -1; --i) // Clean up the flags from the gid (thanks marwes91).
{ gid &= ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag);
// If the gid beyond the tileset gid return it.
if (gid >= tilesets[i]->GetFirstGid()) for (int i = tilesets.size() - 1; i > -1; --i)
{ {
return tilesets[i]; // If the gid beyond the tileset gid return its index.
} if (gid >= tilesets[i]->GetFirstGid())
} {
return i;
return NULL; }
} }
};
return -1;
}
const Tileset *Map::FindTileset(int gid) const
{
for (int i = tilesets.size() - 1; i > -1; --i)
{
// If the gid beyond the tileset gid return it.
if (gid >= tilesets[i]->GetFirstGid())
{
return tilesets[i];
}
}
return NULL;
}
};

View File

@ -1,172 +1,175 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxMap.h // TmxMap.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <vector> #include <vector>
#include <string> #include <string>
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
namespace Tmx namespace Tmx
{ {
class Layer; class Layer;
class ObjectGroup; class ObjectGroup;
class Tileset; class Tileset;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Error in handling of the Map class. // Error in handling of the Map class.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
enum MapError enum MapError
{ {
// A file could not be opened. (usually due to permission problems) // A file could not be opened. (usually due to permission problems)
TMX_COULDNT_OPEN = 0x01, TMX_COULDNT_OPEN = 0x01,
// There was an error in parsing the TMX file. // There was an error in parsing the TMX file.
// This is being caused by TinyXML parsing problems. // This is being caused by TinyXML parsing problems.
TMX_PARSING_ERROR = 0x02, TMX_PARSING_ERROR = 0x02,
}; };
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// The way the map is viewed. // The way the map is viewed.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
enum MapOrientation enum MapOrientation
{ {
// This map is an orthogonal map. // This map is an orthogonal map.
TMX_MO_ORTHOGONAL = 0x01, TMX_MO_ORTHOGONAL = 0x01,
// This map is an isometric map. // This map is an isometric map.
TMX_MO_ISOMETRIC = 0x02 TMX_MO_ISOMETRIC = 0x02
}; };
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// This class is the root class of the parser. // This class is the root class of the parser.
// It has all of the information in regard to the TMX file. // It has all of the information in regard to the TMX file.
// This class has a property set. // This class has a property set.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Map class Map
{ {
public: public:
Map(); Map();
~Map(); ~Map();
// Read a file and parse it. // Read a file and parse it.
// Note: use '/' instead of '\\' as it is using '/' to find the path. // Note: use '/' instead of '\\' as it is using '/' to find the path.
void ParseFile(const std::string &fileName); void ParseFile(const std::string &fileName);
// Parse text containing TMX formatted XML. // Parse text containing TMX formatted XML.
void ParseText(const std::string &text); void ParseText(const std::string &text);
// Get the filename used to read the map. // Get the filename used to read the map.
const std::string &GetFilename() { return file_name; } const std::string &GetFilename() { return file_name; }
// Get a path to the directory of the map file if any. // Get a path to the directory of the map file if any.
const std::string &GetFilepath() const { return file_path; } const std::string &GetFilepath() const { return file_path; }
// Get the version of the map. // Get the version of the map.
double GetVersion() const { return version; } double GetVersion() const { return version; }
// Get the orientation of the map. // Get the orientation of the map.
MapOrientation GetOrientation() const { return orientation; } Tmx::MapOrientation GetOrientation() const { return orientation; }
// Get the width of the map, in tiles. // Get the width of the map, in tiles.
int GetWidth() const { return width; } int GetWidth() const { return width; }
// Get the height of the map, in tiles. // Get the height of the map, in tiles.
int GetHeight() const { return height; } int GetHeight() const { return height; }
// Get the width of a tile, in pixels. // Get the width of a tile, in pixels.
int GetTileWidth() const { return tile_width; } int GetTileWidth() const { return tile_width; }
// Get the height of a tile, in pixels. // Get the height of a tile, in pixels.
int GetTileHeight() const { return tile_height; } int GetTileHeight() const { return tile_height; }
// Get the layer at a certain index. // Get the layer at a certain index.
const Layer *GetLayer(int index) const { return layers.at(index); } const Tmx::Layer *GetLayer(int index) const { return layers.at(index); }
// Get the amount of layers. // Get the amount of layers.
int GetNumLayers() const { return layers.size(); } int GetNumLayers() const { return layers.size(); }
// Get the whole layers collection. // Get the whole layers collection.
const std::vector< Layer* > &GetLayers() const { return layers; } const std::vector< Tmx::Layer* > &GetLayers() const { return layers; }
// Get the object group at a certain index. // Get the object group at a certain index.
const ObjectGroup *GetObjectGroup(int index) const { return object_groups.at(index); } const Tmx::ObjectGroup *GetObjectGroup(int index) const { return object_groups.at(index); }
// Get the amount of object groups. // Get the amount of object groups.
int GetNumObjectGroups() const { return object_groups.size(); } int GetNumObjectGroups() const { return object_groups.size(); }
// Get the whole object group collection. // Get the whole object group collection.
const std::vector< ObjectGroup* > &GetObjectGroups() const { return object_groups; } const std::vector< Tmx::ObjectGroup* > &GetObjectGroups() const { return object_groups; }
// Find a tileset for a specific gid. // Find the tileset index for a tileset using a tile gid.
const Tileset *FindTileset(int gid) const; int FindTilesetIndex(int gid) const;
// Get a tileset by an index. // Find a tileset for a specific gid.
const Tileset *GetTileset(int index) const { return tilesets.at(index); } const Tmx::Tileset *FindTileset(int gid) const;
// Get the amount of tilesets. // Get a tileset by an index.
int GetNumTilesets() const { return tilesets.size(); } const Tmx::Tileset *GetTileset(int index) const { return tilesets.at(index); }
// Get the collection of tilesets. // Get the amount of tilesets.
const std::vector< Tileset* > &GetTilesets() const { return tilesets; } int GetNumTilesets() const { return tilesets.size(); }
// Get whether there was an error or not. // Get the collection of tilesets.
bool HasError() const { return has_error; } const std::vector< Tmx::Tileset* > &GetTilesets() const { return tilesets; }
// Get an error string containing the error in text format. // Get whether there was an error or not.
const std::string &GetErrorText() const { return error_text; } bool HasError() const { return has_error; }
// Get a number that identifies the error. (TMX_ preceded constants) // Get an error string containing the error in text format.
unsigned char GetErrorCode() const { return error_code; } const std::string &GetErrorText() const { return error_text; }
// Get the property set. // Get a number that identifies the error. (TMX_ preceded constants)
const PropertySet &GetProperties() { return properties; } unsigned char GetErrorCode() const { return error_code; }
private: // Get the property set.
std::string file_name; const Tmx::PropertySet &GetProperties() { return properties; }
std::string file_path;
private:
double version; std::string file_name;
MapOrientation orientation; std::string file_path;
int width; double version;
int height; Tmx::MapOrientation orientation;
int tile_width;
int tile_height; int width;
int height;
std::vector< Layer* > layers; int tile_width;
std::vector< ObjectGroup* > object_groups; int tile_height;
std::vector< Tileset* > tilesets;
std::vector< Tmx::Layer* > layers;
bool has_error; std::vector< Tmx::ObjectGroup* > object_groups;
unsigned char error_code; std::vector< Tmx::Tileset* > tilesets;
std::string error_text;
bool has_error;
PropertySet properties; unsigned char error_code;
}; std::string error_text;
Tmx::PropertySet properties;
};
}; };

View File

@ -1,75 +1,80 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxMapTile.h // TmxMapTile.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Flags that may be in the first two bits of the gid. // Flags that may be in the first two bits of the gid.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
const unsigned FlippedHorizontallyFlag = 0x80000000; const unsigned FlippedHorizontallyFlag = 0x80000000;
const unsigned FlippedVerticallyFlag = 0x40000000; const unsigned FlippedVerticallyFlag = 0x40000000;
const unsigned FlippedDiagonallyFlag = 0x20000000; const unsigned FlippedDiagonallyFlag = 0x20000000;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Struct to store information about a specific tile in the map layer. // Struct to store information about a specific tile in the map layer.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
struct MapTile struct MapTile
{ {
// Default constructor. // Default constructor.
MapTile() MapTile()
: gid(0) : tilesetId(0)
, flippedHorizontally(false) , id(0)
, flippedVertically(false) , flippedHorizontally(false)
, flippedDiagonally(false) , flippedVertically(false)
{} , flippedDiagonally(false)
{}
// Will take a gid and read the attributes from the first
// two bits of it. // Will take a gid and read the attributes from the first
MapTile(unsigned _gid) // two bits of it.
: gid(_gid) MapTile(unsigned _gid, int _tilesetFirstGid, unsigned _tilesetId)
, flippedHorizontally((_gid & FlippedHorizontallyFlag) != 0) : tilesetId(_tilesetId)
, flippedVertically((_gid & FlippedVerticallyFlag) != 0) , id(_gid & ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag))
, flippedDiagonally((_gid & FlippedDiagonallyFlag) != 0) , flippedHorizontally((_gid & FlippedHorizontallyFlag) != 0)
{ , flippedVertically((_gid & FlippedVerticallyFlag) != 0)
gid &= ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag); , flippedDiagonally((_gid & FlippedDiagonallyFlag) != 0)
} {
id -= _tilesetFirstGid;
// Global id. }
unsigned gid;
// Tileset id.
// True when the tile should be drawn flipped horizontally. int tilesetId;
bool flippedHorizontally;
// Id.
// True when the tile should be drawn flipped vertically. unsigned id;
bool flippedVertically;
// True when the tile should be drawn flipped horizontally.
// True when the tile should be drawn flipped diagonally. bool flippedHorizontally;
bool flippedDiagonally;
}; // True when the tile should be drawn flipped vertically.
bool flippedVertically;
// True when the tile should be drawn flipped diagonally.
bool flippedDiagonally;
};
}; };

View File

@ -1,107 +1,107 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxObject.cpp // TmxObject.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxObject.h" #include "TmxObject.h"
#include "TmxPolygon.h" #include "TmxPolygon.h"
#include "TmxPolyline.h" #include "TmxPolyline.h"
namespace Tmx namespace Tmx
{ {
Object::Object() Object::Object()
: name() : name()
, type() , type()
, x(0) , x(0)
, y(0) , y(0)
, width(0) , width(0)
, height(0) , height(0)
, gid(0) , gid(0)
, polygon(0) , polygon(0)
, polyline(0) , polyline(0)
, properties() , properties()
{} {}
Object::~Object() Object::~Object()
{ {
if (polygon != 0) if (polygon != 0)
{ {
delete polygon; delete polygon;
polygon = 0; polygon = 0;
} }
if (polyline != 0) if (polyline != 0)
{ {
delete polyline; delete polyline;
polyline = 0; polyline = 0;
} }
} }
void Object::Parse(const TiXmlNode *objectNode) void Object::Parse(const TiXmlNode *objectNode)
{ {
const TiXmlElement *objectElem = objectNode->ToElement(); const TiXmlElement *objectElem = objectNode->ToElement();
// Read the attributes of the object. // Read the attributes of the object.
const char *tempName = objectElem->Attribute("name"); const char *tempName = objectElem->Attribute("name");
const char *tempType = objectElem->Attribute("type"); const char *tempType = objectElem->Attribute("type");
if (tempName) name = tempName; if (tempName) name = tempName;
if (tempType) type = tempType; if (tempType) type = tempType;
objectElem->Attribute("x", &x); objectElem->Attribute("x", &x);
objectElem->Attribute("y", &y); objectElem->Attribute("y", &y);
objectElem->Attribute("width", &width); objectElem->Attribute("width", &width);
objectElem->Attribute("height", &height); objectElem->Attribute("height", &height);
objectElem->Attribute("gid", &gid); objectElem->Attribute("gid", &gid);
// Read the Polygon and Polyline of the object if there are any. // Read the Polygon and Polyline of the object if there are any.
const TiXmlNode *polygonNode = objectNode->FirstChild("polygon"); const TiXmlNode *polygonNode = objectNode->FirstChild("polygon");
if (polygonNode) if (polygonNode)
{ {
if (polygon != 0) if (polygon != 0)
delete polygon; delete polygon;
polygon = new Polygon(); polygon = new Polygon();
polygon->Parse(polygonNode); polygon->Parse(polygonNode);
} }
const TiXmlNode *polylineNode = objectNode->FirstChild("polyline"); const TiXmlNode *polylineNode = objectNode->FirstChild("polyline");
if (polylineNode) if (polylineNode)
{ {
if (polyline != 0) if (polyline != 0)
delete polyline; delete polyline;
polyline = new Polyline(); polyline = new Polyline();
polyline->Parse(polylineNode); polyline->Parse(polylineNode);
} }
// Read the properties of the object. // Read the properties of the object.
const TiXmlNode *propertiesNode = objectNode->FirstChild("properties"); const TiXmlNode *propertiesNode = objectNode->FirstChild("properties");
if (propertiesNode) if (propertiesNode)
{ {
properties.Parse(propertiesNode); properties.Parse(propertiesNode);
} }
} }
}; };

View File

@ -1,98 +1,98 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxObject.h // TmxObject.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
class Polygon; class Polygon;
class Polyline; class Polyline;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Class used for representing a single object from the objectgroup. // Class used for representing a single object from the objectgroup.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Object class Object
{ {
public: public:
Object(); Object();
~Object(); ~Object();
// Parse an object node. // Parse an object node.
void Parse(const TiXmlNode *objectNode); void Parse(const TiXmlNode *objectNode);
// Get the name of the object. // Get the name of the object.
const std::string &GetName() const { return name; } const std::string &GetName() const { return name; }
// Get the type of the object. // Get the type of the object.
const std::string &GetType() const { return type; } const std::string &GetType() const { return type; }
// Get the left side of the object, in pixels. // Get the left side of the object, in pixels.
int GetX() const { return x; } int GetX() const { return x; }
// Get the top side of the object, in pixels. // Get the top side of the object, in pixels.
int GetY() const { return y; } int GetY() const { return y; }
// Get the width of the object, in pixels. // Get the width of the object, in pixels.
int GetWidth() const { return width; } int GetWidth() const { return width; }
// Get the height of the object, in pixels. // Get the height of the object, in pixels.
int GetHeight() const { return height; } int GetHeight() const { return height; }
// Get the Global ID of the tile associated with this object. // Get the Global ID of the tile associated with this object.
int GetGid() const { return gid; } int GetGid() const { return gid; }
// Get the Polygon. // Get the Polygon.
const Polygon *GetPolygon() const { return polygon; } const Tmx::Polygon *GetPolygon() const { return polygon; }
// Get the Polyline. // Get the Polyline.
const Polyline *GetPolyline() const { return polyline; } const Tmx::Polyline *GetPolyline() const { return polyline; }
// Get the property set. // Get the property set.
const PropertySet &GetProperties() const { return properties; } const Tmx::PropertySet &GetProperties() const { return properties; }
private: private:
std::string name; std::string name;
std::string type; std::string type;
int x; int x;
int y; int y;
int width; int width;
int height; int height;
int gid; int gid;
Polygon *polygon; Tmx::Polygon *polygon;
Polyline *polyline; Tmx::Polyline *polyline;
PropertySet properties; Tmx::PropertySet properties;
}; };
}; };

View File

@ -1,76 +1,76 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxObjectGroup.cpp // TmxObjectGroup.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxObjectGroup.h" #include "TmxObjectGroup.h"
#include "TmxObject.h" #include "TmxObject.h"
namespace Tmx namespace Tmx
{ {
ObjectGroup::ObjectGroup() ObjectGroup::ObjectGroup()
: name() : name()
, width(0) , width(0)
, height(0) , height(0)
{} {}
ObjectGroup::~ObjectGroup() ObjectGroup::~ObjectGroup()
{ {
for(std::size_t i = 0; i < objects.size(); i++) for(std::size_t i = 0; i < objects.size(); i++)
{ {
Object *obj = objects.at(i); Object *obj = objects.at(i);
delete obj; delete obj;
} }
} }
void ObjectGroup::Parse(const TiXmlNode *objectGroupNode) void ObjectGroup::Parse(const TiXmlNode *objectGroupNode)
{ {
const TiXmlElement *objectGroupElem = objectGroupNode->ToElement(); const TiXmlElement *objectGroupElem = objectGroupNode->ToElement();
// Read the object group attributes. // Read the object group attributes.
name = objectGroupElem->Attribute("name"); name = objectGroupElem->Attribute("name");
objectGroupElem->Attribute("width", &width); objectGroupElem->Attribute("width", &width);
objectGroupElem->Attribute("height", &height); objectGroupElem->Attribute("height", &height);
objectGroupElem->Attribute("visible", &visible); objectGroupElem->Attribute("visible", &visible);
// Iterate through all of the object elements. // Iterate through all of the object elements.
const TiXmlNode *objectNode = objectGroupNode->FirstChild("object"); const TiXmlNode *objectNode = objectGroupNode->FirstChild("object");
while (objectNode) while (objectNode)
{ {
// Allocate a new object and parse it. // Allocate a new object and parse it.
Object *object = new Object(); Object *object = new Object();
object->Parse(objectNode); object->Parse(objectNode);
// Add the object to the list. // Add the object to the list.
objects.push_back(object); objects.push_back(object);
objectNode = objectGroupNode->IterateChildren("object", objectNode); objectNode = objectGroupNode->IterateChildren("object", objectNode);
} }
} }
}; };

View File

@ -1,84 +1,84 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxObjectGroup.h // TmxObjectGroup.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
#include <vector> #include <vector>
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
class Object; class Object;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// A class used for holding a list of objects. // A class used for holding a list of objects.
// This class doesn't have a property set. // This class doesn't have a property set.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class ObjectGroup class ObjectGroup
{ {
public: public:
ObjectGroup(); ObjectGroup();
~ObjectGroup(); ~ObjectGroup();
// Parse an objectgroup node. // Parse an objectgroup node.
void Parse(const TiXmlNode *objectGroupNode); void Parse(const TiXmlNode *objectGroupNode);
// Get the name of the object group. // Get the name of the object group.
const std::string &GetName() const { return name; } const std::string &GetName() const { return name; }
// Get the width of the object group, in pixels. // Get the width of the object group, in pixels.
// Note: do not rely on this due to temporary bug in tiled. // Note: do not rely on this due to temporary bug in tiled.
int GetWidth() const { return width; } int GetWidth() const { return width; }
// Get the height of the object group, in pixels. // Get the height of the object group, in pixels.
// Note: do not rely on this due to temporary bug in tiled. // Note: do not rely on this due to temporary bug in tiled.
int GetHeight() const { return height; } int GetHeight() const { return height; }
// Get a single object. // Get a single object.
const Object *GetObject(int index) const { return objects.at(index); } const Tmx::Object *GetObject(int index) const { return objects.at(index); }
// Get the number of objects in the list. // Get the number of objects in the list.
int GetNumObjects() const { return objects.size(); } int GetNumObjects() const { return objects.size(); }
// Get whether the object layer is visible. // Get whether the object layer is visible.
int GetVisibility() const { return visible; } int GetVisibility() const { return visible; }
// Get the whole list of objects. // Get the whole list of objects.
const std::vector< Object* > &GetObjects() const { return objects; } const std::vector< Tmx::Object* > &GetObjects() const { return objects; }
private: private:
std::string name; std::string name;
int width; int width;
int height; int height;
int visible; int visible;
std::vector< Object* > objects; std::vector< Tmx::Object* > objects;
}; };
}; };

View File

@ -1,40 +1,40 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPoint.h // TmxPoint.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Used to store a vertex of a Polygon/Polyline. // Used to store a vertex of a Polygon/Polyline.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
struct Point struct Point
{ {
int x; int x;
int y; int y;
}; };
} }

View File

@ -1,56 +1,56 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPolygon.cpp // TmxPolygon.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxPolygon.h" #include "TmxPolygon.h"
namespace Tmx namespace Tmx
{ {
Polygon::Polygon() Polygon::Polygon()
: points() : points()
{ {
} }
void Polygon::Parse(const TiXmlNode *polygonNode) void Polygon::Parse(const TiXmlNode *polygonNode)
{ {
char *pointsLine = strdup(polygonNode->ToElement()->Attribute("points")); char *pointsLine = strdup(polygonNode->ToElement()->Attribute("points"));
char *token = strtok(pointsLine, " "); char *token = strtok(pointsLine, " ");
while (token) while (token)
{ {
Point point; Point point;
sscanf(token, "%d,%d", &point.x, &point.y); sscanf(token, "%d,%d", &point.x, &point.y);
points.push_back(point); points.push_back(point);
token = strtok(0, " "); token = strtok(0, " ");
} }
free(pointsLine); free(pointsLine);
} }
} }

View File

@ -1,58 +1,58 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPolygon.h // TmxPolygon.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <vector> #include <vector>
#include "TmxPoint.h" #include "TmxPoint.h"
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Class to store a Polygon of an Object. // Class to store a Polygon of an Object.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Polygon class Polygon
{ {
public: public:
Polygon(); Polygon();
// Parse the polygon node. // Parse the polygon node.
void Parse(const TiXmlNode *polygonNode); void Parse(const TiXmlNode *polygonNode);
// Get one of the vertices. // Get one of the vertices.
const Point &GetPoint(int index) const { return points[index]; } const Tmx::Point &GetPoint(int index) const { return points[index]; }
// Get the number of vertices. // Get the number of vertices.
int GetNumPoints() const { return points.size(); } int GetNumPoints() const { return points.size(); }
private: private:
std::vector< Point > points; std::vector< Tmx::Point > points;
}; };
}; };

View File

@ -1,56 +1,56 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPolyline.cpp // TmxPolyline.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxPolyline.h" #include "TmxPolyline.h"
namespace Tmx namespace Tmx
{ {
Polyline::Polyline() Polyline::Polyline()
: points() : points()
{ {
} }
void Polyline::Parse(const TiXmlNode *polylineNode) void Polyline::Parse(const TiXmlNode *polylineNode)
{ {
char *pointsLine = strdup(polylineNode->ToElement()->Attribute("points")); char *pointsLine = strdup(polylineNode->ToElement()->Attribute("points"));
char *token = strtok(pointsLine, " "); char *token = strtok(pointsLine, " ");
while (token) while (token)
{ {
Point point; Point point;
sscanf(token, "%d,%d", &point.x, &point.y); sscanf(token, "%d,%d", &point.x, &point.y);
points.push_back(point); points.push_back(point);
token = strtok(0, " "); token = strtok(0, " ");
} }
free(pointsLine); free(pointsLine);
} }
} }

View File

@ -1,58 +1,58 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPolyline.h // TmxPolyline.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <vector> #include <vector>
#include "TmxPoint.h" #include "TmxPoint.h"
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Class to store a Polyline of an Object. // Class to store a Polyline of an Object.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Polyline class Polyline
{ {
public: public:
Polyline(); Polyline();
// Parse the polyline node. // Parse the polyline node.
void Parse(const TiXmlNode *polylineNode); void Parse(const TiXmlNode *polylineNode);
// Get one of the vertices. // Get one of the vertices.
const Point &GetPoint(int index) const { return points[index]; } const Tmx::Point &GetPoint(int index) const { return points[index]; }
// Get the number of vertices. // Get the number of vertices.
int GetNumPoints() const { return points.size(); } int GetNumPoints() const { return points.size(); }
private: private:
std::vector< Point > points; std::vector< Tmx::Point > points;
}; };
}; };

View File

@ -1,78 +1,78 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPropertySet.cpp // TmxPropertySet.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
using std::string; using std::string;
using std::map; using std::map;
namespace Tmx namespace Tmx
{ {
PropertySet::PropertySet() : properties() PropertySet::PropertySet() : properties()
{} {}
void PropertySet::Parse(const TiXmlNode *propertiesNode) void PropertySet::Parse(const TiXmlNode *propertiesNode)
{ {
// Iterate through all of the property nodes. // Iterate through all of the property nodes.
const TiXmlNode *propertyNode = propertiesNode->FirstChild("property"); const TiXmlNode *propertyNode = propertiesNode->FirstChild("property");
string propertyName; string propertyName;
string propertyValue; string propertyValue;
while (propertyNode) while (propertyNode)
{ {
const TiXmlElement* propertyElem = propertyNode->ToElement(); const TiXmlElement* propertyElem = propertyNode->ToElement();
// Read the attributes of the property and add it to the map // Read the attributes of the property and add it to the map
propertyName = string(propertyElem->Attribute("name")); propertyName = string(propertyElem->Attribute("name"));
propertyValue = string(propertyElem->Attribute("value")); propertyValue = string(propertyElem->Attribute("value"));
properties[propertyName] = propertyValue; properties[propertyName] = propertyValue;
propertyNode = propertiesNode->IterateChildren( propertyNode = propertiesNode->IterateChildren(
"property", propertyNode); "property", propertyNode);
} }
} }
string PropertySet::GetLiteralProperty(const string &name) const string PropertySet::GetLiteralProperty(const string &name) const
{ {
// Find the property in the map. // Find the property in the map.
map< string, string >::const_iterator iter = properties.find(name); map< string, string >::const_iterator iter = properties.find(name);
if (iter == properties.end()) if (iter == properties.end())
return std::string("No such property!"); return std::string("No such property!");
return iter->second; return iter->second;
} }
int PropertySet::GetNumericProperty(const string &name) const int PropertySet::GetNumericProperty(const string &name) const
{ {
return atoi(GetLiteralProperty(name).c_str()); return atoi(GetLiteralProperty(name).c_str());
} }
}; };

View File

@ -1,68 +1,68 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxPropertySet.h // TmxPropertySet.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <map> #include <map>
#include <string> #include <string>
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// This class contains a map of properties. // This class contains a map of properties.
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
class PropertySet class PropertySet
{ {
public: public:
PropertySet(); PropertySet();
// Parse a node containing all the property nodes. // Parse a node containing all the property nodes.
void Parse(const TiXmlNode *propertiesNode); void Parse(const TiXmlNode *propertiesNode);
// Get a numeric property (integer). // Get a numeric property (integer).
int GetNumericProperty(const std::string &name) const; int GetNumericProperty(const std::string &name) const;
// Get a literal property (string). // Get a literal property (string).
std::string GetLiteralProperty(const std::string &name) const; std::string GetLiteralProperty(const std::string &name) const;
// Returns the amount of properties. // Returns the amount of properties.
int GetSize() const { return properties.size(); } int GetSize() const { return properties.size(); }
// Returns the STL map of the properties. // Returns the STL map of the properties.
std::map< std::string, std::string > GetList() const std::map< std::string, std::string > GetList() const
{ return properties; } { return properties; }
// Returns whether there are no properties. // Returns whether there are no properties.
bool Empty() const { return properties.empty(); } bool Empty() const { return properties.empty(); }
private: private:
std::map< std::string, std::string > properties; std::map< std::string, std::string > properties;
}; };
}; };

View File

@ -1,55 +1,55 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxTile.cpp // TmxTile.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxTile.h" #include "TmxTile.h"
namespace Tmx namespace Tmx
{ {
Tile::Tile() : properties() Tile::Tile() : properties()
{} {}
Tile::~Tile() Tile::~Tile()
{} {}
void Tile::Parse(const TiXmlNode *tileNode) void Tile::Parse(const TiXmlNode *tileNode)
{ {
const TiXmlElement *tileElem = tileNode->ToElement(); const TiXmlElement *tileElem = tileNode->ToElement();
// Parse the attributes. // Parse the attributes.
tileElem->Attribute("id", &id); tileElem->Attribute("id", &id);
// Parse the properties if any. // Parse the properties if any.
const TiXmlNode *propertiesNode = tileNode->FirstChild("properties"); const TiXmlNode *propertiesNode = tileNode->FirstChild("properties");
if (propertiesNode) if (propertiesNode)
{ {
properties.Parse(propertiesNode); properties.Parse(propertiesNode);
} }
} }
}; };

View File

@ -1,61 +1,61 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxTile.h // TmxTile.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include "TmxPropertySet.h" #include "TmxPropertySet.h"
namespace Tmx namespace Tmx
{ {
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// Class to contain information about every tile in the tileset/tiles // Class to contain information about every tile in the tileset/tiles
// element. // element.
// It may expand if there are more elements or attributes added into the // It may expand if there are more elements or attributes added into the
// the tile element. // the tile element.
// This class also contains a property set. // This class also contains a property set.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Tile class Tile
{ {
public: public:
Tile(); Tile();
~Tile(); ~Tile();
// Parse a tile node. // Parse a tile node.
void Parse(const TiXmlNode *tileNode); void Parse(const TiXmlNode *tileNode);
// Get the Id. (relative to the tilset) // Get the Id. (relative to the tilset)
int GetId() const { return id; } int GetId() const { return id; }
// Get a set of properties regarding the tile. // Get a set of properties regarding the tile.
const PropertySet &GetProperties() const { return properties; } const Tmx::PropertySet &GetProperties() const { return properties; }
private: private:
int id; int id;
PropertySet properties; Tmx::PropertySet properties;
}; };
}; };

View File

@ -1,123 +1,123 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxTileset.cpp // TmxTileset.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <tinyxml.h> #include <tinyxml.h>
#include "TmxTileset.h" #include "TmxTileset.h"
#include "TmxImage.h" #include "TmxImage.h"
#include "TmxTile.h" #include "TmxTile.h"
using std::vector; using std::vector;
using std::string; using std::string;
namespace Tmx namespace Tmx
{ {
Tileset::Tileset() Tileset::Tileset()
: first_gid(0) : first_gid(0)
, name() , name()
, tile_width(0) , tile_width(0)
, tile_height(0) , tile_height(0)
, margin(0) , margin(0)
, spacing(0) , spacing(0)
, image(NULL) , image(NULL)
, tiles() , tiles()
{ {
} }
Tileset::~Tileset() Tileset::~Tileset()
{ {
// Delete the image from memory if allocated. // Delete the image from memory if allocated.
if (image) if (image)
{ {
delete image; delete image;
image = NULL; image = NULL;
} }
// Iterate through all of the tiles in the set and delete each of them. // Iterate through all of the tiles in the set and delete each of them.
vector< Tile* >::iterator tIter; vector< Tile* >::iterator tIter;
for (tIter = tiles.begin(); tIter != tiles.end(); ++tIter) for (tIter = tiles.begin(); tIter != tiles.end(); ++tIter)
{ {
Tile *tile = (*tIter); Tile *tile = (*tIter);
if (tile) if (tile)
{ {
delete tile; delete tile;
tile = NULL; tile = NULL;
} }
} }
} }
void Tileset::Parse(const TiXmlNode *tilesetNode) void Tileset::Parse(const TiXmlNode *tilesetNode)
{ {
const TiXmlElement *tilesetElem = tilesetNode->ToElement(); const TiXmlElement *tilesetElem = tilesetNode->ToElement();
// Read all the attributes into local variables. // Read all the attributes into local variables.
tilesetElem->Attribute("firstgid", &first_gid); tilesetElem->Attribute("firstgid", &first_gid);
tilesetElem->Attribute("tilewidth", &tile_width); tilesetElem->Attribute("tilewidth", &tile_width);
tilesetElem->Attribute("tileheight", &tile_height); tilesetElem->Attribute("tileheight", &tile_height);
tilesetElem->Attribute("margin", &margin); tilesetElem->Attribute("margin", &margin);
tilesetElem->Attribute("spacing", &spacing); tilesetElem->Attribute("spacing", &spacing);
name = tilesetElem->Attribute("name"); name = tilesetElem->Attribute("name");
// Parse the image. // Parse the image.
const TiXmlNode *imageNode = tilesetNode->FirstChild("image"); const TiXmlNode *imageNode = tilesetNode->FirstChild("image");
if (imageNode) if (imageNode)
{ {
image = new Image(); image = new Image();
image->Parse(imageNode); image->Parse(imageNode);
} }
// Iterate through all of the tile elements and parse each. // Iterate through all of the tile elements and parse each.
const TiXmlNode *tileNode = tilesetNode->FirstChild("tile"); const TiXmlNode *tileNode = tilesetNode->FirstChild("tile");
while (tileNode) while (tileNode)
{ {
// Allocate a new tile and parse it. // Allocate a new tile and parse it.
Tile *tile = new Tile(); Tile *tile = new Tile();
tile->Parse(tileNode); tile->Parse(tileNode);
// Add the tile to the collection. // Add the tile to the collection.
tiles.push_back(tile); tiles.push_back(tile);
tileNode = tilesetNode->IterateChildren("tile", tileNode); tileNode = tilesetNode->IterateChildren("tile", tileNode);
} }
} }
const Tile *Tileset::GetTile(int index) const const Tile *Tileset::GetTile(int index) const
{ {
for (unsigned int i = 0; i < tiles.size(); ++i) for (unsigned int i = 0; i < tiles.size(); ++i)
{ {
if (tiles.at(i)->GetId() == index) if (tiles.at(i)->GetId() == index)
{ {
return tiles.at(i); return tiles.at(i);
} }
} }
return NULL; return NULL;
} }
}; };

View File

@ -1,96 +1,96 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxTileset.h // TmxTileset.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
#include <vector> #include <vector>
class TiXmlNode; class TiXmlNode;
namespace Tmx namespace Tmx
{ {
class Image; class Image;
class Tile; class Tile;
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
// A class used for storing information about each of the tilesets. // A class used for storing information about each of the tilesets.
// A tileset is a collection of tiles, of whom each may contain properties. // A tileset is a collection of tiles, of whom each may contain properties.
// The tileset class itself does not have properties. // The tileset class itself does not have properties.
//------------------------------------------------------------------------- //-------------------------------------------------------------------------
class Tileset class Tileset
{ {
public: public:
Tileset(); Tileset();
~Tileset(); ~Tileset();
// Parse a tileset element. // Parse a tileset element.
void Parse(const TiXmlNode *tilesetNode); void Parse(const TiXmlNode *tilesetNode);
// Returns the global id of the first tile. // Returns the global id of the first tile.
int GetFirstGid() const { return first_gid; } int GetFirstGid() const { return first_gid; }
// Returns the name of the tileset. // Returns the name of the tileset.
const std::string &GetName() const { return name; } const std::string &GetName() const { return name; }
// Get the width of a single tile. // Get the width of a single tile.
int GetTileWidth() const { return tile_width; } int GetTileWidth() const { return tile_width; }
// Get the height of a single tile. // Get the height of a single tile.
int GetTileHeight() const { return tile_height; } int GetTileHeight() const { return tile_height; }
// Get the margin of the tileset. // Get the margin of the tileset.
int GetMargin() const { return margin; } int GetMargin() const { return margin; }
// Get the spacing of the tileset. // Get the spacing of the tileset.
int GetSpacing() const { return spacing; } int GetSpacing() const { return spacing; }
// Returns a variable containing information // Returns a variable containing information
// about the image of the tileset. // about the image of the tileset.
const Image* GetImage() const { return image; } const Tmx::Image* GetImage() const { return image; }
// Returns a a single tile of the set. // Returns a a single tile of the set.
const Tile *GetTile(int index) const; const Tmx::Tile *GetTile(int index) const;
// Returns the whole tile collection. // Returns the whole tile collection.
const std::vector< Tile *> &GetTiles() const { return tiles; } const std::vector< Tmx::Tile *> &GetTiles() const { return tiles; }
private: private:
int first_gid; int first_gid;
std::string name; std::string name;
int tile_width; int tile_width;
int tile_height; int tile_height;
int margin; int margin;
int spacing; int spacing;
Image* image; Tmx::Image* image;
std::vector< Tile* > tiles; std::vector< Tmx::Tile* > tiles;
}; };
}; };

View File

@ -1,107 +1,107 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxUtil.cpp // TmxUtil.cpp
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <stdlib.h> #include <stdlib.h>
#include <zlib.h> #include <zlib.h>
#include "TmxUtil.h" #include "TmxUtil.h"
#include "base64.h" #include "base64/base64.h"
namespace Tmx { namespace Tmx {
std::string Util::DecodeBase64(const std::string &str) std::string Util::DecodeBase64(const std::string &str)
{ {
return base64_decode(str); return base64_decode(str);
} }
char *Util::DecompressGZIP(const char *data, int dataSize, int expectedSize) char *Util::DecompressGZIP(const char *data, int dataSize, int expectedSize)
{ {
int bufferSize = expectedSize; int bufferSize = expectedSize;
int ret; int ret;
z_stream strm; z_stream strm;
char *out = (char*)malloc(bufferSize); char *out = (char*)malloc(bufferSize);
strm.zalloc = Z_NULL; strm.zalloc = Z_NULL;
strm.zfree = Z_NULL; strm.zfree = Z_NULL;
strm.opaque = Z_NULL; strm.opaque = Z_NULL;
strm.next_in = (Bytef*)data; strm.next_in = (Bytef*)data;
strm.avail_in = dataSize; strm.avail_in = dataSize;
strm.next_out = (Bytef*)out; strm.next_out = (Bytef*)out;
strm.avail_out = bufferSize; strm.avail_out = bufferSize;
ret = inflateInit2(&strm, 15 + 32); ret = inflateInit2(&strm, 15 + 32);
if (ret != Z_OK) if (ret != Z_OK)
{ {
free(out); free(out);
return NULL; return NULL;
} }
do do
{ {
ret = inflate(&strm, Z_SYNC_FLUSH); ret = inflate(&strm, Z_SYNC_FLUSH);
switch (ret) switch (ret)
{ {
case Z_NEED_DICT: case Z_NEED_DICT:
case Z_STREAM_ERROR: case Z_STREAM_ERROR:
ret = Z_DATA_ERROR; ret = Z_DATA_ERROR;
case Z_DATA_ERROR: case Z_DATA_ERROR:
case Z_MEM_ERROR: case Z_MEM_ERROR:
inflateEnd(&strm); inflateEnd(&strm);
free(out); free(out);
return NULL; return NULL;
} }
if (ret != Z_STREAM_END) if (ret != Z_STREAM_END)
{ {
out = (char *) realloc(out, bufferSize * 2); out = (char *) realloc(out, bufferSize * 2);
if (!out) if (!out)
{ {
inflateEnd(&strm); inflateEnd(&strm);
free(out); free(out);
return NULL; return NULL;
} }
strm.next_out = (Bytef *)(out + bufferSize); strm.next_out = (Bytef *)(out + bufferSize);
strm.avail_out = bufferSize; strm.avail_out = bufferSize;
bufferSize *= 2; bufferSize *= 2;
} }
} }
while (ret != Z_STREAM_END); while (ret != Z_STREAM_END);
if (strm.avail_in != 0) if (strm.avail_in != 0)
{ {
free(out); free(out);
return NULL; return NULL;
} }
inflateEnd(&strm); inflateEnd(&strm);
return out; return out;
} }
}; };

View File

@ -1,43 +1,43 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// TmxUtil.h // TmxUtil.h
// //
// Copyright (c) 2010-2012, Tamir Atias // Copyright (c) 2010-2012, Tamir Atias
// All rights reserved. // All rights reserved.
// //
// Redistribution and use in source and binary forms, with or without // Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: // modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright // * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer. // notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright // * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the // notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution. // documentation and/or other materials provided with the distribution.
// //
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// //
// Author: Tamir Atias // Author: Tamir Atias
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#pragma once #pragma once
#include <string> #include <string>
namespace Tmx namespace Tmx
{ {
class Util class Util
{ {
public: public:
// Decode a base-64 encoded string. // Decode a base-64 encoded string.
static std::string DecodeBase64(const std::string &str); static std::string DecodeBase64(const std::string &str);
// Decompress a gzip encoded byte array. // Decompress a gzip encoded byte array.
static char* DecompressGZIP(const char *data, int dataSize, int expectedSize); static char* DecompressGZIP(const char *data, int dataSize, int expectedSize);
}; };
}; };

View File

@ -1,123 +1,123 @@
/* /*
base64.cpp and base64.h base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages warranty. In no event will the author be held liable for any damages
arising from the use of this software. arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions: freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not 1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code. misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/ */
#include "base64.h" #include "base64.h"
#include <iostream> #include <iostream>
static const std::string base64_chars = static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz" "abcdefghijklmnopqrstuvwxyz"
"0123456789+/"; "0123456789+/";
static inline bool is_base64(unsigned char c) { static inline bool is_base64(unsigned char c) {
return (isalnum(c) || (c == '+') || (c == '/')); return (isalnum(c) || (c == '+') || (c == '/'));
} }
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
std::string ret; std::string ret;
int i = 0; int i = 0;
int j = 0; int j = 0;
unsigned char char_array_3[3]; unsigned char char_array_3[3];
unsigned char char_array_4[4]; unsigned char char_array_4[4];
while (in_len--) { while (in_len--) {
char_array_3[i++] = *(bytes_to_encode++); char_array_3[i++] = *(bytes_to_encode++);
if (i == 3) { if (i == 3) {
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f; char_array_4[3] = char_array_3[2] & 0x3f;
for(i = 0; (i <4) ; i++) for(i = 0; (i <4) ; i++)
ret += base64_chars[char_array_4[i]]; ret += base64_chars[char_array_4[i]];
i = 0; i = 0;
} }
} }
if (i) if (i)
{ {
for(j = i; j < 3; j++) for(j = i; j < 3; j++)
char_array_3[j] = '\0'; char_array_3[j] = '\0';
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
char_array_4[3] = char_array_3[2] & 0x3f; char_array_4[3] = char_array_3[2] & 0x3f;
for (j = 0; (j < i + 1); j++) for (j = 0; (j < i + 1); j++)
ret += base64_chars[char_array_4[j]]; ret += base64_chars[char_array_4[j]];
while((i++ < 3)) while((i++ < 3))
ret += '='; ret += '=';
} }
return ret; return ret;
} }
std::string base64_decode(std::string const& encoded_string) { std::string base64_decode(std::string const& encoded_string) {
int in_len = encoded_string.size(); int in_len = encoded_string.size();
int i = 0; int i = 0;
int j = 0; int j = 0;
int in_ = 0; int in_ = 0;
unsigned char char_array_4[4], char_array_3[3]; unsigned char char_array_4[4], char_array_3[3];
std::string ret; std::string ret;
while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
char_array_4[i++] = encoded_string[in_]; in_++; char_array_4[i++] = encoded_string[in_]; in_++;
if (i ==4) { if (i ==4) {
for (i = 0; i <4; i++) for (i = 0; i <4; i++)
char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_4[i] = base64_chars.find(char_array_4[i]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (i = 0; (i < 3); i++) for (i = 0; (i < 3); i++)
ret += char_array_3[i]; ret += char_array_3[i];
i = 0; i = 0;
} }
} }
if (i) { if (i) {
for (j = i; j <4; j++) for (j = i; j <4; j++)
char_array_4[j] = 0; char_array_4[j] = 0;
for (j = 0; j <4; j++) for (j = 0; j <4; j++)
char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_4[j] = base64_chars.find(char_array_4[j]);
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
} }
return ret; return ret;
} }

View File

@ -1,35 +1,35 @@
/* /*
base64.cpp and base64.h base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages warranty. In no event will the author be held liable for any damages
arising from the use of this software. arising from the use of this software.
Permission is granted to anyone to use this software for any purpose, Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions: freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not 1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be in a product, an acknowledgment in the product documentation would be
appreciated but is not required. appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be 2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code. misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution. 3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/ */
#ifndef TMXPARSER_BASE64_H_ #ifndef TMXPARSER_BASE64_H_
#define TMXPARSER_BASE64_H_ #define TMXPARSER_BASE64_H_
#include <string> #include <string>
std::string base64_encode(unsigned char const* , unsigned int len); std::string base64_encode(unsigned char const* , unsigned int len);
std::string base64_decode(std::string const& s); std::string base64_decode(std::string const& s);
#endif #endif