[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/Texture/Texture.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/Collision/TileCollision.h \
../src/Actor/NPC.h \
@ -68,7 +54,21 @@ HEADERS += ../src/Actor/Player.h \
../src/Main/TitleScreen.h \
../src/Level/Warp.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 \
../src/Collision/AABB.cpp \
@ -87,18 +87,6 @@ SOURCES += ../src/Actor/Player.cpp \
../src/System/Debug.cpp \
../src/Texture/Texture.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/Sound/SoundEffect.cpp \
../src/Actor/Actor.cpp \
@ -111,6 +99,18 @@ SOURCES += ../src/Actor/Player.cpp \
../src/UI/Button.cpp \
../src/Main/TitleScreen.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

View File

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

View File

@ -25,17 +25,21 @@
//
// Author: Tamir Atias
//-----------------------------------------------------------------------------
#include <stdlib.h>
#include <zlib.h>
#include <tinyxml.h>
#include <zlib.h>
#include <stdlib.h>
#include <stdio.h>
#include "TmxLayer.h"
#include "TmxUtil.h"
#include "TmxMap.h"
#include "TmxTileset.h"
namespace Tmx
{
Layer::Layer()
: name()
Layer::Layer(const Map *_map)
: map(_map)
, name()
, width(0)
, height(0)
, opacity(1.0f)
@ -148,15 +152,30 @@ namespace Tmx
{
const TiXmlElement *tileElem = tileNode->ToElement();
int gid = 0;
unsigned gid = 0;
// Read the Global-ID of the tile directly into the array entry.
tileElem->Attribute("gid", &gid);
// Read the Global-ID of the tile.
const char* gidText = tileElem->Attribute("gid");
// Convert the gid to a map tile.
tile_map[tileCount++] = MapTile((unsigned)gid);
// Convert to an unsigned.
sscanf(gidText, "%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);
}
tileNode = dataNode->IterateChildren("tile", tileNode);
tileCount++;
}
}
@ -199,7 +218,21 @@ namespace Tmx
{
for (int y = 0; y < height; y++)
{
tile_map[y * width + x] = MapTile(out[y * width + x]);
unsigned gid = out[y * width + x];
// 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[y * width + x] = MapTile(gid, tileset->GetFirstGid(), tilesetIndex);
}
else
{
// Otherwise, make it null.
tile_map[y * width + x] = MapTile(gid, 0, -1);
}
}
}
@ -213,15 +246,30 @@ namespace Tmx
char *csv = strdup(innerText.c_str());
// Iterate through every token of ';' in the CSV string.
char *pch = strtok(csv, ";");
char *pch = strtok(csv, ",");
int tileCount = 0;
while (pch)
{
tile_map[tileCount] = MapTile((unsigned)atoi(pch));
unsigned gid;
sscanf(pch, "%u", &gid);
++tileCount;
pch = strtok(NULL, ";");
// 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

@ -36,6 +36,8 @@ class TiXmlNode;
namespace Tmx
{
class Map;
//-------------------------------------------------------------------------
// Type used for the encoding of the layer data.
//-------------------------------------------------------------------------
@ -63,7 +65,7 @@ namespace Tmx
class Layer
{
public:
Layer();
Layer(const Tmx::Map *_map);
~Layer();
// Parse a layer node.
@ -73,48 +75,53 @@ namespace Tmx
const std::string &GetName() const { return name; }
// Get the width of the layer, in tiles.
float GetWidth() const { return width; }
int GetWidth() const { return width; }
// Get the height of the layer, in tiles.
float GetHeight() const { return height; }
int GetHeight() const { return height; }
// Get the visibility of the layer
bool IsVisible() const { return visible; }
// Get the property set.
const PropertySet &GetProperties() const { return properties; }
const Tmx::PropertySet &GetProperties() const { return properties; }
// Pick a specific tile from the list.
unsigned GetTileGid(int x, int y) const { return tile_map[y * width + x].gid; }
unsigned GetTileId(int x, int y) const { return tile_map[y * width + x].id; }
// Get whether the tile is flipped horizontally.
// 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 a tile is flipped horizontally.
bool IsTileFlippedHorizontally(int x, int y) const
{ return tile_map[y * width + x].flippedHorizontally; }
// Get whether the tile is flipped vertically.
// Get whether a tile is flipped vertically.
bool IsTileFlippedVertically(int x, int y) const
{ return tile_map[y * width + x].flippedVertically; }
// Get whether the tile is flipped diagonally.
// Get whether a tile is flipped diagonally.
bool IsTileFlippedDiagonally(int x, int y) const
{ return tile_map[y * width + x].flippedDiagonally; }
// Get the tile specific to the map.
MapTile GetTile(int x, int y) const { return tile_map[y * width + x]; }
// 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 encoding that was used for parsing the layer data.
// See: LayerEncodingType
LayerEncodingType GetEncoding() const { return encoding; }
Tmx::LayerEncodingType GetEncoding() const { return encoding; }
// Get the type of compression that was used for parsing the layer data.
// See: LayerCompressionType
LayerCompressionType GetCompression() const { return compression; }
Tmx::LayerCompressionType GetCompression() const { return compression; }
private:
void ParseXML(const TiXmlNode *dataNode);
void ParseBase64(const std::string &innerText);
void ParseCSV(const std::string &innerText);
const Tmx::Map *map;
std::string name;
int width;
@ -123,11 +130,11 @@ namespace Tmx
float opacity;
bool visible;
PropertySet properties;
Tmx::PropertySet properties;
MapTile *tile_map;
Tmx::MapTile *tile_map;
LayerEncodingType encoding;
LayerCompressionType compression;
Tmx::LayerEncodingType encoding;
Tmx::LayerCompressionType compression;
};
};

View File

@ -25,8 +25,8 @@
//
// Author: Tamir Atias
//-----------------------------------------------------------------------------
#include <cstdio>
#include <tinyxml.h>
#include <stdio.h>
#include "TmxMap.h"
#include "TmxTileset.h"
@ -209,7 +209,7 @@ namespace Tmx
while (layerNode)
{
// Allocate a new layer and parse it.
Layer *layer = new Layer();
Layer *layer = new Layer(this);
layer->Parse(layerNode);
// Add the layer to the list.
@ -233,6 +233,23 @@ namespace Tmx
}
}
int Map::FindTilesetIndex(int gid) const
{
// Clean up the flags from the gid (thanks marwes91).
gid &= ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag);
for (int i = tilesets.size() - 1; i > -1; --i)
{
// If the gid beyond the tileset gid return its index.
if (gid >= tilesets[i]->GetFirstGid())
{
return i;
}
}
return -1;
}
const Tileset *Map::FindTileset(int gid) const
{
for (int i = tilesets.size() - 1; i > -1; --i)

View File

@ -91,7 +91,7 @@ namespace Tmx
double GetVersion() const { return version; }
// 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.
int GetWidth() const { return width; }
@ -106,34 +106,37 @@ namespace Tmx
int GetTileHeight() const { return tile_height; }
// 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.
int GetNumLayers() const { return layers.size(); }
// 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.
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.
int GetNumObjectGroups() const { return object_groups.size(); }
// 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 the tileset index for a tileset using a tile gid.
int FindTilesetIndex(int gid) const;
// Find a tileset for a specific gid.
const Tileset *FindTileset(int gid) const;
const Tmx::Tileset *FindTileset(int gid) const;
// Get a tileset by an index.
const Tileset *GetTileset(int index) const { return tilesets.at(index); }
const Tmx::Tileset *GetTileset(int index) const { return tilesets.at(index); }
// Get the amount of tilesets.
int GetNumTilesets() const { return tilesets.size(); }
// Get the collection of tilesets.
const std::vector< Tileset* > &GetTilesets() const { return tilesets; }
const std::vector< Tmx::Tileset* > &GetTilesets() const { return tilesets; }
// Get whether there was an error or not.
bool HasError() const { return has_error; }
@ -145,28 +148,28 @@ namespace Tmx
unsigned char GetErrorCode() const { return error_code; }
// Get the property set.
const PropertySet &GetProperties() { return properties; }
const Tmx::PropertySet &GetProperties() { return properties; }
private:
std::string file_name;
std::string file_path;
double version;
MapOrientation orientation;
Tmx::MapOrientation orientation;
int width;
int height;
int tile_width;
int tile_height;
std::vector< Layer* > layers;
std::vector< ObjectGroup* > object_groups;
std::vector< Tileset* > tilesets;
std::vector< Tmx::Layer* > layers;
std::vector< Tmx::ObjectGroup* > object_groups;
std::vector< Tmx::Tileset* > tilesets;
bool has_error;
unsigned char error_code;
std::string error_text;
PropertySet properties;
Tmx::PropertySet properties;
};
};

