momento/src/level/draw.c

63 lines
1.4 KiB
C

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
#include "conf.h"
#include "level.h"
#include "tiles.h"
#include <libimg.h>
extern struct Level level;
extern img_t const img_tileset;
static int autotile_value(int x, int y);
void level_draw(void)
{
int x;
int y;
const int tileset_width = img_tileset.width / TILE_WIDTH;
y = level.height;
while (y-- > 0) {
const int draw_y = y * TILE_HEIGHT;
x = level.width;
while (x-- > 0) {
const int draw_x = x * TILE_WIDTH;
const Tile tile =
level.data[x + y * level.width];
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,
TILE_HEIGHT, TILE_WIDTH,
TILE_HEIGHT),
draw_x, draw_y);
} break;
default:
img_render_vram(
img_sub(
img_tileset,
(int)(tile % tileset_width) *
TILE_WIDTH,
(int)(tile / tileset_width) *
TILE_HEIGHT,
TILE_WIDTH, TILE_HEIGHT),
draw_x, draw_y);
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));
}