diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 63 |
1 files changed, 42 insertions, 21 deletions
@@ -76,6 +76,26 @@ process_tilelayer(cJSON *json) } static int +process_property(cJSON *json) +{ + JSON_GET_STRING(json, name); + JSON_GET_STRING(json, type); + printf("{\"%s\",\"%s\",", + name->valuestring, type->valuestring); + + if (type->valuestring[0] == 's') { + JSON_GET_STRING(json, value); + printf("\"%s\",0},", value->valuestring); + } else { + JSON_GET(json, value); + printf("\"%f\",%f},", + value->valuedouble, value->valuedouble); + } + + return 0; +} + +static int process_object(cJSON *json) { JSON_GET_STRING(json, name); @@ -96,26 +116,14 @@ process_object(cJSON *json) if (!cJSON_IsArray(properties)) printf("0,0"); else { - printf("(const Tiled2cProperty[]){"); - int size = 0; + printf("%d,(const Tiled2cProperty[]){", + cJSON_GetArraySize(properties)); cJSON *property; cJSON_ArrayForEach(property, properties) { - size += 1; - JSON_GET_STRING(property, name); - JSON_GET_STRING(property, type); - printf("{\"%s\",\"%s\",", - name->valuestring, type->valuestring); - - if (type->valuestring[0] == 's') { - JSON_GET_STRING(property, value); - printf("\"%s\"}", value->valuestring); - } - if (type->valuestring[0] == 'o') { - JSON_GET(property, value); - printf("\"%f\"}", value->valuedouble); - } + if (process_property(property)) + return -1; } - printf("},%d", size); + printf("}"); } printf("},"); @@ -163,8 +171,7 @@ process_map(const char *word, const char *path, cJSON *json) return -1; } } - printf("},(const Tiled2cObject[]){"); - int numobjects = 0; + printf("},%d,(const Tiled2cObject[]){", cJSON_GetArraySize(layers)); cJSON_ArrayForEach(layer, layers) { JSON_GET_STRING(layer, type); if (type->valuestring[0] != 'o') @@ -174,10 +181,9 @@ process_map(const char *word, const char *path, cJSON *json) cJSON_ArrayForEach(object, objects) { if (process_object(object) < 0) return -1; - numobjects += 1; } } - printf("},%d};\n", numobjects); + printf("}};\n"); return 0; } @@ -225,6 +231,21 @@ process_tile(cJSON *json) printf("0,0"); else if (process_animation(anim) < 0) return -1; + + cJSON *properties = cJSON_GetObjectItem(json, "properties"); + if (!cJSON_IsArray(properties)) + printf(",0,0"); + else { + printf(",%d,(const Tiled2cProperty[]){", + cJSON_GetArraySize(properties)); + cJSON *property; + cJSON_ArrayForEach(property, properties) { + if (process_property(property)) + return -1; + } + printf("}"); + } + printf("},"); return 0; } |