A library for parsing Tiled maps.
0
fork

Configure Feed

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

add image and group layer types

+73 -19
+1 -1
src/Map.zig
··· 7 7 8 8 layers: std.StringHashMapUnmanaged(Layer), 9 9 properties: std.StringHashMapUnmanaged(Property), 10 - tilesets: std.ArrayList(Tileset), 10 + tilesets: std.ArrayListUnmanaged(Tileset), 11 11 12 12 background_color: ?Color = null, 13 13 class: ?[]const u8,
+53 -18
src/layer.zig
··· 174 174 }; 175 175 176 176 pub const TileLayer = struct { 177 - data: std.ArrayList(u32), 177 + data: std.ArrayListUnmanaged(u32), 178 178 chunks: ?[]Chunk = null, 179 179 180 180 pub fn fromJson(allocator: Allocator, json_layer: Layer.JsonLayer) !TileLayer { ··· 248 248 return null; 249 249 } 250 250 }; 251 - pub const ImageLayer = struct {}; 252 - pub const Group = struct {}; 251 + pub const ImageLayer = struct { 252 + image: []const u8, 253 + repeat_x: bool, 254 + repeat_y: bool, 255 + transparent_color: ?Color = null, 256 + 257 + pub fn fromJson(allocator: Allocator, json_layer: Layer.JsonLayer) !ImageLayer { 258 + return .{ 259 + .image = try allocator.dupe(u8, json_layer.image orelse return error.MissingField), 260 + .repeat_x = json_layer.repeatx orelse false, 261 + .repeat_y = json_layer.repeaty orelse false, 262 + .transparent_color = json_layer.transparentcolor, 263 + }; 264 + } 265 + 266 + pub fn deinit(self: *ImageLayer, allocator: Allocator) void { 267 + allocator.free(self.image); 268 + } 269 + }; 270 + 271 + pub const Group = struct { 272 + layers: std.StringHashMapUnmanaged(Layer), 273 + 274 + pub fn fromJson(allocator: Allocator, json_layer: Layer.JsonLayer) anyerror!Group { 275 + var layers: std.StringHashMapUnmanaged(Layer) = .empty; 276 + if (json_layer.layers) |json_layers| { 277 + for (json_layers) |sub_layer| { 278 + const l = try Layer.fromJson(allocator, sub_layer); 279 + try layers.put(allocator, l.name, l); 280 + } 281 + } 282 + return .{ .layers = layers }; 283 + } 284 + 285 + pub fn deinit(self: *Group, allocator: Allocator) void { 286 + var it = self.layers.valueIterator(); 287 + while (it.next()) |l| { 288 + l.*.deinit(allocator); 289 + } 290 + self.layers.deinit(allocator); 291 + } 292 + }; 253 293 254 294 pub const LayerContent = union(enum) { 255 295 tile_layer: TileLayer, ··· 258 298 group: Group, 259 299 260 300 pub fn fromJson(allocator: Allocator, json_layer: Layer.JsonLayer) !LayerContent { 261 - switch (json_layer.type) { 262 - .tilelayer => { 263 - return .{ 264 - .tile_layer = try TileLayer.fromJson(allocator, json_layer), 265 - }; 266 - }, 267 - else => return .{ .object_group = try ObjectGroup.fromJson(allocator, json_layer) }, 268 - } 301 + return switch (json_layer.type) { 302 + .tilelayer => .{ .tile_layer = try TileLayer.fromJson(allocator, json_layer) }, 303 + .objectgroup => .{ .object_group = try ObjectGroup.fromJson(allocator, json_layer) }, 304 + .imagelayer => .{ .image_layer = try ImageLayer.fromJson(allocator, json_layer) }, 305 + .group => .{ .group = try Group.fromJson(allocator, json_layer) }, 306 + }; 269 307 } 270 308 271 309 pub fn deinit(self: *LayerContent, allocator: Allocator) void { 272 310 switch (self.*) { 273 - .tile_layer => |*layer| { 274 - layer.deinit(allocator); 275 - }, 276 - .object_group => |*group| { 277 - group.deinit(allocator); 278 - }, 279 - else => {}, 311 + .tile_layer => |*layer| layer.deinit(allocator), 312 + .object_group => |*og| og.deinit(allocator), 313 + .image_layer => |*image| image.deinit(allocator), 314 + .group => |*group| group.deinit(allocator), 280 315 } 281 316 } 282 317 };
+19
test/layers.zig
··· 50 50 try expectEqual(null, object_group.get("nonexistent")); 51 51 } 52 52 53 + test "image layer" { 54 + try changeTestDir(); 55 + 56 + const allocator = std.testing.allocator; 57 + const test_map = @embedFile("map.tmj"); 58 + 59 + var map = try tmz.Map.initFromSlice(allocator, test_map); 60 + defer map.deinit(allocator); 61 + 62 + const layer = map.findLayer("Image Layer 1").?; 63 + const image_layer = layer.content.image_layer; 64 + try expectEqualStrings("", image_layer.image); 65 + try expectEqual(false, image_layer.repeat_x); 66 + try expectEqual(false, image_layer.repeat_y); 67 + try expectEqual(null, image_layer.transparent_color); 68 + } 69 + 53 70 test "Compression parses empty string as none" { 54 71 const source: std.json.Value = .{ .string = "" }; 55 72 const compression = try Compression.jsonParseFromValue(std.testing.allocator, source, .{}); ··· 59 76 const tmz = @import("tmz"); 60 77 const Layer = tmz.Layer; 61 78 const Compression = @import("tmz").layer.Compression; 79 + 80 + const changeTestDir = @import("tests.zig").changeTestDir; 62 81 63 82 const std = @import("std"); 64 83 const expectEqual = std.testing.expectEqual;