Tiled Map Editor integration

Simply save the Tiled map as a .txt file

typedef struct {
	int width;
	int height;
	int layercount;
	int*** layers;
} AD_Map;

AD_Map* AD_GetMap(char* url) {
	FILE* file = fopen(url, "r");

	AD_Map* map = (AD_Map*) malloc(sizeof(AD_Map));
	map->layers = NULL;
	map->layercount = 0;

	char* ind;
	int   k, r, c;
	char  temp[BUFSIZ];
	char  line[BUFSIZ];

	while(fgets(line, BUFSIZ, file)!=NULL) {
		if(!strcmp(line, "[header]n")) {
			fgets(line, BUFSIZ, file);
			sscanf(line, "width=%dn", &map->width);
			fgets(line, BUFSIZ, file);
			sscanf(line, "height=%dn", &map->height);

		if(!strcmp(line, "[layer]n")) {
			k = map->layercount++;
			map->layers = (int***) realloc(map->layers, map->layercount*sizeof(int**));
			map->layers[k] = (int**) malloc(map->height*sizeof(int*));

			fgets(line, BUFSIZ, file);
			fgets(line, BUFSIZ, file);
			fgets(line, BUFSIZ, file);
			strcpy(temp, line);

			for(r=0; r<map->height; r++) {
				ind = strtok(temp, ",");
				map->layers[k][r] = (int*) malloc(map->width*sizeof(int));
				for(c=0; c<map->width; c++) {
					map->layers[k][r][c] = atoi(ind);
					ind = strtok(NULL, ",");
				fgets(line, BUFSIZ, file);
				strcpy(temp, line);

	return map;

void AD_FreeMap(AD_Map* map) {
	int k, r;
	for(k=0; k<map->layercount; k++) {
		for(r=0; r<map->height; r++)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s