Skip to content

Commit 0c66457

Browse files
committed
made mapobject class an sf::Transformable
added parsing of map object rotation property fixed setting object transparency actually having an effect updated doxy comments for map object class
1 parent 9ea7ae0 commit 0c66457

File tree

8 files changed

+304
-191
lines changed

8 files changed

+304
-191
lines changed

build/VS2013/Example.vcxproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</ProjectConfiguration>
2020
</ItemGroup>
2121
<ItemGroup>
22-
<ClCompile Include="..\..\examples\ShaderEffects.cpp" />
22+
<ClCompile Include="..\..\examples\DrawMapWithDebug.cpp" />
2323
</ItemGroup>
2424
<PropertyGroup Label="Globals">
2525
<ProjectGuid>{7EDA038A-8CCC-492E-8E6F-56782E3A562E}</ProjectGuid>
@@ -89,7 +89,7 @@
8989
</ClCompile>
9090
<Link>
9191
<AdditionalLibraryDirectories>$(SolutionDir)$(Configuration)\Shared\;extlibs/bin</AdditionalLibraryDirectories>
92-
<AdditionalDependencies>tmxsfml-d.lib;Box2D-d.lib;sfml-graphics-d.lib;sfml-system-d.lib;sfml-window-d.lib;%(AdditionalDependencies)</AdditionalDependencies>
92+
<AdditionalDependencies>tmxsfml-d.lib;Box2D-d.lib;sfml-graphics-d.lib;sfml-system-d.lib;sfml-window-d.lib;zlibstat.lib;%(AdditionalDependencies)</AdditionalDependencies>
9393
<GenerateDebugInformation>true</GenerateDebugInformation>
9494
</Link>
9595
</ItemDefinitionGroup>

build/VS2013/Example.vcxproj.filters

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</Filter>
1616
</ItemGroup>
1717
<ItemGroup>
18-
<ClCompile Include="..\..\examples\ShaderEffects.cpp">
18+
<ClCompile Include="..\..\examples\DrawMapWithDebug.cpp">
1919
<Filter>Source Files</Filter>
2020
</ClCompile>
2121
</ItemGroup>

build/VS2013/tmxsfml.vcxproj

+3
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@
6666
<Lib>
6767
<AdditionalLibraryDirectories>extlibs/bin</AdditionalLibraryDirectories>
6868
</Lib>
69+
<PostBuildEvent>
70+
<Command>copy /Y "$(TargetDir)$(TargetName).dll" "$(SolutionDir)Debug\Example\$(TargetName).dll"</Command>
71+
</PostBuildEvent>
6972
</ItemDefinitionGroup>
7073
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
7174
<ClCompile>

include/tmx/MapLayer.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,14 @@ namespace tmx
4545
using Ptr = std::shared_ptr<TileQuad>; //TODO shared libs don't like this being a unique_ptr
4646
TileQuad(sf::Uint16 i0, sf::Uint16 i1, sf::Uint16 i2, sf::Uint16 i3);
4747
void move(const sf::Vector2f& distance);
48+
void setVisible(bool);
4849
private:
4950
std::array<sf::Uint16, 4u> m_indices;
51+
sf::Color m_colour;
5052
sf::Vector2f m_movement;
5153
LayerSet* m_parentSet;
5254
sf::Int32 m_patchIndex;
55+
void setDirty();
5356
};
5457

