A library for parsing Tiled maps.
0
fork

Configure Feed

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

Merge pull request #2 from pajanowski/fix-infinite-maps-negative-chunks

Fix parsing for infinite maps with chunks starting in negative coordi…

authored by

Kent Smith and committed by
GitHub
71a81a45 890dcd17

+330 -6
+6 -6
src/layer.zig
··· 285 285 data: []u32, 286 286 height: u32, 287 287 width: u32, 288 - x: u32, 289 - y: u32, 288 + x: i32, 289 + y: i32, 290 290 291 291 /// https://doc.mapeditor.org/en/stable/reference/json-map-format/#chunk 292 292 pub const JsonChunk = struct { ··· 294 294 data: EncodedData, 295 295 height: u32, 296 296 width: u32, 297 - x: u32, 298 - y: u32, 297 + x: i32, 298 + y: i32, 299 299 300 300 const EncodedData = union(Layer.JsonLayer.Encoding) { 301 301 csv: []u32, ··· 306 306 var chunk: JsonChunk = .{ 307 307 .height = try innerParseFromValue(u32, allocator, source.object.get("height").?, options), 308 308 .width = try innerParseFromValue(u32, allocator, source.object.get("width").?, options), 309 - .x = try innerParseFromValue(u32, allocator, source.object.get("x").?, options), 310 - .y = try innerParseFromValue(u32, allocator, source.object.get("y").?, options), 309 + .x = try innerParseFromValue(i32, allocator, source.object.get("x").?, options), 310 + .y = try innerParseFromValue(i32, allocator, source.object.get("y").?, options), 311 311 .data = undefined, 312 312 }; 313 313
+134
test/map-infinite-base64-zstd-negative.tmj
··· 1 + { "backgroundcolor":"#aabbccdd", 2 + "class":"bar", 3 + "compressionlevel":-1, 4 + "editorsettings": 5 + { 6 + "export": 7 + { 8 + "format":"json", 9 + "target":"map-infinite-base64-zstd.tmj" 10 + } 11 + }, 12 + "height":30, 13 + "infinite":true, 14 + "layers":[ 15 + { 16 + "chunks":[ 17 + { 18 + "data":"KLUv\/WAAA8UAAHgAAQAAAAIAAwAAAAQAAAACAAdn0aZyVw==", 19 + "height":16, 20 + "width":16, 21 + "x":-16, 22 + "y":-16 23 + }, 24 + { 25 + "data":"KLUv\/WAAA60BAEgHAAAABgUFBQURgHA7Bx0upICpABq3OgGHrQrwylFzR\/asmZlUAAUwCNoZZgElX0nFT5cC", 26 + "height":16, 27 + "width":16, 28 + "x":16, 29 + "y":16 30 + }], 31 + "class":"bar", 32 + "compression":"zstd", 33 + "encoding":"base64", 34 + "height":48, 35 + "id":1, 36 + "name":"ground", 37 + "opacity":1, 38 + "startx":-16, 39 + "starty":-16, 40 + "type":"tilelayer", 41 + "visible":true, 42 + "width":48, 43 + "x":0, 44 + "y":0 45 + }, 46 + { 47 + "draworder":"topdown", 48 + "id":2, 49 + "name":"objects", 50 + "objects":[ 51 + { 52 + "height":16, 53 + "id":1, 54 + "name":"pool", 55 + "rotation":0, 56 + "type":"", 57 + "visible":true, 58 + "width":16, 59 + "x":88, 60 + "y":96 61 + }, 62 + { 63 + "height":20, 64 + "id":2, 65 + "name":"", 66 + "rotation":0, 67 + "text": 68 + { 69 + "color":"#241f31", 70 + "fontfamily":"Serif", 71 + "text":"tmz", 72 + "wrap":true 73 + }, 74 + "type":"", 75 + "visible":true, 76 + "width":30, 77 + "x":70, 78 + "y":16 79 + }], 80 + "opacity":1, 81 + "type":"objectgroup", 82 + "visible":true, 83 + "x":0, 84 + "y":0 85 + }, 86 + { 87 + "id":3, 88 + "image":"tilemap.png", 89 + "imageheight":32, 90 + "imagewidth":32, 91 + "name":"tile_image", 92 + "offsetx":1, 93 + "offsety":1, 94 + "opacity":1, 95 + "type":"imagelayer", 96 + "visible":true, 97 + "x":0, 98 + "y":0 99 + }], 100 + "nextlayerid":4, 101 + "nextobjectid":3, 102 + "orientation":"orthogonal", 103 + "properties":[ 104 + { 105 + "name":"beautiful", 106 + "type":"bool", 107 + "value":true 108 + }], 109 + "renderorder":"right-down", 110 + "tiledversion":"1.11.2", 111 + "tileheight":16, 112 + "tilesets":[ 113 + { 114 + "firstgid":1, 115 + "source":"source_tileset.tsj" 116 + }, 117 + { 118 + "columns":2, 119 + "firstgid":5, 120 + "image":"tilemap.png", 121 + "imageheight":32, 122 + "imagewidth":32, 123 + "margin":0, 124 + "name":"embedded_tiles", 125 + "spacing":0, 126 + "tilecount":4, 127 + "tileheight":16, 128 + "tilewidth":16 129 + }], 130 + "tilewidth":16, 131 + "type":"map", 132 + "version":"1.10", 133 + "width":30 134 + }
+162
test/map-infinite-csv-negative.tmj
··· 1 + { "backgroundcolor":"#aabbccdd", 2 + "class":"bar", 3 + "compressionlevel":-1, 4 + "editorsettings": 5 + { 6 + "export": 7 + { 8 + "format":"json", 9 + "target":"map-infinite-csv-negative.tmj" 10 + } 11 + }, 12 + "height":30, 13 + "infinite":true, 14 + "layers":[ 15 + { 16 + "chunks":[ 17 + { 18 + "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 19 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 20 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 26 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 27 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 28 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 29 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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x":16, 59 + "y":16 60 + }], 61 + "class":"bar", 62 + "height":48, 63 + "id":1, 64 + "name":"ground", 65 + "opacity":1, 66 + "startx":-16, 67 + "starty":-16, 68 + "type":"tilelayer", 69 + "visible":true, 70 + "width":48, 71 + "x":0, 72 + "y":0 73 + }, 74 + { 75 + "draworder":"topdown", 76 + "id":2, 77 + "name":"objects", 78 + "objects":[ 79 + { 80 + "height":16, 81 + "id":1, 82 + "name":"pool", 83 + "rotation":0, 84 + "type":"", 85 + "visible":true, 86 + "width":16, 87 + "x":88, 88 + "y":96 89 + }, 90 + { 91 + "height":20, 92 + "id":2, 93 + "name":"", 94 + "rotation":0, 95 + "text": 96 + { 97 + "color":"#241f31", 98 + "fontfamily":"Serif", 99 + "text":"tmz", 100 + "wrap":true 101 + }, 102 + "type":"", 103 + "visible":true, 104 + "width":30, 105 + "x":70, 106 + "y":16 107 + }], 108 + "opacity":1, 109 + "type":"objectgroup", 110 + "visible":true, 111 + "x":0, 112 + "y":0 113 + }, 114 + { 115 + "id":3, 116 + "image":"tilemap.png", 117 + "imageheight":32, 118 + "imagewidth":32, 119 + "name":"tile_image", 120 + "offsetx":1, 121 + "offsety":1, 122 + "opacity":1, 123 + "type":"imagelayer", 124 + "visible":true, 125 + "x":0, 126 + "y":0 127 + }], 128 + "nextlayerid":4, 129 + "nextobjectid":3, 130 + "orientation":"orthogonal", 131 + "properties":[ 132 + { 133 + "name":"beautiful", 134 + "type":"bool", 135 + "value":true 136 + }], 137 + "renderorder":"right-down", 138 + "tiledversion":"1.11.2", 139 + "tileheight":16, 140 + "tilesets":[ 141 + { 142 + "firstgid":1, 143 + "source":"source_tileset.tsj" 144 + }, 145 + { 146 + "columns":2, 147 + "firstgid":5, 148 + "image":"tilemap.png", 149 + "imageheight":32, 150 + "imagewidth":32, 151 + "margin":0, 152 + "name":"embedded_tiles", 153 + "spacing":0, 154 + "tilecount":4, 155 + "tileheight":16, 156 + "tilewidth":16 157 + }], 158 + "tilewidth":16, 159 + "type":"map", 160 + "version":"1.10", 161 + "width":30 162 + }
+28
test/maps.zig
··· 59 59 } 60 60 } 61 61 62 + test "negative coordinate infinite maps" { 63 + try changeTestDir(); 64 + 65 + const allocator = std.testing.allocator; 66 + 67 + const test_maps = [_][]const u8{ 68 + "map-infinite-csv-negative.tmj", 69 + "map-infinite-base64-zstd-negative.tmj", 70 + }; 71 + 72 + 73 + for (test_maps) |test_map| { 74 + var map = try Map.initFromFile(allocator, test_map); 75 + defer map.deinit(allocator); 76 + 77 + try baseTests(map); 78 + 79 + try expectEqual(true, map.infinite); 80 + 81 + const layer = map.layers_by_name.get("ground").?; 82 + const chunks = layer.content.tile_layer.chunks.?; 83 + try expectEqual(2, chunks.len); 84 + try expectEqual(7, chunks[1].data[0]); 85 + try expectEqual(0, chunks[0].data[0]); 86 + try expectEqual(4, chunks[0].data[chunks[0].data.len - 1]); 87 + } 88 + } 89 + 62 90 fn infiniteMapTests(map: Map) !void { 63 91 try baseTests(map); 64 92