Tileset rendering optimisations

This commit is contained in:
KikooDX 2021-04-09 15:35:31 +02:00
parent 5479316478
commit 9d9339e921
4 changed files with 38 additions and 27 deletions

View File

@ -1,6 +1,6 @@
tileset.png:
type: libimg-image
name: img_tileset
type: bopti-image
name: bimg_tileset
player.png:
type: libimg-image
name: img_player

View File

@ -11,6 +11,7 @@ typedef unsigned int Tile;
struct Level {
Tile data[LEVEL_WIDTH * LEVEL_HEIGHT];
int autotiling[LEVEL_WIDTH * LEVEL_HEIGHT];
int width;
int height;
int gold;

View File

@ -4,18 +4,16 @@
#include "conf.h"
#include "level.h"
#include "tiles.h"
#include <libimg.h>
#include <gint/display.h>
extern struct Level level;
extern img_t const img_tileset;
static int autotile_value(int x, int y);
extern const bopti_image_t bimg_tileset;
void level_draw(void)
{
int x;
int y;
const int tileset_width = img_tileset.width / TILE_WIDTH;
const int tileset_width = bimg_tileset.width / TILE_WIDTH;
y = level.height;
while (y-- > 0) {
@ -23,40 +21,30 @@ void level_draw(void)
x = level.width;
while (x-- > 0) {
const int draw_x = x * TILE_WIDTH;
const int tile_index = x + y * level.width;
const Tile tile =
level.data[x + y * level.width];
level.data[tile_index];
switch (tile) {
case TILE_VOID:
break;
case TILE_SOLID: {
const int autotile =
autotile_value(x, y) * TILE_WIDTH;
img_render_vram(
img_sub(img_tileset, autotile,
level.autotiling[tile_index];
dsubimage(draw_x, draw_y,
&bimg_tileset, autotile,
TILE_HEIGHT, TILE_WIDTH,
TILE_HEIGHT),
draw_x, draw_y);
TILE_HEIGHT, DIMAGE_NOCLIP);
} break;
default:
img_render_vram(
img_sub(
img_tileset,
dsubimage(draw_x, draw_y,
&bimg_tileset,
(int)(tile % tileset_width) *
TILE_WIDTH,
(int)(tile / tileset_width) *
TILE_HEIGHT,
TILE_WIDTH, TILE_HEIGHT),
draw_x, draw_y);
TILE_HEIGHT, TILE_WIDTH,
TILE_HEIGHT, DIMAGE_NOCLIP);
break;
}
}
}
}
static int autotile_value(int x, int y)
{
return ((level_get_tile(x - 1, y) == TILE_SOLID) |
((level_get_tile(x + 1, y) == TILE_SOLID) << 1) |
((level_get_tile(x, y - 1) == TILE_SOLID) << 2) |
((level_get_tile(x, y + 1) == TILE_SOLID) << 3));
}

View File

@ -1,6 +1,7 @@
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "filepaths.h"
#include "level.h"
#include "tiles.h"
@ -24,6 +25,7 @@ int debug_value = 0;
static int read_byte(int file);
static Tile read_merge_bytes(int file, int size);
static int autotile_value(int x, int y);
/* Load level from storage memory. */
void level_load(void)
@ -33,6 +35,8 @@ void level_load(void)
int level_size;
int file_size;
int i;
int x;
int y;
char byte;
Tile tile;
@ -89,6 +93,16 @@ void level_load(void)
/* set level id for display */
level.id = level_id;
/* compute autotiling */
y = level.height;
while (y-- > 0) {
x = level.width;
while (x-- > 0) {
level.autotiling[x + y * level.width] =
autotile_value(x, y) * TILE_WIDTH;
}
}
/* close file */
file = BFile_Close(file);
assert(file >= 0, "file closure failed miserably");
@ -123,3 +137,11 @@ static Tile read_merge_bytes(int file, int size)
}
return merged;
}
static int autotile_value(int x, int y)
{
return ((level_get_tile(x - 1, y) == TILE_SOLID) |
((level_get_tile(x + 1, y) == TILE_SOLID) << 1) |
((level_get_tile(x, y - 1) == TILE_SOLID) << 2) |
((level_get_tile(x, y + 1) == TILE_SOLID) << 3));
}