5558
/*!

include/tmx/MapObject.hpp

+134-61
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ it freely, subject to the following restrictions:
3232
#include <tmx/Helpers.hpp>
3333
#include <tmx/DebugShape.hpp>
3434

35+
//TODO what's with all these includes???
3536
#include <SFML/Graphics/Color.hpp>
3637
#include <SFML/Graphics/Drawable.hpp>
3738
#include <SFML/Graphics/Font.hpp>
@@ -43,6 +44,7 @@ it freely, subject to the following restrictions:
4344
#include <SFML/Graphics/Sprite.hpp>
4445
#include <SFML/Graphics/Text.hpp>
4546

47+
#include <SFML/Graphics/Transformable.hpp>
4648
#include <SFML/System/NonCopyable.hpp>
4749

4850
#include <string>
@@ -65,8 +67,10 @@ namespace tmx
6567
Tile
6668
};
6769

68-
//map object class.
69-
class TMX_EXPORT_API MapObject final
70+
/*!
71+
\brief Map Object class.
72+
*/
73+
class TMX_EXPORT_API MapObject final : public sf::Transformable
7074
{
7175
private:
7276
struct Segment
@@ -82,81 +86,146 @@ namespace tmx
8286
public:
8387
MapObject();
8488

85-
//**accessors**//
86-
//returns empty string if property not found
89+
/*!
90+
\brief Returns requested property or an empty string if property not found
91+
*/
8792
std::string getPropertyString(const std::string& name);
88-
//returns top left corner of bounding rectangle
89-
sf::Vector2f getPosition() const {return m_position;}
90-
//returns precomputed centre of mass, or zero for polylines
91-
sf::Vector2f getCentre() const {return m_centrePoint;};
92-
//returns the type of shape of the object
93-
MapObjectShape getShapeType() const {return m_shape;};
94-
//returns and object's name
95-
std::string getName() const {return m_name;};
96-
//returns the object's type
97-
std::string getType() const {return m_type;};
98-
//returns the name of the object's parent layer
99-
std::string getParent() const {return m_parent;};
100-
//returns the objects AABB in world coordinates
101-
sf::FloatRect getAABB() const {return m_AABB;};
102-
//returns visibility
103-
bool visible() const {return m_visible;}
104-
//sets a property value, or adds it if property doesn't exist
93+
/*!
94+
\brief Returns precomputed centre of mass, or zero for polylines
95+
*/
96+
sf::Vector2f getCentre() const { return m_centrePoint; }
97+
/*!
98+
\brief Returns the type of shape of the object
99+
*/
100+
MapObjectShape getShapeType() const { return m_shape; }
101+
/*!
102+
\brief Returns the object's name
103+
*/
104+
std::string getName() const { return m_name; }
105+
/*!
106+
\brief Returns the object's type
107+
*/
108+
std::string getType() const { return m_type; }
109+
/*!
110+
\brief Returns the name of the object's parent layer
111+
*/
112+
std::string getParent() const { return m_parent; }
113+
/*!
114+
\brief Returns the object's AABB in world coordinates
115+
*/
116+
sf::FloatRect getAABB() const { return getTransform().transformRect(m_AABB); }
117+
/*!
118+
\brief Returns visibility
119+
*/
120+
bool visible() const { return m_visible; }
121+
/*!
122+
\brief Sets a property value, or adds it if property doesn't exist
123+
*/
105124
void setProperty(const std::string& name, const std::string& value);
106-
//sets the object position in world coords
107-
void setPosition(float x, float y);
108-
void setPosition(const sf::Vector2f& position);
109-
//moves the object by given amount
110-
void move(float x, float y);
111-
void move(const sf::Vector2f& distance);
112-
//sets the width and height of the object
113-
void setSize(const sf::Vector2f& size){m_size = size;};
114-
//sets the object's name
115-
void setName(const std::string& name){m_name = name;}
116-
//sets the object's type
117-
void setType(const std::string& type){m_type = type;};
118-
//sets the name of the object's parent layer
119-
void setParent(const std::string& parent){m_parent = parent;};
120-
//sets the shape type
121-
void setShapeType(MapObjectShape shape){m_shape = shape;};
122-
//sets visibility
123-
void setVisible(bool visible){m_visible = visible;};
124-
//adds a point to the list of polygonal points. If calling this manually
125-
//call CreateDebugShape() afterwards to rebuild debug output
126-
void addPoint(const sf::Vector2f& point){m_polypoints.push_back(point);};
127-
128-
//checks if an object contains given point in world coords.
129-
//Always returns false for polylines.
125+
/*!
126+
\brief Sets the width and height of the object
127+
*/
128+
void setSize(const sf::Vector2f& size){ m_size = size; }
129+
/*!
130+
\brief Sets the object's name
131+
*/
132+
void setName(const std::string& name){ m_name = name; }
133+
/*!
134+
\brief Ssets the object's type
135+
*/
136+
void setType(const std::string& type){ m_type = type; }
137+
/*!
138+
\brief Sets the name of the object's parent layer
139+
*/
140+
void setParent(const std::string& parent){ m_parent = parent; }
141+
/*!
142+
\brief Sets the shape type
143+
*/
144+
void setShapeType(MapObjectShape shape){ m_shape = shape; }
145+
/*!
146+
\brief Sets visibility
147+
*/
148+
void setVisible(bool visible);
149+
/*!
150+
\brief Adds a point to the list of polygonal points.
151+
If calling this manually call createDebugShape() afterwards to
152+
rebuild debug output and AABB
153+
*/
154+
void addPoint(const sf::Vector2f& point){ m_polypoints.push_back(point); }
155+
156+
/*!
157+
\brief Checks if an object contains given point in world coords.
158+
Always returns false for polylines.
159+
*/
130160
bool contains(sf::Vector2f point) const;
131-
//checks if two objects intersect, including polylines
161+
/*!
162+
\brief Checks if two objects intersect, including polylines
163+
*/
132164
bool intersects(const MapObject& object) const;
133-
//creates a shape used for debug drawing - points are in world space
165+
/*!
166+
\brief Creates a shape used for debug drawing - points are in world space
167+
*/
134168
void createDebugShape(const sf::Color& colour);
135-
//draws debug shape to given target
169+
/*!
170+
\brief Draws debug shape to given target
171+
*/
136172
void drawDebugShape(sf::RenderTarget& rt) const;
137-
//returns the first point of poly point member (if any)
173+
/*!
174+
\brief Returns the first point of poly point member (if any) in world coordinates
175+
*/
138176
sf::Vector2f firstPoint() const;
139-
//returns the last point of poly point member (if any)
177+
/*!
178+
\brief Returns the last point of poly point member (if any) in world coordinates
179+
*/
140180
sf::Vector2f lastPoint() const;
141-
//returns a unit vector normal to the polyline segment if intersected
142-
//takes the start and end point of a trajectory
181+
/*!
182+
\brief Returns a unit vector normal to the polyline segment if intersected.
183+
\param start The start point of the segment to test in world coords
184+
\param end The end point of the segment to test in world coords
185+
*/
143186
sf::Vector2f collisionNormal(const sf::Vector2f& start, const sf::Vector2f& end) const;
144-
//creates a vector of segments making up the poly shape
187+
/*!
188+
\brief Creates a vector of segments making up the poly shape
189+
*/
145190
void createSegments();
146-
//returns if an objects poly shape is convex or not
191+
/*!
192+
\brief Returns whether an object's poly shape is convex or not
193+
*/
147194
bool convex() const;
148-
//returns a reference to the array of points making up the object
195+
/*!
196+
\brief Returns a const reference to the array of points making up the object
197+
*/
149198
const std::vector<sf::Vector2f>& polyPoints() const;
150-
//reversing winding of object points
199+
/*!
200+
\brief Reverses the winding of object points
201+
*/
151202
void reverseWinding();
152-
//sets the quad used to draw the tile for tile objects
203+
/*!
204+
\brief Sets the quad used to draw the tile for tile objects
205+
*/
153206
void setQuad(TileQuad* quad);
154207

208+
/*!
209+
\brief Set the position of the object in world coordinates
210+
*/
211+
void setPosition(float, float);
212+
/*!
213+
\brief Set the position of the object in world coordinates
214+
*/
215+
void setPosition(const sf::Vector2f&);
216+
/*!
217+
\brief Move the objects position, in world coordinates
218+
*/
219+
void move(float, float);
220+
/*!
221+
\brief Set the position of the object in world coordinates
222+
*/
223+
void move(const sf::Vector2f&);
224+
155225
private:
156226
//object properties, reflects those which are part of the tmx format
157227
std::string m_name, m_type, m_parent; //parent is name of layer to which object belongs
158-
//sf::FloatRect m_rect; //width / height property of object plus position in world space
159-
sf::Vector2f m_position, m_size;
228+
sf::Vector2f m_size;
160229
std::map <std::string, std::string> m_properties;//map of custom name/value properties
161230
bool m_visible;
162231
std::vector<sf::Vector2f> m_polypoints; //list of points defining any polygonal shape
@@ -183,10 +252,14 @@ namespace tmx
183252
};
184253
using MapObjects = std::vector<MapObject>;
185254

