summaryrefslogtreecommitdiff
path: root/src/map.c
blob: 05c4862a9634acc67a19eccfec93ab7acdec9ab6 (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
#include "map.h"
#include "cfg.h"
#include "TZR.h"
#include "tiled2c.h"
#include "tileset.h"
#include "camera.h"
#include <stddef.h>

extern const Tiled2cMap embed_game_tmj;
extern const Tiled2cMap embed_game2_tmj;
extern const Tiled2cMap embed_game3_tmj;
extern const Tiled2cMap embed_game4_tmj;

static const Tiled2cMap *const maps[] = {
	&embed_game_tmj,
	&embed_game2_tmj,
	NULL
};
static unsigned int map_id;

void
map_reset(void)
{
	map_id = 0;
}

void
map_next(void)
{
	map_id += (maps[map_id + 1] != NULL);
}

void
map_previous(void)
{
	if (map_id > 0)
		map_id -= 1;
}

int
map_width(void)
{
	return maps[map_id]->width;
}

int
map_height(void)
{
	return maps[map_id]->height;
}

bool
map_get(int x, int y, unsigned int type)
{
	type &= ~TILED_TRANSFORMS;
	const Tiled2cMap *const map = maps[map_id];
	if (x >= map_width() || y < 0 || y >= map_height())
		return 0;
	if (x < 0)
		return (type != TILE_SPIKE);
	if (map->numlayers < 1)
		return (type != TILE_SPIKE);
	for (unsigned int i = 0; i < map->numlayers; i++) {
		const unsigned int tile = map->layers[i]
		          .data[x + y * map_width()];
		if (tile_type(tile) == type)
			return true;
	}
	return false;
}

bool
map_get_px(int x, int y, unsigned int type)
{
	if (y < 0)
		return 0;
	if (x < 0)
		return (type != TILE_SPIKE);
	return map_get(x / TSIZE, y / TSIZE, type);
}

static void
draw_layer(const Tiled2cLayer *layer)
{
	extern const PxSpr spr_tset;
	const int tset_w = spr_tset.w;
	for (int y = 0; y < map_height(); y++)
		for (int x = 0; x < map_width(); x++) {
			const unsigned tile =
			  layer->data[x + y * map_width()];
			if (tile == 0)
				continue;
			const unsigned vtile = tile_visual(tile);
			const int dx = camera_x(layer->parallaxx) + x * TSIZE;
			const int dy = camera_y(layer->parallaxy) + y * TSIZE;
			const int ix = vtile % (tset_w / TSIZE) * TSIZE;
			const int iy = vtile / (tset_w / TSIZE) * TSIZE;
			pxSpr(&spr_tset, dx, dy, ix, iy, TSIZE, TSIZE);
		}
}

void
map_draw(void)
{
	const Tiled2cMap *const map = maps[map_id];
	TZR_DrawSetColor(1, 1, 1);
	for (unsigned int i = 0; i < map->numlayers; i++)
		if (map->layers[i].visible && map->layers[i].parallaxx <= 1.0)
			draw_layer(&map->layers[i]);
}

void
map_draw_foreground(void)
{
	const Tiled2cMap *const map = maps[map_id];
	TZR_DrawSetColor(1, 1, 1);
	for (unsigned int i = 0; i < map->numlayers; i++)
		if (map->layers[i].visible && map->layers[i].parallaxx > 1.0)
			draw_layer(&map->layers[i]);
}

const Tiled2cObject *
map_objects(unsigned int *size)
{
	if (size != NULL)
		*size = maps[map_id]->numobjects;
	return maps[map_id]->objects;
}