diff options
author | kdx <kikoodx@paranoici.org> | 2023-05-18 06:50:43 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-05-18 06:54:32 +0200 |
commit | 1a093f6c18c7159dd4640b8bd3da718e9c508b19 (patch) | |
tree | b55c39ac5e6a8dfa8c8f3b3f2563130b6407d20a | |
parent | c665580621fcbd6dfdbf9cc8e35f02a00a3bf241 (diff) | |
download | tiled2c-1a093f6c18c7159dd4640b8bd3da718e9c508b19.tar.gz |
imagelayer support
-rw-r--r-- | src/main.c | 60 | ||||
-rw-r--r-- | tiled2c.h | 16 |
2 files changed, 67 insertions, 9 deletions
@@ -24,24 +24,54 @@ (void)s##_size static int -process_tilelayer(cJSON *json) +process_layer(cJSON *json) { + JSON_GET_STRING(json, type); JSON_GET_STRING(json, name); JSON_GET(json, opacity); JSON_GET(json, visible); - JSON_GET_ARRAY(json, data); cJSON *const parallaxx = cJSON_GetObjectItem(json, "parallaxx"); cJSON *const parallaxy = cJSON_GetObjectItem(json, "parallaxy"); - printf("{\"%s\",%f,%d,%f,%f,(const unsigned int[]){", - name->valuestring, opacity->valuedouble, + printf("{\"%s\",'%c',%f,%d,%f,%f,", + name->valuestring, + type->valuestring[0], + opacity->valuedouble, cJSON_IsTrue(visible), cJSON_IsNumber(parallaxx) ? parallaxx->valuedouble : 1.0, cJSON_IsNumber(parallaxy) ? parallaxy->valuedouble : 1.0); + + return 0; +} + +static int +process_imagelayer(cJSON *json) +{ + if (process_layer(json)) + return -1; + JSON_GET_STRING(json, image); + JSON_GET(json, x); + JSON_GET(json, y) + + printf(".imagelayer={\"%s\",%d,%d}},", + image->valuestring, x->valueint, y->valueint); + + return 0; +} + +static int +process_tilelayer(cJSON *json) +{ + if (process_layer(json)) + return -1; + JSON_GET_ARRAY(json, data); + + printf(".tilelayer={(const unsigned int[]){"); cJSON *tile; cJSON_ArrayForEach(tile, data) printf("%u,", tile->valueint); - printf("}},"); + printf("}}},"); + return 0; } @@ -77,7 +107,8 @@ process_map(const char *word, const char *path, cJSON *json) cJSON *layer; cJSON_ArrayForEach(layer, layers) { JSON_GET_STRING(layer, type); - numlayers += (type->valuestring[0] == 't'); + numlayers += (type->valuestring[0] == 't' || + type->valuestring[0] == 'i'); } printf("#include \"tiled2c.h\"\n" @@ -87,9 +118,22 @@ 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 (type->valuestring[0] == 't' && - process_tilelayer(layer) < 0) + switch (type->valuestring[0]) { + case 'i': /* imagelayer */ + if (process_imagelayer(layer) < 0) + return -1; + break; + case 'o': /* objectgroup */ + break; + case 't': /* tilelayer */ + if (process_tilelayer(layer) < 0) + return -1; + break; + default: + fprintf(stderr, "unknown layer type '%s'\n", + type->valuestring); return -1; + } } printf("},(const Tiled2cObject[]){"); int numobjects = 0; @@ -28,12 +28,26 @@ typedef struct { } Tiled2cSet; typedef struct { + const unsigned int *data; +} Tiled2cTilelayer; + +typedef struct { + const char *image; + int x; + int y; +} Tiled2cImagelayer; + +typedef struct { const char *name; + unsigned int type; double opacity; unsigned int visible; double parallaxx; double parallaxy; - const unsigned int *data; + union { + Tiled2cTilelayer tilelayer; + Tiled2cImagelayer imagelayer; + }; } Tiled2cLayer; typedef struct { |