186-
//represents a single tile on a layer
255+
/*!
256+
\brief Represents a single tile on a layer
257+
*/
187258
struct TMX_EXPORT_API MapTile final
188259
{
189-
//returns the base centre point of sprite / tile
260+
/*!
261+
\brief Returns the base centre point of sprite / tile
262+
*/
190263
sf::Vector2f getBase() const
191264
{
192265
return sf::Vector2f(sprite.getPosition().x + (sprite.getLocalBounds().width / 2.f),

src/MapLayer.cpp

+26-7
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ it freely, subject to the following restrictions:
3131
using namespace tmx;
3232
///------TileQuad-----///
3333
TileQuad::TileQuad(sf::Uint16 i0, sf::Uint16 i1, sf::Uint16 i2, sf::Uint16 i3)
34-
: m_parentSet (nullptr),
34+
: m_colour (sf::Color::White),
35+
m_parentSet (nullptr),
3536
m_patchIndex (-1)
3637
{
3738
m_indices[0] = i0;
@@ -43,12 +44,29 @@ TileQuad::TileQuad(sf::Uint16 i0, sf::Uint16 i1, sf::Uint16 i2, sf::Uint16 i3)
4344
void TileQuad::move(const sf::Vector2f& distance)
4445
{
4546
m_movement = distance;
46-
if(m_parentSet)
47-
{
48-
m_parentSet->m_dirtyQuads.push_back(this);
49-
}
47+
setDirty();
5048
}
5149

50+
void TileQuad::setVisible(bool visible)
51+
{
52+
if (visible)
53+
{
54+
m_colour.a = 255u;
55+
}
56+
else
57+
{
58+
m_colour.a = 0u;
59+
}
60+
setDirty();
61+
}
62+
63+
void TileQuad::setDirty()
64+
{
65+
if (m_parentSet)
66+
{
67+
m_parentSet->m_dirtyQuads.push_back(this);
68+
}
69+
}
5270

5371
///------LayerSet-----///
5472

@@ -112,6 +130,7 @@ void LayerSet::draw(sf::RenderTarget& rt, sf::RenderStates states) const
112130
for(const auto& p : q->m_indices)
113131
{
114132
m_patches[q->m_patchIndex][p].position += q->m_movement;
133+
m_patches[q->m_patchIndex][p].color = q->m_colour;
115134
}
116135
//mark AABB as dirty if patch size has changed - TODO this doesn't shrink AABB :/
117136
//if(!m_boundingBox.contains(m_patches[q->m_patchIndex][0].position)
@@ -139,8 +158,8 @@ void LayerSet::draw(sf::RenderTarget& rt, sf::RenderStates states) const
139158
// //TODO this doesn't shrink the AABB!
140159
// if(m_boundingBox.left > min.x) m_boundingBox.left = min.x;
141160
// if(m_boundingBox.top > min.y) m_boundingBox.top = min.y;
142-
// if(std::fabs(m_boundingBox.left) + m_boundingBox.width < max.x) m_boundingBox.width = std::fabs(m_boundingBox.left) + max.x;
143-
// if(std::fabs(m_boundingBox.top) + m_boundingBox.height < max.y) m_boundingBox.width = std::fabs(m_boundingBox.top) + max.y;
161+
// if(std::abs(m_boundingBox.left) + m_boundingBox.width < max.x) m_boundingBox.width = std::fabs(m_boundingBox.left) + max.x;
162+
// if(std::abs(m_boundingBox.top) + m_boundingBox.height < max.y) m_boundingBox.width = std::fabs(m_boundingBox.top) + max.y;
144163
//}
145164

146165
if(!m_visible) return;

src/MapLoaderPrivate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -885,7 +885,7 @@ bool MapLoader::parseObjectgroup(const pugi::xml_node& groupNode)
885885
//set object properties
886886
if(objectNode.attribute("name")) object.setName(objectNode.attribute("name").as_string());
887887
if(objectNode.attribute("type")) object.setType(objectNode.attribute("type").as_string());
888-
//if(objectNode.attribute("rotation")) {} //TODO handle rotation attribute
888+
if(objectNode.attribute("rotation")) object.setRotation(objectNode.attribute("rotation").as_float());
889889
if(objectNode.attribute("visible")) object.setVisible(objectNode.attribute("visible").as_bool());
890890
if(objectNode.attribute("gid"))
891891
{

0 commit comments

Comments
 (0)