Skip to content

Commit 4bf72eb

Browse files
committed
initial loading and parsing of object templates
1 parent 756052d commit 4bf72eb

18 files changed

Lines changed: 177 additions & 33 deletions

ParseTest/ParseTest.vcxproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,32 @@
2121
<PropertyGroup Label="Globals">
2222
<ProjectGuid>{4C5C8EE4-25F6-4121-B23D-89CD8D9FD74E}</ProjectGuid>
2323
<RootNamespace>ParseTest</RootNamespace>
24-
<WindowsTargetPlatformVersion>10.0.16299.0</WindowsTargetPlatformVersion>
24+
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
2525
</PropertyGroup>
2626
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
2727
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
2828
<ConfigurationType>Application</ConfigurationType>
2929
<UseDebugLibraries>true</UseDebugLibraries>
30-
<PlatformToolset>v141</PlatformToolset>
30+
<PlatformToolset>v142</PlatformToolset>
3131
<CharacterSet>MultiByte</CharacterSet>
3232
</PropertyGroup>
3333
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
3434
<ConfigurationType>Application</ConfigurationType>
3535
<UseDebugLibraries>false</UseDebugLibraries>
36-
<PlatformToolset>v141</PlatformToolset>
36+
<PlatformToolset>v142</PlatformToolset>
3737
<WholeProgramOptimization>true</WholeProgramOptimization>
3838
<CharacterSet>MultiByte</CharacterSet>
3939
</PropertyGroup>
4040
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
4141
<ConfigurationType>Application</ConfigurationType>
4242
<UseDebugLibraries>true</UseDebugLibraries>
43-
<PlatformToolset>v141</PlatformToolset>
43+
<PlatformToolset>v142</PlatformToolset>
4444
<CharacterSet>MultiByte</CharacterSet>
4545
</PropertyGroup>
4646
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
4747
<ConfigurationType>Application</ConfigurationType>
4848
<UseDebugLibraries>false</UseDebugLibraries>
49-
<PlatformToolset>v141</PlatformToolset>
49+
<PlatformToolset>v142</PlatformToolset>
5050
<WholeProgramOptimization>true</WholeProgramOptimization>
5151
<CharacterSet>MultiByte</CharacterSet>
5252
</PropertyGroup>

