summaryrefslogtreecommitdiff
path: root/src/level.c
blob: 72e60d1010151155e6dc99fc92e98a1c76d47d10 (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
#include "level.h"
#include "conf.h"
#include "levels_bin.h"
#include "lzy.h"
#include "player.h"
#include <stdint.h>
#include <stdlib.h>

static int width, height, id;
static uint8_t *data = NULL;

void level_deinit(void)
{
	if (data != NULL) {
		free(data);
		data = NULL;
	}
}

void level_load(int nid)
{
	const uint8_t *const s = levels[nid].data;

	width = s[3];
	height = s[5];
	data = calloc(width * height, sizeof(uint8_t));
	id = nid;

	for (int i = 0; i < width * height; i++)
		data[i] = s[6 + i];

	int px = 0, py = 0;
	level_find(2, &px, &py);
	player_init(px, py);
}

void level_find(int tile, int *x, int *y)
{
	for (int i = 0; i < width * height; i++) {
		if (data[i] == tile) {
			if (x != NULL)
				*x = i % width * TILE_SIZE;
			if (y != NULL)
				*y = i % height * TILE_SIZE;
			return;
		}
	}
}