TheBoneJarmer / TiledCS

TiledCS is a dotnet library for loading Tiled tilesets and maps
MIT License
139 stars 51 forks source link

Add support for TileSet.Spacing and TileSet.Margin #95

Closed robsoft closed 1 year ago

robsoft commented 1 year ago

I appreciate this might be niche, but I came to explore TiledCS using the 'Tiled' example tileset texture tmw_desert_spacing.png and I was confused for a bit when I got odd results!

This particular example tileset has a Spacing property of 1 and a Margin property of 1. I believe these are pixel values, measuring (in Spacing) the number of pixels 'between' successive tiles in the texture, and (in Margin) the number of pixels 'in' before the first tile starts. There is only 1 integer value for each property so I assume it applies to both horizontal and vertical measurements.

Anyway, when it's used with TiledCS, the TiledMap.GetSourceRect seems to ignore these properties and return the rectangle from the texture as if there was no Margin or Spacing.

I have a 5 minute play and realised that TileMap.GetSourceRect needs to do something like this;

(line 710 ish, TiledMap.cs)

                        x = tileset.Margin + num * (tileset.TileWidth + tileset.Spacing),
                        y = tileset.Margin + num2 * (tileset.TileHeight + tileset.Spacing),

The source texture; tmw_desert_spacing

Without Spacing & Margin observed Screenshot 2022-10-11 123940

With Spacing and Margin observed Screenshot 2022-10-11 124017

Hope this makes sense! There might well be other places in the code where this would have to be adapted of course, and I guess this isn't much of a priority to anyone else, but the initial result puzzled me for a while at least.

robsoft commented 1 year ago

Sorry - just looked at the outstanding pulls and saw this has already been covered. Sorry to waste your time!