summary refs log tree commit diff
path: root/utl/json2map
diff options
context:
space:
mode:
Diffstat (limited to 'utl/json2map')
-rw-r--r--utl/json2map/main.c50
1 files changed, 49 insertions, 1 deletions
diff --git a/utl/json2map/main.c b/utl/json2map/main.c
index f3e7716..b298626 100644
--- a/utl/json2map/main.c
+++ b/utl/json2map/main.c
@@ -12,15 +12,22 @@ struct tilemap_T {
 
 #define PACKED __attribute__((__packed__))
 
+#define MAGIC "TMv1"
+
 struct PACKED sets {
+	uint8_t magic[4];
 	uint32_t tilesets_count;
 	uint32_t wang_count;
 };
 
 struct PACKED map {
+	struct {
+		uint8_t r, g, b, a;
+	} backdrop;
 	uint32_t width;
 	uint32_t height;
 	uint32_t layers;
+	uint32_t middleground;
 	uint8_t tiles[];
 };
 
@@ -119,7 +126,7 @@ int main(int argc, char **argv) {
 	uint32_t wang_height;
 	char *tilesets_data = NULL;
 	size_t tilesets_size = 0;
-	struct sets counts = {0, 0};
+	struct sets counts = {MAGIC, 0, 0};
 	
 	cJSON_ArrayForEach(tileset, tilesets) {
 		//printf("=== tileset (%% tiles) ===\n");
@@ -217,12 +224,39 @@ int main(int argc, char **argv) {
 		return 1;
 	}
 	struct map map = {
+		.backdrop = {0x9f, 0x9f, 0x9f, 0xff},
 		.width = width_obj->valueint - 1,
 		.height = height_obj->valueint - 1,
 		.layers = 0,
+		.middleground = 0,
 	};
 	uint8_t *tile_data = NULL;
 	float *parallaxes = NULL;
+
+	cJSON const *backdrop = cJSON_GetObjectItemCaseSensitive(json, "backgroundcolor");
+	if (cJSON_IsString(backdrop)) {
+		switch (strlen(backdrop->valuestring)) {
+			unsigned value;
+			case 9:
+				value = strtoul(backdrop->valuestring + 1, NULL, 16);
+				map.backdrop.r = value >> 16;
+				map.backdrop.g = value >> 8;
+				map.backdrop.b = value >> 0;
+				map.backdrop.a = value >> 24;
+				break;
+			case 7:
+				value = strtoul(backdrop->valuestring + 1, NULL, 16);
+				map.backdrop.r = value >> 16;
+				map.backdrop.g = value >> 8;
+				map.backdrop.b = value >> 0;
+				map.backdrop.a = 0xff;
+				break;
+			default:
+				fputs("incorrect backdrop color format\n", stderr);
+				return 1;
+		}
+	}
+	//printf("sky: # %02x %02x %02x %02x\n", map.backdrop.r, map.backdrop.g, map.backdrop.b, map.backdrop.a);
 	
 	cJSON const *layers = cJSON_GetObjectItemCaseSensitive(json, "layers");
 	cJSON const *layer = NULL;
@@ -334,6 +368,20 @@ int main(int argc, char **argv) {
 		} else {
 			// ??? layer
 		}
+
+		cJSON const *properties = cJSON_GetObjectItemCaseSensitive(layer, "properties");
+		cJSON const *property = NULL;
+		cJSON_ArrayForEach (property, properties) {
+			char const *const name = cJSON_GetObjectItemCaseSensitive(property, "name")->valuestring;
+			if (strcmp(name, "middleground") == 0) {
+				if (cJSON_IsTrue(cJSON_GetObjectItemCaseSensitive(property, "value"))) {
+					map.middleground = map.layers;
+				}
+			} else {
+				printf("unknown property '%s'\n", name);
+				return 1;
+			}
+		}
 	}
 	
 	fwrite(&counts, 1, sizeof (counts), outfile);