Skip to content

TiledMaps

Madour edited this page Jul 8, 2021 · 11 revisions

TiledMap is part of the tilemapping module

Tile mapping one of the most common technique we can find in games.

NasNas supports TiledMap rendering from Tiled tmx files exported in csv format.

All Tiled features are implemented, except :

  • Isometric and hexagonal maps
  • Image layers
  • Tile collisions data

Other than those 5 items listed above, you can freely create your map level in Tiled, using tile flips and rotations, animated tiles, multiple tilesets, extern and embedded tilesets etc and expect it to render correctly in NasNas.

Loading and drawing a tiled map is effortless in NasNas, let's see an example :

First of all, add a TiledMap member variable to the Game class header.

#include <NasNas/Tilemapping.hpp>
Game::Game(){
    auto& scene = createScene("main");

    // let's suppose we want to load the level.tmx tiled map.
    // it has 2 tile layers named bg and front, and an object layer named coins
    this->tiled_map.loadFromFile("assets/level.tmx");
    
    // iterate over point objects in the coin object layer
    for (auto& object : this->tiled_map.getObjectLayer("coins").allPoints()) {
        // do something with the objects
    }
    
    // adding tile layers to the scene
    scene.getDefaultLayer().add(this->tiled_map.getTileLayer("bg"));
    scene.getDefaultLayer().add(this->tiled_map.getTileLayer("front"));
    // we can also add an object layer to the Scene
    scene.getDefaultLayer().add(this->tiled_map.getObjectLayer("coins"));

    auto& camera = createCamera("main", 0);
    camera.lookAt(scene);
    // we can set camera limits to be the tiled map bounds
    camera.setLimitsRect({{0, 0}, sf::Vector2i(tiled_map.getSize())});

}

Game::update() {
    this->tiled_map.update();
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        getCamera("main").move(-1, 0);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        getCamera("main").move(1, 0);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        getCamera("main").move(0, -1);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        getCamera("main").move(0, 1);
}

This example will load a Tiled map, draw it on the screen and update its animated tiles. Press the arrow keys to move the Camera.

Other things you can do with the loaded tiled map :

  • Get specific tile data
tiled_map.getTileLayer("layer_name").getTile(10, 5)->gid;
tiled_map.getTileLayer("layer_name").getTile(10, 5)->flip;
// etc
  • Get tilesets data
tiled_map.allTilesets()[0].firstgid;
tiled_map.allTilesets()[0].getTexture();
auto tile_gid = tiled_map.getTileLayer("layer_name").getTile(5, 10).gid;
tiled_map.getTileTileset(tile_gid)->name;
// etc
  • Get properties (from map, layer, object, tile or tileset)
tiled_map.getProperty<std::string>("prop_name");
tiled_map.getTileLayer("layer_name").getProperty<bool>("prop_name");
tiled_map.getObjectLayer("layer_name").getEllipse(5).printProperties();
tiled_map.getTileLayer("layer_name").getTile(10, 5).data.addProperty("prop_name", sf::Color(0, 0, 0));
tiled_map.allTilesets()[0].getProperty<float>("prop_name");
// etc
  • Get objects data
// works also for any type of shape
tiled_map.getObjectLayer("layer_name").getPolyline(6).points;
tiled_map.getObjectLayer("layer_name").getEllipse(6).type;
//etc
  • Get objects by name or by type
auto& objects_layer = tiled_map.getObjectLayer("layer_name");

// get all objects with type "some_type"
for (ns::tm::Object& object : objects_layer.getObjectsWithType("some_type")) {
    if (object.shapetype == ns::tm::Object::Shape::Ellipse) {
        // do something with the ellipse
        object.asEllipse()
    }
}

// get all objects named "some_name"
for (ns::tm::Object& object : objects_layer.getObjectsWithName("some_name")) {
    // ...
}

The tilemapping module is quite big, please check its documentation for more details