16 if (tileset == NULL) {
23 json_t* tileoffset = NULL;
24 json_t* transformations = NULL;
25 json_t* properties = NULL;
26 json_t* terrains = NULL;
30 int unpk = json_unpack_ex(tileset,
34 "s?s, s?s, s?s, s:s, s:s, s?s, s?s, s:s, s?s, s?s, s:s, s:s,"
35 "s:i, s?i, s:i, s:i, s:i, s:i, s:i, s:i, s:i,"
36 "s?o, s?o, s?o, s?o, s?o, s?o"
94 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset, %s at line %d column %d", error.text, error.line, error.column);
101 ret->
grid = calloc(1,
sizeof(
Grid));
103 if (ret->
grid == NULL) {
109 unpk = json_unpack_ex(
110 grid, &error, 0,
"{s:i, s:s, s:i}",
"height", &ret->
grid->
height,
"orientation", &ret->
grid->
orientation,
"width", &ret->
grid->
width);
113 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->grid, %s at line %d column %d", ret->
name, error.text, error.line, error.column);
124 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->tileoffset, the system is out of memory", ret->
name);
129 unpk = json_unpack_ex(tileoffset, &error, 0,
"{s:i, s:i}",
"x", &ret->
tileoffset->
x,
"y", &ret->
tileoffset->
y);
132 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->tileoffset, %s at line %d column %d", ret->
name, error.text, error.line, error.column);
134 goto fail_tileoffset;
139 if (transformations) {
143 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->transformations, the system is out of memory", ret->
name);
145 goto fail_tileoffset;
148 unpk = json_unpack_ex(transformations,
151 "{s:b, s:b, s:b, s:b}",
158 "preferuntransformed",
163 "Unable to unpack tileset[%s]->transformations, %s at line %d column %d",
169 goto fail_transformations;
178 goto fail_transformations;
186 if (!json_is_array(terrains)) {
187 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->terrains, terrains must be an array of Terrains", ret->
name);
189 goto fail_properties;
197 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->terrains, the system is out of memory", ret->
name);
199 goto fail_properties;
205 json_array_foreach(terrains, idx, terrain) {
208 unpk = json_unpack_ex(terrain,
221 "Unable to unpack tileset[%s]->terrains, %s at line %d column %d",
244 if (!json_is_array(tiles)) {
245 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->tiles, tiles must be an array of Tiles", ret->
name);
254 if (ret->
tiles == NULL) {
263 json_array_foreach(tiles, idx, tile) {
265 json_t* animation = NULL;
266 json_t* objectgroup = NULL;
267 json_t* properties = NULL;
268 json_t* terrain = NULL;
270 unpk = json_unpack_ex(tile,
275 "s:i, s?i, s?i, s?i, s?i, s?i, s?i,"
309 logmsg(
TMJ_LOG_ERR,
"Unable to unpack tileset[%s]->tiles, %s at line %d column %d", ret->
name, error.text, error.line, error.column);
320 "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, the system is out of memory",
327 json_t* objects = NULL;
328 json_t* layer_properties = NULL;
330 unpk = json_unpack_ex(objectgroup,
335 "s?s, s?s, s:s, s?s, s:s,"
337 "s?F, s?F, s:F, s?F, s?F,"
377 "Unable to unpack tileset[%s]->tiles[%d]->objectgroup, %s at line %d column %d",
399 if (layer_properties) {
414 if (!json_is_array(animation)) {
416 "Unable to unpack tileset[%s]->tiles[%d]->animation, animation must be an array of Frames",
427 "Unable to unpack tileset[%s]->tiles[%d]->animation, the system is out of memory",
435 json_t* frame = NULL;
437 json_array_foreach(animation, idx2, frame) {
438 unpk = json_unpack_ex(frame,
451 "Unable to unpack tileset[%s]->tiles[%d]->animation, %s at line %d column %d",
480 if (!json_is_array(terrain)) {
482 "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of terrain "
491 json_t* terrain_idx = NULL;
493 json_array_foreach(terrain, idx2, terrain_idx) {
494 if (!json_is_integer(terrain_idx)) {
496 "Unable to unpack tileset[%s]->tiles[%d]->terrain, terrain must be an array of "
502 ret->
tiles[idx].
terrain[idx2] = (int)json_integer_value(terrain_idx);
511 for (
size_t i = 0; i < ret->
tile_count; i++) {
535 fail_transformations:
551 for (
size_t i = 0; i < tileset_count; i++) {
553 if (tilesets[i].tiles) {
554 for (
size_t j = 0; j < tilesets[i].
tile_count; j++) {
555 free(tilesets[i].tiles[j].animation);
556 if (tilesets[i].tiles[j].objectgroup != NULL) {
557 free(tilesets[i].tiles[j].objectgroup->
properties);
560 free(tilesets[i].tiles[j].objectgroup);
561 free(tilesets[i].tiles[j].properties);
564 free(tilesets[i].tiles);
568 if (tilesets[i].terrains) {
570 free(tilesets[i].terrains[j].properties);
573 free(tilesets[i].terrains);
577 free(tilesets[i].properties);
578 free(tilesets[i].transformations);
579 free(tilesets[i].tileoffset);
580 free(tilesets[i].grid);
582 json_decref(tilesets[i].root);
591 char* ext = strrchr(path,
'.');
593 if (check_extension) {
600 if (strcmp(ext,
".tsj") != 0 && strcmp(ext,
".json") != 0) {
601 logmsg(
TMJ_LOG_ERR,
"Tileset filename '%s' has unknown extension, '%s'", path, ext);
602 logmsg(
TMJ_LOG_ERR,
"Tileset filename '%s' must have '.tsj' or '.json' extension to be loaded", path);
609 json_t* root = json_load_file(path, JSON_REJECT_DUPLICATES, &error);
612 logmsg(
TMJ_LOG_ERR,
"Could not load tileset '%s', %s at line %d column %d", path, error.text, error.line, error.column);
620 logmsg(
TMJ_LOG_ERR,
"Unable to load tileset[%s], the system is out of memory", path);
642 json_t* root = json_loads(tileset, JSON_REJECT_DUPLICATES, &error);
645 logmsg(
TMJ_LOG_ERR,
"Could not load tileset, %s at line %d column %d", error.text, error.line, error.column);
void logmsg(tmj_log_priority priority, char *msg,...)
Processes log messages and passes them to the active logging callback, if there is one.
void tmj_tileset_free(Tileset *tileset)
Frees the memory associated with the given tileset.
Tileset * tmj_tileset_load(const char *tileset)
Loads the Tiled tileset from the given JSON object string.
Tileset * tmj_tileset_loadf(const char *path, bool check_extension)
Loads the Tiled tileset at the given path.
Object * unpack_objects(json_t *objects)
Property * unpack_properties(json_t *properties)
void free_objects(Object *objects, size_t object_count)
Helper function to free Objects.
https://doc.mapeditor.org/en/stable/reference/json-map-format/#frame
https://doc.mapeditor.org/en/stable/reference/json-map-format/#grid
https://doc.mapeditor.org/en/stable/reference/json-map-format/#layer
https://doc.mapeditor.org/en/stable/reference/json-map-format/#terrain
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-offset
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-definition
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tileset
json_t * root
The root object returned by jansson after parsing.
Transformations * transformations
int unpack_tileset(json_t *tileset, Tileset *ret)
void tilesets_free(Tileset *tilesets, size_t tileset_count)
Helper function for freeing tilesets embedded in maps.