A library for parsing Tiled maps.
const std = @import("std");
const tmz = @import("tmz");
pub fn main() !void {
const allocator = std.heap.smp_allocator;
const map = try tmz.Map.initFromFile(allocator, "map.tmj");
defer map.deinit();
std.debug.info("Map size: {d} × {d}\n", .{ map.width, map.height });
}Parses Maps and Tilesets in JSON
Format -
.tmj and .tsj.
- Add
tmzas a dependency in yourbuild.zig.zon:
zig fetch --save git+https://github.com/coat/tmz.git- Add module to
build.zig:
const tmz = b.dependency("tmz", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("tmz", tmz.module("tmz"));const map = try tmz.Map.initFromFile(allocator, "map.tmj");
defer map.deinit(allocator);
std.debug.info("Map size: {d} x {d}\n", .{ map.width, map.height });
const object = map.getObject("player");
if (object) |player| {
std.debug.info("Player position: {d},{d}\n", .{ player.x, player.y });
}
const ground_layer = map.layers.get("ground");
if (ground_layer) |layer| {
for (layer.content.data.items) |gid| {
const tile = map.getTile(gid);
if (tile) |t| {
drawTile(tile.image, tile.x, tile.y, tile.orientation);
}
}
}initFromSlice and initFromFile expect a JSON Map Format
(.tmj)
document.
const tileset = try tmz.Tileset.initFromSlice(allocator, @embedFile("tileset.tsj"));
defer tileset.deinit(allocator);
if (tileset.name) |name| {
std.debug.info("Tileset name: {s}", .{ name });
}initFromSlice and initFromFile expect a JSON Map Format Tileset
(.tsj)
document.
Building the library requires Zig 0.15.1.
zig build install will build the full library and output a FHS-compatible
directory in zig-out. You can customize the output directory with the --prefix
flag.
If you have Nix installed, simply use the included flake to get an environment with Zig installed:
nix developIf you have direnv installed, run direnv allow to automatically load the
dev shell when changing into the project directory.
tmx - portable C library to load TMX maps with great documentation used as inspiration.
libtmj - Another great C library for JSON formatted Maps and Tilesets