0
\$\begingroup\$

I am trying to make use of the Tiled Map Editor for my SDL2 projects. (By using the TMXLITE parser from Fallahn on Github) https://github.com/fallahn/tmxlite

#include "PlayingField.h"

#include <tmxlite/Map.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/TileLayer.hpp>
#include <tmxlite/ObjectGroup.hpp>


void PlayingField::InitPlayingField()
{
    tmx::Map map;
    map.load("Assets/pitch.tmx");
    auto layers=map.getLayers();
}

I have used this editor in the past with SFML, Unity and other C# frameworks but this was some years ago.

As you can see from my code, I believe I have correctly linked and have indeed loaded the map into memory. I also have the array of layers now.

But I am reading through all the docs and really struggling where to turn next.

In this example, I have just 1 layer. I am not even yet concerned with drawing the tiles to screen. But what I wanted from the TMX export really is just the map of the tiles. In other words, I want to create an array (or std::vector) or all the tiles.

My Tile would be simply X,Y location floats and the BMP image for that tile. I am looking here: https://doc.mapeditor.org/en/stable/reference/tmx-map-format/ and I see the properties tileoffset and image but I do not see how to access those from the TMX file using TmxLite. (I did read somewhere TmxLite can't use Embedded assets. Well even that would be ok I can load the tile textures myself if necessary but still I do not see how the TMX file is used to layout and position these tiles.)

(Note: For this small example I could pretty easily wrote my own file format for the maps and using std::getline I could make some simple/crappy way of reading the file. The whole point to my exercise today is trying to learn Tiled specifically so that I can begin to make use of Tiled Editors full features (and also for making very complex maps).

Sorry for the broad question but I am just hoping to receive correct help within a fast time so that I can get on with more game dev :D (I did consider writing on TmxLite github support pages but I felt it might be better asking here first).

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

Well I doubt this is the proper way to use this. But I was able to get the array of Tiles (which was essentially just an array of int 'ID') as follows:

void PlayingField::InitPlayingField()
{
    tmx::Map map;
    map.load("Assets/pitch.tmx");
    const auto& layers=map.getLayers();
    const auto& tiles=layers[0]->getLayerAs<tmx::TileLayer>().getTiles();

    printf("Chunks Count=%d\n", (int)tiles.size());
    for(int i=0; i<tiles.size(); i++)
    {
        printf("tile id=%d\n", (int)tiles[i].ID);
    }

    
}

With this (and as I know how wide the map is). I can use that to draw my tiled background with a little extra code.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .