summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-09 16:31:13 +0200
committerkdx <kikoodx@paranoici.org>2023-04-09 16:31:13 +0200
commit40e2178f66f7689fd48fba6f94f330afbe7d3bb7 (patch)
treebc4d8436c505f725d290c5b8ede2c1ab7a02d8d8
parent62e99f0a0d730d06cd0c183e059a471eb0a90cd2 (diff)
downloadtiled2c-40e2178f66f7689fd48fba6f94f330afbe7d3bb7.tar.gz
convert objects
-rw-r--r--src/main.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/src/main.c b/src/main.c
index f584c92..b4bedb8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -2,7 +2,6 @@
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
-#include <string.h>
#define JSON_GET(j, s) cJSON *const s = cJSON_GetObjectItem(j, #s); \
if (s == NULL) { \
@@ -36,12 +35,31 @@ process_tilelayer(cJSON *json)
cJSON_IsTrue(visible));
cJSON *tile;
cJSON_ArrayForEach(tile, data)
- printf("%d,", tile->valueint);
+ printf("%u,", tile->valueint);
printf("}},");
return 0;
}
static int
+process_object(cJSON *json)
+{
+ JSON_GET_STRING(json, name);
+ JSON_GET_STRING(json, type);
+ JSON_GET(json, id);
+ JSON_GET(json, x);
+ JSON_GET(json, y);
+ JSON_GET(json, width);
+ JSON_GET(json, height);
+ JSON_GET(json, rotation);
+ JSON_GET(json, visible);
+ printf("{\"%s\",\"%s\",%d,%d,%d,%d,%d,%f,%d},",
+ name->valuestring, type->valuestring, id->valueint, x->valueint,
+ y->valueint, width->valueint, height->valueint,
+ rotation->valuedouble, cJSON_IsTrue(visible));
+ return 0;
+}
+
+static int
process_map(const char *word, const char *path, cJSON *json)
{
JSON_GET(json, width);
@@ -54,7 +72,7 @@ process_map(const char *word, const char *path, cJSON *json)
cJSON *layer;
cJSON_ArrayForEach(layer, layers) {
JSON_GET_STRING(layer, type);
- numlayers += (strcmp(type->valuestring, "tilelayer") == 0);
+ numlayers += (type->valuestring[0] == 't');
}
printf("#include \"tiled2c.h\"\n"
@@ -64,12 +82,25 @@ process_map(const char *word, const char *path, cJSON *json)
tilewidth->valueint, tileheight->valueint, numlayers);
cJSON_ArrayForEach(layer, layers) {
JSON_GET_STRING(layer, type);
- if (strcmp(type->valuestring, "tilelayer") == 0 &&
+ if (type->valuestring[0] == 't' &&
process_tilelayer(layer) < 0)
return -1;
}
- printf("},/*TODO: objects*/0,(const Tiled2cObject[]){");
- printf("}};\n");
+ printf("},(const Tiled2cObject[]){");
+ int numobjects = 0;
+ cJSON_ArrayForEach(layer, layers) {
+ JSON_GET_STRING(layer, type);
+ if (type->valuestring[0] != 'o')
+ continue;
+ JSON_GET_ARRAY(layer, objects);
+ cJSON *object;
+ cJSON_ArrayForEach(object, objects) {
+ if (process_object(object) < 0)
+ return -1;
+ numobjects += 1;
+ }
+ }
+ printf("},%d};\n", numobjects);
return 0;
}