summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-09-14 12:34:31 +0200
committerkdx <kikoodx@paranoici.org>2023-09-14 12:34:31 +0200
commit7027243d1e97e361201ab3bfa62ec0610c6d26dd (patch)
tree205190b1ca8e2870fb60285b831debb85ab112c0
parent1ff165df318c97fa0d9a4803a5c5149e0896f2ba (diff)
downloadtiled2c-7027243d1e97e361201ab3bfa62ec0610c6d26dd.tar.gz
tile properties
-rw-r--r--src/main.c63
-rw-r--r--tiled2c.h24
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
@@ -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;
}
diff --git a/tiled2c.h b/tiled2c.h
index 4f3ded0..a35971b 100644
--- a/tiled2c.h
+++ b/tiled2c.h
@@ -9,11 +9,20 @@ typedef struct {
} 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 {
@@ -56,15 +65,6 @@ typedef struct {
typedef struct {
const char *name;
const char *type;
- union {
- const char *valuestring;
- double valuenumber;
- };
-} Tiled2cProperty;
-
-typedef struct {
- const char *name;
- const char *type;
unsigned int id;
double x;
double y;
@@ -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