A library for parsing Tiled maps.
0
fork

Configure Feed

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

Zig 60.4%
Nix 0.7%
Shell 0.2%
Other 38.7%
33 2 4

Clone this repository

https://tangled.org/sadbeast.com/tmz https://tangled.org/did:plc:wi5mkv7h26ruiilukqeydm4r/tmz
git@tangled.org:sadbeast.com/tmz git@tangled.org:did:plc:wi5mkv7h26ruiilukqeydm4r/tmz

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

tmz#

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

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://github.com/coat/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(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.

Building#

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.

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