View File

@ -43,7 +43,8 @@ namespace Tmx
{
// Default constructor.
MapTile()
: gid(0)
: tilesetId(0)
, id(0)
, flippedHorizontally(false)
, flippedVertically(false)
, flippedDiagonally(false)
@ -51,17 +52,21 @@ namespace Tmx
// Will take a gid and read the attributes from the first
// two bits of it.
MapTile(unsigned _gid)
: gid(_gid)
MapTile(unsigned _gid, int _tilesetFirstGid, unsigned _tilesetId)
: tilesetId(_tilesetId)
, id(_gid & ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag))
, flippedHorizontally((_gid & FlippedHorizontallyFlag) != 0)
, flippedVertically((_gid & FlippedVerticallyFlag) != 0)
, flippedDiagonally((_gid & FlippedDiagonallyFlag) != 0)
{
gid &= ~(FlippedHorizontallyFlag | FlippedVerticallyFlag | FlippedDiagonallyFlag);
id -= _tilesetFirstGid;
}
// Global id.
unsigned gid;
// Tileset id.
int tilesetId;
// Id.
unsigned id;
// True when the tile should be drawn flipped horizontally.
bool flippedHorizontally;

View File

@ -72,13 +72,13 @@ namespace Tmx
int GetGid() const { return gid; }
// Get the Polygon.
const Polygon *GetPolygon() const { return polygon; }
const Tmx::Polygon *GetPolygon() const { return polygon; }
// Get the Polyline.
const Polyline *GetPolyline() const { return polyline; }
const Tmx::Polyline *GetPolyline() const { return polyline; }
// Get the property set.
const PropertySet &GetProperties() const { return properties; }
const Tmx::PropertySet &GetProperties() const { return properties; }
private:
std::string name;
@ -90,9 +90,9 @@ namespace Tmx
int height;
int gid;
Polygon *polygon;
Polyline *polyline;
Tmx::Polygon *polygon;
Tmx::Polyline *polyline;
PropertySet properties;
Tmx::PropertySet properties;
};
};

