various: sub-tile lighting; nicer visibility calculations

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I0f0a0c12db76cc8e0f4c8ccc72ca4b826a6a6964
This commit is contained in:
raf 2026-04-28 15:57:17 +03:00
commit 00b3798ae0
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF
8 changed files with 206 additions and 47 deletions

View file

@ -1,6 +1,7 @@
#include "render.h"
#include "items.h"
#include "settings.h"
#include "map/map.h"
#include "map/utils.h"
#include <math.h>
#include <stddef.h>
@ -69,10 +70,10 @@ void render_map(const Map *map, const Tileset *tileset) {
for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) {
Rectangle dst = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE};
int visible = map->visible[y][x];
int remembered = map->remembered[y][x];
int brightness = tile_brightness(map, x, y);
if (!visible && !remembered) {
if (!remembered) {
DrawRectangleRec(dst, (Color){5, 5, 10, 255});
continue;
}
@ -102,9 +103,12 @@ void render_map(const Map *map, const Tileset *tileset) {
if (tile_id >= 0 && tileset != NULL && tileset->finalized) {
Rectangle src = tileset_get_region(tileset, tile_id);
if (src.width > 0) {
Color tint = WHITE;
if (!visible) {
// Dim remembered tiles
Color tint;
if (brightness > 0) {
float lit = (float)brightness / 255.0f;
tint = (Color){(unsigned char)(128 + (int)(127.0f * lit)), (unsigned char)(128 + (int)(127.0f * lit)),
(unsigned char)(128 + (int)(127.0f * lit)), 255};
} else {
tint = (Color){128, 128, 128, 255};
}
DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, tint);
@ -112,11 +116,10 @@ void render_map(const Map *map, const Tileset *tileset) {
}
}
// Fallback to solid colors if tileset not available
Color wall_color = visible ? DARKGRAY : (Color){25, 25, 30, 255};
Color floor_color = visible ? BLACK : (Color){15, 15, 20, 255};
Color stairs_color = visible ? (Color){180, 160, 100, 255} : (Color){60, 55, 50, 255};
Color door_color = visible ? (Color){139, 119, 89, 255} : (Color){60, 55, 50, 255};
Color wall_color = brightness > 0 ? DARKGRAY : (Color){25, 25, 30, 255};
Color floor_color = brightness > 0 ? BLACK : (Color){15, 15, 20, 255};
Color stairs_color = brightness > 0 ? (Color){180, 160, 100, 255} : (Color){60, 55, 50, 255};
Color door_color = brightness > 0 ? (Color){139, 119, 89, 255} : (Color){60, 55, 50, 255};
switch (map->tiles[y][x]) {
case TILE_WALL:
@ -136,13 +139,13 @@ void render_map(const Map *map, const Tileset *tileset) {
}
}
}
if (is_adjacent_to_stairs && visible) {
if (is_adjacent_to_stairs && brightness > 0) {
int flicker = (int)(sinf(GetTime() * 5.0f) * 15.0f);
DrawRectangleRec(dst, (Color){40 + flicker, 25, 10, 60});
}
}
// Grid lines
if (DRAW_GRID_LINES && visible) {
if (DRAW_GRID_LINES && brightness > 0) {
DrawRectangleLines((int)dst.x, (int)dst.y, (int)dst.width, (int)dst.height, (Color){20, 20, 20, 80});
}
break;
@ -151,7 +154,7 @@ void render_map(const Map *map, const Tileset *tileset) {
// Make stairs very visible with bright symbol and bounce
{
int bounce = (int)(sinf(GetTime() * 3.0f) * 1.5f);
if (visible)
if (brightness > 0)
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 1 + bounce, NORM_FONT + 2, (Color){255, 255, 200, 255});
else
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 1 + bounce, NORM_FONT + 2, (Color){100, 90, 70, 255});
@ -159,7 +162,7 @@ void render_map(const Map *map, const Tileset *tileset) {
break;
case TILE_DOOR_CLOSED:
DrawRectangleRec(dst, door_color);
if (visible) {
if (brightness > 0) {
DrawRectangle(x * TILE_SIZE + 2, y * TILE_SIZE + 2, TILE_SIZE - 4, TILE_SIZE - 4, (Color){100, 80, 60, 255});
DrawRectangleLines(x * TILE_SIZE + 2, y * TILE_SIZE + 2, TILE_SIZE - 4, TILE_SIZE - 4,
(Color){60, 50, 40, 255});
@ -168,7 +171,7 @@ void render_map(const Map *map, const Tileset *tileset) {
break;
case TILE_DOOR_OPEN:
DrawRectangleRec(dst, floor_color);
if (visible) {
if (brightness > 0) {
DrawRectangle(x * TILE_SIZE + 2, y * TILE_SIZE + 2, TILE_SIZE - 4, TILE_SIZE - 4, (Color){80, 70, 50, 180});
DrawRectangleLines(x * TILE_SIZE + 2, y * TILE_SIZE + 2, TILE_SIZE - 4, TILE_SIZE - 4,
(Color){60, 50, 40, 200});
@ -177,7 +180,7 @@ void render_map(const Map *map, const Tileset *tileset) {
break;
case TILE_DOOR_RUINED:
DrawRectangleRec(dst, (Color){60, 45, 30, 255});
if (visible) {
if (brightness > 0) {
DrawRectangle(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2, (Color){80, 60, 40, 200});
DrawLine(x * TILE_SIZE + 2, y * TILE_SIZE + 2, x * TILE_SIZE + TILE_SIZE - 2, y * TILE_SIZE + TILE_SIZE - 2,
(Color){120, 90, 60, 255});
@ -260,12 +263,11 @@ void render_player(const Player *p, const Tileset *tileset, int frame_counter) {
}
}
void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH],
const Tileset *tileset, int frame_counter) {
void render_enemies(const Enemy *enemies, int count, const Map *map, const Tileset *tileset, int frame_counter) {
for (int i = 0; i < count; i++) {
if (!enemies[i].alive)
continue;
if (!visible[enemies[i].position.y][enemies[i].position.x])
if (!is_tile_revealed(map, enemies[i].position.x, enemies[i].position.y))
continue;
Rectangle dst = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE),
@ -367,12 +369,11 @@ void render_enemies(const Enemy *enemies, int count, const unsigned char visible
}
}
void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH],
const Tileset *tileset) {
void render_items(const Item *items, int count, const Map *map, const Tileset *tileset) {
for (int i = 0; i < count; i++) {
if (items[i].picked_up)
continue;
if (!visible[items[i].y][items[i].x])
if (!is_tile_revealed(map, items[i].x, items[i].y))
continue;
Rectangle dst = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE,