draw level

This commit is contained in:
KikooDX 2021-12-16 15:57:50 +01:00
parent 7157b827c5
commit a3c7245dd0
6 changed files with 58 additions and 0 deletions

View File

@ -18,6 +18,7 @@ set(SOURCES
)
set(ASSETS
res/tileset.png
res/test.kble
)

View File

@ -1,4 +1,5 @@
#pragma once
#include "visual_data.h"
#include <stdint.h>
struct LevelBin {
@ -11,6 +12,7 @@ struct Level {
int width, height, size;
const struct LevelBin *bin;
uint8_t *data;
struct VisualData *visual_data;
};
void level_init(void);
@ -18,3 +20,5 @@ void level_deinit(void);
void level_load(const struct LevelBin *);
void level_reload(void);
void level_regen_visual_data(void);
void level_draw(void);

5
inc/visual_data.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
struct VisualData {
int x, y, img_x, img_y, visible;
};

View File

@ -1,3 +1,6 @@
*.png:
type: bopti-image
name_regex: (.*)\.png bimg_\1
*.kble:
type: binary
name_regex: (.*)\.kble kble_\1

View File

@ -1,7 +1,11 @@
#include "level.h"
#include "conf.h"
#include "visual_data.h"
#include <gint/display.h>
#include <stdlib.h>
static struct Level level;
extern bopti_image_t bimg_tileset;
static void level_free(void);
@ -9,6 +13,7 @@ void
level_init(void)
{
level.data = NULL;
level.visual_data = NULL;
}
void
@ -26,9 +31,11 @@ level_load(const struct LevelBin *b)
level.height = b->height;
level.size = i;
level.data = malloc(i);
level.visual_data = malloc(i * sizeof(struct VisualData));
level.bin = b;
while (i-- > 0)
level.data[i] = b->data[i];
level_regen_visual_data();
}
void
@ -37,6 +44,39 @@ level_reload(void)
level_load(level.bin);
}
void
level_regen_visual_data(void)
{
const int tileset_width = bimg_tileset.width / TILE_SIZE;
int y = level.height;
while (y-- > 0) {
int x = level.width;
while (x-- > 0) {
const int i = x + y * level.width;
const int tile = level.data[i];
struct VisualData *const vd = &level.visual_data[i];
vd->x = x * TILE_SIZE;
vd->y = y * TILE_SIZE;
vd->img_x = tile % tileset_width * TILE_SIZE;
vd->img_y = (int)(tile / tileset_width) * TILE_SIZE;
vd->visible = tile != 0;
}
}
}
void
level_draw(void)
{
int i = level.size;
while (i-- > 0) {
const struct VisualData *const vd = &level.visual_data[i];
if (vd->visible) {
dsubimage(vd->x, vd->y, &bimg_tileset, vd->img_x,
vd->img_y, TILE_SIZE, TILE_SIZE, 0);
}
}
}
static void
level_free(void)
{
@ -44,4 +84,8 @@ level_free(void)
free(level.data);
level.data = NULL;
}
if (level.visual_data != NULL) {
free(level.visual_data);
level.visual_data = NULL;
}
}

View File

@ -53,6 +53,7 @@ static void
draw(void)
{
dclear(C_BLACK);
level_draw();
player_draw(&player);
dupdate();
}