TileMapLayer *TileMapLayer_create() { TileMapLayer *layer = calloc(1, sizeof(TileMapLayer)); check(layer != NULL, "Couldn't create layer"); layer->opacity = 1; layer->visible = 1; return layer; error: if (layer) TileMapLayer_destroy(layer); return NULL; }
TileMapParseStatus TileMap_parse_layer(xmlTextReaderPtr reader, TileMap *map, TileMapLayer **out_layer) { TileMapParseStatus status = TILEMAP_PARSE_OK; TileMapLayer *layer = TileMapLayer_create(); check(layer != NULL, "Couldn't create layer"); while (xmlTextReaderMoveToNextAttribute(reader)) { xmlChar *attrName = xmlTextReaderName(reader); xmlChar *attrVal = xmlTextReaderValue(reader); if (streq(attrName, "name")) { layer->name = calloc(1, strlen((const char *)attrVal) + 1); strcpy(layer->name, (const char *)attrVal); } else if (streq(attrName, "opacity")) { layer->opacity = atof((const char *)attrVal); } else if (streq(attrName, "visible")) { layer->visible = atoi((const char *)attrVal); } } while (xmlTextReaderRead(reader)) { xmlChar *childName = xmlTextReaderName(reader); if (xmlTextReaderNodeType(reader) == XML_ELEMENT_DECL && streq(childName, "layer")) { break; } else if (streq(childName, "data")) { status = TileMapLayer_parse_data(reader, map, layer); check(status == TILEMAP_PARSE_OK, "Failed to parse layer data"); } } if (status == TILEMAP_PARSE_OK) { *out_layer = layer; return status; } error: if (layer) TileMapLayer_destroy(layer); if (status == TILEMAP_PARSE_OK) status = TILEMAP_PARSE_UNKNOWN_ERR; return status; }