2021-03-28 19:35:07 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
|
|
/* Copyright (C) 2021 KikooDX */
|
|
|
|
|
|
|
|
#include "conf.h"
|
|
|
|
#include "level.h"
|
2021-04-08 18:04:24 +02:00
|
|
|
#include "tiles.h"
|
2021-04-07 11:45:47 +02:00
|
|
|
#include <libimg.h>
|
2021-03-28 19:35:07 +02:00
|
|
|
|
|
|
|
extern struct Level level;
|
2021-04-07 11:45:47 +02:00
|
|
|
extern img_t const img_tileset;
|
2021-03-28 19:35:07 +02:00
|
|
|
|
2021-04-08 18:04:24 +02:00
|
|
|
static int autotile_value(int x, int y);
|
|
|
|
|
2021-03-28 19:35:07 +02:00
|
|
|
void level_draw(void)
|
|
|
|
{
|
|
|
|
int x;
|
|
|
|
int y;
|
2021-04-07 11:45:47 +02:00
|
|
|
const int tileset_width = img_tileset.width / TILE_WIDTH;
|
2021-04-08 00:21:26 +02:00
|
|
|
|
|
|
|
y = level.height;
|
2021-04-08 00:38:40 +02:00
|
|
|
while (y-- > 0) {
|
2021-04-08 15:34:16 +02:00
|
|
|
const int draw_y = y * TILE_HEIGHT;
|
2021-04-08 00:21:26 +02:00
|
|
|
x = level.width;
|
2021-04-08 00:38:40 +02:00
|
|
|
while (x-- > 0) {
|
2021-03-28 19:35:07 +02:00
|
|
|
const int draw_x = x * TILE_WIDTH;
|
|
|
|
const Tile tile =
|
|
|
|
level.data[x + y * level.width];
|
2021-04-08 18:04:24 +02:00
|
|
|
switch (tile) {
|
2021-04-09 00:31:24 +02:00
|
|
|
case TILE_VOID:
|
|
|
|
break;
|
2021-04-08 18:04:24 +02:00
|
|
|
case TILE_SOLID: {
|
2021-04-09 00:31:24 +02:00
|
|
|
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;
|
2021-04-08 18:04:24 +02:00
|
|
|
}
|
2021-03-28 19:35:07 +02:00
|
|
|
}
|
2021-04-08 00:21:26 +02:00
|
|
|
}
|
2021-03-28 19:35:07 +02:00
|
|
|
}
|
2021-04-08 18:04:24 +02:00
|
|
|
|
2021-04-09 00:31:24 +02:00
|
|
|
static int autotile_value(int x, int y)
|
|
|
|
{
|
2021-04-08 18:04:24 +02:00
|
|
|
return ((level_get_tile(x - 1, y) == TILE_SOLID) |
|
2021-04-09 00:31:24 +02:00
|
|
|
((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));
|
2021-04-08 18:04:24 +02:00
|
|
|
}
|