Rotation and mirroring (flipping) with Tiled Map Editor and XNA

Update: the library is now hosted on GitHub: https://github.com/zachmu/tiled-xna

I’ve had very good luck with Tiled Map Editor and the very simple XNA library I found to work with it, written by Kevin Gadd and Stephen Belanger. I’ve since tweaked the hell out of it to work with my own game engine, but it never needed any major overhauls until I started using a tile set creation tool called Pyxel Edit. Pyxel Edit is great for making quick mockups of a tileset (although it desperately needs a selection tool), and it has a great feature where you can rotate a tile into whatever orientation you want. The only issue is that the tile sheets it exports don’t contain duplicate rotated tiles for each orientation you need, so I thought that I couldn’t easily use it with Tiled, because you can’t rotate tiles in Tiled.

Except you can rotate tiles in the latest version of Tiled, even if there’s no UI for it. But it works as advertised. Pushing Z will rotate a tile, while X and Y mirror the tile horizontally or vertically.

Once I figured this out, I just needed to add support for it to the above XNA library. I found the TMX Map Format documentation to be very useful in this process, especially since it contained pseudo code examples. And I got to write some bit-masking code, which I always enjoy.

// Bits on the far end of the 32-bit global tile ID are used for tile flags
const unsigned FLIPPED_HORIZONTALLY_FLAG = 0x80000000;
const unsigned FLIPPED_VERTICALLY_FLAG   = 0x40000000;
const unsigned FLIPPED_DIAGONALLY_FLAG   = 0x20000000;

Since this was a bit tricky to get right, especially the drawing, I figured I should share it back. So here it is: a simple XNA Tiled library with support for flipping and rotating tiles. Enjoy!

13 thoughts on “Rotation and mirroring (flipping) with Tiled Map Editor and XNA

  1. Awesome! Open source rocks. It’s nice to see someone pick up the project. I’ve been off in mobile development land for awhile, so I hadn’t touched the code in over a year! You should put your changes up on Github.

  2. Pingback: Tiled Maps for XNA – Full support for the Tiled Map XML specification! | Nerd Culture

  3. Good work, very interesting!!!
    Can you comment about your experience with this Tiled Map Editor and XNA?
    I’m thinking about use it but I’m very worried about performance, stability and etc.
    Best regards.

    • The performance seems adequate for everything I’ve tried it on so far, including very large maps. The tiles are stored in a dense array, so memory is a bit wasteful for maps with a lot of mostly-empty layers, but it’s very fast. It wouldn’t be too hard to optimize it to be more conservative in its memory usage, but I haven’t had any need to do so yet.

      As for stability, I’m the only person to touch it in three years, but the tiled map format doesn’t seem to change. One issue I had is that newer versions of Tiled default to using zlib compression, and the library only supports gzip.

  4. I am using VS 2010.
    I can run the x86 version fine.
    However if I target the XBox 360, I get “Error 3 The type or namespace name ‘Compression’ does not exist in the namespace ‘System.IO’ (are you missing an assembly reference?)”
    I have .NET 4.5 installed.
    I have a reference to System.dll for the 360 project, however its version 2.0.5, not 4.0 which is what the x86 references. I suspect this is the problem.

    Any ideas?
    Has anyone gotten this example working for the 360?

    • Hi rich,

      I got this working on the 360, but you either need to turn off compression in your Tiled map storage (totally doable unless your map is truly gigantic) or else include a 360-compatible version of System.IO.Compression (which Microsoft doesn’t provide). I’ve generated a release of the project which includes the necessary dll:

      https://github.com/zachmu/tiled-xna/releases/v0.9

      You just need to add a reference to that dll in your 360 project. Let me know if that does the trick for you. Works for me.

      • Thanks for the quick response Zach

        Yes, I saw the link to the Compression site, but it wasn’t valid anymore. Thanks for adding that. That does indeed fix the problem.

        However it still complains about SortedList (sorry I forgot to mention this in the previous post)

        “Error 3 The type or namespace name ‘SortedList’ could not be found (are you missing a using directive or an assembly reference?)”

        I thought SortedList was available in 2.0.5 System.dll, but maybe its not in the XNA implementation.

        Regardless, I could easily switch the code to use a different container, but I found a different TMX lib last night that I got running on the 360 anyway, so no big deal about this!

        • I customized the tiled library quite heavily for my own game, and I think that work included using another container for that same reason. My copy is too far diverged to easily submit a patch, but if you wanted to submit this change as a pull request on github I would gladly take it.

Leave a Reply