1
- /*********************************************************************
1
+ Tiled tmx map loader for SFML 2.0.0
2
+ -----------------------------------
3
+
2
4
3
- Matt Marchant 2013 - 2015
4
- SFML Tiled Map Loader - https://github.com/bjorn/tiled/wiki/TMX-Map-Format
5
+ /*********************************************************************
5
6
6
- The zlib license has been used to make this software fully compatible
7
- with SFML. See http ://www.sfml-dev.org/license.php
7
+ Matt Marchant 2013 - 2016
8
+ SFML Tiled Map Loader - https ://github.com/bjorn/tiled/wiki/TMX-Map-Format
8
9
9
- This software is provided 'as-is', without any express or
10
- implied warranty. In no event will the authors be held
11
- liable for any damages arising from the use of this software.
10
+ Zlib License:
11
+ This software is provided 'as-is', without any express or
12
+ implied warranty. In no event will the authors be held
13
+ liable for any damages arising from the use of this software.
12
14
13
- Permission is granted to anyone to use this software for any purpose,
14
- including commercial applications, and to alter it and redistribute
15
- it freely, subject to the following restrictions:
15
+ Permission is granted to anyone to use this software for any purpose,
16
+ including commercial applications, and to alter it and redistribute
17
+ it freely, subject to the following restrictions:
16
18
17
- 1 . The origin of this software must not be misrepresented;
18
- you must not claim that you wrote the original software.
19
- If you use this software in a product, an acknowledgment
20
- in the product documentation would be appreciated but
21
- is not required.
19
+ 1 . The origin of this software must not be misrepresented;
20
+ you must not claim that you wrote the original software.
21
+ If you use this software in a product, an acknowledgment
22
+ in the product documentation would be appreciated but
23
+ is not required.
22
24
23
- 2 . Altered source versions must be plainly marked as such,
24
- and must not be misrepresented as being the original software.
25
+ 2 . Altered source versions must be plainly marked as such,
26
+ and must not be misrepresented as being the original software.
25
27
26
- 3 . This notice may not be removed or altered from any
27
- source distribution.
28
+ 3 . This notice may not be removed or altered from any
29
+ source distribution.
28
30
29
- ********************************************************************* /
31
+ ********************************************************************* /
30
32
31
- This class is designed to load TILED .tmx format maps, compatible with
33
+ This class is designed to load TILED .tmx format maps, compatible with
32
34
TILED up to version 0.9.0
33
35
34
36
http://trederia.blogspot.co.uk/2013/05/tiled-map-loader-for-sfml.html
@@ -56,13 +58,22 @@ What's not supported / limitations
56
58
Parsing of individual tile properties
57
59
Staggered isometric maps
58
60
61
+
62
+ Requirements
63
+ ------------
64
+
65
+ pugixml (included)
66
+ zlib (http://zlib.net/ )
67
+ SFML 2.x (http://sfml-dev.org )
68
+ Minimal C++11 compiler support (tested with VS11 and GCC4.7)
69
+
70
+
59
71
Usage
60
72
-----
61
73
62
- First download the zlib library which matches your compiler and link it to your
63
- project, then add the map loader .cpp files to your project/command line. Only
64
- add the tmx2box2d files if you are using box2D for physics and wish to convert
65
- map object data to box2D physics bodies.
74
+ With the Cmake file provided create a project for the compiler of your choice to build and
75
+ install the map loader as either a static or shared library. You can use cmake-gui (useful
76
+ for windows users) to see all the options, such as building the example applications.
66
77
67
78
68
79
To quickly get up and running create an instance of the MapLoader class
@@ -71,25 +82,25 @@ To quickly get up and running create an instance of the MapLoader class
71
82
72
83
load a map file
73
84
74
- ml.Load ("map.tmx");
85
+ ml.load ("map.tmx");
75
86
76
87
and draw it in your main loop
77
88
78
89
renderTarget.draw(ml);
79
90
80
91
81
92
Note that the constructor takes a path to the directory containing the map files as a parameter (with
82
- or without the trailing '/') so that you only need pass the map name to MapLoader::Load(). If you have
93
+ or without the trailing '/') so that you only need pass the map name to ` MapLoader::load() ` . If you have
83
94
images and/or tileset data in another directory you may add it with:
84
95
85
- ml.AddSearchPath (path);
96
+ ml.addSearchPath (path);
86
97
87
98
* before* attempting to load the map file.
88
99
89
100
New maps can be loaded simply by calling the load function again, existing maps will be automatically
90
- unloaded. MapLoader::Load() also returns true on success and false on failure, to aid running the function
101
+ unloaded. ` MapLoader::load() ` also returns true on success and false on failure, to aid running the function
91
102
in its own thread for example. Conversion functions are provided for converting coordinate spaces between
92
- orthogonal and isometric. For instance MapLoader::OrthogonalToIsometric will convert mouse coordinates from
103
+ orthogonal and isometric. For instance ` MapLoader::orthogonalToIsometric() ` will convert mouse coordinates from
93
104
screen space:
94
105
95
106
0--------X
@@ -100,7 +111,7 @@ screen space:
100
111
|
101
112
Y
102
113
103
- to Isometric space:
114
+ to isometric space:
104
115
105
116
0
106
117
/ \
@@ -109,16 +120,17 @@ to Isometric space:
109
120
/ \
110
121
Y X
111
122
112
- Layer information can be accessed through MapLoader::GetLayers()
123
+ Layer information can be accessed through ` MapLoader::getLayers() `
113
124
114
125
bool collision;
115
- for(auto layer = ml.GetLayers().begin(); layer != ml.GetLayers().end(); ++layer)
126
+ const auto& layers = ml.getLayers();
127
+ for(const auto& layer : layers)
116
128
{
117
- if(layer-> name == "Collision")
129
+ if(layer. name == "Collision")
118
130
{
119
- for(auto object = layer->objects.begin(); object != layer-> objects.end(); ++object )
131
+ for(const auto& object : layer. objects)
120
132
{
121
- collision = object->Contains (point);
133
+ collision = object.contains (point);
122
134
}
123
135
}
124
136
}
@@ -128,34 +140,39 @@ The quad tree is used to reduce the number of MapObjects used during a query. If
128
140
hundreds of objects which are used for collision it makes little sense to test them all if only
129
141
a few are ever within collision range. For example in your update loop first call:
130
142
131
- ml.UpdateQuadTree (myRect);
143
+ ml.updateQuadTree (myRect);
132
144
133
- where myRect is the area representing the root node. You will probably only want to start with
134
- MapObjects which are visible on screen, so set myRect to your view area. Then query the quad tree
135
- with another floatRect, representing the area for potential collision. This could be the bounding
145
+ where ` myRect ` is the area representing the root node. You will probably only want to start with
146
+ MapObjects which are visible on screen, so set ` myRect ` to your view area. Then query the quad tree
147
+ with another FloatRect representing the area for potential collision. This could be the bounding
136
148
box of your sprite:
137
149
138
- std::vector<MapObject*> objects = ml.QueryQuadTree (mySprite.getGlobalBounds());
150
+ std::vector<MapObject*> objects = ml.queryQuadTree (mySprite.getGlobalBounds());
139
151
140
152
This returns a vector of pointers to MapObjects contained within any quads which are intersected
141
153
by your sprite's bounds. You can then proceed to perform any collision testing as usual.
142
154
143
155
144
- Some utility functions are providied in tmx2box2d.h/cpp. If you use box2D for physics then add these
145
- files to you project and then create box2D physics bodies using:
156
+ Some utility functions are providied in tmx2box2d.h/cpp. If you use box2d for physics then add these
157
+ files to you project, or set the box2d option to true when configuring the cmake file. You may then
158
+ create box2d physics bodies using the BodyCreator:
146
159
147
- b2Body* body = tmx::BodyCreator::Add(mapObject, b2World);
160
+ tmx::BodyCreator bodyCreator;
161
+ b2Body* body = bodyCreator.add(mapObject, b2World);
148
162
149
- where b2World is a reference to a valid box2D physics world. As box2D uses a different coordinate
150
- system to SFML there are 4 functions for converting from one space to another:
163
+ where b2World is a reference to a valid box2D physics world. The body creator is only a utility
164
+ class, so it is no problem letting it go out of scope once your bodies are all created. As box2d
165
+ uses a different coordinate system to SFML there are 4 functions for converting from one space to
166
+ another:
151
167
152
- b2Vec2 SfToBoxVec (const sf::Vector2f& vec);
153
- sf::Vector2f BoxToSfVec (const b2Vec2& vec);
154
- float SfToBoxFloat (float val);
155
- float BoxToSfFloat (float val);
168
+ b2Vec2 sfToBoxVec (const sf::Vector2f& vec);
169
+ sf::Vector2f boxToSfVec (const b2Vec2& vec);
170
+ float sfToBoxFloat (float val);
171
+ float boxToSfFloat (float val);
156
172
157
173
You should use these whenever you are trying to draw with SFML based on the physics output, or set
158
- box2D parameters in SFML world values.
174
+ box2d parameters in SFML world values. When using box2d you may find that the build types of both
175
+ the map loader and box2d should match (ie both static or both shared libs).
159
176
160
177
161
178
Debugging output can be enabled with one of the following preprocessor directives:
@@ -172,76 +189,18 @@ all output is written to a file named output.log in the executable directory
172
189
173
190
log output is directed to both the console and output.log
174
191
175
- Logging is diabled by default. The level of log information can be set with
192
+ Logging is disabled by default. The level of log information can be set with
176
193
177
- Logger::SetLogLevel ()
194
+ tmx::setLogLevel ()
178
195
179
196
by providing a bitmask for the level required. For instance to only log warnings
180
197
and errors use:
181
198
182
- Logger::SetLogLevel( Logger::Warning | Logger::Error);
199
+ tmx::setLogLevel(tmx:: Logger::Warning | tmx:: Logger::Error);
183
200
184
201
185
202
186
203
For more detailed examples see the source in the examples folder, and the wiki on github:
187
204
https://github.com/fallahn/sfml-tmxloader/wiki
188
205
189
-
190
-
191
- Requirements
192
- ------------
193
-
194
- pugixml (included)
195
- zlib (http://zlib.net/ )
196
- SFML 2.x (http://sfml-dev.org )
197
- Minimal C++11 compiler support (tested with VS11 and GCC4.7)
198
-
199
-
200
- Note about CMake file
201
- ---------------------
202
-
203
- The tmx map loader was designed to have its source included as part of another project. The
204
- CMake file is included for linux / mac platforms where building installing libraries is the
205
- norm. The tmx source doesn't explicitly export any functions, so when building the tmx files
206
- as a library under Windows (particularly Visual Studio) VC will not generate any lib files.
207
- To work around this just include the tmx source files in your Visual Studio project (as well
208
- as pugixml), so that they are compiled as part of the final executable.
209
-
210
-
211
- Revision History
212
- ----------------
213
-
214
- 300513 0.1 Initial Release
215
-
216
- 140613 0.1a Bug fix compiling with MinGW/GCC
217
-
218
- 240613 0.2 Added: Quadtree support for partitioning of MapObjects, Getter for map size property
219
-
220
- 030713 0.3 Added: rendering of map via vertex array. Old style rendering is still available with Draw2()
221
-
222
- 190713 0.4 Updated: MapObject::SetPosition and MapObject::Move now update object's debug and AABB properties.
223
- Added: Map layers can be drawn by type (front, back, all or debug) and by index.
224
- Removed: debug param from draw function - debug info is now drawn separately
225
- Updated: minimum version of visual studio required is now VS11
226
-
227
- 060813 0.5 Updated: tileset images are cached and reused when the same tileset is used more than once
228
-
229
- 280813 0.6 Updated: optimised draw calls by checking if a tileset vertex array is actually visible before
230
- drawing
231
- Fixed: Textures not properly being flushed when loading new maps causing tile sets to be incorrectly
232
- displayed
233
- Added: MapLayer RenderStates property and setter for shader
234
-
235
- 041013 0.7 Added: Getters for object first / last points
236
- Removed: old Draw2 function
237
- Updated: constness of some function parameters
238
- Updated: inherited load class from sf::Drawable and sf::NonCopyable
239
- Updated: removed tile caching in favour of drawing objects via sprites with subrects for significantly
240
- improved loading times - particularly with large or multiple tile sets
241
-
242
- 291013 0.8 Updated: Refactored object class and cleaned up interface
243
- Added: utility functions to MapObject class for collision calculations
244
-
245
- 131113 0.9 Fixed: MapObject::Intersects failed due to const correctness
246
- Rehosted project on GitHub
247
-
206
+ Doxygen documentation can also be generated with the doxy file in the docs folder.
0 commit comments