summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 9e13c113f293090206bba49f9ea40b2fdf6a8dba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include "cJSON.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#define JSON_GET(j, s)                                                         \
  cJSON *const s = cJSON_GetObjectItem(j, #s);                                 \
  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_OPT_STRING(j, s)                                              \
  cJSON *const s = cJSON_GetObjectItem(j, #s);                                 \
  if (s != NULL && !cJSON_IsString(s)) {                                       \
    fprintf(stderr, #s " is no string\n");                                     \
    return -1;                                                                 \
  }

#define JSON_GET_BOOL(j, s)                                                    \
  JSON_GET(j, s);                                                              \
  if (!cJSON_IsBool(s)) {                                                      \
    fprintf(stderr, #s " is no bool\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_layer(cJSON *json) {
  JSON_GET_STRING(json, type);
  JSON_GET_STRING(json, name);
  JSON_GET_OPT_STRING(json, class);
  JSON_GET(json, opacity);
  JSON_GET(json, visible);
  cJSON *const parallaxx = cJSON_GetObjectItem(json, "parallaxx");
  cJSON *const parallaxy = cJSON_GetObjectItem(json, "parallaxy");

  printf("{\"%s\",%s%s%s,'%c',%f,%d,%f,%f,", name->valuestring,
         class ? "\"" : "", class ? class->valuestring : "0", class ? "\"" : "",
         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, offsetx);
  JSON_GET(json, offsety)

  printf(".imagelayer={\"%s\",%d,%d}},", image->valuestring, offsetx->valueint,
         offsety->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,", (unsigned)tile->valuedouble);
  printf("}}},");

  return 0;
}

static int process_property(cJSON *json) {
  JSON_GET_STRING(json, name);
  JSON_GET_STRING(json, type);
  printf("{\"%s\",\"%s\",", name->valuestring, type->valuestring);

  if (type->valuestring[0] == 's' || type->valuestring[0] == 'c') {
    JSON_GET_STRING(json, value);
    printf("\"%s\",0},", value->valuestring);
  } else if (type->valuestring[0] == 'b') {
    JSON_GET_BOOL(json, value);
    printf("\"%s\",%d},", value->valueint ? "true" : "false", value->valueint);
  } else {
    JSON_GET(json, value);
    printf("\"%f\",%f},", value->valuedouble, value->valuedouble);
  }

  return 0;
}

static int process_object(cJSON *json) {
  JSON_GET_STRING(json, name);
  JSON_GET_STRING(json, type);
  JSON_GET(json, id);
  cJSON *const tile = cJSON_GetObjectItem(json, "gid");
  JSON_GET(json, x);
  JSON_GET(json, y);
  JSON_GET(json, width);
  JSON_GET(json, height);
  JSON_GET(json, rotation);
  JSON_GET(json, visible);
  printf("{\"%s\",\"%s\",%d,%d,%d,%d,%d,%d,%f,%d", name->valuestring,
         type->valuestring, id->valueint, tile ? tile->valueint : 0,
         x->valueint, y->valueint, width->valueint, height->valueint,
         rotation->valuedouble, cJSON_IsTrue(visible));

  cJSON *properties = cJSON_GetObjectItem(json, "properties");
  if (!cJSON_IsArray(properties) || cJSON_GetArraySize(properties) == 0)
    printf(",0,0");
  else {
    printf(",%d,(const Tiled2cProperty[]){", cJSON_GetArraySize(properties));
    cJSON *property;
    cJSON_ArrayForEach(property, properties) {
      if (process_property(property))
        return -1;
    }
    printf("}");
  }

  printf("},");
  return 0;
}

static int process_map(const char *word, const char *path, cJSON *json) {
  JSON_GET(json, width);
  JSON_GET(json, height);
  JSON_GET(json, tilewidth);
  JSON_GET(json, tileheight);
  JSON_GET_ARRAY(json, layers);

  int numlayers = 0;
  cJSON *layer;
  cJSON_ArrayForEach(layer, layers) {
    JSON_GET_STRING(layer, type);
    numlayers += (type->valuestring[0] == 't' || type->valuestring[0] == 'i');
  }

  printf("#include \"tiled2c.h\"\n"
         "const Tiled2cMap %s={\"%s\",%d,%d,%d,%d,"
         "%d,(const Tiled2cLayer[]){",
         word, path, width->valueint, height->valueint, tilewidth->valueint,
         tileheight->valueint, numlayers);
  cJSON_ArrayForEach(layer, layers) {
    JSON_GET_STRING(layer, type);
    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;
    }
  }
  int numobjects = 0;
  cJSON_ArrayForEach(layer, layers) {
    JSON_GET_STRING(layer, type);
    if (type->valuestring[0] != 'o')
      continue;
    JSON_GET_ARRAY(layer, objects);
    numobjects += cJSON_GetArraySize(objects);
  }
  printf("},%d,(const Tiled2cObject[]){", numobjects);
  cJSON_ArrayForEach(layer, layers) {
    JSON_GET_STRING(layer, type);
    if (type->valuestring[0] != 'o')
      continue;
    JSON_GET_ARRAY(layer, objects);
    cJSON *object;
    cJSON_ArrayForEach(object, objects) {
      if (process_object(object) < 0)
        return -1;
    }
  }
  printf("}};\n");
  return 0;
}

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%s%s,%f,", id->valueint, (type != NULL) ? "\"" : "",
         (type != NULL) ? type->valuestring : "0", (type != NULL) ? "\"" : "",
         (proba != NULL) ? proba->valuedouble : 0.0);
  if (anim == NULL)
    printf("0,0");
  else if (process_animation(anim) < 0)
    return -1;

  cJSON *properties = cJSON_GetObjectItem(json, "properties");
  if (!cJSON_IsArray(properties))
    printf(",0,0");
  else {
    printf(",%d,(const Tiled2cProperty[]){", cJSON_GetArraySize(properties));
    cJSON *property;
    cJSON_ArrayForEach(property, properties) {
      if (process_property(property))
        return -1;
    }
    printf("}");
  }

  cJSON *objectgroup = cJSON_GetObjectItem(json, "objectgroup");
  if (!cJSON_IsObject(objectgroup))
    printf(",0,0");
  else {
    JSON_GET_ARRAY(objectgroup, objects);
    printf(",%d,(const Tiled2cObject[]){", cJSON_GetArraySize(objects));
    cJSON *object;
    cJSON_ArrayForEach(object, objects) {
      if (process_object(object) < 0)
        return -1;
    }
    printf("}");
  }

  printf("},");
  return 0;
}

