Tiled JSON Loader in C (single-file header)


(Randy Gaul) #1

Hi all! I’m new here :slight_smile:
I wrote a JSON loader specifically for parsing/loading up Tiled maps as a single-file header implemenation.

Here’s a link to the source: https://github.com/RandyGaul/tinyheaders/blob/master/tinytiled.h

The idea is that Tiled is a rather nice map editor, but loading up maps into a game can sometimes be difficult. Writing a custom loader for the tmx format can be time consuming, and using a heavy-weight JSON or XML loader can be cumbersome. Instead, tinytiled.h is a custom solution to specifically load only Tiled JSON map files.

Maps are loaded up into C structs, and members of the structs can be directly accessed by the user. The structs are just one-to-one mirrors of the json documentation for Tiled: http://doc.mapeditor.org/en/latest/reference/json-map-format/

Here’s an example of a typical use-case:

	// Load up a Tiled json exported file, either from disk or memory, like so:

		tinytiled_map_t* map = tinytiled_load_map_from_memory(memory, size, 0);

	// Then simply access the map's fields like so:

		// get map width and height
		int w = map->w;
		int h = map->h;

		// loop over the map's layers
		tinytiled_layer_t* layer = map->layers;
		while (layer)
		{
			int* data = layer->data;
			int data_count = layer->data_count;

			// do something with the tile data
			UserFunction_HandleTiles(data, data_count);

			layer = layer->next;
		}

	// Finally, free it like so:

		tinytiled_free_map(map);

Hopefully someone finds this header useful! Please let me know if anyone takes a peek or tries it out and has any feedback :slight_smile: