From 7027243d1e97e361201ab3bfa62ec0610c6d26dd Mon Sep 17 00:00:00 2001 From: kdx Date: Thu, 14 Sep 2023 12:34:31 +0200 Subject: tile properties --- src/main.c | 63 +++++++++++++++++++++++++++++++++++++++++--------------------- tiled2c.h | 24 ++++++++++++------------ 2 files changed, 54 insertions(+), 33 deletions(-) diff --git a/src/main.c b/src/main.c index 2ca8ef2..21fdd75 100644 --- a/src/main.c +++ b/src/main.c @@ -75,6 +75,26 @@ process_tilelayer(cJSON *json) return 0; } +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) { @@ -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; } diff --git a/tiled2c.h b/tiled2c.h index 4f3ded0..a35971b 100644 --- a/tiled2c.h +++ b/tiled2c.h @@ -8,12 +8,21 @@ typedef struct { double tileid; } Tiled2cFrame; +typedef struct { + const char *name; + const char *type; + const char *valuestring; + double valuenumber; +} Tiled2cProperty; + typedef struct { unsigned int id; const char *type; double probability; - const int numframes; + int numframes; const Tiled2cFrame *frames; + unsigned int numproperties; + const Tiled2cProperty *properties; } Tiled2cTile; typedef struct { @@ -53,15 +62,6 @@ typedef struct { }; } Tiled2cLayer; -typedef struct { - const char *name; - const char *type; - union { - const char *valuestring; - double valuenumber; - }; -} Tiled2cProperty; - typedef struct { const char *name; const char *type; @@ -72,8 +72,8 @@ typedef struct { double height; double rotation; unsigned int visible; - const Tiled2cProperty *properties; unsigned int numproperties; + const Tiled2cProperty *properties; } Tiled2cObject; typedef struct { @@ -84,8 +84,8 @@ typedef struct { unsigned int tileheight; unsigned int numlayers; const Tiled2cLayer *layers; - const Tiled2cObject *objects; unsigned int numobjects; + const Tiled2cObject *objects; } Tiled2cMap; #ifdef __cplusplus -- cgit v1.2.3