summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-04-09 14:51:20 +0200
committerkdx <kikoodx@paranoici.org>2023-04-09 14:51:20 +0200
commitda990c8b6de5be4019c04d3ac1ccd2ea07dc68a8 (patch)
tree5d087da506f160563123ec22c0cdc36a3e4f3640
parent53e64b956015ff5d233d82b6a9bbe070dc61f55b (diff)
downloadtiled2c-da990c8b6de5be4019c04d3ac1ccd2ea07dc68a8.tar.gz
process tileset
-rw-r--r--Tupfile6
-rw-r--r--src/main.c92
2 files changed, 85 insertions, 13 deletions
diff --git a/Tupfile b/Tupfile
index e56ac31..cd74bdb 100644
--- a/Tupfile
+++ b/Tupfile
@@ -1,5 +1,5 @@
-CC = gcc
-LD = $(CC)
+CC = gcc
+LD = $(CC)
-: foreach src/*.c |> $(CC) -std=c99 -Wall -Wextra -c -o %o %f |> build/%B.o
+: foreach src/*.c |> $(CC) -O3 -std=c99 -Wall -Wextra -c -o %o %f |> build/%B.o
: build/*.o |> $(LD) -o %o %f |> build/tiled2c
diff --git a/src/main.c b/src/main.c
index 4aac00d..e7a67d4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,10 +5,24 @@
#include <string.h>
#define JSON_GET(j, s) cJSON *const s = cJSON_GetObjectItem(j, #s); \
-if (s == NULL) { \
- fprintf(stderr, #s " not found\n"); \
- return -1; \
-}
+ if (s == NULL) { \
+ fprintf(stderr, #s " not found\n"); \
+ return -1; \
+ }
+
+#define JSON_GET_STRING(j, s) JSON_GET(j, s); \
+ if (!cJSON_IsString(s)) { \
+ fprintf(stderr, #s " is no string\n"); \
+ return -1; \
+ }
+
+#define JSON_GET_ARRAY(j, s) JSON_GET(j, s); \
+ if (!cJSON_IsArray(s)) { \
+ fprintf(stderr, #s " is no array\n"); \
+ return -1; \
+ } \
+ const int s##_size = cJSON_GetArraySize(s); \
+ (void)s##_size
static int
process_map(const char *word, cJSON *json)
@@ -18,9 +32,59 @@ process_map(const char *word, cJSON *json)
}
static int
+process_frame(cJSON *json)
+{
+ JSON_GET(json, duration);
+ JSON_GET(json, tileid);
+ printf("{%d,%d}", duration->valueint, tileid->valueint);
+ return 0;
+}
+
+static int
+process_animation(cJSON *json)
+{
+ printf("%d,(const Tiled2cFrame[]){", cJSON_GetArraySize(json));
+ cJSON *frame;
+ cJSON_ArrayForEach(frame, json)
+ if (process_frame(frame) < 0)
+ return -1;
+ printf("}");
+ return 0;
+}
+
+static int
+process_tile(cJSON *json)
+{
+ JSON_GET(json, id);
+ cJSON *const type = cJSON_GetObjectItem(json, "type");
+ if (type != NULL && !cJSON_IsString(type)) {
+ fprintf(stderr, "type is no string\n");
+ return -1;
+ }
+ cJSON *const proba = cJSON_GetObjectItem(json, "probability");
+ cJSON *const anim = cJSON_GetObjectItem(json, "animation");
+ if (anim != NULL && !cJSON_IsArray(anim)) {
+ fprintf(stderr, "animation is no array\n");
+ return -1;
+ }
+ printf("{%d,\"%s\",%f,",
+ id->valueint,
+ (type != NULL) ? type->valuestring : "0",
+ (proba != NULL) ? proba->valuedouble : 0.0);
+ if (anim == NULL)
+ printf("0,0");
+ else if (process_animation(anim) < 0)
+ return -1;
+ printf("}");
+ return 0;
+}
+
+static int
process_tileset(const char *word, cJSON *json)
{
- JSON_GET(json, name);
+ JSON_GET_STRING(json, name);
+ JSON_GET_STRING(json, image);
+ JSON_GET_ARRAY(json, tiles);
JSON_GET(json, imagewidth);
JSON_GET(json, imageheight);
JSON_GET(json, tilewidth);
@@ -29,10 +93,18 @@ process_tileset(const char *word, cJSON *json)
JSON_GET(json, columns);
JSON_GET(json, tilecount);
- if (!cJSON_IsString(name)) {
- fprintf(stderr, "name is no string\n");
- return -1;
- }
+ printf("static const Tiled2cSet %s={\"%s\",\"%s\",%d,%d,%d,%d,%d,%d,%d,"
+ "%d,(const Tiled2cTile[]){",
+ word, name->valuestring, image->valuestring,
+ imagewidth->valueint, imageheight->valueint,
+ tilewidth->valueint, tileheight->valueint, margin->valueint,
+ columns->valueint, tilecount->valueint, tiles_size);
+
+ cJSON *tile;
+ cJSON_ArrayForEach(tile, tiles)
+ if (process_tile(tile))
+ return -1;
+ printf("};\n");
return 0;
}
@@ -112,7 +184,7 @@ process(const char *path)
process_panic:
cJSON_Delete(json);
free(content);
- return 1;
+ return -1;
}
int