ParseTest/src/main.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int main()
3535
{
3636
tmx::Map map;
3737

38-
if (map.load("maps/platform.tmx"))
38+
if (map.load("maps/untitled.tmx"))
3939
{
4040
std::cout << "Loaded Map version: " << map.getVersion().upper << ", " << map.getVersion().lower << std::endl;
4141

@@ -74,14 +74,19 @@ int main()
7474
std::cout << "Found " << objects.size() << " objects in layer" << std::endl;
7575
for(const auto& object : objects)
7676
{
77-
std::cout << "Object " << object.getName() << std::endl;
77+
std::cout << "Object " << object.getUID() << ", " << object.getName() << std::endl;
7878
const auto& properties = object.getProperties();
7979
std::cout << "Object has " << properties.size() << " properties" << std::endl;
8080
for(const auto& prop : properties)
8181
{
8282
std::cout << "Found property: " << prop.getName() << std::endl;
8383
std::cout << "Type: " << int(prop.getType()) << std::endl;
8484
}
85+
86+
if (!object.getTilesetName().empty())
87+
{
88+
std::cout << "Object uses template tile set " << object.getTilesetName() << "\n";
89+
}
8590
}
8691
}
8792

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tmxlite uses [pugixml](https://pugixml.org/) and [miniz](https://github.com/rich
3131

3232
***
3333

34-
Matt Marchant & contributors 2016 - 2019
34+
Matt Marchant & contributors 2016 - 2020
3535
http://trederia.blogspot.com
3636

3737
tmxlite - Zlib license.

tmxlite/include/tmxlite/ImageLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace tmx
4646
~ImageLayer() = default;
4747

4848
Type getType() const override { return Layer::Type::Image; }
49-
void parse(const pugi::xml_node&) override;
49+
void parse(const pugi::xml_node&, Map*) override;
5050

5151
/*!
5252
\brief Returns the path, relative to the working directory,

tmxlite/include/tmxlite/Layer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace tmx
102102
/*!
103103
\brief Attempts to parse the specific node layer type
104104
*/
105-
virtual void parse(const pugi::xml_node&) = 0;
105+
virtual void parse(const pugi::xml_node&, Map* = nullptr) = 0;
106106

107107
/*!
108108
\brief Returns the name of the layer

tmxlite/include/tmxlite/LayerGroup.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace tmx
5353

5454

5555
Type getType() const override { return Layer::Type::Group; }
56-
void parse(const pugi::xml_node&) override;
56+
void parse(const pugi::xml_node&, Map*) override;
5757

5858
/*!
5959
\brief Returns a reference to the vector containing the layer data.

tmxlite/include/tmxlite/Map.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@ source distribution.
3232
#include <tmxlite/Layer.hpp>
3333
#include <tmxlite/Property.hpp>
3434
#include <tmxlite/Types.hpp>
35+
#include <tmxlite/Object.hpp>
3536

3637
#include <string>
3738
#include <vector>
3839
#include <map>
40+
#include <unordered_map>
3941

4042
namespace tmx
4143
{
@@ -181,6 +183,26 @@ namespace tmx
181183
*/
182184
const std::map<std::uint32_t, Tileset::Tile>& getAnimatedTiles() const { return m_animTiles; }
183185

186+
/*!
187+
\brief Returns the current working directory of the map. Images and
188+
other resources are loaded relative to this.
189+
*/
190+
const std::string& getWorkingDirectory() const { return m_workingDirectory; }
191+
192+
/*!
193+
\brief Returns an unordered_map of template objects indexed by file name
194+
*/
195+
std::unordered_map<std::string, Object>& getTemplateObjects() { return m_templateObjects; }
196+
const std::unordered_map<std::string, Object>& getTemplateObjects() const { return m_templateObjects; }
197+
198+
/*!
199+
\brief Returns an unordered_map of tilesets used by templated objects.
200+
If Object::getTileSetName() is not empty it can be used to retreive a tileset
201+
from this map. Otherwise the object's tileset can be found from in the map's
202+
global tilesets returned by getTilesets().
203+
*/
204+
std::unordered_map<std::string, Tileset>& getTemplateTilesets() { return m_templateTilesets; }
205+
const std::unordered_map<std::string, Tileset>& getTemplateTilesets() const { return m_templateTilesets; }
184206

185207
private:
186208
Version m_version;
@@ -203,6 +225,9 @@ namespace tmx
203225
std::vector<Property> m_properties;
204226
std::map<std::uint32_t, Tileset::Tile> m_animTiles;
205227

228+
std::unordered_map<std::string, Object> m_templateObjects;
229+
std::unordered_map<std::string, Tileset> m_templateTilesets;
230+
206231
//always returns false so we can return this
207232
//on load failure
208233
bool reset();

tmxlite/include/tmxlite/Object.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ namespace pugi
4242

4343
namespace tmx
4444
{
45+
class Map;
46+
4547
/*!
4648
\brief Contains the text information stored in a Text object.
4749
*/
@@ -98,7 +100,7 @@ namespace tmx
98100
\brief Attempts to parse the given xml node and
99101
read the Object properties if it is valid.
100102
*/
101-
void parse(const pugi::xml_node&);
103+
void parse(const pugi::xml_node&, Map*);
102104

103105
/*!
104106
\brief Returns the unique ID of the Object
@@ -174,6 +176,14 @@ namespace tmx
174176
const Text& getText() const { return m_textData; }
175177
Text& getText() { return m_textData; }
176178

179+
/*!
180+
\brief Returns the tileset name used by this object if it is derived
181+
from a template, else returns an empty string.
182+
If the string is not empty use it to index the unordered_map returned
183+
by Map::getTemplateTilesets()
184+
*/
185+
const std::string& getTilesetName() const { return m_tilesetName; }
186+
177187
private:
178188
std::uint32_t m_UID;
179189
std::string m_name;
@@ -190,9 +200,11 @@ namespace tmx
190200

191201
Text m_textData;
192202

203+
std::string m_tilesetName;
204+
193205
void parsePoints(const pugi::xml_node&);
194206
void parseText(const pugi::xml_node&);
195-
void parseTemplate(const std::string&);
207+
void parseTemplate(const std::string&, Map*);
196208
};
197209
}
198210

tmxlite/include/tmxlite/ObjectGroup.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ namespace tmx
5353
~ObjectGroup() = default;
5454

5555
Type getType() const override { return Layer::Type::Object; }
56-
void parse(const pugi::xml_node&) override;
56+
void parse(const pugi::xml_node&, Map*) override;
5757

5858
/*!
5959
\brief Returns the colour associated with this layer

tmxlite/include/tmxlite/TileLayer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ namespace tmx
6161
~TileLayer() = default;
6262

6363
Type getType() const override { return Layer::Type::Tile; }
64-
void parse(const pugi::xml_node&) override;
64+
void parse(const pugi::xml_node&, Map*) override;
6565

6666
/*!
6767
\brief Returns the list of tiles used to make up the layer

0 commit comments

Comments
 (0)