A library for parsing Tiled maps.

Files
README.md

tmz #

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 });
}

Features #

Parses Maps and Tilesets in JSON Format - .tmj and .tsj.

Installation #

Zig #

  1. Add tmz as a dependency in your build.zig.zon:
zig fetch --save git+https://codeberg.org/sadbeast/tmz.git
  1. Add module to build.zig:
const tmz = b.dependency("tmz", .{ .target = target, .optimize = optimize });

exe.root_module.addImport("tmz", tmz.module("tmz"));

Usage #

Maps #

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.

Tilesets #

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 #

Building the library requires Zig 0.16.0.

Development Environment #

Nix #

If you have Nix installed, simply use the included flake to get an environment with Zig installed:

nix develop

If you have direnv installed, run direnv allow to automatically load the dev shell when changing into the project directory.

Prior Art #

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