Hi @bjorn . I am indeed thankful to @eishiya and all others who are kind enough to help out.
I am using Tmxlite (although I also tried Tileson) . Somehow I have managed to fudge through it and I now have a rudimentary platforrmer with box2d and Tiled map support. There was one bit of my code that I don’t fully understand, at first when i got it to work, all the ‘empty’ tiles where rendering some other tile everywhere (so not empty in other words). I had to get the sourceTiles_index minus 1 when i accessed my ‘sourceTiles’ array which contains the member ‘ImagePosition’.
I might be doing it wrong, as it seemed I had to make to iterating loops, one to get the ‘Tile’ which only really contained the ‘ImagePosition’ vector, then i had to iterate through the layers to find each ‘object layer’ and ‘tile layer’.
Maybe i have done it wrong but i will post it here (whilst editing out the non-Tiled related code where possible) in case it helps someone, or if someone can see any obvious mistakes:
#include <tmxlite/Map.hpp>
#include <tmxlite/Layer.hpp>
#include <tmxlite/TileLayer.hpp>
#include <tmxlite/ObjectGroup.hpp>
void LevelManager::start(b2World* world)
{
worldPtr = world;
tmx::Map map;
unsigned int level_width = 0;
unsigned int level_height = 0;
unsigned int tile_width = 0;
unsigned int tile_height = 0;
if (map.load("LevelTmx/1-1.tmx"))
{
level_width = map.getTileCount().x;
level_height = map.getTileCount().y;
tile_width = map.getTileSize().x;
tile_height = map.getTileSize().y;
std::vector<tmx::Tileset::Tile> sourceTiles;
const auto& tilesets = map.getTilesets();
for (const auto& tileset : tilesets) // as above
{
//read out tile set properties, load textures etc...
sourceTiles = tileset.getTiles();
for (int i = 0; i < sourceTiles.size(); i++)
{
//std::cout << "TILESET: i:" << i << " || sourceTile ID:" << sourceTiles[i].ID << std::endl;
//std::cout << "TILESET: Image position: " << sourceTiles[i].imagePosition.x << " , " << sourceTiles[i].imagePosition.y << std::endl;
}
if (level_spriteSheet.loadFromFile(tileset.getImagePath()))
{
std::cout << "Level sprite sheet path: " << tileset.getImagePath() << std::endl;
}
else
{
std::cout << "Texture Not Loaded!" << std::endl;
}
}
const auto& layers = map.getLayers();
for (const auto& layer : layers)
{
if (layer->getType() == tmx::Layer::Type::Object)
{
const auto& objectLayer = layer->getLayerAs<tmx::ObjectGroup>();
if (objectLayer.getName() == "solid")
{
const auto& objects = objectLayer.getObjects();
for (const auto object : objects)
{
Collider collider;
collider.start(object.getAABB(), worldPtr);
colliders_ground.push_back(collider);
}
}
if (objectLayer.getName() == "blocks_?")
{
const auto& objects = objectLayer.getObjects();
for (const auto object : objects)
{
Block block;
tmx::FloatRect aabb = object.getAABB();
block.start(Block::BlockType::QUESTION_MARK, aabb.left, aabb.top, tile_width, tile_height, level_spriteSheet, sourceTiles[Constants::BLOCK_Q_ID].imagePosition.x, sourceTiles[Constants::BLOCK_Q_ID].imagePosition.y, worldPtr);
all_blocks.push_back(block);
}
}
if (objectLayer.getName() == "blocks_brick")
{
const auto& objects = objectLayer.getObjects();
for (const auto object : objects)
{
Block block;
tmx::FloatRect aabb = object.getAABB();
block.start(Block::BlockType::BRICK, aabb.left, aabb.top, tile_width, tile_height, level_spriteSheet, sourceTiles[Constants::BLOCK_BRICK_ID].imagePosition.x, sourceTiles[Constants::BLOCK_BRICK_ID].imagePosition.y, worldPtr);
all_blocks.push_back(block);
}
}
}
else if (layer->getType() == tmx::Layer::Type::Tile)
{
const auto& tileLayer = layer->getLayerAs<tmx::TileLayer>();
// read out the layer properties etc...
auto& tiles = tileLayer.getTiles();
for (int i = 0; i < tiles.size(); i++)
{
uint32_t tileID = tiles[i].ID;
uint8_t flipFlag = tiles[i].flipFlags;
unsigned long long tile_x = i % level_width;
unsigned long long tile_y = i / level_width;
if (tileID <= 0)
continue;
Tile tile;
// NOTE: Here is where i added minus 1, but couldnt quite understand why i needed it (it was from trial and error)
tile.start(tile_x, tile_y, tile_width, tile_height, level_spriteSheet, sourceTiles[tileID - 1].imagePosition.x, sourceTiles[tileID - 1].imagePosition.y);
levelTiles.push_back(tile);
}
}
}
}
else
{
std::cout << "Failed to load map" << std::endl;
}
camera.initView(level_width * tile_width, level_height * tile_height);
guy.start(worldPtr);
}