A library for parsing Tiled maps.
0
fork

Configure Feed

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

Fixes tileset local ids

+14 -20
+1 -1
build.zig.zon
··· 1 1 .{ 2 2 .name = .tmz, 3 - .version = "0.2.0", 3 + .version = "0.2.1", 4 4 .fingerprint = 0xcc3a94566dc4ab5d, // Changing this has security and trust implications. 5 5 .minimum_zig_version = "0.15.1", 6 6 .dependencies = .{},
+12 -18
src/tileset.zig
··· 10 10 tiles: std.AutoHashMapUnmanaged(u32, Tile), 11 11 properties: std.StringHashMapUnmanaged(Property), 12 12 13 - pub fn initFromSlice(alloc: Allocator, json: []const u8) !Tileset { 14 - var arena = std.heap.ArenaAllocator.init(alloc); 15 - defer arena.deinit(); 16 - const arena_allocator = arena.allocator(); 13 + pub fn initFromSlice(allocator: Allocator, json: []const u8) !Tileset { 14 + var arena_state = std.heap.ArenaAllocator.init(allocator); 15 + defer arena_state.deinit(); 16 + const arena = arena_state.allocator(); 17 17 18 18 const parsed_value = try std.json.parseFromSliceLeaky( 19 19 std.json.Value, 20 - arena_allocator, 20 + arena, 21 21 json, 22 22 .{ .ignore_unknown_fields = true }, 23 23 ); 24 24 25 25 const json_tileset = try std.json.parseFromValueLeaky( 26 26 JsonTileset, 27 - arena_allocator, 27 + arena, 28 28 parsed_value, 29 29 .{ .ignore_unknown_fields = true }, 30 30 ); 31 31 32 - return try fromJson(alloc, json_tileset); 32 + return try fromJson(allocator, json_tileset); 33 33 } 34 34 35 35 pub fn fromJson(allocator: Allocator, json_tileset: JsonTileset) !Tileset { ··· 62 62 63 63 var x: u32 = 0; 64 64 var y: u32 = 0; 65 - 66 - var gid = tileset.first_gid - 1; 65 + var tile_id: u32 = 0; 67 66 68 - const last_gid: u32 = gid + tileset.tilecount -| 1; 69 - while (gid <= last_gid) : (gid += 1) { 67 + while (tile_id < tileset.tilecount) : (tile_id += 1) { 70 68 const tile_x = x * (tileset.tile_width + json_tileset.spacing) + json_tileset.margin; 71 69 const tile_y = y * (tileset.tile_height + json_tileset.spacing) + json_tileset.margin; 72 70 73 71 var tile: Tile = fetch_or_create: { 74 - if (tileset.tiles.getPtr(gid)) |t| { 72 + if (tileset.tiles.getPtr(tile_id)) |t| { 75 73 t.*.x = tile_x; 76 74 t.*.y = tile_y; 77 75 break :fetch_or_create t.*; 78 76 } else { 79 77 break :fetch_or_create .{ 80 - .id = gid, 78 + .id = tile_id, 81 79 .x = tile_x, 82 80 .y = tile_y, 83 81 ··· 87 85 }; 88 86 tile.width = tileset.tile_width; 89 87 tile.height = tileset.tile_height; 90 - try tileset.tiles.put(allocator, gid, tile); 88 + try tileset.tiles.put(allocator, tile_id, tile); 91 89 92 90 if (x >= @as(i64, @intCast(tileset.columns)) - 1) { 93 91 y += 1; ··· 203 201 duration: u32, 204 202 tileid: u32, 205 203 }; 206 - 207 - inline fn get(value: anytype) @TypeOf(value) { 208 - return if (value) |v| return v else null; 209 - } 210 204 211 205 const Property = @import("Property.zig"); 212 206
+1 -1
test/maps.zig
··· 90 90 try expectEqual(null, bad_tile); 91 91 92 92 const foo = map.getTile(2).?; 93 - try expectEqual(1, foo.id); 93 + try expectEqual(2 - tileset.first_gid, foo.id); 94 94 try expectEqual(16, foo.x); 95 95 try expectEqual(0, foo.y); 96 96