summaryrefslogtreecommitdiff
path: root/src/map.c
blob: f98cfe6e53a6252fe033d17959d1d0a14597d81a (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
Map *map_new(const Tiled2cMap *t2c) {
	assert(t2c != nullptr);

	Map *this = alloc(sizeof(Map));

	this->t2c = t2c;

	return this;
}

bool map_get_px(Map *this, int x, int y, u32 type) {
	assert(this != nullptr);
	if (x < 0 || y < 0)
		return type == TILE_NONE;
	x /= cfg.tile_width;
	y /= cfg.tile_height;
	if ((u32)x >= this->t2c->width || (u32)y >= this->t2c->height)
		return type == TILE_NONE;
	rfor(i, 0u, this->t2c->numlayers) {
		assert(this->t2c->layers->type == 't');
		const auto layer = &this->t2c->layers->tilelayer;
		const u32 tile = layer->data[x + y * this->t2c->width];
		if (tile_type(tile) == type)
			return true;
	}
	return false;
}

static void _draw_layer(const Tiled2cMap *map, const Tiled2cLayer *this,
                        vec2 off) {
	assert(this != nullptr);
	TZR_DrawSetColor(.a = this->opacity);
	rfor(y, 0u, map->height) {
		const int dy = y * cfg.tile_height + off.y;
		rfor(x, 0u, map->width) {
			const int dx = x * cfg.tile_width + off.x;
			const auto tile =
			    this->tilelayer.data[x + y * map->width];
			tile_draw(tile, dx, dy);
		}
	}
	TZR_DrawSetColor(.a = 1);
}

void map_draw(Map *this, vec2 off) {
	assert(this != nullptr);
	rfor(i, 0u, this->t2c->numlayers) {
		const auto layer = &this->t2c->layers[i];
		assert(layer->type == 't');
		if (layer->visible && !map_object_layer(layer))
			_draw_layer(this->t2c, layer, off);
	}
}

bool map_object_layer(const Tiled2cLayer *layer) {
	assert(layer != nullptr);
	return (layer->type == 't' && layer->class && layer->class[0]);
}

vec_impl(Map, Map *);