mirror of https://git.sr.ht/~kikoodx/momento
Tileset rendering optimisations
This commit is contained in:
parent
5479316478
commit
9d9339e921
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue