summary refs log tree commit diff
path: root/src/tilemap.c
blob: 7dbc4ddf986a508eda31a5e9d2f9b81270a9f1f9 (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
#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include <SDL2/SDL.h>

#include "loader.h"
#include "main.h"

#include "tilemap.h"
#include "collision.h"

struct tilemap *tilemap = NULL, *next_tilemap = NULL;

#define PACKED __attribute__((__packed__))

uint8_t const MAGIC[4] = "TMv1";

struct PACKED sets {
	uint8_t magic[4];
	uint32_t tilesets_count;
	uint32_t wang_count;
};

struct PACKED map {
	struct {
		uint8_t r, g, b, a;
	} backdrop;
	uint32_t width;
	uint32_t height;
	uint32_t layers;
	uint32_t middleground;
	uint8_t tiles[];
};

collision_T tilemap_tile_raw(struct tilemap *tilemap, int x, int y) {
	return tilemap->collision[x + y * tilemap->width];
}

collision_T tilemap_tile(struct tilemap *tilemap, int x, int y) {
	x /= 8;
	y /= 8;
	if (x < 0) {
		x = 0;
	} else if ((unsigned) x >= tilemap->width) {
		x = tilemap->width - 1;
	}
	if (y < 0) {
		y = 0;
	} else if ((unsigned) y >= tilemap->height) {
		y = tilemap->height - 1;
	}
	return tilemap_tile_raw(tilemap, x, y);
}

collision_T tilemap_area_raw(struct tilemap *tilemap, int x1, int y1, int x2, int y2) {
	collision_T value = 0;
	for (int x = x1; x <= x2; x++) {
		for (int y = y1; y <= y2; y++) {
			value |= tilemap->collision[x + y * tilemap->width];
		}
	}
	return value;
}

collision_T tilemap_area(struct tilemap *tilemap, int x1, int y1, int x2, int y2) {
	x1 /= 8;
	y1 /= 8;
	x2 = (x2 - 1) / 8;
	y2 = (y2 - 1) / 8;
	if (x1 < 0) {
		x1 = 0;
	}
	if (x2 >= tilemap->width) {
		x2 = tilemap->width - 1;
	}
	if (y1 < 0) {
		y1 = 0;
	}
	if (y2 >= tilemap->height) {
		y2 = tilemap->height - 1;
	}
	return tilemap_area_raw(tilemap, x1, y1, x2, y2);
}

void tilemap_background(struct tilemap *tilemap, int x, int y, int w, int h) {
	SDL_SetRenderDrawColor(renderer, tilemap->backdrop.r, tilemap->backdrop.g, tilemap->backdrop.b, tilemap->backdrop.a);
	SDL_RenderFillRect(renderer, &(SDL_Rect) {0, 0, w, h});
	for (int i = 0; i < tilemap->middleground; i++) {
		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h});
	}
}

void tilemap_foreground(struct tilemap *tilemap, int x, int y, int w, int h) {
	for (int i = tilemap->middleground; i < tilemap->layers; i++) {
		SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h});
	}
}

void tilemap_free(struct tilemap *tilemap) {
	SDL_DestroyTexture(tilemap->tileset);
	SDL_DestroyTexture(tilemap->wang_tileset);
	free(tilemap->parallax);
	free(tilemap->collision);
	for (int i = 0; i < tilemap->layers; i++) {
		SDL_DestroyTexture(tilemap->tilemaps[i]);
	}
	free(tilemap->tilemaps);
	free(tilemap);
}

struct tilemap *tilemap_load(void *data, size_t size) {
	size_t const insize = size;

	struct sets *const sets = data;
	data = sets + 1;
	size -= sizeof (struct sets);

	if (memcmp(sets->magic, MAGIC, sizeof (MAGIC)) != 0) {
		fprintf(stderr, "error: unsupported level format (got '%.4s' expected '%.4s')\n", sets->magic, MAGIC);
		return NULL;
	}

	struct tilemap *tilemap = malloc(sizeof (struct tilemap));
	if (tilemap == NULL) {
		return NULL;
	}

