A library for parsing Tiled maps.
const std = @import("std");
const tmz = @import("tmz");
pub fn main(init: std.process.Init) !void {
const map = try tmz.Map.initFromFile(init.io, init.gpa, "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://codeberg.org/sadbeast/tmz.git- Add module to
build.zig:
const tmz = b.dependency("tmz", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("tmz", tmz.module("tmz"));var map = try tmz.Map.initFromFile(io, allocator, "map.tmj");
defer map.deinit(allocator);
std.debug.info("Map size: {d} x {d}\n", .{ map.width, map.height });
const object = map.findObject("player");
if (object) |player| {
std.debug.info("Player position: {d},{d}\n", .{ player.x, player.y });
}
const ground_layer = map.findLayer("ground");
if (ground_layer) |layer| {
for (layer.content.data.items) |gid| {
const tile = map.getTile(gid);
if (tile) |t| {
drawTile(t.image, t.x, t.y, t.orientation);
}
}
}initFromSlice and initFromFile expect a JSON Map Format
(.tmj)
document.
var 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.
An Io instance is used for loading files (tilesets, templates, etc.).
Building the library requires Zig 0.16.0.
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