LibTMJ  1.0.0
A library for loading JSON Tiled maps
tmj.h
Go to the documentation of this file.
1 #ifndef LIBTMJ_API
2 #define LIBTMJ_API
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 
7 #include <jansson.h>
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
24 typedef struct Property {
25  char* name;
26  char* propertytype;
27  char* type;
28 
29  union {
30  char* value_string;
31  int value_int;
32  double value_float;
33  bool value_bool;
34  char* value_color;
35  char* value_file;
37  };
39 
43 typedef struct Chunk {
44  int height;
45  int width;
46  int x;
47  int y;
48 
50  size_t data_count;
51 
52  union {
53  char* data_str;
54  unsigned int* data_uint;
55  };
57 
61 typedef struct Point {
62  double x;
63  double y;
65 
69 typedef struct Text {
70  bool bold;
71  bool italic;
72  bool kerning;
73  bool strikeout;
74  bool underline;
75  bool wrap;
76 
77  char* color;
78  char* fontfamily;
79  char* halign;
80  char* text;
81  char* valign;
82 
83  int pixelsize;
84 } Text;
85 
89 typedef struct Object {
90  bool ellipse;
91  bool point;
92  bool visible;
93 
94  char* name;
95  char* template;
96  char* type; // Optional
97 
98  int gid;
99  int id;
100 
101  double height;
102  double rotation;
103  double width;
104  double x;
105  double y;
106 
108 
109  union {
112  };
113  union {
116  };
117 
120 
123 
127 typedef struct Layer {
128  bool locked;
129  bool repeatx; // Imagelayer only
130  bool repeaty; // Imagelayer only
131  bool visible;
132 
133  char* class; // Optional
134  char* compression; // tilelayer only
135  char* draworder; // objectgroup only
136  char* encoding; // tilelayer only
137  char* image; // imagelayer only
138  char* name;
139  char* tintcolor; // Optional
140  char* transparentcolor; // Optional, imagelayer only
141  char* type;
142 
144  size_t data_count;
145  union {
146  unsigned int* data_uint;
147  char* data_str;
148  }; // tilelayer only
149 
150  int height; // tilelayer only
151  int id;
152  int startx;
153  int starty;
154  int width; // tilelayer only
155  int x;
156  int y;
157 
158  double offsetx;
159  double offsety;
160  double opacity;
161  double parallaxx;
162  double parallaxy;
163 
164  size_t chunk_count;
165  Chunk* chunks; // Optional, tilelayer only
166 
167  size_t layer_count;
168  struct Layer* layers; // group only
169 
170  size_t object_count;
171  Object* objects; // objectgroup only
172 
176 
180 typedef struct Transformations {
181  bool hflip;
183  bool rotate;
184  bool vflip;
186 
190 typedef struct Terrain {
191  char* name;
192 
193  int tile;
194 
198 
202 typedef struct Frame {
203  int duration;
204  int tileid;
206 
210 typedef struct Tile {
211  char* image; // Optional
212  char* type; // Optional
213 
214  int id;
217  int x;
218  int y;
219  int width;
220  int height;
221 
222  double probability; // Optional
223 
226 
227  int terrain[4]; // Optional
228 
231 
232  Layer* objectgroup; // Optional
234 
238 typedef struct WangTile {
239  int tileid;
240 
241  unsigned char wangid[8];
243 
247 typedef struct WangColor {
248  char* class;
249  char* color;
250  char* name;
251 
252  int tile;
253 
254  double probability;
255 
259 
263 typedef struct WangSet {
264  char* class; // Optional
265  char* name;
266  char* type;
267 
268  int tile;
269 
270  size_t color_count;
272 
275 
279 
283 typedef struct Grid {
284  char* orientation;
285 
286  int height;
287  int width;
289 
293 typedef struct TileOffset {
294  int x;
295  int y;
297 
303 typedef struct Tileset {
308  json_t* root; // NULL for embedded tilesets; only set for external tilesets
309 
310  char* backgroundcolor; // Optional
311  char* class; // Optional
312  char* fillmode;
313  char* image;
314  char* name;
316  char* source;
319  char* transparentcolor; // Optional
320  char* type;
321  char* version;
322 
323  int columns;
324  int firstgid;
327  int margin;
328  int spacing;
332 
335 
337  Terrain* terrains; // Optional
338 
339  size_t tile_count;
340  Tile* tiles; // Optional
341 
344 
345  Grid* grid; // Optional
346 
347  TileOffset* tileoffset; // Optional
348 
351 
355 typedef struct Map {
360  json_t* root;
361 
362  bool infinite;
363 
364  char* backgroundcolor; // Optional
365  char* class; // Optional
366  char* orientation;
367  char* renderorder; // Orthogonal maps only
368  char* staggeraxis; // Staggered/hexagonal maps only
369  char* staggerindex; // Staggered/hexagonal maps only
371  char* type;
372  char* version;
373 
375  int height;
376  int hexsidelength; // Hexagonal maps only
381  int width;
382 
385 
386  // Layer tree
387  size_t layer_count;
389 
392 
395 } Map;
396 
400 typedef struct ObjectTemplate {
401  char* type;
402 
403  Tileset* tileset; // Optional
406 
426 Map* tmj_map_loadf(const char* path, bool check_extension);
427 
444 Map* tmj_map_load(const char* map, const char* name);
445 
458 Tileset* tmj_tileset_loadf(const char* path, bool check_extension);
459 
471 Tileset* tmj_tileset_load(const char* tileset);
472 
482 void tmj_map_free(Map* map);
483 
491 void tmj_tileset_free(Tileset* tileset);
492 
496 typedef enum TMJ_LOG_PRIORITY {
503 
513 void tmj_log_regcb(bool debug, void (*callback)(tmj_log_priority, const char*));
514 
516 // * @defgroup util Util
517 // *
518 // * Helper functions.
519 // */
520 
528 extern const unsigned int TMJ_VERSION_MAJOR;
529 
537 extern const unsigned int TMJ_VERSION_MINOR;
538 
546 extern const unsigned int TMJ_VERSION_PATCH;
547 
554 extern const char* const TMJ_VERSION;
555 
570 uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size);
571 
572 #ifdef __cplusplus
573 }
574 #endif
575 
576 #endif
Map * tmj_map_load(const char *map, const char *name)
Loads the Tiled map from the given JSON object string.
Definition: map.c:1120
uint32_t * tmj_decode_layer(const char *data, const char *encoding, const char *compression, size_t *size)
Decodes layer data from a Tiled map layer.
Definition: util.c:15
Map * tmj_map_loadf(const char *path, bool check_extension)
Loads the Tiled map from the file at the given path.
Definition: map.c:1088
const char *const TMJ_VERSION
The full library version.
Definition: util.c:13
enum TMJ_LOG_PRIORITY tmj_log_priority
void tmj_log_regcb(bool debug, void(*callback)(tmj_log_priority, const char *))
Registers a callback function to handle logging events.
Definition: log.c:18
void tmj_map_free(Map *map)
Frees the memory associated with the given map.
Definition: map.c:1134
const unsigned int TMJ_VERSION_MINOR
The library minor version.
Definition: util.c:11
TMJ_LOG_PRIORITY
Definition: tmj.h:496
void tmj_tileset_free(Tileset *tileset)
Frees the memory associated with the given tileset.
Definition: tileset.c:671
Tileset * tmj_tileset_load(const char *tileset)
Loads the Tiled tileset from the given JSON object string.
Definition: tileset.c:638
Tileset * tmj_tileset_loadf(const char *path, bool check_extension)
Loads the Tiled tileset at the given path.
Definition: tileset.c:588
const unsigned int TMJ_VERSION_PATCH
The library patch version.
Definition: util.c:12
const unsigned int TMJ_VERSION_MAJOR
‍**
Definition: util.c:10
@ TMJ_LOG_WARNING
Definition: tmj.h:499
@ TMJ_LOG_INFO
Definition: tmj.h:498
@ TMJ_LOG_ERR
Definition: tmj.h:500
@ TMJ_LOG_DEBUG
Definition: tmj.h:497
@ TMJ_LOG_CRIT
Definition: tmj.h:501
https://doc.mapeditor.org/en/stable/reference/json-map-format/#chunk
Definition: tmj.h:43
int y
Definition: tmj.h:47
unsigned int * data_uint
Definition: tmj.h:54
int x
Definition: tmj.h:46
bool data_is_str
Definition: tmj.h:49
char * data_str
Definition: tmj.h:53
int width
Definition: tmj.h:45
size_t data_count
Definition: tmj.h:50
int height
Definition: tmj.h:44
https://doc.mapeditor.org/en/stable/reference/json-map-format/#frame
Definition: tmj.h:202
int tileid
Definition: tmj.h:204
int duration
Definition: tmj.h:203
https://doc.mapeditor.org/en/stable/reference/json-map-format/#grid
Definition: tmj.h:283
int height
Definition: tmj.h:286
int width
Definition: tmj.h:287
char * orientation
Definition: tmj.h:284
https://doc.mapeditor.org/en/stable/reference/json-map-format/#layer
Definition: tmj.h:127
char * encoding
Definition: tmj.h:136
int startx
Definition: tmj.h:152
unsigned int * data_uint
Definition: tmj.h:146
size_t property_count
Definition: tmj.h:173
int x
Definition: tmj.h:155
char * tintcolor
Definition: tmj.h:139
char * draworder
Definition: tmj.h:135
Property * properties
Definition: tmj.h:174
double offsetx
Definition: tmj.h:158
double parallaxy
Definition: tmj.h:162
Object * objects
Definition: tmj.h:171
char * transparentcolor
Definition: tmj.h:140
size_t layer_count
Definition: tmj.h:167
size_t chunk_count
Definition: tmj.h:164
double opacity
Definition: tmj.h:160
int starty
Definition: tmj.h:153
size_t data_count
Definition: tmj.h:144
double parallaxx
Definition: tmj.h:161
char * type
Definition: tmj.h:141
int height
Definition: tmj.h:150
char * image
Definition: tmj.h:137
int width
Definition: tmj.h:154
char * data_str
Definition: tmj.h:147
char * compression
Definition: tmj.h:134
bool locked
Definition: tmj.h:128
bool repeaty
Definition: tmj.h:130
Chunk * chunks
Definition: tmj.h:165
bool data_is_str
Definition: tmj.h:143
bool repeatx
Definition: tmj.h:129
int y
Definition: tmj.h:156
struct Layer * layers
Definition: tmj.h:168
size_t object_count
Definition: tmj.h:170
char * name
Definition: tmj.h:138
int id
Definition: tmj.h:151
double offsety
Definition: tmj.h:159
bool visible
Definition: tmj.h:131
https://doc.mapeditor.org/en/stable/reference/json-map-format/#json-map-format
Definition: tmj.h:355
int nextlayerid
Definition: tmj.h:377
int height
Definition: tmj.h:375
char * backgroundcolor
Definition: tmj.h:364
char * staggeraxis
Definition: tmj.h:368
char * renderorder
Definition: tmj.h:367
char * staggerindex
Definition: tmj.h:369
Property * properties
Definition: tmj.h:391
size_t property_count
Definition: tmj.h:390
int hexsidelength
Definition: tmj.h:376
double parallaxoriginy
Definition: tmj.h:384
json_t * root
The root object returned by jansson after parsing.
Definition: tmj.h:360
double parallaxoriginx
Definition: tmj.h:383
char * type
Definition: tmj.h:371
Layer * layers
Definition: tmj.h:388
int compressionlevel
Definition: tmj.h:374
char * version
Definition: tmj.h:372
char * orientation
Definition: tmj.h:366
size_t tileset_count
Definition: tmj.h:393
int width
Definition: tmj.h:381
bool infinite
Definition: tmj.h:362
int tileheight
Definition: tmj.h:379
char * tiledversion
Definition: tmj.h:370
size_t layer_count
Definition: tmj.h:387
int tilewidth
Definition: tmj.h:380
Tileset * tilesets
Definition: tmj.h:394
int nextobjectid
Definition: tmj.h:378
https://doc.mapeditor.org/en/stable/reference/json-map-format/#object-template
Definition: tmj.h:400
Tileset * tileset
Definition: tmj.h:403
char * type
Definition: tmj.h:401
Object * object
Definition: tmj.h:404
https://doc.mapeditor.org/en/stable/reference/json-map-format/#object
Definition: tmj.h:89
double x
Definition: tmj.h:104
char * name
Definition: tmj.h:94
double width
Definition: tmj.h:103
double y
Definition: tmj.h:105
size_t property_count
Definition: tmj.h:118
int gid
Definition: tmj.h:98
double height
Definition: tmj.h:101
Point * polygon
Definition: tmj.h:114
bool ellipse
Definition: tmj.h:90
int id
Definition: tmj.h:99
bool point
Definition: tmj.h:91
Text * text
Definition: tmj.h:121
bool is_polygon
Definition: tmj.h:107
size_t polygon_point_count
Definition: tmj.h:110
bool visible
Definition: tmj.h:92
char * type
Definition: tmj.h:96
size_t polyline_point_count
Definition: tmj.h:111
Point * polyline
Definition: tmj.h:115
Property * properties
Definition: tmj.h:119
double rotation
Definition: tmj.h:102
https://doc.mapeditor.org/en/stable/reference/json-map-format/#point
Definition: tmj.h:61
double x
Definition: tmj.h:62
double y
Definition: tmj.h:63
https://doc.mapeditor.org/en/stable/reference/json-map-format/#property
Definition: tmj.h:24
int value_object
Definition: tmj.h:36
char * type
Definition: tmj.h:27
char * propertytype
Definition: tmj.h:26
char * value_string
Definition: tmj.h:30
double value_float
Definition: tmj.h:32
int value_int
Definition: tmj.h:31
char * value_color
Definition: tmj.h:34
char * value_file
Definition: tmj.h:35
bool value_bool
Definition: tmj.h:33
char * name
Definition: tmj.h:25
https://doc.mapeditor.org/en/stable/reference/json-map-format/#terrain
Definition: tmj.h:190
Property * properties
Definition: tmj.h:196
char * name
Definition: tmj.h:191
int tile
Definition: tmj.h:193
size_t property_count
Definition: tmj.h:195
https://doc.mapeditor.org/en/stable/reference/json-map-format/#text
Definition: tmj.h:69
bool wrap
Definition: tmj.h:75
bool underline
Definition: tmj.h:74
char * fontfamily
Definition: tmj.h:78
bool bold
Definition: tmj.h:70
bool strikeout
Definition: tmj.h:73
char * color
Definition: tmj.h:77
bool kerning
Definition: tmj.h:72
char * valign
Definition: tmj.h:81
char * halign
Definition: tmj.h:79
bool italic
Definition: tmj.h:71
char * text
Definition: tmj.h:80
int pixelsize
Definition: tmj.h:83
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-offset
Definition: tmj.h:293
int y
Definition: tmj.h:295
int x
Definition: tmj.h:294
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-definition
Definition: tmj.h:210
Frame * animation
Definition: tmj.h:225
int height
Definition: tmj.h:220
double probability
Definition: tmj.h:222
int y
Definition: tmj.h:218
int id
Definition: tmj.h:214
char * image
Definition: tmj.h:211
char * type
Definition: tmj.h:212
int x
Definition: tmj.h:217
size_t animation_count
Definition: tmj.h:224
int imageheight
Definition: tmj.h:215
Property * properties
Definition: tmj.h:230
Layer * objectgroup
Definition: tmj.h:232
int imagewidth
Definition: tmj.h:216
size_t property_count
Definition: tmj.h:229
int width
Definition: tmj.h:219
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tileset
Definition: tmj.h:303
char * fillmode
Definition: tmj.h:312
size_t tile_count
Definition: tmj.h:339
char * transparentcolor
Definition: tmj.h:319
int tileheight
Definition: tmj.h:330
char * name
Definition: tmj.h:314
char * image
Definition: tmj.h:313
Grid * grid
Definition: tmj.h:345
int imagewidth
Definition: tmj.h:326
size_t wang_set_count
Definition: tmj.h:342
char * tiledversion
Definition: tmj.h:317
char * objectalignment
Definition: tmj.h:315
char * source
Definition: tmj.h:316
int firstgid
Definition: tmj.h:324
TileOffset * tileoffset
Definition: tmj.h:347
int tilewidth
Definition: tmj.h:331
char * tilerendersize
Definition: tmj.h:318
size_t property_count
Definition: tmj.h:333
size_t terrain_count
Definition: tmj.h:336
int margin
Definition: tmj.h:327
WangSet * wangsets
Definition: tmj.h:343
Property * properties
Definition: tmj.h:334
json_t * root
The root object returned by jansson after parsing.
Definition: tmj.h:308
int tilecount
Definition: tmj.h:329
char * backgroundcolor
Definition: tmj.h:310
int columns
Definition: tmj.h:323
char * version
Definition: tmj.h:321
Transformations * transformations
Definition: tmj.h:349
Tile * tiles
Definition: tmj.h:340
Terrain * terrains
Definition: tmj.h:337
int spacing
Definition: tmj.h:328
char * type
Definition: tmj.h:320
int imageheight
Definition: tmj.h:325
https://doc.mapeditor.org/en/stable/reference/json-map-format/#transformations
Definition: tmj.h:180
bool preferuntransformed
Definition: tmj.h:182
bool hflip
Definition: tmj.h:181
bool rotate
Definition: tmj.h:183
bool vflip
Definition: tmj.h:184
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-color
Definition: tmj.h:247
double probability
Definition: tmj.h:254
char * color
Definition: tmj.h:249
size_t property_count
Definition: tmj.h:256
Property * properties
Definition: tmj.h:257
int tile
Definition: tmj.h:252
char * name
Definition: tmj.h:250
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-set
Definition: tmj.h:263
size_t property_count
Definition: tmj.h:273
Property * properties
Definition: tmj.h:274
char * name
Definition: tmj.h:265
size_t wangtile_count
Definition: tmj.h:276
int tile
Definition: tmj.h:268
WangTile * wangtiles
Definition: tmj.h:277
WangColor * colors
Definition: tmj.h:271
size_t color_count
Definition: tmj.h:270
char * type
Definition: tmj.h:266
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-tile
Definition: tmj.h:238
int tileid
Definition: tmj.h:239
struct Tileset Tileset
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tileset
struct Layer Layer
https://doc.mapeditor.org/en/stable/reference/json-map-format/#layer
struct Property Property
https://doc.mapeditor.org/en/stable/reference/json-map-format/#property
struct Tile Tile
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-definition
struct Text Text
https://doc.mapeditor.org/en/stable/reference/json-map-format/#text
struct Point Point
https://doc.mapeditor.org/en/stable/reference/json-map-format/#point
struct Transformations Transformations
https://doc.mapeditor.org/en/stable/reference/json-map-format/#transformations
struct WangTile WangTile
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-tile
struct ObjectTemplate ObjectTemplate
https://doc.mapeditor.org/en/stable/reference/json-map-format/#object-template
struct Map Map
https://doc.mapeditor.org/en/stable/reference/json-map-format/#json-map-format
struct Frame Frame
https://doc.mapeditor.org/en/stable/reference/json-map-format/#frame
struct Grid Grid
https://doc.mapeditor.org/en/stable/reference/json-map-format/#grid
struct WangSet WangSet
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-set
struct Object Object
https://doc.mapeditor.org/en/stable/reference/json-map-format/#object
struct TileOffset TileOffset
https://doc.mapeditor.org/en/stable/reference/json-map-format/#tile-offset
struct Chunk Chunk
https://doc.mapeditor.org/en/stable/reference/json-map-format/#chunk
struct Terrain Terrain
https://doc.mapeditor.org/en/stable/reference/json-map-format/#terrain
struct WangColor WangColor
https://doc.mapeditor.org/en/stable/reference/json-map-format/#wang-color