summaryrefslogtreecommitdiff
path: root/src/level.c
blob: 12599227d56850e6561f57c3ee286f5db460c0f7 (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
#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 / width * TILE_SIZE;
			return;
		}
	}
}

void level_draw(void)
{
	for (int y = 0; y < height; y++) {
		for (int x = 0; x < width; x++) {
			const int t = data[x + y * width];
			if (t && t != 2)
				LZY_DrawTile(t, x * TILE_SIZE, y * TILE_SIZE);
		}
	}
}