static int process_tileset(const char *word, cJSON *json) {
  JSON_GET_STRING(json, name);
  JSON_GET_STRING(json, image);
  cJSON *tiles = cJSON_GetObjectItem(json, "tiles");
  if (tiles != NULL && !cJSON_IsArray(tiles)) {
    fprintf(stderr, "tiles is no array\n");
    return -1;
  }
  JSON_GET(json, imagewidth);
  JSON_GET(json, imageheight);
  JSON_GET(json, tilewidth);
  JSON_GET(json, tileheight);
  JSON_GET(json, margin);
  JSON_GET(json, columns);
  JSON_GET(json, tilecount);

  printf("#include \"tiled2c.h\"\n"
         "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 != NULL) ? cJSON_GetArraySize(tiles) : 0);

  if (tiles != NULL) {
    cJSON *tile;
    cJSON_ArrayForEach(tile, tiles) if (process_tile(tile)) return -1;
  }
  printf("}};\n");
  return 0;
}

char *drain(FILE *fp, long *size) {
  if (fseek(fp, 0, SEEK_END) < 0) {
    perror("drain:SEEK_END");
    return NULL;
  }
  *size = ftell(fp);
  if (fseek(fp, 0, SEEK_SET) < 0) {
    perror("drain:SEEK_SET");
    return NULL;
  }
  char *const data = malloc(*size);
  if (data == NULL) {
    perror("drain:malloc");
    return NULL;
  }
  if ((long)fread(data, 1, *size, fp) != *size) {
    perror("drain:fread");
    free(data);
    return NULL;
  }
  return data;
}

static int process(const char *path, const char *name) {
  FILE *fp = fopen(path, "rb");
  if (fp == NULL) {
    perror("process:fopen");
    return -1;
  }
  long size;
  char *const content = drain(fp, &size);
  fclose(fp);
  if (content == NULL)
    return -1;

  cJSON *const json = cJSON_ParseWithLength(content, size);
  if (json == NULL) {
    fprintf(stderr, "cJSON error: %s\n", cJSON_GetErrorPtr());
    return -1;
  }

  cJSON *const type = cJSON_GetObjectItem(json, "type");
  if (type == NULL || !cJSON_IsString(type)) {
    fprintf(stderr, "type not found\n");
    return -1;
  }

  const char *const type_s = cJSON_GetStringValue(type);
  switch (type_s[0]) {
  case 'm': /* map */
    if (process_map(name, path, json) < 0)
      goto process_panic;
    break;
  case 't': /* tileset */
    if (process_tileset(name, json) < 0)
      goto process_panic;
    break;
  default:
    fprintf(stderr, "unknown type '%s'\n", type_s);
    goto process_panic;
  }

  cJSON_Delete(json);
  free(content);
  return 0;
process_panic:
  cJSON_Delete(json);
  free(content);
  return -1;
}

int main(int argc, char **argv) {
  if (argc != 3) {
    fprintf(stderr, "usage: %s <tiled .tsj or .tmj> <name>\n", argv[0]);
    return 1;
  }
  if (process(argv[1], argv[2]) < 0)
    return 1;
  return 0;
}