diff --git a/CMakeLists.txt b/CMakeLists.txt index 642d3fa..6f5c3c7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,12 +14,15 @@ include_directories(inc) set(SOURCES src/main.c src/input.c + src/level.c ) set(LEVELS + lvl/test.kble ) set(ASSETS + res/tileset.png ${LEVELS} ) diff --git a/inc/conf.h b/inc/conf.h new file mode 100644 index 0000000..230cd9b --- /dev/null +++ b/inc/conf.h @@ -0,0 +1,3 @@ +#pragma once + +#define TILE_SIZE 16 diff --git a/inc/level.h b/inc/level.h new file mode 100644 index 0000000..3c0ce81 --- /dev/null +++ b/inc/level.h @@ -0,0 +1,19 @@ +#pragma once + +struct LevelBin { + unsigned char format; + unsigned char chunk_size; + unsigned short width; + unsigned short height; + unsigned char data[]; +} __attribute__((__packed__)); + +struct Level { + int width; + int height; + char *data; +}; + +struct Level level_load(struct LevelBin *restrict); +void level_free(struct Level *restrict); +void level_draw(struct Level *restrict); diff --git a/jle.ini b/jle.ini index b6c2cd2..dd20334 100644 --- a/jle.ini +++ b/jle.ini @@ -1,5 +1,5 @@ # https://git.sr.ht/~kikoodx/jle -tileset = assets/graphics/tileset.png +tileset = res/tileset.png tile_size = 16 new_level_width = 25 new_level_height = 14 diff --git a/lvl/fxconv-metadata.txt b/lvl/fxconv-metadata.txt new file mode 100644 index 0000000..7b61593 --- /dev/null +++ b/lvl/fxconv-metadata.txt @@ -0,0 +1,3 @@ +*.kble: + type: binary + name_regex: (.*)\.kble lvl_\1 diff --git a/lvl/test.kble b/lvl/test.kble new file mode 100644 index 0000000..5bc4bcd Binary files /dev/null and b/lvl/test.kble differ diff --git a/res/fxconv-metadata.txt b/res/fxconv-metadata.txt new file mode 100644 index 0000000..8134606 --- /dev/null +++ b/res/fxconv-metadata.txt @@ -0,0 +1,3 @@ +tileset.png: + type: bopti-image + name: bimg_tileset diff --git a/res/tileset.png b/res/tileset.png new file mode 100644 index 0000000..9dfce00 Binary files /dev/null and b/res/tileset.png differ diff --git a/src/level.c b/src/level.c new file mode 100644 index 0000000..180314e --- /dev/null +++ b/src/level.c @@ -0,0 +1,50 @@ +#include "level.h" +#include "conf.h" +#include +#include + +struct Level +level_load(struct LevelBin *restrict s) +{ + struct Level level; + int i = s->width * s->height; + level.width = s->width; + level.height = s->height; + level.data = malloc(i); + while (i-- > 0) { + level.data[i] = s->data[i]; + } + return level; +} + +void +level_free(struct Level *restrict s) +{ + free(s->data); +} + +void +level_draw(struct Level *restrict s) +{ + extern bopti_image_t bimg_tileset; + const int tileset_width = bimg_tileset.width / TILE_SIZE; + int i; + int x = 0; + int y = 0; + + for (i = 0; i < s->width * s->height; i++) { + const int sx = x * TILE_SIZE; + const int sy = y * TILE_SIZE; + const int tile = s->data[i]; + const int rx = tile % tileset_width * TILE_SIZE; + const int ry = tile / tileset_width * TILE_SIZE; + + dsubimage(sx, sy, &bimg_tileset, rx, ry, TILE_SIZE, TILE_SIZE, + DIMAGE_NONE); + + if (++x >= s->width) { + x = 0; + y++; + } + } +} diff --git a/src/main.c b/src/main.c index cccc79d..a2a49d1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,10 +1,30 @@ #include "input.h" +#include "level.h" +#include + +extern struct LevelBin lvl_test; +struct Level level; + +static void draw(void); int main(void) { input_init(); + level = level_load(&lvl_test); + + draw(); while (!input_pressed(K_EXIT)) input_update(); + + level_free(&level); return 1; } + +static void +draw(void) +{ + dclear(C_BLACK); + level_draw(&level); + dupdate(); +}