A library for parsing Tiled maps.
Zig | C |
const std = @import("std");
const tmz = @import("tmz");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}).init;
defer gpa.deinit();
const allocator = gpa.allocator();
const map = try tmz.loadMapFromFile(gpa.allocator(), "map.tmj");
defer map.deinit();
std.debug.info("Map size: {} × {}", .{ map.width, map.height });
} |
#include "tmz.h"
#include <stdio.h>
int main(void) {
tmz_map map;
tmz_load_map_from_file(&map, "map.tmj")
printf("Map size: %i x %i\n", map->width, map->height);
tmz_map_deinit(&map);
return 0;
} |
Parses Maps and Tilesets in JSON
Format -
.tmj
, .tsj
).
1 Add tmz
as a dependency in your build.zig.zon
:
zig fetch --save git+https://github.com/coat/tmz.zig#main
2 Then add module to build.zig
:
const tmz = b.dependency("tmz", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("tmz", tmz.module("tmz"));
If you are using the C API to tmz, see the Build section below.
const map = try tmz.loadMap(allocator, @embedFile("map.tmj"));
defer map.deinit(allocator);
std.debug.info("Map size: {} x {}", .{ map.width, map.height });
loadMap
and loadMapFromFile
expect a JSON Map Format (.tmj).
const tileset = try tmz.loadTilesetFromFile(allocator, "tileset.tsj");
defer tileset.deinit(allocator);
if (tileset.name) |name| {
std.debug.info("Tileset name: {s}", .{ name });
}
loadTileset
and loadTilesetFromFile
expect a JSON Map Format Tileset (.tsj).
Building the library requires Zig 0.14.0. To build and run the examples (TODO), SDL 3.2 is also required.
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 develop
If you have direnv
installed, run direnv allow
to automatically load the
dev shell when changing into the project directory.