#map #gamedev #tmx

tiled

A rust crate for loading maps created by the Tiled editor

30 releases

0.11.2 Oct 4, 2023
0.11.1 May 19, 2023
0.11.0 Mar 16, 2023
0.10.2 Apr 26, 2022
0.1.1 Nov 21, 2014

#19 in Game dev

Download history 195/week @ 2023-12-12 136/week @ 2023-12-19 149/week @ 2023-12-26 67/week @ 2024-01-02 154/week @ 2024-01-09 100/week @ 2024-01-16 51/week @ 2024-01-23 70/week @ 2024-01-30 209/week @ 2024-02-06 215/week @ 2024-02-13 313/week @ 2024-02-20 431/week @ 2024-02-27 265/week @ 2024-03-05 300/week @ 2024-03-12 341/week @ 2024-03-19 232/week @ 2024-03-26

1,187 downloads per month
Used in 7 crates

MIT license

145KB
3K SLoC

rs-tiled

tiled = "0.11.2"

Rust Crates.io Docs Status dependency status

A crate for reading TMX (map) and TSX (tileset) files from the Tiled Map Editor into Rust. It provides a huge set of features as well as a strong wrapper over internal features such as GIDs.

Documentation is available on docs.rs.

Code contributions are welcome as are bug reports, documentation, suggestions and criticism.

The minimum supported TMX version is 0.13.

Example

use tiled::Loader;

fn main() {
    let mut loader = Loader::new();
    let map = loader.load_tmx_map("assets/tiled_base64_zlib.tmx").unwrap();
    println!("{:?}", map);
    println!("{:?}", map.tilesets()[0].get_tile(0).unwrap().probability);
    
    let tileset = loader.load_tsx_tileset("assets/tilesheet.tsx").unwrap();
    assert_eq!(*map.tilesets()[0], tileset);
}

WASM

The crate supports WASM, but since it does not currently support asynchronous loading, there are some gotchas.

  • First, to make it work on any WASM target, enable the wasm feature, like so:
[dependencies]
# ...
tiled = { version = ".....", features = ["wasm"] }
  • Second, since you cannot use the filesystem as normally on the web, you cannot use FilesystemResourceReader. As such, you'll need to implement your own ResourceReader. This is a pretty simple task, as you just need to return anything that is Readable when given a path, e.g.:
use std::io::Cursor;

struct MyReader;

impl tiled::ResourceReader for MyReader {
    type Resource = Cursor<&'static [u8]>;
    type Error = std::io::Error;

    // really dumb example implementation that just keeps resources in memory
    fn read_from(&mut self, path: &std::path::Path) -> std::result::Result<Self::Resource, Self::Error> {
        if path == std::path::Path::new("my_map.tmx") {
            Ok(Cursor::new(include_bytes!("../assets/tiled_xml.tmx")))
        } else {
            Err(std::io::Error::new(std::io::ErrorKind::NotFound, "file not found"))
        }
    }
}

Check the ResourceReader docs for more information.

Licences

assets/tilesheet.png by Buch

Licenced under MIT

Dependencies

~6MB
~97K SLoC