comparison tools/tileset/main.c @ 218:71f989ae8de9

rpg: add support for animated tiles
author David Demelier <markand@malikania.fr>
date Wed, 18 Nov 2020 13:46:29 +0100
parents 64f24b482722
children d01e83210ca2
comparison
equal deleted inserted replaced
217:836bac1419c7 218:71f989ae8de9
1 /* 1 /*
2 * mlk-tileset.c -- convert tiled tilesets JSON files into custom files 2 * main.c -- convert tiled tilesets JSON files into custom files
3 * 3 *
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr> 4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
5 * 5 *
6 * Permission to use, copy, modify, and/or distribute this software for any 6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above 7 * purpose with or without fee is hereby granted, provided that the above
18 18
19 #include <assert.h> 19 #include <assert.h>
20 #include <stdarg.h> 20 #include <stdarg.h>
21 #include <stdio.h> 21 #include <stdio.h>
22 #include <stdnoreturn.h> 22 #include <stdnoreturn.h>
23 #include <string.h>
23 24
24 #include <jansson.h> 25 #include <jansson.h>
25 26
26 static noreturn void 27 static noreturn void
27 die(const char *fmt, ...) 28 die(const char *fmt, ...)
58 59
59 if (!json_is_string(image)) 60 if (!json_is_string(image))
60 die("invalid 'image' property\n"); 61 die("invalid 'image' property\n");
61 62
62 printf("image|%s\n", json_string_value(image)); 63 printf("image|%s\n", json_string_value(image));
64 }
65
66 static const json_t *
67 find_property_value(const json_t *array, const char *prop)
68 {
69 const json_t *obj;
70 size_t i;
71
72 json_array_foreach(array, i, obj) {
73 const json_t *name = json_object_get(obj, "name");
74
75 if (!name || !json_is_string(name))
76 die("invalid property object\n");
77
78 if (strcmp(json_string_value(name), prop) == 0)
79 return json_object_get(obj, "value");
80 }
81
82 return NULL;
83 }
84
85 static void
86 write_animation(const json_t *tile)
87 {
88 const json_t *id = json_object_get(tile, "id");
89 const json_t *properties = json_object_get(tile, "properties");
90 const json_t *file = find_property_value(properties, "animation-file");
91 const json_t *delay = find_property_value(properties, "animation-delay");
92
93 /* Animations are completely optional. */
94 if (!json_is_array(properties))
95 return;
96
97 if (!json_is_integer(id))
98 die("invalid 'id' property in tile\n");
99
100 if (json_is_string(file)) {
101 printf("%d|%s|", (int)json_integer_value(id), json_string_value(file));
102
103 if (json_is_integer(delay))
104 printf("%d\n", (int)json_integer_value(delay));
105 else
106 printf("10\n");
107 }
63 } 108 }
64 109
65 static void 110 static void
66 write_tiledef(const json_t *tile) 111 write_tiledef(const json_t *tile)
67 { 112 {
69 const json_t *objectgroup = json_object_get(tile, "objectgroup"); 114 const json_t *objectgroup = json_object_get(tile, "objectgroup");
70 const json_t *objects = json_object_get(objectgroup, "objects"); 115 const json_t *objects = json_object_get(objectgroup, "objects");
71 const json_t *first = json_array_get(objects, 0); 116 const json_t *first = json_array_get(objects, 0);
72 const json_t *x, *y, *w, *h; 117 const json_t *x, *y, *w, *h;
73 118
119 /* Collisions are optional. */
120 if (!json_is_object(objectgroup))
121 return;
122
74 if (!json_is_integer(id)) 123 if (!json_is_integer(id))
75 die("invalid 'id' property in tile\n"); 124 die("invalid 'id' property in tile\n");
76 if (!json_is_object(objectgroup))
77 die("invalid 'objectgroup' property in tile\n");
78 if (!json_is_array(objects)) 125 if (!json_is_array(objects))
79 die("invalid 'objects' property in tile\n"); 126 die("invalid 'objects' property in tile\n");
80 127
81 x = json_object_get(first, "x"); 128 x = json_object_get(first, "x");
82 y = json_object_get(first, "y"); 129 y = json_object_get(first, "y");
112 159
113 write_tiledef(object); 160 write_tiledef(object);
114 } 161 }
115 } 162 }
116 163
164 static void
165 write_animations(const json_t *tiles)
166 {
167 size_t index;
168 json_t *object;
169
170 if (!json_is_array(tiles))
171 return;
172
173 puts("animations");
174
175 json_array_foreach(tiles, index, object) {
176 if (!json_is_object(object))
177 die("tile is not an object\n");
178
179 write_animation(object);
180 }
181 }
182
117 int 183 int
118 main(int argc, char *argv[]) 184 main(int argc, char *argv[])
119 { 185 {
186 (void)argc;
187 (void)argv;
188
120 json_t *document; 189 json_t *document;
121 json_error_t error; 190 json_error_t error;
122 191
123 document = json_loadf(stdin, 0, &error); 192 document = json_loadf(stdin, 0, &error);
124 193
128 die("root value isn't an object\n"); 197 die("root value isn't an object\n");
129 198
130 write_dimensions(document); 199 write_dimensions(document);
131 write_image(document); 200 write_image(document);
132 write_tiledefs(json_object_get(document, "tiles")); 201 write_tiledefs(json_object_get(document, "tiles"));
202 write_animations(json_object_get(document, "tiles"));
133 203
134 json_decref(document); 204 json_decref(document);
135 } 205 }