diff options
Diffstat (limited to 'src/level.c')
-rw-r--r-- | src/level.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/level.c b/src/level.c new file mode 100644 index 0000000..72e60d1 --- /dev/null +++ b/src/level.c @@ -0,0 +1,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; + } + } +} |