1
SFML projects / Re: 'Tiled' Tile-map Loader
« on: March 21, 2015, 04:28:48 pm »
Hello. I have encountered another problem while using the map loader and solved it, so I am posting this here in case anyone else gets the same problem or fallahn decides to update it.
The loader works with no problem when the tilesets have 1 px spacing and 1 px margin. But I had a map made with a tileset that had no margins and despite Tiled showing it without a problem, in the game, the tiles were all messed up. I looked through the code and noticed these lines in MapLoaderPrivate.cpp:
In my case, this resulted in 56 instead of 57 tiles in a row: (968 - 0)/(16 + 1) = 56.94 which rounds down to 56.
I think the correct calculation should be like this:
The loader works with no problem when the tilesets have 1 px spacing and 1 px margin. But I had a map made with a tileset that had no margins and despite Tiled showing it without a problem, in the game, the tiles were all messed up. I looked through the code and noticed these lines in MapLoaderPrivate.cpp:
//slice into tiles
int columns = (sourceImage.getSize().x - margin) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - margin) / (tileHeight + spacing);
Here, sourceImage.getSize().x and .y are the dimensions of the tileset image. The problem with this is, there are 2 margins in both dimensions (one left + one right and one up + one down) and "tiles in a row/column" - 1 spacings (since there is no spacing before the first and after the last tile). int columns = (sourceImage.getSize().x - margin) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - margin) / (tileHeight + spacing);
In my case, this resulted in 56 instead of 57 tiles in a row: (968 - 0)/(16 + 1) = 56.94 which rounds down to 56.
I think the correct calculation should be like this:
//slice into tiles
int columns = (sourceImage.getSize().x - 2*margin + spacing) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - 2*margin + spacing) / (tileHeight + spacing);
So I changed it and now it works.
int columns = (sourceImage.getSize().x - 2*margin + spacing) / (tileWidth + spacing);
int rows = (sourceImage.getSize().y - 2*margin + spacing) / (tileHeight + spacing);