TheBoneJarmer / TiledCS

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

Fixed TiledMap.GetSourceRect #86

Open Frankfires opened 2 years ago

Frankfires commented 2 years ago

TiledMap.GetSourceRect wasn't returning the correct frame with tile spacing in mind. TODO: figure out what margins do and update this method again

TheBoneJarmer commented 2 years ago

Hey

Thanks for the fix! I would like to test it out myself as well and see what I can do with the margin after the merge. Can you share your tileset with me? I never had a tileset with margins or spacing before. And to be honest, I still do not understand why some artists do that. lol

With kind regards, TheBoneJarmer

Frankfires commented 2 years ago

Giving spacing ( and I think margins too ) to tilesets is kinda helps you differ between sort of similar tiles by adding a gaps of pixels that are skipped over and not rendered in between each tile:

image

The Tiled tileset editor when being prompted for an image also asks you if you margined or spaced out your tiles, this way it can render your tiles correctly by skipping over 1 pixel every frame:

image image

If I were to remove the spacing from the tileset properties but keep it in the sprite my tiles when rendered would look something like this:

image image

This is also what a rendered map that uses TiledMap.GetSourceRect to get each tile's frame would look like if I tried to render tiles with spacing. ( not good )

By adding the tileset's spacing multiplied by its frame onto the frame's x and y values you'd be skipping over the spacing that was inputted allowing the tiles to be rendered properly.

TheBoneJarmer commented 2 years ago

Hey

Sorry for the late reply. Thanks a lot for the explanation! That does make sense indeed. I do have one question though. Do you think you can still share one of your test maps with me, please? That way I can test it properly and round things up.

With kind regards, TheBoneJarmer

TheBoneJarmer commented 2 years ago

Hey @Frankfires

I still have not heard back from you. Do you mind to share your tileset with me so I can properly test things? Thanks!

With kind regards, TheBoneJarmer

robsoft commented 2 years ago

If it helps at all, here is another texture which requires Spacing=1 and Margin=1... tmw_desert_spacing

kaltinril commented 1 year ago

Did a quick review, Not knowing much about the properties available on the class, assuming that the Spacing is a valid property on tileSet, then I think the code could be merged. However, may want to adjust the math to do the addition first, and the multiply second, so only 1 multiply is performed vs 2 per line.

kaltinril commented 1 year ago

It appears #94 has an added fix that also includes margins in the position calculation. May want to use that one instead of the PR from this one.