A library for parsing Tiled maps.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add README

Used https://github.com/mitchellh/libxev/blob/main/README.md as
reference.

+121
+121
README.md
··· 1 + # tmz 2 + 3 + A library for parsing [Tiled](https://www.mapeditor.org/) maps. 4 + 5 + ```zig 6 + const std = @import("std"); 7 + const tmz = @import("tmz"); 8 + 9 + pub fn main() !void { 10 + const allocator = std.heap.smp_allocator; 11 + 12 + const map = try tmz.Map.initFromFile(allocator, "map.tmj"); 13 + defer map.deinit(); 14 + 15 + std.debug.info("Map size: {d} × {d}\n", .{ map.width, map.height }); 16 + } 17 + ``` 18 + 19 + ## Features 20 + 21 + Parses Maps and Tilesets in [JSON 22 + Format](https://doc.mapeditor.org/en/stable/reference/json-map-format/) - 23 + `.tmj` and `.tsj`. 24 + 25 + ## Installation 26 + 27 + ### Zig 28 + 29 + 1. Add `tmz` as a dependency in your `build.zig.zon`: 30 + 31 + ```bash 32 + zig fetch --save git+https://github.com/coat/tmz.zig#main 33 + ``` 34 + 35 + 2. Add module to `build.zig`: 36 + 37 + ```zig 38 + const tmz = b.dependency("tmz", .{ .target = target, .optimize = optimize }); 39 + 40 + exe.root_module.addImport("tmz", tmz.module("tmz")); 41 + ``` 42 + 43 + ## Usage 44 + 45 + ### Maps (TODO) 46 + 47 + ```zig 48 + const map = try tmz.Map.initFromFile(allocator, "map.tmj"); 49 + defer map.deinit(allocator); 50 + 51 + std.debug.info("Map size: {d} x {d}\n", .{ map.width, map.height }); 52 + 53 + const object = map.getObject("player"); 54 + if (object) |player| { 55 + std.debug.info("Player position: {d},{d}\n", .{ player.x, player.y }); 56 + } 57 + 58 + const ground_layer = map.getLayer("ground"); 59 + if (ground_layer) |layer| { 60 + for (layer.content.data.items) |gid| { 61 + if (gid == 0) continue; 62 + const tile = map.getTile(gid); 63 + if (tile) |t| { 64 + drawTile(tile.image, tile.x, tile.y); 65 + } 66 + } 67 + } 68 + ``` 69 + 70 + `initFromSlice` and `initFromFile` expect a [JSON Map Format 71 + (.tmj)](https://doc.mapeditor.org/en/stable/reference/json-map-format/#map) 72 + document. 73 + 74 + ### Tilesets 75 + 76 + ```zig 77 + const tileset = try tmz.Tileset.initFromSlice(allocator, @embedFile("tileset.tsj")); 78 + defer tileset.deinit(allocator); 79 + 80 + if (tileset.name) |name| { 81 + std.debug.info("Tileset name: {s}", .{ name }); 82 + } 83 + ``` 84 + 85 + `initFromSlice` and `initFromFile` expect a [JSON Map Format Tileset 86 + (.tsj)](https://doc.mapeditor.org/en/stable/reference/json-map-format/#tileset) 87 + document. 88 + 89 + ## Building 90 + 91 + Building the library requires [Zig 92 + 0.14.0](https://ziglang.org/download/#release-0.14.0). To build and run the 93 + examples (TODO), [SDL 3.2](https://github.com/libsdl-org/SDL/releases/latest) 94 + is also required. 95 + 96 + `zig build install` will build the full library and output a FHS-compatible 97 + directory in zig-out. You can customize the output directory with the --prefix 98 + flag. 99 + 100 + ### Development Environment 101 + 102 + #### Nix 103 + 104 + If you have Nix installed, simply use the included flake to get an environment 105 + with Zig installed: 106 + 107 + ```sh 108 + nix develop 109 + ``` 110 + 111 + If you have `direnv` installed, run `direnv allow` to automatically load the 112 + dev shell when changing into the project directory. 113 + 114 + ## Prior Art 115 + 116 + [tmx](https://github.com/baylej/tmx) - portable C library to load TMX maps with 117 + [great documentation](https://libtmx.readthedocs.io/en/latest/) used as 118 + inspiration. 119 + 120 + [libtmj](https://github.com/Zer0-One/libtmj) - Another great C library for JSON 121 + formatted Maps and Tilesets