	tilemap->tileset = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, 128, 128);
	tilemap->wang_tileset = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, 128, 128);
	SDL_SetTextureBlendMode(tilemap->wang_tileset, SDL_BLENDMODE_BLEND);
	tilemap->layers = 0;
	tilemap->parallax = NULL;
	tilemap->collision = NULL;
	tilemap->tilemaps = NULL;

	char *str = data;
	int y = 0;
	SDL_SetRenderTarget(renderer, tilemap->tileset);
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
	SDL_RenderClear(renderer);
	
	collision_T collision[256] = {0};
	
	for (uint32_t count = sets->tilesets_count; count > 0; count--) {
		int height = *(uint32_t *) str;
		str += sizeof (uint32_t);
		size_t len = strnlen(str, size);
		if (len == size) {
			goto fail;
		}
		void *tex = res_get_texture(str).data;
		struct blob col = res_get_collision(str);
		
		SDL_RenderCopy(renderer, tex, &(SDL_Rect) {0, 0, 128, height}, &(SDL_Rect) {0, y, 128, height});
		if (col.data != NULL) { // silence -fsanitize=undefined
			if (col.size + y * 2 > 0xf0) {
				fprintf(stderr, "warn: '%s' overflows tile properties\n", str);
				col.size = 0xf0 - y * 2;
			}
			memcpy(collision + y * 2, col.data, col.size * sizeof (collision_T));
		}
		
		y += height;
		if (y + height > 0xf0) {
			fputs("error: too many tiles\n", stderr);
			tilemap_free(tilemap);
			return NULL;
		}
		str += len + 1;
		size -= len + 4 + 1;
	}
	
	SDL_SetRenderTarget(renderer, tilemap->wang_tileset);
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
	SDL_RenderClear(renderer);
	
	int wangs = 0;
	if (sets->wang_count) {
		int height = *(uint32_t *) str;
		wangs = height / 8;
		str += sizeof (uint32_t);
		size_t len = strnlen(str, size);
		if (len == size) {
			tilemap_free(tilemap);
			return NULL;
		}
		void *tex = res_get_texture(str).data;
		struct blob col = res_get_collision(str);
		
		SDL_RenderCopy(renderer, tex, &(SDL_Rect) {0, 0, 128, height}, &(SDL_Rect) {0, 0, 128, height});
		if (col.size > 16) {
			fprintf(stderr, "warn: '%s' overflows tile properties\n", str);
			col.size = 16;
		}
		memcpy(collision + 0xf0, col.data, col.size * sizeof (collision_T));

		str += len + 1;
		size -= len + 4 + 1;
	}

	struct map const *const map = (void *) str;
	if (map->width * map->height * map->layers > size - sizeof (struct map)) {
		goto fail;
	}

	tilemap->backdrop.r = map->backdrop.r;
	tilemap->backdrop.g = map->backdrop.g;
	tilemap->backdrop.b = map->backdrop.b;
	tilemap->backdrop.a = map->backdrop.a;
	tilemap->width = map->width;
	tilemap->height = map->height;
	tilemap->layers = map->layers;
	tilemap->middleground = map->middleground;
	tilemap->tilemaps = malloc(sizeof (void *) * tilemap->layers);
	
	if (tilemap->middleground > tilemap->layers) {
		goto fail;
	} else if (tilemap->middleground == 0) {
		fputs("warn: map has no middle ground\n", stderr);
		tilemap->collision = calloc(tilemap->width, tilemap->height * sizeof (collision_T));
	} else {
		tilemap->collision = malloc(tilemap->width * tilemap->height * sizeof (collision_T));
		size_t const ll = (tilemap->middleground - 1) * map->width * map->height;
		for (size_t i = 0; i < tilemap->width * tilemap->height; i++) {
			tilemap->collision[i] = collision[map->tiles[i + ll]];
		}
	}
	
	for (int l = 0; l < tilemap->layers; l++) {
		SDL_Texture *layer = SDL_CreateTexture(renderer, SDL_PIXELTYPE_UNKNOWN, SDL_TEXTUREACCESS_TARGET, map->width * 8, map->height * 8);
		SDL_SetTextureBlendMode(layer, SDL_BLENDMODE_BLEND);
		SDL_SetRenderTarget(renderer, layer);
		
		size_t const ll = l * map->width * map->height;
		for (int y = 0; y < map->height; y++) {
			size_t const yy = y * map->width;
			for (int x = 0; x < map->width; x++) {
				unsigned tile = map->tiles[x + yy + ll];
				int tileX = tile & 0x0f;
				int tileY = tile >> 4;
				SDL_RenderCopy(renderer, tilemap->tileset, &(SDL_Rect) {tileX * 8, tileY * 8, 8, 8}, &(SDL_Rect) {x * 8, y * 8, 8, 8});
			}
		}
		
		for (int tile = 0xf0; tile < 0xf0 + wangs; tile++) {
			for (int y = 0; y < map->height - 1; y++) {
				size_t const yy = y * map->width;
				for (int x = 0; x < map->width - 1; x++) {
					int tl = map->tiles[x + yy + ll] == tile;
					int tr = map->tiles[x + 1 + yy + ll] == tile;
					int bl = map->tiles[x + yy + map->width + ll] == tile;
					int br = map->tiles[x + 1 + yy + map->width + ll] == tile;
					int tileX = tl << 0 | tr << 1 | bl << 2 | br << 3;
					int tileY = tile & 0x0f;
					SDL_RenderCopy(renderer, tilemap->wang_tileset, &(SDL_Rect) {tileX * 8, tileY * 8, 8, 8}, &(SDL_Rect) {x * 8 + 4, y * 8 + 4, 8, 8});
				}
			}
		}

		tilemap->tilemaps[l] = layer;
	}
	
	float *parallax = (void *) (map->tiles + map->width * map->height * map->layers);
	size -= (char *) parallax - (char *) map;
	if (sizeof (struct parallax) * map->layers > size) {
		goto fail;
	}
	
	tilemap->parallax = malloc(sizeof (struct parallax) * map->layers);
	for (int i = 0; i < map->layers; i++) {
		tilemap->parallax[i].x = *parallax++;
		tilemap->parallax[i].y = *parallax++;
	}

	size -= sizeof (struct parallax) * map->layers;
	
	tilemap->input_bytes = insize - size;
	//fprintf(stderr, "size: %zuB remaining: %zuB total: %zuB\n", tilemap->input_bytes, size, insize);
	SDL_SetRenderTarget(renderer, NULL);
	return tilemap;
	
	fail:
	tilemap_free(tilemap);
	SDL_SetRenderTarget(renderer, NULL);
	return NULL;
}