wehfou/src/level.c

60 lines
1.0 KiB
C
Raw Normal View History

2022-04-01 18:31:34 +02:00
#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)
2022-04-01 18:41:25 +02:00
*y = i / width * TILE_SIZE;
2022-04-01 18:31:34 +02:00
return;
}
}
}
2022-04-01 18:41:25 +02:00
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);
}
}
}