-
Notifications
You must be signed in to change notification settings - Fork 19
Tiled tmx map parser Legacy
Current Wiki index can be found here
The xy::tmx::Map
class is provided to parse the xml format map files created with the Tiled 2D tile map editor. The map class can be used to completely parse an xml document, including zlib compressed files[1], and provide read-only access to the data stored within via a simple API.
xy::tmx::Map map;
if(map.load("myMapFile.tmx"))
{
const auto& layers = map.getLayers();
}
The map parsing / loading of data is deliberately separated out from rendering to provide more flexibility. Once the map data is loaded the user is then free to create any kind of renderer he or she wishes with the data. xygine does, however, include some basic rendering functionality for Orthogonal type maps, and also provides an interface for creating Physics components from tmx map objects. To render an orthogonal map, first load the map file with the Map
class, then use the getDrawable()
function:
auto drawable = map.getDrawable(messagebus, *map.getLayers()[0], textureResource, shaderResource);
auto entity = xy::Entity::Create(messagebus);
entity->addComponent(drawable);
scene.addEntity(entity, xy::Scene::layer::BackRear);
The getDrawable()
function converts a given tile or image layer into a drawable component which can be added to a scene as any other drawable. This allows each layer of a Tiled map to be separated out and rendered as part of the scene with all the flexibility of any other xygine component. Once all the required layers of a map are converted to drawable components this way the Map
object itself can be freely discarded.
Similarly the Map
class offers conversion functions for ObjectGroup
layers, which will create RigidBody
components that are part of xygine's physics system:
auto rigidBody = map.getRigidBody(messagebus, *map.getLayers()[2], xy::Physics::BodyType::Static);
auto entity = xy::Entity::Create(messagebus);
entity->addComponent(rigidBody);
scene.addEntity(entity, xy::Scene::Layer::FrontMiddle);
Parsing an entire layer like this is useful for creating static geometry for maps such as those in platform games or top-down shooters. If a layer contains multiple dynamic objects each object can also be converted to a RigidBody
individually.
for(const auto& object : layer->getObjects())
{
auto rigidBody = map.getRigidBody(messagebus, object, xy::Physics::BodyType::Dynamic);
auto drawable = xy::Component::create<MyCrateDrawable>(messagebus, textureResource);
auto entity = xy::Entity::Create(messagebus);
entity->setPosition(object.getPosition());
entity->addComponent(rigidBody);
entity->addComponent(drawable);
scene.addEntity(entity, xy::Scene::Layer::FrontMiddle);
}
Map types such as staggered, isometric or hexagonal are all fully parsed by the map class, but xygine currently does not provide renderers for those types. If a user wishes to implement a renderer themselves, however, using the Map
API should make things relatively simple.
(1) gzip format currently not supported