rogged/src/render.c
NotAShelf bb10fb88f0
various: upgrade Zig version
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ia97044bd7d44c776f217f2223e35ae3b6a6a6964
2026-06-11 10:39:30 +03:00

1325 lines
52 KiB
C

#include "render.h"
#include "items.h"
#include "settings.h"
#include "map/map.h"
#include "map/utils.h"
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
Font load_font_with_fallback(const char *path, int font_size, const char *fallback_path) {
if (path == NULL)
goto try_fallback;
Font f = LoadFontEx(path, font_size, NULL, 0);
if (f.texture.id != 0)
return f;
try_fallback:
if (fallback_path != NULL) {
Font fb = LoadFontEx(fallback_path, font_size, NULL, 0);
if (fb.texture.id != 0)
return fb;
}
Font none = {0};
return none;
}
int init_fonts(FontManager *fm) {
if (fm == NULL)
return 0;
fm->title_font =
load_font_with_fallback("./assets/fonts/Royal_Decree_Bold.ttf", 36, "./assets/fonts/spartan_500.ttf");
fm->hud_font = load_font_with_fallback("./assets/fonts/Tomorrow_Night.ttf", 36, "./assets/fonts/spartan_500.ttf");
fm->body_font = load_font_with_fallback("./assets/fonts/spartan_500.ttf", 36, NULL);
fm->inv_font = load_font_with_fallback("./assets/fonts/Royal_Decree.ttf", 36, "./assets/fonts/spartan_500.ttf");
return fm->title_font.texture.id != 0 && fm->hud_font.texture.id != 0 && fm->body_font.texture.id != 0 &&
fm->inv_font.texture.id != 0;
}
void destroy_fonts(FontManager *fm) {
if (fm == NULL)
return;
if (fm->title_font.texture.id != 0)
UnloadFont(fm->title_font);
if (fm->hud_font.texture.id != 0)
UnloadFont(fm->hud_font);
if (fm->body_font.texture.id != 0)
UnloadFont(fm->body_font);
if (fm->inv_font.texture.id != 0)
UnloadFont(fm->inv_font);
memset(fm, 0, sizeof(FontManager));
}
static void draw_text_hud(Font f, const char *text, float x, float y, int size, float spacing, Color c) {
if (f.texture.id == 0)
return;
DrawTextEx(f, text, (Vector2){x, y}, (float)size, spacing, c);
}
static void draw_text_body(Font f, const char *text, float x, float y, int size, float spacing, Color c) {
if (f.texture.id == 0)
return;
DrawTextEx(f, text, (Vector2){x, y}, (float)size, spacing, c);
}
static int is_deep_corridor(const Map *map, int x, int y) {
if (map->tiles[y][x] != TILE_FLOOR)
return 0;
int walls_ns = (y > 0 && map->tiles[y - 1][x] == TILE_WALL ? 1 : 0) +
(y < MAP_HEIGHT - 1 && map->tiles[y + 1][x] == TILE_WALL ? 1 : 0);
int walls_ew = (x > 0 && map->tiles[y][x - 1] == TILE_WALL ? 1 : 0) +
(x < MAP_WIDTH - 1 && map->tiles[y][x + 1] == TILE_WALL ? 1 : 0);
int is_corridor = (walls_ns >= 2 && walls_ew == 0) || (walls_ew >= 2 && walls_ns == 0);
if (!is_corridor)
return 0;
// A "deep" corridor is surrounded by corridor on all floor sides.
// If any floor neighbor is NOT a corridor, this is an entrance/exit.
const int dx4[4] = {0, 0, 1, -1};
const int dy4[4] = {1, -1, 0, 0};
for (int i = 0; i < 4; i++) {
int nx = x + dx4[i];
int ny = y + dy4[i];
if (nx < 0 || nx >= MAP_WIDTH || ny < 0 || ny >= MAP_HEIGHT)
continue;
if (map->tiles[ny][nx] != TILE_FLOOR)
continue;
int nw_ns = (ny > 0 && map->tiles[ny - 1][nx] == TILE_WALL ? 1 : 0) +
(ny < MAP_HEIGHT - 1 && map->tiles[ny + 1][nx] == TILE_WALL ? 1 : 0);
int nw_ew = (nx > 0 && map->tiles[ny][nx - 1] == TILE_WALL ? 1 : 0) +
(nx < MAP_WIDTH - 1 && map->tiles[ny][nx + 1] == TILE_WALL ? 1 : 0);
int neighbor_is_corridor = (nw_ns >= 2 && nw_ew == 0) || (nw_ew >= 2 && nw_ns == 0);
if (!neighbor_is_corridor)
return 0;
}
return 1;
}
static Color color_lerp(Color a, Color b, float t) {
return (Color){(unsigned char)(a.r + (int)((b.r - a.r) * t)), (unsigned char)(a.g + (int)((b.g - a.g) * t)),
(unsigned char)(a.b + (int)((b.b - a.b) * t)), (unsigned char)(a.a + (int)((b.a - a.a) * t))};
}
static float light_factor_from_brightness(int brightness) {
float base_light = brightness > 0 ? AMBIENT_LIGHT_FACTOR : REMEMBERED_LIGHT_FACTOR;
return base_light + (1.0f - base_light) * powf((float)brightness / 255.0f, LIGHT_EXPONENT);
}
static int sample_light(const Map *map, int tx, int ty, int sx, int sy) {
int base_x = tx * SUB_TILE_RES + sx;
int base_y = ty * SUB_TILE_RES + sy;
int sum = 0;
int count = 0;
for (int dy = -1; dy <= 1; dy++) {
for (int dx = -1; dx <= 1; dx++) {
int x = base_x + dx;
int y = base_y + dy;
if (x < 0 || y < 0 || x >= MAP_WIDTH * SUB_TILE_RES || y >= MAP_HEIGHT * SUB_TILE_RES)
continue;
sum += map->light_map[y][x];
count++;
}
}
return count > 0 ? sum / count : 0;
}
static Color tint_for_light(const Map *map, int tx, int ty, int sx, int sy, int is_opaque, float dim) {
float factor = light_factor_from_brightness(sample_light(map, tx, ty, sx, sy)) * dim;
if (is_opaque)
factor = AMBIENT_LIGHT_FACTOR + 0.92f * factor;
int value = (int)(255.0f * factor);
if (value > 255)
value = 255;
return (Color){(unsigned char)value, (unsigned char)value, (unsigned char)value, 255};
}
static void draw_lit_texture_tile(const Map *map, const Tileset *tileset, Rectangle src, Rectangle dst, int tx, int ty,
int is_opaque, float dim) {
const int parts = 4;
float src_w = src.width / (float)parts;
float src_h = src.height / (float)parts;
float dst_w = dst.width / (float)parts;
float dst_h = dst.height / (float)parts;
for (int py = 0; py < parts; py++) {
for (int px = 0; px < parts; px++) {
int sx = (px * SUB_TILE_RES) / parts + SUB_TILE_RES / (parts * 2);
int sy = (py * SUB_TILE_RES) / parts + SUB_TILE_RES / (parts * 2);
Rectangle s = {src.x + src_w * px, src.y + src_h * py, src_w, src_h};
Rectangle d = {dst.x + dst_w * px, dst.y + dst_h * py, dst_w + 0.5f, dst_h + 0.5f};
DrawTexturePro(tileset->atlas, s, d, (Vector2){0, 0}, 0.0f, tint_for_light(map, tx, ty, sx, sy, is_opaque, dim));
}
}
}
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 remembered = map->remembered[y][x];
int brightness = tile_brightness(map, x, y);
if (!remembered) {
DrawRectangleRec(dst, (Color){5, 5, 10, 255});
continue;
}
float light_factor = light_factor_from_brightness(brightness);
if (is_deep_corridor(map, x, y))
light_factor *= 0.82f;
int tile_id = -1;
switch (map->tiles[y][x]) {
case TILE_WALL:
tile_id = TILE_WALL_0 + ((x * 7 + y * 13) % 2);
break;
case TILE_FLOOR:
tile_id = TILE_FLOOR_0 + ((x * 7 + y * 13) % 4);
break;
case TILE_STAIRS:
tile_id = TILE_STAIRS_SPRITE;
break;
case TILE_DOOR_CLOSED:
tile_id = TILE_DOOR_CLOSED_SPRITE;
break;
case TILE_DOOR_OPEN:
tile_id = TILE_DOOR_OPEN_SPRITE;
break;
case TILE_DOOR_RUINED:
tile_id = TILE_DOOR_OPEN_SPRITE;
break;
case TILE_RUBBLE:
case TILE_SHALLOW_WATER:
tile_id = TILE_FLOOR_0 + ((x * 7 + y * 13) % 4);
break;
case TILE_STATUE:
tile_id = TILE_WALL_0 + ((x * 7 + y * 13) % 2);
break;
}
int is_door = (map->tiles[y][x] == TILE_DOOR_CLOSED || map->tiles[y][x] == TILE_DOOR_OPEN);
int tile_drawn = 0;
int decor_base_drawn = 0;
// Draw floor underneath doors using tileset if available, so the
// floor matches adjacent tiles
if (is_door && tileset != NULL && tileset->finalized) {
int floor_id = TILE_FLOOR_0 + ((x * 7 + y * 13) % 4);
Rectangle floor_src = tileset_get_region(tileset, floor_id);
if (floor_src.width > 0) {
draw_lit_texture_tile(map, tileset, floor_src, dst, x, y, 0, 1.0f);
tile_drawn = 1;
}
}
int is_decor =
map->tiles[y][x] == TILE_RUBBLE || map->tiles[y][x] == TILE_SHALLOW_WATER || map->tiles[y][x] == TILE_STATUE;
if (tile_id >= 0 && tileset != NULL && tileset->finalized && !is_door && !is_decor) {
Rectangle src = tileset_get_region(tileset, tile_id);
if (src.width > 0) {
int is_opaque = (map->tiles[y][x] == TILE_WALL || map->tiles[y][x] == TILE_STATUE);
draw_lit_texture_tile(map, tileset, src, dst, x, y, is_opaque, is_deep_corridor(map, x, y) ? 0.82f : 1.0f);
tile_drawn = 1;
}
}
if (is_decor && tileset != NULL && tileset->finalized) {
int base_id = TILE_FLOOR_0 + ((x * 7 + y * 13) % 4);
Rectangle src = tileset_get_region(tileset, base_id);
if (src.width > 0) {
draw_lit_texture_tile(map, tileset, src, dst, x, y, 0, 1.0f);
decor_base_drawn = 1;
}
}
if (!tile_drawn || is_door) {
Color wall_color = color_lerp((Color){42, 42, 52, 255}, DARKGRAY, light_factor);
Color floor_color = color_lerp((Color){32, 32, 42, 255}, BLACK, light_factor);
Color stairs_color = color_lerp((Color){85, 80, 70, 255}, (Color){180, 160, 100, 255}, light_factor);
Color door_color = color_lerp((Color){38, 34, 30, 255}, (Color){120, 92, 58, 255}, light_factor);
Color door_handle_color = color_lerp((Color){42, 38, 32, 255}, (Color){145, 122, 82, 255}, light_factor);
int is_currently_lit = brightness > 0;
if (is_door && !is_currently_lit) {
door_color = (Color){30, 27, 24, 255};
door_handle_color = (Color){34, 31, 27, 255};
}
switch (map->tiles[y][x]) {
case TILE_WALL:
DrawRectangleRec(dst, wall_color);
break;
case TILE_FLOOR:
DrawRectangleRec(dst, floor_color);
// Grid lines
if (DRAW_GRID_LINES && light_factor > 0.05f) {
DrawRectangleLines((int)dst.x, (int)dst.y, (int)dst.width, (int)dst.height, (Color){20, 20, 20, 80});
}
break;
case TILE_STAIRS:
DrawRectangleRec(dst, stairs_color);
// Make stairs very visible with bright symbol and bounce
{
int bounce = (int)(sinf(GetTime() * 3.0f) * 1.5f);
if (light_factor > 0.05f)
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});
}
break;
case TILE_DOOR_CLOSED:
case TILE_DOOR_OPEN: {
// Determine door orientation by adjacent walls
int wall_n = (y > 0 && map->tiles[y - 1][x] == TILE_WALL);
int wall_s = (y < MAP_HEIGHT - 1 && map->tiles[y + 1][x] == TILE_WALL);
int wall_e = (x < MAP_WIDTH - 1 && map->tiles[y][x + 1] == TILE_WALL);
int wall_w = (x > 0 && map->tiles[y][x - 1] == TILE_WALL);
int is_vertical = (wall_n && wall_s) || (!(wall_e && wall_w) && (wall_e || wall_w));
// Animation progress: 0=fully closed, 1=fully open
float t = 0.0f;
if (map->door_anim_timer[y][x] > 0) {
float progress = 1.0f - (float)map->door_anim_timer[y][x] / DOOR_ANIM_FRAMES;
t = map->door_anim_target[y][x] ? progress : (1.0f - progress);
} else {
t = (map->tiles[y][x] == TILE_DOOR_OPEN) ? 1.0f : 0.0f;
}
if (!tile_drawn)
DrawRectangleRec(dst, floor_color);
if (is_vertical) {
// Vertical door: swings left/right toward approaching wall
int open_dir = map->door_open_from[y][x];
int swing_to_left = 0;
if (open_dir == 2)
swing_to_left = 1; // approached from E, swing W (left)
else if (open_dir == 3)
swing_to_left = 0; // approached from W, swing E (right)
else
swing_to_left = (x % 2 == 0); // deterministic fallback
int closed_x = x * TILE_SIZE + 7; // center
int open_x = swing_to_left ? x * TILE_SIZE + 1 : x * TILE_SIZE + 13;
int px = (int)(closed_x + (open_x - closed_x) * t);
int alpha = (int)(255 * (1.0f - t * 0.45f));
if (!is_currently_lit)
alpha = (int)(alpha * 0.55f);
int width = (int)(2 + (1 - t)); // 2px closed, 1px open
Color panel_color = door_color;
panel_color.a = alpha;
DrawRectangle(px, y * TILE_SIZE + 1, width, TILE_SIZE - 2, panel_color);
if (t < 0.5f && is_currently_lit) {
int hx = px + (swing_to_left ? -1 : width + 1);
Color handle_color = door_handle_color;
handle_color.a = alpha;
DrawRectangle(hx, y * TILE_SIZE + 6, 1, 2, handle_color);
}
} else {
// Horizontal door: swings up/down toward approaching wall
int open_dir = map->door_open_from[y][x];
int swing_up = 0;
if (open_dir == 0)
swing_up = 1; // approached from N, swing S (down)
else if (open_dir == 1)
swing_up = 0; // approached from S, swing N (up)
else
swing_up = (y % 2 == 0); // deterministic fallback
int closed_y = y * TILE_SIZE + 7; // center
int open_y = swing_up ? y * TILE_SIZE + 1 : y * TILE_SIZE + 13;
int py = (int)(closed_y + (open_y - closed_y) * t);
int alpha = (int)(255 * (1.0f - t * 0.45f));
if (!is_currently_lit)
alpha = (int)(alpha * 0.55f);
int height = (int)(2 + (1 - t)); // 2px closed, 1px open
Color panel_color = door_color;
panel_color.a = alpha;
DrawRectangle(x * TILE_SIZE + 1, py, TILE_SIZE - 2, height, panel_color);
if (t < 0.5f && is_currently_lit) {
int hy = py + (swing_up ? -1 : height + 1);
Color handle_color = door_handle_color;
handle_color.a = alpha;
DrawRectangle(x * TILE_SIZE + 6, hy, 2, 1, handle_color);
}
}
break;
}
case TILE_DOOR_RUINED:
DrawRectangleRec(dst, (Color){60, 45, 30, 255});
if (light_factor > 0.05f) {
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});
DrawLine(x * TILE_SIZE + TILE_SIZE - 2, y * TILE_SIZE + 2, x * TILE_SIZE + 2, y * TILE_SIZE + TILE_SIZE - 2,
(Color){120, 90, 60, 255});
}
break;
case TILE_RUBBLE:
if (!decor_base_drawn)
DrawRectangleRec(dst, floor_color);
if (light_factor > 0.05f) {
Color stone = color_lerp((Color){54, 50, 46, 255}, (Color){126, 118, 102, 255}, light_factor);
Color light = color_lerp((Color){72, 68, 60, 255}, (Color){166, 156, 132, 255}, light_factor);
Color dark = color_lerp((Color){22, 20, 19, 255}, (Color){67, 60, 52, 255}, light_factor);
int ox = x * TILE_SIZE;
int oy = y * TILE_SIZE;
DrawRectangle(ox + 3, oy + 12, 10, 2, (Color){0, 0, 0, 55});
DrawRectangle(ox + 5, oy + 7, 5, 5, dark);
DrawRectangle(ox + 6, oy + 6, 5, 5, stone);
DrawLine(ox + 6, oy + 6, ox + 10, oy + 6, light);
DrawRectangle(ox + 2, oy + 10, 5, 3, dark);
DrawRectangle(ox + 3, oy + 9, 5, 3, stone);
DrawPixel(ox + 4, oy + 9, light);
DrawRectangle(ox + 10, oy + 10, 4, 3, dark);
DrawRectangle(ox + 10, oy + 9, 4, 3, stone);
DrawPixel(ox + 12, oy + 9, light);
DrawPixel(ox + 5, oy + 13, stone);
DrawPixel(ox + 12, oy + 13, stone);
}
break;
case TILE_SHALLOW_WATER:
if (!decor_base_drawn)
DrawRectangleRec(dst, floor_color);
if (light_factor > 0.05f) {
Color water = color_lerp((Color){12, 30, 39, 165}, (Color){36, 94, 116, 175}, light_factor);
Color edge = color_lerp((Color){7, 18, 24, 170}, (Color){23, 62, 76, 180}, light_factor);
Color glint = color_lerp((Color){48, 91, 105, 130}, (Color){128, 184, 196, 155}, light_factor);
int shimmer = (int)(sinf(GetTime() * 2.0f + (float)(x + y)) * 10.0f);
int ox = x * TILE_SIZE;
int oy = y * TILE_SIZE;
DrawRectangle(ox + 3, oy + 5, 10, 7, water);
DrawRectangle(ox + 4, oy + 4, 8, 1, water);
DrawRectangle(ox + 4, oy + 12, 8, 1, edge);
DrawPixel(ox + 2, oy + 7, edge);
DrawPixel(ox + 13, oy + 9, edge);
glint.a = (unsigned char)(glint.a + shimmer);
DrawLine(ox + 5, oy + 7, ox + 10, oy + 7, glint);
DrawLine(ox + 7, oy + 10, ox + 12, oy + 10, glint);
}
break;
case TILE_STATUE:
if (!decor_base_drawn)
DrawRectangleRec(dst, floor_color);
if (light_factor > 0.05f) {
Color stone = color_lerp((Color){52, 52, 56, 255}, (Color){142, 138, 128, 255}, light_factor);
Color shade = color_lerp((Color){22, 22, 26, 255}, (Color){68, 66, 64, 255}, light_factor);
Color light = color_lerp((Color){78, 78, 82, 255}, (Color){176, 170, 150, 255}, light_factor);
int ox = x * TILE_SIZE;
int oy = y * TILE_SIZE;
DrawRectangle(ox + 3, oy + 12, 10, 2, (Color){0, 0, 0, 70});
DrawRectangle(ox + 4, oy + 11, 8, 3, shade);
DrawRectangle(ox + 5, oy + 10, 6, 2, stone);
DrawRectangle(ox + 5, oy + 5, 6, 6, shade);
DrawRectangle(ox + 6, oy + 4, 5, 6, stone);
DrawRectangle(ox + 5, oy + 7, 1, 3, shade);
DrawRectangle(ox + 11, oy + 7, 1, 3, shade);
DrawRectangle(ox + 6, oy + 2, 4, 3, stone);
DrawRectangle(ox + 7, oy + 1, 2, 1, light);
DrawPixel(ox + 7, oy + 6, light);
DrawPixel(ox + 9, oy + 6, shade);
DrawLine(ox + 6, oy + 10, ox + 10, oy + 10, light);
}
break;
}
}
// Wall base shadow: dark strip at bottom of wall tiles
if (map->tiles[y][x] == TILE_WALL && brightness > 0) {
int shadow_alpha = (int)(60.0f * ((float)brightness / 255.0f));
DrawRectangle(dst.x, dst.y + dst.height - 2, dst.width, 2, (Color){0, 0, 0, shadow_alpha});
}
}
}
}
void render_player(const Player *p, const Tileset *tileset, int frame_counter) {
Rectangle dst = {(float)(p->position.x * TILE_SIZE), (float)(p->position.y * TILE_SIZE), (float)TILE_SIZE,
(float)TILE_SIZE};
int cx = p->position.x * TILE_SIZE + TILE_SIZE / 2;
int cy = p->position.y * TILE_SIZE + TILE_SIZE / 2;
DrawEllipse(cx, cy + 5, 6.0f, 3.0f, (Color){0, 0, 0, 60});
if (tileset != NULL && tileset->finalized) {
int tile_id = p->sprite_tile_id;
switch (p->anim_state) {
case PLAYER_ANIM_WALK:
tile_id = (p->anim_frame == 0) ? SPRITE_PLAYER_WALK_0 : SPRITE_PLAYER_WALK_1;
break;
case PLAYER_ANIM_ATTACK:
tile_id = SPRITE_PLAYER_ATTACK;
break;
default:
// Idle breathing: subtle bob every 60 frames
if ((frame_counter / 30) % 2 == 0) {
dst.y -= 1;
}
tile_id = p->sprite_tile_id;
break;
}
Rectangle src = tileset_get_region(tileset, tile_id);
if (src.width > 0) {
// Flip horizontally if facing left
if (!p->facing_right) {
src.width = -src.width;
}
DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, WHITE);
// Draw status effect overlays
for (int e = 0; e < p->effect_count && e < MAX_EFFECTS; e++) {
if (p->effects[e].duration > 0) {
int effect_tile = -1;
switch (p->effects[e].type) {
case EFFECT_BURN:
effect_tile = SPRITE_EFFECT_BURN;
break;
case EFFECT_POISON:
effect_tile = SPRITE_EFFECT_POISON;
break;
default:
break;
}
if (effect_tile >= 0) {
Rectangle eff_src = tileset_get_region(tileset, effect_tile);
if (eff_src.width > 0) {
Rectangle eff_dst = {dst.x + 8, dst.y + 8, 8, 8};
DrawTexturePro(tileset->atlas, eff_src, eff_dst, (Vector2){0, 0}, 0.0f, (Color){255, 255, 255, 180});
}
}
}
}
// Damage flash overlay
if (p->flash_timer > 0) {
DrawRectangleRec(dst, (Color){255, 0, 0, 128});
}
return;
}
}
// Fallback to solid color
DrawRectangleRec(dst, BLUE);
if (p->flash_timer > 0) {
DrawRectangleRec(dst, (Color){255, 0, 0, 128});
}
}
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 (!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),
(float)TILE_SIZE, (float)TILE_SIZE};
// Select animation frame based on sprite_tile_id base
int base_tile = enemies[i].sprite_tile_id;
int tile_id;
if (enemies[i].anim_state == ENEMY_ANIM_WALK) {
tile_id = (enemies[i].anim_frame == 0) ? base_tile + 1 : base_tile + 2;
} else if (enemies[i].anim_state == ENEMY_ANIM_ATTACK) {
tile_id = base_tile + 3;
} else if (enemies[i].anim_state == ENEMY_ANIM_IDLE) {
// Idle breathing: subtle bob every 60 frames
if ((frame_counter / 30) % 2 == 0) {
dst.y -= 1;
}
tile_id = base_tile;
} else {
tile_id = base_tile;
}
if (tile_id >= 0 && tileset != NULL && tileset->finalized) {
Rectangle src = tileset_get_region(tileset, tile_id);
if (src.width > 0) {
// Flip horizontally if facing left
if (!enemies[i].facing_right) {
src.width = -src.width;
}
DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, WHITE);
// Draw status effect overlays
for (int e = 0; e < enemies[i].effect_count && e < MAX_EFFECTS; e++) {
if (enemies[i].effects[e].duration > 0) {
int effect_tile = -1;
switch (enemies[i].effects[e].type) {
case EFFECT_BURN:
effect_tile = SPRITE_EFFECT_BURN;
break;
case EFFECT_POISON:
effect_tile = SPRITE_EFFECT_POISON;
break;
default:
break;
}
if (effect_tile >= 0) {
Rectangle eff_src = tileset_get_region(tileset, effect_tile);
if (eff_src.width > 0) {
Rectangle eff_dst = {dst.x + 8, dst.y + 8, 8, 8};
DrawTexturePro(tileset->atlas, eff_src, eff_dst, (Vector2){0, 0}, 0.0f, (Color){255, 255, 255, 180});
}
}
}
}
// Enemy alert overlay (yellow tint when alert)
if (enemies[i].alert) {
DrawRectangleRec(dst, (Color){255, 255, 0, 30});
}
}
} else {
// Fallback to solid colors
Color enemy_color;
switch (enemies[i].type) {
case ENEMY_GOBLIN:
enemy_color = COLOR_ENEMY_GOBLIN;
break;
case ENEMY_SKELETON:
enemy_color = COLOR_ENEMY_SKELETON;
break;
case ENEMY_ORC:
enemy_color = COLOR_ENEMY_ORC;
break;
default:
enemy_color = RED;
break;
}
DrawRectangleRec(dst, enemy_color);
if (enemies[i].alert) {
DrawRectangleRec(dst, (Color){255, 255, 0, 30});
}
}
// Draw hp bar above enemy, color-coded by health remaining
int hp_pixels = (enemies[i].max_hp > 0) ? (enemies[i].hp * TILE_SIZE) / enemies[i].max_hp : 0;
if (hp_pixels > 0) {
float hp_ratio = (float)enemies[i].hp / (float)enemies[i].max_hp;
Color bar_color;
if (hp_ratio > 0.5f)
bar_color = (Color){60, 180, 60, 255}; // green
else if (hp_ratio > 0.25f)
bar_color = (Color){200, 180, 40, 255}; // yellow
else
bar_color = (Color){200, 60, 60, 255}; // red
Rectangle hp_bar = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE - 4),
(float)hp_pixels, 3};
DrawRectangleRec(hp_bar, bar_color);
}
}
}
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 (!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,
(float)TILE_SIZE};
int tile_id = items[i].sprite_tile_id;
if (tile_id >= 0 && tileset != NULL && tileset->finalized) {
Rectangle src = tileset_get_region(tileset, tile_id);
if (src.width > 0) {
DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, WHITE);
}
} else {
// Fallback to solid colors
Color item_color;
switch (items[i].type) {
case ITEM_POTION:
item_color = COLOR_ITEM_POTION;
break;
case ITEM_WEAPON:
item_color = COLOR_ITEM_WEAPON;
break;
case ITEM_ARMOR:
item_color = COLOR_ITEM_ARMOR;
break;
default:
item_color = GREEN;
break;
}
DrawRectangleRec(dst, item_color);
}
}
}
void render_ui(const Player *p, const Tileset *tileset, const FontManager *fm) {
// HUD Panel
const int hud_y = MAP_HEIGHT * TILE_SIZE;
const int hud_height = 60;
const Color hud_bg = {20, 18, 22, 255}; // dark bluish to separate from map
const Color hud_border = {139, 119, 89, 255}; // bronze/brown border
const Color text_dim = {160, 150, 140, 255}; // dimmed text
const Color text_bright = {240, 230, 220, 255}; // bright text
// Main HUD background with border
Rectangle ui_bg = {0, (float)hud_y, (float)SCREEN_WIDTH, (float)hud_height};
DrawRectangleRec(ui_bg, hud_bg);
// Subtle shadow separating HUD from game view
DrawLine(0, hud_y - 1, SCREEN_WIDTH, hud_y - 1, (Color){0, 0, 0, 80});
DrawRectangleLines(0, hud_y, SCREEN_WIDTH, hud_height, hud_border);
DrawLine(0, hud_y + 1, SCREEN_WIDTH, hud_y + 1, (Color){60, 55, 50, 255});
DrawLine(0, hud_y + hud_height - 2, SCREEN_WIDTH, hud_y + hud_height - 2, (Color){15, 12, 10, 255});
// Section dividers
int section1_end = 180; // after portrait + HP bar
int section2_end = 310; // after stats
int section3_end = 480; // after equipment
DrawLine(section1_end, hud_y + 5, section1_end, hud_y + hud_height - 5,
(Color){60, 55, 50, 255}); // after portrait + HP bar
DrawLine(section1_end + 1, hud_y + 5, section1_end + 1, hud_y + hud_height - 5, (Color){15, 12, 10, 255});
DrawLine(section2_end, hud_y + 5, section2_end, hud_y + hud_height - 5, (Color){60, 55, 50, 255}); // after stats
DrawLine(section2_end + 1, hud_y + 5, section2_end + 1, hud_y + hud_height - 5, (Color){15, 12, 10, 255});
int portrait_x = 8;
int portrait_y = hud_y + 8;
int portrait_size = 44;
// Draw player sprite in portrait
if (tileset != NULL && tileset->finalized) {
Rectangle src = tileset_get_region(tileset, SPRITE_PLAYER);
if (src.width > 0) {
Rectangle dst = {(float)portrait_x, (float)portrait_y, (float)portrait_size, (float)portrait_size};
DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, WHITE);
} else {
DrawRectangle(portrait_x, portrait_y, portrait_size, portrait_size, BLUE);
}
} else {
DrawRectangle(portrait_x, portrait_y, portrait_size, portrait_size, BLUE);
}
DrawRectangleLines(portrait_x, portrait_y, portrait_size, portrait_size, (Color){139, 119, 89, 255});
// HP Bar, to the right of portrait
int bar_x = portrait_x + portrait_size + 8;
int bar_y = hud_y + 20;
int bar_width = 100;
int bar_height = 16;
// HP Label, above bar
// Vector2 hp_width = MeasureTextEx(*font, "HP", BIG_FONT, NAR_CHAR_SPACE);
draw_text_hud(fm->hud_font, "HP", (float)bar_x, (float)bar_y - 17, BIG_FONT, NAR_CHAR_SPACE, text_dim);
// HP Bar background
DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){20, 15, 15, 255});
DrawRectangleLines(bar_x, bar_y, bar_width, bar_height, (Color){80, 70, 60, 255});
// HP Bar fill
float hp_percent = (float)p->hp / p->max_hp;
int fill_width = (int)(bar_width * hp_percent);
Color hp_color;
if (hp_percent > 0.6f) {
hp_color = (Color){60, 180, 60, 255};
} else if (hp_percent > 0.3f) {
hp_color = (Color){200, 180, 40, 255};
} else {
hp_color = (Color){200, 60, 60, 255};
}
if (fill_width > 0) {
DrawRectangle(bar_x + 1, bar_y + 1, fill_width - 2, bar_height - 2, hp_color);
}
// HP text, centered in bar
char hp_text[32];
snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp);
int hp_text_w = MeasureText(hp_text, MEDIUM_FONT);
draw_text_hud(fm->hud_font, hp_text, (float)bar_x + (bar_width - hp_text_w) / 2.0f, (float)bar_y + 2, MEDIUM_FONT,
SMALL_CHAR_SPACE, WHITE);
// Status effects
int effect_x = bar_x;
int effect_y = bar_y + bar_height + 5;
for (int i = 0; i < p->effect_count && i < MAX_EFFECTS; i++) {
Color eff_color;
const char *eff_label = "";
switch (p->effects[i].type) {
case EFFECT_POISON:
eff_color = (Color){50, 200, 50, 255};
eff_label = "PSN";
break;
case EFFECT_BLEED:
eff_color = (Color){200, 50, 50, 255};
eff_label = "BLD";
break;
case EFFECT_STUN:
eff_color = (Color){200, 200, 50, 255};
eff_label = "STN";
break;
case EFFECT_WEAKEN:
eff_color = (Color){120, 120, 120, 255};
eff_label = "WKN";
break;
case EFFECT_BURN:
eff_color = (Color){230, 130, 30, 255};
eff_label = "BRN";
break;
default:
continue;
}
if (p->effects[i].duration > 0) {
char eff_text[16];
snprintf(eff_text, sizeof(eff_text), "%s%d", eff_label, p->effects[i].duration);
draw_text_hud(fm->hud_font, eff_text, (float)effect_x, (float)effect_y, SMALL_FONT, NAR_CHAR_SPACE, eff_color);
effect_x += 28;
}
}
int stats_x = section1_end + 15;
int stats_y = hud_y + 12;
int stat_spacing = 40;
// Floor
char floor_text[16];
snprintf(floor_text, sizeof(floor_text), "F%d", p->floor);
draw_text_hud(fm->hud_font, floor_text, (float)stats_x, (float)stats_y, LARGE_FONT, NORM_CHAR_SPACE, text_bright);
draw_text_hud(fm->hud_font, "Floor", (float)stats_x, (float)stats_y + 16, NORM_FONT, NAR_CHAR_SPACE, text_dim);
// ATK
char atk_text[16];
snprintf(atk_text, sizeof(atk_text), "%d", p->attack);
draw_text_hud(fm->hud_font, atk_text, (float)(stats_x + stat_spacing), (float)stats_y, LARGE_FONT, NORM_CHAR_SPACE,
YELLOW);
draw_text_hud(fm->hud_font, "ATK", (float)(stats_x + stat_spacing), (float)stats_y + 16, NORM_FONT, NAR_CHAR_SPACE,
text_dim);
// DEF
char def_text[16];
snprintf(def_text, sizeof(def_text), "%d", p->defense);
draw_text_hud(fm->hud_font, def_text, (float)(stats_x + stat_spacing * 2), (float)stats_y, LARGE_FONT,
NORM_CHAR_SPACE, (Color){100, 150, 255, 255});
draw_text_hud(fm->hud_font, "DEF", (float)(stats_x + stat_spacing * 2), (float)stats_y + 16, NORM_FONT,
NAR_CHAR_SPACE, text_dim);
int equip_x = section2_end + 15;
int equip_y = hud_y + 8;
// Weapon slot
draw_text_hud(fm->hud_font, "WEAPON", (float)equip_x, (float)equip_y, MEDIUM_FONT, NAR_CHAR_SPACE, text_dim);
if (p->has_weapon) {
const char *weapon_name = item_get_name(&p->equipped_weapon);
if (weapon_name) {
char weapon_text[64];
snprintf(weapon_text, sizeof(weapon_text), "%s +%d [%s]", weapon_name, p->equipped_weapon.power,
dmg_class_get_short(p->equipped_weapon.dmg_class));
draw_text_hud(fm->hud_font, weapon_text, (float)equip_x, (float)equip_y + 11, SMALL_FONT, NAR_CHAR_SPACE,
(Color){255, 220, 100, 255});
}
} else {
draw_text_hud(fm->hud_font, "None [IMP]", (float)equip_x, (float)equip_y + 11, SMALL_FONT, NAR_CHAR_SPACE,
(Color){80, 75, 70, 255});
}
// Armor slot
draw_text_hud(fm->hud_font, "ARMOR", (float)equip_x, (float)equip_y + 26, MEDIUM_FONT, NAR_CHAR_SPACE, text_dim);
if (p->has_armor) {
const char *armor_name = item_get_name(&p->equipped_armor);
if (armor_name) {
char armor_text[48];
snprintf(armor_text, sizeof(armor_text), "%s +%d", armor_name, p->equipped_armor.power);
draw_text_hud(fm->hud_font, armor_text, (float)equip_x, (float)equip_y + 37, SMALL_FONT, NAR_CHAR_SPACE,
(Color){100, 150, 255, 255});
}
} else {
draw_text_hud(fm->hud_font, "None", (float)equip_x, (float)equip_y + 37, SMALL_FONT, NAR_CHAR_SPACE,
(Color){80, 75, 70, 255});
}
int ctrl_x = section3_end + 20;
int ctrl_y = hud_y + 14;
draw_text_hud(fm->hud_font, "[WASD] Move [G] Pickup [I] Inventory [U] Use", (float)ctrl_x, (float)ctrl_y,
MEDIUM_FONT, MED_CHAR_SPACE, (Color){139, 119, 89, 255});
draw_text_hud(fm->hud_font, "[E] Equip [D] Drop [Q] Quit", (float)ctrl_x, (float)ctrl_y + 16, MEDIUM_FONT,
MED_CHAR_SPACE, (Color){139, 119, 89, 255});
// INV count in top-right corner of HUD
char inv_text[16];
snprintf(inv_text, sizeof(inv_text), "INV: %d/%d", p->inventory_count, MAX_INVENTORY);
int inv_width = MeasureText(inv_text, SMALL_FONT);
draw_text_hud(fm->hud_font, inv_text, (float)SCREEN_WIDTH - inv_width - 10, (float)hud_y + 5, SMALL_FONT,
NAR_CHAR_SPACE, GREEN);
}
void render_action_log(const char log[5][128], int count, int head, const FontManager *fm) {
// Roguelike scroll/log panel styling
const int log_width = 250;
const int log_height = 90;
const int log_x = 2;
const int log_y = MAP_HEIGHT * TILE_SIZE - log_height - 5;
const Color log_bg = {15, 12, 10, 230}; // dark parchment
const Color log_border = {100, 85, 65, 255}; // bronze border
const Color log_border_dark = {60, 50, 40, 255}; // shadow
// Background panel with border
Rectangle log_rect = {(float)log_x, (float)log_y, (float)log_width, (float)log_height};
DrawRectangleRec(log_rect, log_bg);
DrawRectangleLines(log_x, log_y, log_width, log_height, log_border);
// Inner shadow line
DrawLine(log_x + 1, log_y + log_height - 1, log_x + log_width - 1, log_y + log_height - 1, log_border_dark);
DrawLine(log_x + log_width - 1, log_y + 1, log_x + log_width - 1, log_y + log_height - 1, log_border_dark);
// Title bar
DrawRectangle(log_x + 4, log_y + 4, log_width - 8, 16, (Color){30, 25, 20, 255});
draw_text_hud(fm->hud_font, "MESSAGE LOG", (float)log_x + 8, (float)log_y + 6, MEDIUM_FONT, NAR_CHAR_SPACE,
(Color){180, 160, 130, 255});
// Separator line under title
DrawLine(log_x + 4, log_y + 22, log_x + log_width - 5, log_y + 22, log_border_dark);
// Log entries
int text_x = log_x + 8;
int text_start_y = log_y + 28;
int line_height = 12;
for (int i = 0; i < count && i < 5; i++) {
int idx = (head - count + i + 5) % 5;
if (log[idx][0] != '\0') {
// Fade older messages
int age = count - i - 1;
Color text_color;
if (age == 0) {
text_color = (Color){220, 210, 200, 255}; // newest: bright
} else if (age == 1) {
text_color = (Color){180, 170, 160, 255}; // recent
} else if (age == 2) {
text_color = (Color){150, 140, 130, 230}; // older
} else {
text_color = (Color){120, 110, 100, 200}; // oldest: dim
}
draw_text_hud(fm->hud_font, log[idx], (float)text_x, (float)text_start_y + i * line_height, NORM_FONT,
SMALL_CHAR_SPACE, text_color);
}
}
}
void render_inventory_overlay(const Player *p, int selected, const FontManager *fm) {
// Overlay dimensions
int ov_width = 360;
int ov_height = 320;
Rectangle overlay = {(float)(SCREEN_WIDTH - ov_width) / 2, (float)(SCREEN_HEIGHT - ov_height) / 2 - 60,
(float)ov_width, (float)ov_height};
DrawRectangleRec(overlay, (Color){12, 12, 12, 252});
DrawRectangleLines((int)overlay.x, (int)overlay.y, (int)overlay.width, (int)overlay.height, (Color){70, 70, 70, 255});
// Title
const char *title = "INVENTORY";
// int title_w = MeasureText(title, 24);
Vector2 t_w = MeasureTextEx(fm->inv_font, title, HUGE_FONT, NORM_CHAR_SPACE);
draw_text_body(fm->inv_font, title, overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10, HUGE_FONT,
NORM_CHAR_SPACE, WHITE);
// Draw each inventory slot
char slot_text[64];
int row_height = 26;
int start_y = (int)overlay.y + 40;
for (int i = 0; i < MAX_INVENTORY; i++) {
int y_pos = start_y + (i * row_height);
if (i < p->inventory_count && !p->inventory[i].picked_up) {
const Item *item = &p->inventory[i];
// Selection highlight
if (i == selected) {
DrawRectangle((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2, (Color){45, 45, 45, 255});
DrawRectangleLines((int)overlay.x + 6, y_pos, (int)overlay.width - 12, row_height - 2,
(Color){180, 160, 80, 255});
}
// Slot number
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
draw_text_body(fm->inv_font, slot_text, overlay.x + 16, (float)y_pos + 4, MEDIUM_FONT, SMALL_CHAR_SPACE,
(Color){80, 80, 80, 255});
// Item name
const char *name = item_get_name(item);
if (name) {
Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255}
: (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255}
: (Color){140, 140, 255, 255};
draw_text_body(fm->inv_font, name, overlay.x + 45, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, name_color);
}
// Power
snprintf(slot_text, sizeof(slot_text), "+%d", item->power);
draw_text_body(fm->inv_font, slot_text, overlay.x + 150, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, YELLOW);
// Action
if (item->type == ITEM_POTION) {
draw_text_body(fm->inv_font, "[U]se", overlay.x + 200, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, GREEN);
} else {
draw_text_body(fm->inv_font, "[E]quip [D]rop", overlay.x + 200, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE,
GOLD);
}
} else {
// Empty slot
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
draw_text_body(fm->inv_font, slot_text, overlay.x + 16, (float)y_pos + 4, MEDIUM_FONT, SMALL_CHAR_SPACE,
(Color){40, 40, 40, 255});
}
}
// Instructions at bottom
const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close";
Vector2 hint_w = MeasureTextEx(fm->inv_font, hint, SMALL_FONT, NAR_CHAR_SPACE);
draw_text_body(fm->inv_font, hint, overlay.x + (overlay.width - hint_w.x) / 2.0f, overlay.y + overlay.height - 22,
SMALL_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255});
}
static Color label_color(FloatingText *ft, int alpha) {
if (ft->label == LABEL_NONE)
return FLOAT_DAMAGE; // numeric damage default
switch (ft->label) {
case LABEL_DODGE:
return FLOAT_DODGE;
case LABEL_BLOCK:
return FLOAT_BLOCK;
case LABEL_CRIT:
return FLOAT_CRIT;
case LABEL_SLAIN:
return FLOAT_SLAIN;
case LABEL_PROC:
// Proc label, color driven by effect_type stored in the struct
switch (ft->effect_type) {
case EFFECT_POISON:
return (Color){50, 200, 50, alpha};
case EFFECT_BLEED:
return (Color){200, 50, 50, alpha};
case EFFECT_BURN:
return (Color){230, 130, 30, alpha};
case EFFECT_STUN:
return (Color){200, 200, 50, alpha};
case EFFECT_WEAKEN:
return (Color){120, 120, 120, alpha};
default:
return FLOAT_DEFAULT;
}
default:
return FLOAT_DAMAGE;
}
}
static const char *label_text(FloatingLabel label) {
switch (label) {
case LABEL_DODGE:
return "DODGE";
case LABEL_BLOCK:
return "BLOCK";
case LABEL_CRIT:
return "CRIT!";
case LABEL_SLAIN:
return "SLAIN";
case LABEL_PROC:
return "PROC";
default:
return "";
}
}
static int label_font_size(FloatingLabel label) {
return (label == LABEL_CRIT) ? FONT_SIZE_FLOAT_CRIT : FONT_SIZE_FLOAT_LABEL;
}
void render_slash_effect(int x, int y, DamageClass dmg_class, int timer) {
if (timer <= 0)
return;
float alpha = (float)timer / 8.0f;
if (alpha > 1.0f)
alpha = 1.0f;
int a = (int)(255 * alpha);
int px = x * TILE_SIZE;
int py = y * TILE_SIZE;
switch (dmg_class) {
case DMG_SLASH:
// Red diagonal slash
DrawLine(px + 2, py + 2, px + TILE_SIZE - 2, py + TILE_SIZE - 2, (Color){255, 80, 80, a});
DrawLine(px + 4, py + 2, px + TILE_SIZE - 2, py + TILE_SIZE - 4, (Color){255, 120, 120, a});
break;
case DMG_IMPACT:
// Orange burst (star pattern)
DrawLine(px + TILE_SIZE / 2, py + 2, px + TILE_SIZE / 2, py + TILE_SIZE - 2, (Color){255, 180, 60, a});
DrawLine(px + 2, py + TILE_SIZE / 2, px + TILE_SIZE - 2, py + TILE_SIZE / 2, (Color){255, 180, 60, a});
DrawLine(px + 4, py + 4, px + TILE_SIZE - 4, py + TILE_SIZE - 4, (Color){255, 200, 100, a});
DrawLine(px + 4, py + TILE_SIZE - 4, px + TILE_SIZE - 4, py + 4, (Color){255, 200, 100, a});
break;
case DMG_PIERCE:
// Yellow horizontal streak
DrawLine(px + 2, py + TILE_SIZE / 2, px + TILE_SIZE - 2, py + TILE_SIZE / 2, (Color){255, 255, 100, a});
DrawLine(px + 2, py + TILE_SIZE / 2 - 2, px + TILE_SIZE - 2, py + TILE_SIZE / 2 - 2, (Color){255, 255, 150, a});
DrawLine(px + 2, py + TILE_SIZE / 2 + 2, px + TILE_SIZE - 2, py + TILE_SIZE / 2 + 2, (Color){255, 255, 150, a});
break;
case DMG_FIRE:
// Red-orange flame burst
DrawLine(px + TILE_SIZE / 2, py + TILE_SIZE - 2, px + TILE_SIZE / 2, py + 4, (Color){255, 100, 30, a});
DrawLine(px + TILE_SIZE / 2 - 3, py + TILE_SIZE - 4, px + TILE_SIZE / 2 - 1, py + 6, (Color){255, 150, 50, a});
DrawLine(px + TILE_SIZE / 2 + 3, py + TILE_SIZE - 4, px + TILE_SIZE / 2 + 1, py + 6, (Color){255, 150, 50, a});
break;
case DMG_POISON:
// Green splash
DrawLine(px + 4, py + 4, px + TILE_SIZE - 4, py + TILE_SIZE - 4, (Color){50, 255, 100, a});
DrawLine(px + TILE_SIZE - 4, py + 4, px + 4, py + TILE_SIZE - 4, (Color){80, 255, 120, a});
DrawCircle(px + TILE_SIZE / 2, py + TILE_SIZE / 2, 3.0f, (Color){100, 255, 150, a / 2});
break;
default:
break;
}
}
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y, const FontManager *fm) {
for (int i = 0; i < count; i++) {
if (texts[i].lifetime <= 0)
continue;
int x = texts[i].x + shake_x;
int y = texts[i].y + shake_y - (60 - texts[i].lifetime); // rise over time
float alpha = (float)texts[i].lifetime / 60.0f;
int a = (int)(255 * alpha);
if (texts[i].label != LABEL_NONE) {
// Label text (DODGE, BLOCK, CRIT!, SLAIN, or PROC)
// CRIT! gets larger font size
int font_size = label_font_size(texts[i].label);
Color color = label_color(&texts[i], a);
const char *text = label_text(texts[i].label);
Vector2 text_size = MeasureTextEx(fm->body_font, text, (float)font_size, NORM_CHAR_SPACE);
draw_text_body(fm->body_font, text, (float)(x - (int)text_size.x / 2), (float)y, font_size, NORM_CHAR_SPACE,
color);
} else {
// Numeric damage
Color color = texts[i].is_critical ? (Color){255, 200, 50, a} : (Color){255, 100, 100, a};
char text[16];
snprintf(text, sizeof(text), "%d", texts[i].value);
Vector2 text_size = MeasureTextEx(fm->body_font, text, (float)FONT_SIZE_FLOAT_DMG, NORM_CHAR_SPACE);
draw_text_body(fm->body_font, text, (float)(x - (int)text_size.x / 2), (float)y, FONT_SIZE_FLOAT_DMG,
NORM_CHAR_SPACE, color);
}
}
}
static void draw_stat_line(Font f, char *line_buf, size_t line_buf_size, const char *label, int value, int x, int y,
int font_size, int label_value_gap, Color label_color, Color value_color) {
draw_text_body(f, label, (float)x, (float)y, font_size, NORM_CHAR_SPACE, label_color);
Vector2 label_size = MeasureTextEx(f, label, (float)font_size, NORM_CHAR_SPACE);
snprintf(line_buf, line_buf_size, "%d", value);
draw_text_body(f, line_buf, (float)x + (int)label_size.x + label_value_gap, (float)y, font_size, NORM_CHAR_SPACE,
value_color);
}
void render_end_screen(int is_victory, int kills, int items, int damage_dealt, int damage_taken, int crits,
int times_hit, int potions, int floors, int turns, int score, unsigned int seed,
const FontManager *fm) {
// Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 210});
// Title
const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER";
int title_font_size = HUGE_FONT;
Color title_color = is_victory ? GOLD : RED;
int title_width = MeasureText(title, title_font_size);
draw_text_body(fm->title_font, title, (float)(SCREEN_WIDTH - title_width) / 2.0f, 30.0f, title_font_size,
NORM_CHAR_SPACE, title_color);
char line[64];
int line_height = 24;
int label_value_gap = 10;
int col_padding = 40;
Color label_color = LIGHTGRAY;
Color value_color = WHITE;
// Stats box
const char *all_labels[] = {"Kills:", "Items:", "Damage Dealt:", "Damage Taken:", "Crits:", "Times Hit:",
"Potions:", "Floors:", "Turns:", "SCORE:", "SEED:"};
float max_label_width = 0.0f;
for (size_t i = 0; i < sizeof(all_labels) / sizeof(all_labels[0]); i++) {
Vector2 sz = MeasureTextEx(fm->body_font, all_labels[i], (float)LARGE_FONT, NORM_CHAR_SPACE);
if (sz.x > max_label_width)
max_label_width = sz.x;
}
// Estimate max value width (5 digits + padding) to accommodate large scores/damage
float max_value_width = MeasureTextEx(fm->body_font, "99999", (float)LARGE_FONT, NORM_CHAR_SPACE).x;
// Stats content
float col_width = max_label_width + label_value_gap + max_value_width + col_padding;
int box_w = (int)(col_width * 2.0f) + 40; // two columns + margins
int box_h = 350;
int box_x = (SCREEN_WIDTH - box_w) / 2;
int box_y = 110;
DrawRectangle(box_x, box_y, box_w, box_h, (Color){20, 20, 20, 240});
DrawRectangleLines(box_x, box_y, box_w, box_h, (Color){100, 100, 100, 255});
int col1_x = box_x + 20;
int col2_x = box_x + 20 + (int)col_width; // Column 2
int row_y = box_y + 20; // Column 1
draw_stat_line(fm->body_font, line, sizeof(line), "Kills:", kills, col1_x, row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Items:", items, col1_x, row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Damage Dealt:", damage_dealt, col1_x, row_y, LARGE_FONT,
label_value_gap, label_color, value_color);
row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Damage Taken:", damage_taken, col1_x, row_y, LARGE_FONT,
label_value_gap, label_color, value_color);
row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Crits:", crits, col1_x, row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Times Hit:", times_hit, col1_x, row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
row_y += line_height;
int col2_row_y = box_y + 20;
draw_stat_line(fm->body_font, line, sizeof(line), "Potions:", potions, col2_x, col2_row_y, LARGE_FONT,
label_value_gap, label_color, value_color);
col2_row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Floors:", floors, col2_x, col2_row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
col2_row_y += line_height;
draw_stat_line(fm->body_font, line, sizeof(line), "Turns:", turns, col2_x, col2_row_y, LARGE_FONT, label_value_gap,
label_color, value_color);
col2_row_y += line_height;
// Score: placed below the last row of the longer column (6 items, row_y is already there)
row_y += 10;
draw_text_body(fm->body_font, "SCORE:", (float)col1_x, (float)row_y, BIG_FONT, NORM_CHAR_SPACE, GOLD);
Vector2 score_label_size = MeasureTextEx(fm->body_font, "SCORE:", (float)BIG_FONT, NORM_CHAR_SPACE);
snprintf(line, sizeof(line), "%d", score);
draw_text_body(fm->body_font, line, (float)col1_x + (int)score_label_size.x + label_value_gap, (float)row_y, BIG_FONT,
NORM_CHAR_SPACE, GOLD);
row_y += 35;
// Seed display
draw_text_body(fm->body_font, "SEED:", (float)col1_x, (float)row_y, LARGE_FONT, SMALL_CHAR_SPACE, label_color);
Vector2 seed_label_size = MeasureTextEx(fm->body_font, "SEED:", (float)LARGE_FONT, SMALL_CHAR_SPACE);
snprintf(line, sizeof(line), "%u", seed);
draw_text_body(fm->body_font, line, (float)col1_x + (int)seed_label_size.x + label_value_gap, (float)row_y,
LARGE_FONT, SMALL_CHAR_SPACE, END_SEED);
// Instructions
if (is_victory) {
const char *subtitle = "Press R to play again or Q to quit";
int sub_width = MeasureText(subtitle, LARGE_FONT);
draw_text_body(fm->body_font, subtitle, (float)(SCREEN_WIDTH - sub_width) / 2.0f, (float)SCREEN_HEIGHT - 50,
LARGE_FONT, NORM_CHAR_SPACE, LIGHTGRAY);
} else {
const char *subtitle = "Press R to restart or Q to quit";
int sub_width = MeasureText(subtitle, LARGE_FONT);
draw_text_body(fm->body_font, subtitle, (float)(SCREEN_WIDTH - sub_width) / 2.0f, (float)SCREEN_HEIGHT - 50,
LARGE_FONT, NORM_CHAR_SPACE, LIGHTGRAY);
}
}
void render_message(const char *message, const FontManager *fm) {
if (message == NULL)
return;
const int font_size = NORM_FONT;
const int line_height = font_size + 4;
const int padding_x = 20;
const int padding_y = 15;
const int max_box_width = (int)(SCREEN_WIDTH * 0.75f);
const int max_line_width = max_box_width - (padding_x * 2);
// Calculate line breaks by iterating through message
int line_count = 1;
int current_line_width = 0;
int longest_line_width = 0;
const char *msg_ptr = message;
while (*msg_ptr && line_count <= 10) {
// Estimate character width (average ~10px for 20pt font)
int char_width = 10;
current_line_width += char_width;
if (current_line_width > max_line_width && *msg_ptr == ' ') {
if (current_line_width > longest_line_width)
longest_line_width = current_line_width;
line_count++;
current_line_width = 0;
}
msg_ptr++;
}
if (current_line_width > longest_line_width)
longest_line_width = current_line_width;
// Measure full message
Vector2 total_msg_width = MeasureTextEx(fm->body_font, message, font_size, NORM_CHAR_SPACE);
int box_width = (int)total_msg_width.x + (padding_x * 2);
// If message is too long, use wrapped width
if (box_width > max_box_width) {
box_width = max_box_width;
}
// Ensure minimum width
if (box_width < 200)
box_width = 200;
// Calculate box height based on line count
int box_height = (line_count * line_height) + (padding_y * 2);
// Center the box
float box_x = (SCREEN_WIDTH - box_width) / 2.0f;
float box_y = (SCREEN_HEIGHT - box_height) / 2.0f;
// Draw message box background
Rectangle msg_bg = {box_x, box_y, (float)box_width, (float)box_height};
DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
// Draw text centered
int text_x = (SCREEN_WIDTH - (int)total_msg_width.x) / 2;
int text_y = (SCREEN_HEIGHT - font_size) / 2;
// For wrapped text, draw at box center with padding
if (line_count > 1) {
text_x = (int)box_x + padding_x;
text_y = (int)box_y + padding_y;
}
draw_text_body(fm->body_font, message, (float)text_x, (float)text_y, font_size, NORM_CHAR_SPACE, WHITE);
}
void render_seed_display(unsigned int seed) {
char seed_text[64];
snprintf(seed_text, sizeof(seed_text), "Seed: %u", seed);
const int font_size = TINY_FONT;
int text_width = MeasureText(seed_text, font_size);
// Position at top right with padding
int x = SCREEN_WIDTH - text_width - 10;
int y = 5;
// Draw with non-obstructive dim text color
DrawText(seed_text, x, y, font_size, TEXT_DIM);
}