comparison src/tools/tileset/mlk-tileset.c @ 414:6947c1fefe5c

misc: extreme cleanup - Remove of Javascript binding for many reasons. - Put examples and tests in GNU make. - Create a library with shared example code.
author David Demelier <markand@malikania.fr>
date Sun, 09 Oct 2022 13:51:03 +0200
parents src/tools/tileset/main.c@460c78706989
children
comparison
equal deleted inserted replaced
413:222045c513ec 414:6947c1fefe5c
1 /*
2 * main.c -- convert tiled tilesets JSON files into custom files
3 *
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
5 *
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
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <jansson.h>
25
26 static void
27 die(const char *fmt, ...)
28 {
29 assert(fmt);
30
31 va_list ap;
32
33 va_start(ap, fmt);
34 vfprintf(stderr, fmt, ap);
35 va_end(ap);
36 exit(1);
37 }
38
39 static void
40 write_dimensions(const json_t *document)
41 {
42 const json_t *tilewidth = json_object_get(document, "tilewidth");
43 const json_t *tileheight = json_object_get(document, "tileheight");
44
45 if (!json_is_integer(tilewidth))
46 die("invalid 'tilewidth' property\n");
47 if (!json_is_integer(tileheight))
48 die("invalid 'tileheight' property\n");
49
50 printf("tilewidth|%u\n", (unsigned int)json_integer_value(tilewidth));
51 printf("tileheight|%u\n", (unsigned int)json_integer_value(tileheight));
52 }
53
54 static void
55 write_image(const json_t *document)
56 {
57 const json_t *image = json_object_get(document, "image");
58
59 if (!json_is_string(image))
60 die("invalid 'image' property\n");
61
62 printf("image|%s\n", json_string_value(image));
63 }
64
65 static const json_t *
66 find_property_value(const json_t *array, const char *prop)
67 {
68 const json_t *obj;
69 size_t i;
70
71 json_array_foreach(array, i, obj) {
72 const json_t *name = json_object_get(obj, "name");
73
74 if (!name || !json_is_string(name))
75 die("invalid property object\n");
76
77 if (strcmp(json_string_value(name), prop) == 0)
78 return json_object_get(obj, "value");
79 }
80
81 return NULL;
82 }
83
84 static void
85 write_animation(const json_t *tile)
86 {
87 const json_t *id = json_object_get(tile, "id");
88 const json_t *properties = json_object_get(tile, "properties");
89 const json_t *file = find_property_value(properties, "animation-file");
90 const json_t *delay = find_property_value(properties, "animation-delay");
91
92 /* Animations are completely optional. */
93 if (!json_is_array(properties))
94 return;
95
96 if (!json_is_integer(id))
97 die("invalid 'id' property in tile\n");
98
99 if (json_is_string(file)) {
100 printf("%d|%s|", (int)json_integer_value(id), json_string_value(file));
101
102 if (json_is_integer(delay))
103 printf("%d\n", (int)json_integer_value(delay));
104 else
105 printf("10\n");
106 }
107 }
108
109 static void
110 write_tiledef(const json_t *tile)
111 {
112 const json_t *id = json_object_get(tile, "id");
113 const json_t *objectgroup = json_object_get(tile, "objectgroup");
114 const json_t *objects = json_object_get(objectgroup, "objects");
115 const json_t *first = json_array_get(objects, 0);
116 const json_t *x, *y, *w, *h;
117
118 /* Collisions are optional. */
119 if (!json_is_object(objectgroup))
120 return;
121
122 if (!json_is_integer(id))
123 die("invalid 'id' property in tile\n");
124 if (!json_is_array(objects))
125 die("invalid 'objects' property in tile\n");
126
127 x = json_object_get(first, "x");
128 y = json_object_get(first, "y");
129 w = json_object_get(first, "width");
130 h = json_object_get(first, "height");
131
132 if (!json_is_integer(x) || !json_is_integer(y) ||
133 !json_is_integer(w) || !json_is_integer(h))
134 die("invalid collide object in tile description\n");
135
136 printf("%d|%d|%d|%d|%d\n",
137 (int)json_integer_value(id),
138 (int)json_integer_value(x),
139 (int)json_integer_value(y),
140 (int)json_integer_value(w),
141 (int)json_integer_value(h));
142 }
143
144 static void
145 write_tiledefs(const json_t *tiles)
146 {
147 size_t index;
148 json_t *object;
149
150 if (!json_is_array(tiles))
151 return;
152
153 puts("tiledefs");
154
155 json_array_foreach(tiles, index, object) {
156 if (!json_is_object(object))
157 die("tile is not an object\n");
158
159 write_tiledef(object);
160 }
161 }
162
163 static void
164 write_animations(const json_t *tiles)
165 {
166 size_t index;
167 json_t *object;
168
169 if (!json_is_array(tiles))
170 return;
171
172 puts("animations");
173
174 json_array_foreach(tiles, index, object) {
175 if (!json_is_object(object))
176 die("tile is not an object\n");
177
178 write_animation(object);
179 }
180 }
181
182 int
183 main(int argc, char *argv[])
184 {
185 (void)argc;
186 (void)argv;
187
188 json_t *document;
189 json_error_t error;
190
191 document = json_loadf(stdin, 0, &error);
192
193 if (!document)
194 die("%d:%d: %s\n", error.line, error.column, error.text);
195 if (!json_is_object(document))
196 die("root value isn't an object\n");
197
198 write_dimensions(document);
199 write_image(document);
200 write_tiledefs(json_object_get(document, "tiles"));
201 write_animations(json_object_get(document, "tiles"));
202
203 json_decref(document);
204 }