summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-09 16:16:22 +0200
committerkdx <kikoodx@paranoici.org>2023-04-09 16:19:22 +0200
commit62e99f0a0d730d06cd0c183e059a471eb0a90cd2 (patch)
tree0942e6ad1395fa4962c969689c63886f639b39f8
parent35f474e43848d76fcbe93899fd3ef8569497bde8 (diff)
downloadtiled2c-62e99f0a0d730d06cd0c183e059a471eb0a90cd2.tar.gz
convert map
-rw-r--r--src/main.c51
1 files changed, 46 insertions, 5 deletions
diff --git a/src/main.c b/src/main.c
index 09341a9..f584c92 100644
--- a/src/main.c
+++ b/src/main.c
@@ -25,11 +25,52 @@
(void)s##_size
static int
-process_map(const char *word, cJSON *json)
+process_tilelayer(cJSON *json)
{
- (void)word, (void)json;
- fprintf(stderr, "process_map TODO\n");
- return -1;
+ JSON_GET_STRING(json, name);
+ JSON_GET(json, opacity);
+ JSON_GET(json, visible);
+ JSON_GET_ARRAY(json, data);
+ printf("{\"%s\",%f,%d,(const unsigned int[]){",
+ name->valuestring, opacity->valuedouble,
+ cJSON_IsTrue(visible));
+ cJSON *tile;
+ cJSON_ArrayForEach(tile, data)
+ printf("%d,", tile->valueint);
+ printf("}},");
+ return 0;
+}
+
+static int
+process_map(const char *word, const char *path, cJSON *json)
+{
+ JSON_GET(json, width);
+ JSON_GET(json, height);
+ JSON_GET(json, tilewidth);
+ JSON_GET(json, tileheight);
+ JSON_GET_ARRAY(json, layers);
+
+ int numlayers = 0;
+ cJSON *layer;
+ cJSON_ArrayForEach(layer, layers) {
+ JSON_GET_STRING(layer, type);
+ numlayers += (strcmp(type->valuestring, "tilelayer") == 0);
+ }
+
+ printf("#include \"tiled2c.h\"\n"
+ "const Tiled2cMap %s={\"%s\",%d,%d,%d,%d,"
+ "%d,(const Tiled2cLayer[]){",
+ word, path, width->valueint, height->valueint,
+ tilewidth->valueint, tileheight->valueint, numlayers);
+ cJSON_ArrayForEach(layer, layers) {
+ JSON_GET_STRING(layer, type);
+ if (strcmp(type->valuestring, "tilelayer") == 0 &&
+ process_tilelayer(layer) < 0)
+ return -1;
+ }
+ printf("},/*TODO: objects*/0,(const Tiled2cObject[]){");
+ printf("}};\n");
+ return 0;
}
static int
@@ -164,7 +205,7 @@ process(const char *path, const char *name)
const char *const type_s = cJSON_GetStringValue(type);
switch (type_s[0]) {
case 'm': /* map */
- if (process_map(name, json) < 0)
+ if (process_map(name, path, json) < 0)
goto process_panic;
break;
case 't': /* tileset */