level module

This commit is contained in:
KikooDX 2021-11-10 00:37:45 +01:00
parent c333b69113
commit 0644801b73
10 changed files with 102 additions and 1 deletions

View File

@ -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}
)

3
inc/conf.h Normal file
View File

@ -0,0 +1,3 @@
#pragma once
#define TILE_SIZE 16

19
inc/level.h Normal file
View File

@ -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);

View File

@ -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

3
lvl/fxconv-metadata.txt Normal file
View File

@ -0,0 +1,3 @@
*.kble:
type: binary
name_regex: (.*)\.kble lvl_\1

BIN
lvl/test.kble Normal file

Binary file not shown.

3
res/fxconv-metadata.txt Normal file
View File

@ -0,0 +1,3 @@
tileset.png:
type: bopti-image
name: bimg_tileset

BIN
res/tileset.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

50
src/level.c Normal file
View File

@ -0,0 +1,50 @@
#include "level.h"
#include "conf.h"
#include <gint/display.h>
#include <stdlib.h>
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++;
}
}
}

View File

@ -1,10 +1,30 @@
#include "input.h"
#include "level.h"
#include <gint/display.h>
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();
}