View File

@ -61,7 +61,7 @@ namespace Tmx
int GetHeight() const { return height; }
// 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.
int GetNumObjects() const { return objects.size(); }
@ -70,7 +70,7 @@ namespace Tmx
int GetVisibility() const { return visible; }
// Get the whole list of objects.
const std::vector< Object* > &GetObjects() const { return objects; }
const std::vector< Tmx::Object* > &GetObjects() const { return objects; }
private:
std::string name;
@ -79,6 +79,6 @@ namespace Tmx
int height;
int visible;
std::vector< Object* > objects;
std::vector< Tmx::Object* > objects;
};
};

View File

@ -47,12 +47,12 @@ namespace Tmx
void Parse(const TiXmlNode *polygonNode);
// 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.
int GetNumPoints() const { return points.size(); }
private:
std::vector< Point > points;
std::vector< Tmx::Point > points;
};
};

View File

@ -47,12 +47,12 @@ namespace Tmx
void Parse(const TiXmlNode *polylineNode);
// 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.
int GetNumPoints() const { return points.size(); }
private:
std::vector< Point > points;
std::vector< Tmx::Point > points;
};
};

View File

@ -51,11 +51,11 @@ namespace Tmx
int GetId() const { return id; }
// Get a set of properties regarding the tile.
const PropertySet &GetProperties() const { return properties; }
const Tmx::PropertySet &GetProperties() const { return properties; }
private:
int id;
PropertySet properties;
Tmx::PropertySet properties;
};
};

View File

@ -71,13 +71,13 @@ namespace Tmx
// Returns a variable containing information
// 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.
const Tile *GetTile(int index) const;
const Tmx::Tile *GetTile(int index) const;
// Returns the whole tile collection.
const std::vector< Tile *> &GetTiles() const { return tiles; }
const std::vector< Tmx::Tile *> &GetTiles() const { return tiles; }
private:
int first_gid;
@ -89,8 +89,8 @@ namespace Tmx
int margin;
int spacing;
Image* image;
Tmx::Image* image;
std::vector< Tile* > tiles;
std::vector< Tmx::Tile* > tiles;
};
};

View File

@ -29,7 +29,7 @@
#include <zlib.h>
#include "TmxUtil.h"
#include "base64.h"
#include "base64/base64.h"
namespace Tmx {
std::string Util::DecodeBase64(const std::string &str)