marshallward / TiledSharp

C# library for parsing and importing TMX and TSX files generated by Tiled, a tile map generation tool
http://marshallward.github.io/TiledSharp
Apache License 2.0
328 stars 87 forks source link

Exception thrown when loading maps containing objects with decimal values for x,y coordinates #12

Closed Josh1billion closed 9 years ago

Josh1billion commented 9 years ago

Found a new case where a bug can arise.

In ObjectGroup.cs, on lines 68-69, these lines can sometimes throw unhandled exceptions, crashing the program:

            X = (int)xObject.Attribute("x");
            Y = (int)xObject.Attribute("y");

Specifically, this happens if the x and/or y values for an object are decimal values.

Steps to reproduce:

  1. Create a new map in Tiled.
  2. Create an object layer.
  3. Create an object, and set its X or Y value (under Position) to a decimal value, such as 100.5
  4. Try loading the map in TiledSharp.

This might seem like a rare case ("who would enter a decimal value for an x or y coordinate, anyway?"), but in my case, Tiled actually set it to a decimal value automatically as I was dragging-and-dropping an object around in the editor, so it's entirely possible that users will run into this at some point after messing around in Tiled long enough, like I did. :)

Josh1billion commented 9 years ago

The same also happens with Width and Height on lines 71-72, by the way.

marshallward commented 9 years ago

It looks like libtiled converts these to integers (as in map reader.cpp:readObjectGroup), so it's definitely a good idea to do the same here. Thanks for the report!

marshallward commented 9 years ago

Oops, my mistake. I was reading the (deprecated) object group attributes. The individual object attributes are indeed floats, so this is an error in TiledSharp.

Replacing (int) with (float) should be all that's needed.

marshallward commented 9 years ago

I've updated the master branch to handle object float attributes. Could you give it a try?

Josh1billion commented 9 years ago

Works great! Thanks for fixing it so quickly.