treewide: the renderer rewrite was promised to me 3000 years ago #21

Open
NotAShelf wants to merge 4 commits from notashelf/push-nmuuvryuwrrl into main
3 changed files with 247 additions and 130 deletions
Showing only changes of commit e00424a918 - Show all commits

render: clean up font management; account for differing container sizes

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Icd554815388ec44886245406ac9ea0be6a6a6964
raf 2026-04-22 02:58:39 +03:00
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -431,7 +431,6 @@ static int handle_movement_input(GameState *gs) {
} }
} }
Vec2 direction = {0, 0}; Vec2 direction = {0, 0};
if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))
direction.y = -1; direction.y = -1;
@ -448,7 +447,6 @@ static int handle_movement_input(GameState *gs) {
// Reset combat event before player acts // Reset combat event before player acts
combat_reset_event(); combat_reset_event();
int new_x = gs->player.position.x + direction.x; int new_x = gs->player.position.x + direction.x;
int new_y = gs->player.position.y + direction.y; int new_y = gs->player.position.y + direction.y;
@ -519,7 +517,7 @@ void load_audio_assets(GameState *gs) {
} }
// Main game loop // Main game loop
static void game_loop(unsigned int run_seed) { static void game_loop(unsigned int run_seed, FontManager *fm) {
GameState gs; GameState gs;
memset(&gs, 0, sizeof(GameState)); memset(&gs, 0, sizeof(GameState));
gs.run_seed = run_seed; gs.run_seed = run_seed;
@ -527,7 +525,7 @@ static void game_loop(unsigned int run_seed) {
// sound // sound
load_audio_assets(&gs); load_audio_assets(&gs);
// font // font
Font fontTTF = LoadFontEx("./assets/fonts/spartan_500.ttf", 36, NULL, 0); init_fonts(fm);
// Initialize first floor // Initialize first floor
init_floor(&gs, 1); init_floor(&gs, 1);
@ -553,6 +551,7 @@ static void game_loop(unsigned int run_seed) {
gs.game_over = 0; gs.game_over = 0;
gs.game_won = 0; gs.game_won = 0;
load_audio_assets(&gs); load_audio_assets(&gs);
init_fonts(fm);
init_floor(&gs, 1); init_floor(&gs, 1);
// Update window title with new seed // Update window title with new seed
char title[128]; char title[128];
@ -585,19 +584,19 @@ static void game_loop(unsigned int run_seed) {
// Floating texts follow world shake // Floating texts follow world shake
render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y); render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y);
render_ui(&gs.player, &fontTTF); render_ui(&gs.player, fm);
// Draw action log // Draw action log
render_action_log(gs.action_log, gs.log_count, gs.log_head, &fontTTF); render_action_log(gs.action_log, gs.log_count, gs.log_head, fm);
// Draw inventory overlay if active // Draw inventory overlay if active
if (gs.show_inventory) { if (gs.show_inventory) {
render_inventory_overlay(&gs.player, gs.inv_selected, &fontTTF); render_inventory_overlay(&gs.player, gs.inv_selected, fm);
} }
// Draw message if any // Draw message if any
if (gs.last_message != NULL && gs.message_timer > 0) { if (gs.last_message != NULL && gs.message_timer > 0) {
render_message(gs.last_message, &fontTTF); render_message(gs.last_message, fm);
} }
// Draw persistent seed display in top right // Draw persistent seed display in top right
@ -613,7 +612,7 @@ static void game_loop(unsigned int run_seed) {
} }
render_end_screen(gs.game_won, gs.total_kills, gs.items_collected, gs.damage_dealt, gs.damage_taken, render_end_screen(gs.game_won, gs.total_kills, gs.items_collected, gs.damage_dealt, gs.damage_taken,
gs.crits_landed, gs.times_hit, gs.potions_used, gs.floors_reached, gs.turn_count, gs.crits_landed, gs.times_hit, gs.potions_used, gs.floors_reached, gs.turn_count,
gs.final_score, gs.run_seed, &fontTTF); gs.final_score, gs.run_seed, fm);
} }
EndDrawing(); EndDrawing();
@ -621,6 +620,9 @@ static void game_loop(unsigned int run_seed) {
// small delay for key repeat control // small delay for key repeat control
WaitTime(0.08); WaitTime(0.08);
} }
// Cleanup
destroy_fonts(fm);
} }
// Check if a string is a valid unsigned integer // Check if a string is a valid unsigned integer
@ -683,11 +685,13 @@ int main(int argc, char **argv) {
SetTargetFPS(60); SetTargetFPS(60);
// Run game // Run game
game_loop(run_seed); FontManager fm;
init_fonts(&fm);
game_loop(run_seed, &fm);
// Cleanup // Cleanup
CloseWindow(); CloseWindow();
audio_close(); audio_close();
return 0; return 0;
} }

View file

@ -5,6 +5,64 @@
#include <stdio.h> #include <stdio.h>
#include <string.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);
}
void render_map(const Map *map) { void render_map(const Map *map) {
for (int y = 0; y < MAP_HEIGHT; y++) { for (int y = 0; y < MAP_HEIGHT; y++) {
for (int x = 0; x < MAP_WIDTH; x++) { for (int x = 0; x < MAP_WIDTH; x++) {
@ -31,9 +89,9 @@ void render_map(const Map *map) {
case TILE_STAIRS: case TILE_STAIRS:
DrawRectangleRec(rect, stairs_color); DrawRectangleRec(rect, stairs_color);
if (visible) if (visible)
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, WHITE); DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, NORM_FONT, WHITE);
else else
DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, 12, (Color){60, 60, 65, 255}); DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, NORM_FONT, (Color){60, 60, 65, 255});
break; break;
} }
} }
@ -124,7 +182,7 @@ void render_items(const Item *items, int count, const unsigned char visible[MAP_
} }
} }
void render_ui(const Player *p, Font *font) { void render_ui(const Player *p, const FontManager *fm) {
// HUD Panel // HUD Panel
const int hud_y = MAP_HEIGHT * TILE_SIZE; const int hud_y = MAP_HEIGHT * TILE_SIZE;
const int hud_height = 60; const int hud_height = 60;
@ -145,10 +203,10 @@ void render_ui(const Player *p, Font *font) {
int section2_end = 310; // after stats int section2_end = 310; // after stats
int section3_end = 480; // after equipment int section3_end = 480; // after equipment
DrawLine(section1_end, hud_y + 5, section1_end, hud_y + hud_height - 5, (Color){60, 55, 50, 255}); 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(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, hud_y + 5, section2_end, hud_y + hud_height - 5, (Color){60, 55, 50, 255});
DrawLine(section2_end + 1, hud_y + 5, section2_end + 1, hud_y + hud_height - 5, (Color){15, 12, 10, 255}); 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_x = 8;
@ -169,7 +227,7 @@ void render_ui(const Player *p, Font *font) {
// HP Label, above bar // HP Label, above bar
// Vector2 hp_width = MeasureTextEx(*font, "HP", BIG_FONT, NAR_CHAR_SPACE); // Vector2 hp_width = MeasureTextEx(*font, "HP", BIG_FONT, NAR_CHAR_SPACE);
DrawTextEx(*font, "HP", (Vector2){bar_x, bar_y - 17}, BIG_FONT, NAR_CHAR_SPACE, text_dim); draw_text_hud(fm->hud_font, "HP", (float)bar_x, (float)bar_y - 17, BIG_FONT, NAR_CHAR_SPACE, text_dim);
// HP Bar background // HP Bar background
DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){20, 15, 15, 255}); DrawRectangle(bar_x, bar_y, bar_width, bar_height, (Color){20, 15, 15, 255});
@ -194,9 +252,9 @@ void render_ui(const Player *p, Font *font) {
// HP text, centered in bar // HP text, centered in bar
char hp_text[32]; char hp_text[32];
snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp); snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp);
int hp_text_w = MeasureText(hp_text, 12); int hp_text_w = MeasureText(hp_text, MEDIUM_FONT);
DrawTextEx(*font, hp_text, (Vector2){bar_x + (bar_width - hp_text_w) / 2.0f, bar_y + 2}, 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); SMALL_CHAR_SPACE, WHITE);
// Status effects // Status effects
int effect_x = bar_x; int effect_x = bar_x;
@ -231,7 +289,7 @@ void render_ui(const Player *p, Font *font) {
if (p->effects[i].duration > 0) { if (p->effects[i].duration > 0) {
char eff_text[16]; char eff_text[16];
snprintf(eff_text, sizeof(eff_text), "%s%d", eff_label, p->effects[i].duration); snprintf(eff_text, sizeof(eff_text), "%s%d", eff_label, p->effects[i].duration);
DrawTextEx(*font, eff_text, (Vector2){effect_x, effect_y}, SMALL_FONT, NAR_CHAR_SPACE, eff_color); draw_text_hud(fm->hud_font, eff_text, (float)effect_x, (float)effect_y, SMALL_FONT, NAR_CHAR_SPACE, eff_color);
effect_x += 28; effect_x += 28;
} }
} }
@ -243,68 +301,76 @@ void render_ui(const Player *p, Font *font) {
// Floor // Floor
char floor_text[16]; char floor_text[16];
snprintf(floor_text, sizeof(floor_text), "F%d", p->floor); snprintf(floor_text, sizeof(floor_text), "F%d", p->floor);
DrawTextEx(*font, floor_text, (Vector2){stats_x, stats_y}, 16, NORM_CHAR_SPACE, text_bright); draw_text_hud(fm->hud_font, floor_text, (float)stats_x, (float)stats_y, LARGE_FONT, NORM_CHAR_SPACE, text_bright);
DrawTextEx(*font, "Floor", (Vector2){stats_x, stats_y + 16}, 12, NAR_CHAR_SPACE, text_dim); draw_text_hud(fm->hud_font, "Floor", (float)stats_x, (float)stats_y + 16, NORM_FONT, NAR_CHAR_SPACE, text_dim);
// ATK // ATK
char atk_text[16]; char atk_text[16];
snprintf(atk_text, sizeof(atk_text), "%d", p->attack); snprintf(atk_text, sizeof(atk_text), "%d", p->attack);
DrawTextEx(*font, atk_text, (Vector2){stats_x + stat_spacing, stats_y}, 16, NORM_CHAR_SPACE, YELLOW); draw_text_hud(fm->hud_font, atk_text, (float)(stats_x + stat_spacing), (float)stats_y, LARGE_FONT, NORM_CHAR_SPACE,
DrawTextEx(*font, "ATK", (Vector2){stats_x + stat_spacing, stats_y + 16}, 12, NAR_CHAR_SPACE, text_dim); 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 // DEF
char def_text[16]; char def_text[16];
snprintf(def_text, sizeof(def_text), "%d", p->defense); snprintf(def_text, sizeof(def_text), "%d", p->defense);
DrawTextEx(*font, def_text, (Vector2){stats_x + stat_spacing * 2, stats_y}, 16, NORM_CHAR_SPACE, draw_text_hud(fm->hud_font, def_text, (float)(stats_x + stat_spacing * 2), (float)stats_y, LARGE_FONT,
(Color){100, 150, 255, 255}); NORM_CHAR_SPACE, (Color){100, 150, 255, 255});
DrawTextEx(*font, "DEF", (Vector2){stats_x + stat_spacing * 2, stats_y + 16}, 12, NAR_CHAR_SPACE, text_dim); 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_x = section2_end + 15;
int equip_y = hud_y + 8; int equip_y = hud_y + 8;
// Weapon slot // Weapon slot
DrawTextEx(*font, "WEAPON", (Vector2){equip_x, equip_y}, MEDIUM_FONT, NAR_CHAR_SPACE, text_dim); draw_text_hud(fm->hud_font, "WEAPON", (float)equip_x, (float)equip_y, MEDIUM_FONT, NAR_CHAR_SPACE, text_dim);
if (p->has_weapon) { if (p->has_weapon) {
const char *weapon_name = item_get_name(&p->equipped_weapon); const char *weapon_name = item_get_name(&p->equipped_weapon);
if (weapon_name) { if (weapon_name) {
char weapon_text[64]; char weapon_text[64];
snprintf(weapon_text, sizeof(weapon_text), "%s +%d [%s]", weapon_name, p->equipped_weapon.power, snprintf(weapon_text, sizeof(weapon_text), "%s +%d [%s]", weapon_name, p->equipped_weapon.power,
dmg_class_get_short(p->equipped_weapon.dmg_class)); dmg_class_get_short(p->equipped_weapon.dmg_class));
DrawTextEx(*font, weapon_text, (Vector2){equip_x, equip_y + 11}, 10, NAR_CHAR_SPACE, (Color){255, 220, 100, 255}); 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 { } else {
DrawTextEx(*font, "None [IMP]", (Vector2){equip_x, equip_y + 11}, 10, NAR_CHAR_SPACE, (Color){80, 75, 70, 255}); 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 // Armor slot
DrawTextEx(*font, "ARMOR", (Vector2){equip_x, equip_y + 26}, MEDIUM_FONT, NAR_CHAR_SPACE, text_dim); 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) { if (p->has_armor) {
const char *armor_name = item_get_name(&p->equipped_armor); const char *armor_name = item_get_name(&p->equipped_armor);
if (armor_name) { if (armor_name) {
char armor_text[48]; char armor_text[48];
snprintf(armor_text, sizeof(armor_text), "%s +%d", armor_name, p->equipped_armor.power); snprintf(armor_text, sizeof(armor_text), "%s +%d", armor_name, p->equipped_armor.power);
DrawTextEx(*font, armor_text, (Vector2){equip_x, equip_y + 37}, 10, NAR_CHAR_SPACE, (Color){100, 150, 255, 255}); 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 { } else {
DrawTextEx(*font, "None", (Vector2){equip_x, equip_y + 37}, 10, NAR_CHAR_SPACE, (Color){80, 75, 70, 255}); 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_x = section3_end + 20;
int ctrl_y = hud_y + 14; int ctrl_y = hud_y + 14;
DrawTextEx(*font, "[WASD] Move [G] Pickup [I] Inventory [U] Use", (Vector2){ctrl_x, ctrl_y}, MEDIUM_FONT, draw_text_hud(fm->hud_font, "[WASD] Move [G] Pickup [I] Inventory [U] Use", (float)ctrl_x, (float)ctrl_y,
MED_CHAR_SPACE, (Color){139, 119, 89, 255}); MEDIUM_FONT, MED_CHAR_SPACE, (Color){139, 119, 89, 255});
DrawTextEx(*font, "[E] Equip [D] Drop [Q] Quit", (Vector2){ctrl_x, ctrl_y + 16}, MEDIUM_FONT, MED_CHAR_SPACE, draw_text_hud(fm->hud_font, "[E] Equip [D] Drop [Q] Quit", (float)ctrl_x, (float)ctrl_y + 16, MEDIUM_FONT,
(Color){139, 119, 89, 255}); MED_CHAR_SPACE, (Color){139, 119, 89, 255});
// INV count in top-right corner of HUD // INV count in top-right corner of HUD
char inv_text[16]; char inv_text[16];
snprintf(inv_text, sizeof(inv_text), "INV: %d/%d", p->inventory_count, MAX_INVENTORY); snprintf(inv_text, sizeof(inv_text), "INV: %d/%d", p->inventory_count, MAX_INVENTORY);
int inv_width = MeasureText(inv_text, 10); int inv_width = MeasureText(inv_text, SMALL_FONT);
DrawTextEx(*font, inv_text, (Vector2){SCREEN_WIDTH - inv_width - 10, hud_y + 5}, 10, NAR_CHAR_SPACE, GREEN); 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, Font *font) { void render_action_log(const char log[5][128], int count, int head, const FontManager *fm) {
// Roguelike scroll/log panel styling // Roguelike scroll/log panel styling
const int log_width = 250; const int log_width = 250;
const int log_height = 90; const int log_height = 90;
@ -326,8 +392,8 @@ void render_action_log(const char log[5][128], int count, int head, Font *font)
// Title bar // Title bar
DrawRectangle(log_x + 4, log_y + 4, log_width - 8, 16, (Color){30, 25, 20, 255}); DrawRectangle(log_x + 4, log_y + 4, log_width - 8, 16, (Color){30, 25, 20, 255});
DrawTextEx(*font, "MESSAGE LOG", (Vector2){log_x + 8, log_y + 6}, MEDIUM_FONT, NAR_CHAR_SPACE, 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}); (Color){180, 160, 130, 255});
// Separator line under title // Separator line under title
DrawLine(log_x + 4, log_y + 22, log_x + log_width - 5, log_y + 22, log_border_dark); DrawLine(log_x + 4, log_y + 22, log_x + log_width - 5, log_y + 22, log_border_dark);
@ -352,13 +418,13 @@ void render_action_log(const char log[5][128], int count, int head, Font *font)
} else { } else {
text_color = (Color){120, 110, 100, 200}; // oldest: dim text_color = (Color){120, 110, 100, 200}; // oldest: dim
} }
DrawTextEx(*font, log[idx], (Vector2){text_x, text_start_y + i * line_height}, NORM_FONT, SMALL_CHAR_SPACE, draw_text_hud(fm->hud_font, log[idx], (float)text_x, (float)text_start_y + i * line_height, NORM_FONT,
text_color); SMALL_CHAR_SPACE, text_color);
} }
} }
} }
void render_inventory_overlay(const Player *p, int selected, Font *font) { void render_inventory_overlay(const Player *p, int selected, const FontManager *fm) {
// Overlay dimensions // Overlay dimensions
int ov_width = 360; int ov_width = 360;
int ov_height = 320; int ov_height = 320;
@ -370,14 +436,14 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
// Title // Title
const char *title = "INVENTORY"; const char *title = "INVENTORY";
// int title_w = MeasureText(title, 24); // int title_w = MeasureText(title, 24);
Vector2 t_w = MeasureTextEx(*font, title, 30, NORM_CHAR_SPACE); Vector2 t_w = MeasureTextEx(fm->inv_font, title, HUGE_FONT, NORM_CHAR_SPACE);
DrawTextEx(*font, title, (Vector2){overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10}, HUGE_FONT, draw_text_body(fm->inv_font, title, overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10, HUGE_FONT,
NORM_CHAR_SPACE, WHITE); NORM_CHAR_SPACE, WHITE);
// Draw each inventory slot // Draw each inventory slot
char slot_text[64]; char slot_text[64];
int row_height = 26; int row_height = 26;
int start_y = overlay.y + 40; int start_y = (int)overlay.y + 40;
for (int i = 0; i < MAX_INVENTORY; i++) { for (int i = 0; i < MAX_INVENTORY; i++) {
int y_pos = start_y + (i * row_height); int y_pos = start_y + (i * row_height);
@ -394,8 +460,8 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
// Slot number // Slot number
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawTextEx(*font, slot_text, (Vector2){overlay.x + 16, y_pos + 4}, MEDIUM_FONT, SMALL_CHAR_SPACE, 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}); (Color){80, 80, 80, 255});
// Item name // Item name
const char *name = item_get_name(item); const char *name = item_get_name(item);
@ -403,31 +469,32 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) {
Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255} Color name_color = (item->type == ITEM_POTION) ? (Color){255, 140, 140, 255}
: (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255} : (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 255}
: (Color){140, 140, 255, 255}; : (Color){140, 140, 255, 255};
DrawTextEx(*font, name, (Vector2){overlay.x + 45, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, name_color); draw_text_body(fm->inv_font, name, overlay.x + 45, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, name_color);
} }
// Power // Power
snprintf(slot_text, sizeof(slot_text), "+%d", item->power); snprintf(slot_text, sizeof(slot_text), "+%d", item->power);
DrawTextEx(*font, slot_text, (Vector2){overlay.x + 150, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, YELLOW); draw_text_body(fm->inv_font, slot_text, overlay.x + 150, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, YELLOW);
// Action // Action
if (item->type == ITEM_POTION) { if (item->type == ITEM_POTION) {
DrawTextEx(*font, "[U]se", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GREEN); draw_text_body(fm->inv_font, "[U]se", overlay.x + 200, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE, GREEN);
} else { } else {
DrawTextEx(*font, "[E]quip [D]rop", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GOLD); draw_text_body(fm->inv_font, "[E]quip [D]rop", overlay.x + 200, (float)y_pos + 4, NORM_FONT, SMALL_CHAR_SPACE,
GOLD);
} }
} else { } else {
// Empty slot // Empty slot
snprintf(slot_text, sizeof(slot_text), "%d.", i + 1); snprintf(slot_text, sizeof(slot_text), "%d.", i + 1);
DrawTextEx(*font, slot_text, (Vector2){overlay.x + 16, y_pos + 4}, MEDIUM_FONT, SMALL_CHAR_SPACE, 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}); (Color){40, 40, 40, 255});
} }
} }
// Instructions at bottom // Instructions at bottom
const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close"; const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close";
Vector2 hint_w = MeasureTextEx(*font, hint, SMALL_FONT, NAR_CHAR_SPACE); Vector2 hint_w = MeasureTextEx(fm->inv_font, hint, SMALL_FONT, NAR_CHAR_SPACE);
DrawTextEx(*font, hint, (Vector2){overlay.x + (overlay.width - hint_w.x) / 2.0f, overlay.y + overlay.height - 22}, 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}); SMALL_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255});
} }
static Color label_color(FloatingText *ft, int alpha) { static Color label_color(FloatingText *ft, int alpha) {
@ -508,122 +575,143 @@ void render_floating_texts(FloatingText *texts, int count, int shake_x, int shak
Color color = texts[i].is_critical ? (Color){255, 200, 50, a} : (Color){255, 100, 100, a}; Color color = texts[i].is_critical ? (Color){255, 200, 50, a} : (Color){255, 100, 100, a};
char text[16]; char text[16];
snprintf(text, sizeof(text), "%d", texts[i].value); snprintf(text, sizeof(text), "%d", texts[i].value);
int text_w = MeasureText(text, 18); int text_w = MeasureText(text, FONT_SIZE_FLOAT_DMG);
DrawText(text, x - text_w / 2, y, 18, color); DrawText(text, x - text_w / 2, y, FONT_SIZE_FLOAT_DMG, 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, 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, Font *font) { int times_hit, int potions, int floors, int turns, int score, unsigned int seed,
const FontManager *fm) {
// Semi-transparent overlay // Semi-transparent overlay
Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT}; Rectangle overlay = {0, 0, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT};
DrawRectangleRec(overlay, (Color){0, 0, 0, 210}); DrawRectangleRec(overlay, (Color){0, 0, 0, 210});
// Title // Title
const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER"; const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER";
int title_font_size = 60; int title_font_size = HUGE_FONT;
Color title_color = is_victory ? GOLD : RED; Color title_color = is_victory ? GOLD : RED;
int title_width = MeasureText(title, title_font_size); int title_width = MeasureText(title, title_font_size);
DrawTextEx(*font, title, (Vector2){(SCREEN_WIDTH - title_width) / 2.0f, 30}, title_font_size, NORM_CHAR_SPACE, draw_text_body(fm->title_font, title, (float)(SCREEN_WIDTH - title_width) / 2.0f, 30.0f, title_font_size,
title_color); NORM_CHAR_SPACE, title_color);
// Stats box
int box_x = SCREEN_WIDTH / 2 - 200;
int box_y = 110;
int box_w = 400;
int box_h = 350;
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});
// Stats content
char line[64]; char line[64];
int col1_x = box_x + 20;
int col2_x = box_x + 210;
int row_y = box_y + 20;
int line_height = 24; int line_height = 24;
int label_value_gap = 10;
int col_padding = 40;
Color label_color = LIGHTGRAY; Color label_color = LIGHTGRAY;
Color value_color = WHITE; Color value_color = WHITE;
// Column 1 // Stats box
DrawTextEx(*font, "Kills:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); const char *all_labels[] = {"Kills:", "Items:", "Damage Dealt:", "Damage Taken:", "Crits:", "Times Hit:",
snprintf(line, sizeof(line), "%d", kills); "Potions:", "Floors:", "Turns:", "SCORE:", "SEED:"};
DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color); 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; row_y += line_height;
DrawTextEx(*font, "Items:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Items:", items, col1_x, row_y, LARGE_FONT, label_value_gap,
snprintf(line, sizeof(line), "%d", items); label_color, value_color);
DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color);
row_y += line_height; row_y += line_height;
DrawTextEx(*font, "Damage Dealt:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Damage Dealt:", damage_dealt, col1_x, row_y, LARGE_FONT,
snprintf(line, sizeof(line), "%d", damage_dealt); label_value_gap, label_color, value_color);
DrawTextEx(*font, line, (Vector2){col1_x + 140, row_y}, 18, NORM_CHAR_SPACE, value_color);
row_y += line_height; row_y += line_height;
DrawTextEx(*font, "Damage Taken:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Damage Taken:", damage_taken, col1_x, row_y, LARGE_FONT,
snprintf(line, sizeof(line), "%d", damage_taken); label_value_gap, label_color, value_color);
DrawTextEx(*font, line, (Vector2){col1_x + 140, row_y}, 18, NORM_CHAR_SPACE, value_color);
row_y += line_height; row_y += line_height;
DrawTextEx(*font, "Crits:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Crits:", crits, col1_x, row_y, LARGE_FONT, label_value_gap,
snprintf(line, sizeof(line), "%d", crits); label_color, value_color);
DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color);
row_y += line_height; row_y += line_height;
DrawTextEx(*font, "Times Hit:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Times Hit:", times_hit, col1_x, row_y, LARGE_FONT, label_value_gap,
snprintf(line, sizeof(line), "%d", times_hit); label_color, value_color);
DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color);
row_y += line_height; row_y += line_height;
// Column 2
int col2_row_y = box_y + 20; int col2_row_y = box_y + 20;
DrawTextEx(*font, "Potions:", (Vector2){col2_x, col2_row_y}, 18, NORM_CHAR_SPACE, label_color);
snprintf(line, sizeof(line), "%d", potions); draw_stat_line(fm->body_font, line, sizeof(line), "Potions:", potions, col2_x, col2_row_y, LARGE_FONT,
DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color); label_value_gap, label_color, value_color);
col2_row_y += line_height; col2_row_y += line_height;
DrawTextEx(*font, "Floors:", (Vector2){col2_x, col2_row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Floors:", floors, col2_x, col2_row_y, LARGE_FONT, label_value_gap,
snprintf(line, sizeof(line), "%d", floors); label_color, value_color);
DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color);
col2_row_y += line_height; col2_row_y += line_height;
DrawTextEx(*font, "Turns:", (Vector2){col2_x, col2_row_y}, 18, NORM_CHAR_SPACE, label_color); draw_stat_line(fm->body_font, line, sizeof(line), "Turns:", turns, col2_x, col2_row_y, LARGE_FONT, label_value_gap,
snprintf(line, sizeof(line), "%d", turns); label_color, value_color);
DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color);
col2_row_y += line_height; col2_row_y += line_height;
// Score: placed below the last row of the longer column (6 items, row_y is already there) // Score: placed below the last row of the longer column (6 items, row_y is already there)
row_y += 10; row_y += 10;
DrawTextEx(*font, "SCORE:", (Vector2){col1_x, row_y}, 22, NORM_CHAR_SPACE, GOLD); 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); snprintf(line, sizeof(line), "%d", score);
DrawTextEx(*font, line, (Vector2){col1_x + 90, col2_row_y}, 22, NORM_CHAR_SPACE, GOLD); 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; row_y += 35;
// Seed display // Seed display
DrawTextEx(*font, "SEED:", (Vector2){col1_x, row_y}, 18, SMALL_CHAR_SPACE, label_color); 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); snprintf(line, sizeof(line), "%u", seed);
DrawTextEx(*font, line, (Vector2){col1_x + 60, row_y}, 18, SMALL_CHAR_SPACE, END_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 // Instructions
if (is_victory) { if (is_victory) {
const char *subtitle = "Press R to play again or Q to quit"; const char *subtitle = "Press R to play again or Q to quit";
int sub_width = MeasureText(subtitle, 20); int sub_width = MeasureText(subtitle, LARGE_FONT);
DrawTextEx(*font, subtitle, (Vector2){(SCREEN_WIDTH - sub_width) / 2.0f, SCREEN_HEIGHT - 50}, 20, NORM_CHAR_SPACE, draw_text_body(fm->body_font, subtitle, (float)(SCREEN_WIDTH - sub_width) / 2.0f, (float)SCREEN_HEIGHT - 50,
LIGHTGRAY); LARGE_FONT, NORM_CHAR_SPACE, LIGHTGRAY);
} else { } else {
const char *subtitle = "Press R to restart or Q to quit"; const char *subtitle = "Press R to restart or Q to quit";
int sub_width = MeasureText(subtitle, 20); int sub_width = MeasureText(subtitle, LARGE_FONT);
DrawTextEx(*font, subtitle, (Vector2){(SCREEN_WIDTH - sub_width) / 2.0f, SCREEN_HEIGHT - 50}, 20, NORM_CHAR_SPACE, draw_text_body(fm->body_font, subtitle, (float)(SCREEN_WIDTH - sub_width) / 2.0f, (float)SCREEN_HEIGHT - 50,
LIGHTGRAY); LARGE_FONT, NORM_CHAR_SPACE, LIGHTGRAY);
} }
} }
void render_message(const char *message, Font *font) { void render_message(const char *message, const FontManager *fm) {
if (message == NULL) if (message == NULL)
return; return;
const int font_size = 20; const int font_size = NORM_FONT;
const int line_height = font_size + 4; const int line_height = font_size + 4;
const int padding_x = 20; const int padding_x = 20;
const int padding_y = 15; const int padding_y = 15;
@ -654,8 +742,8 @@ void render_message(const char *message, Font *font) {
longest_line_width = current_line_width; longest_line_width = current_line_width;
// Measure full message // Measure full message
Vector2 total_msg_width = MeasureTextEx(*font, message, font_size, NORM_CHAR_SPACE); Vector2 total_msg_width = MeasureTextEx(fm->body_font, message, font_size, NORM_CHAR_SPACE);
int box_width = total_msg_width.x + (padding_x * 2); int box_width = (int)total_msg_width.x + (padding_x * 2);
// If message is too long, use wrapped width // If message is too long, use wrapped width
if (box_width > max_box_width) { if (box_width > max_box_width) {
@ -679,7 +767,7 @@ void render_message(const char *message, Font *font) {
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255}); 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 // Draw text centered
int text_x = (SCREEN_WIDTH - total_msg_width.x) / 2; int text_x = (SCREEN_WIDTH - (int)total_msg_width.x) / 2;
int text_y = (SCREEN_HEIGHT - font_size) / 2; int text_y = (SCREEN_HEIGHT - font_size) / 2;
// For wrapped text, draw at box center with padding // For wrapped text, draw at box center with padding
@ -688,14 +776,14 @@ void render_message(const char *message, Font *font) {
text_y = (int)box_y + padding_y; text_y = (int)box_y + padding_y;
} }
DrawTextEx(*font, message, (Vector2){text_x, text_y}, font_size, NORM_CHAR_SPACE, WHITE); 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) { void render_seed_display(unsigned int seed) {
char seed_text[64]; char seed_text[64];
snprintf(seed_text, sizeof(seed_text), "Seed: %u", seed); snprintf(seed_text, sizeof(seed_text), "Seed: %u", seed);
const int font_size = 14; const int font_size = TINY_FONT;
int text_width = MeasureText(seed_text, font_size); int text_width = MeasureText(seed_text, font_size);
// Position at top right with padding // Position at top right with padding

View file

@ -1,4 +1,3 @@
#ifndef RENDER_H #ifndef RENDER_H
#define RENDER_H #define RENDER_H
@ -75,6 +74,31 @@
// FIXME: remove when player sprites are available // FIXME: remove when player sprites are available
#define PORTRAIT_BG (Color){30, 30, 45, 255} #define PORTRAIT_BG (Color){30, 30, 45, 255}
// Font manager encapsulates all loaded fonts with role-based mapping
typedef struct {
Font title_font; // Royal_Decree_Bold.ttf -- end/title screens
Font hud_font; // Tomorrow_Night.ttf -- HUD and log panels
Font body_font; // spartan_500.ttf -- body text, floating labels
Font inv_font; // Royal_Decree.ttf -- inventory overlay
} FontManager;
// Font role constants for paint_tile functions (Phase 3)
#define TILE_FONT_NONE 0
// Attempt to load a font from path; if the resulting texture is invalid (texture.id == 0),
// fall back to fallback_path. If fallback also fails, the returned Font will have
// texture.id == 0 and the caller must handle gracefully.
Font load_font_with_fallback(const char *path, int font_size, const char *fallback_path);
// Initialize a FontManager by loading all 4 available fonts with fallback chain.
// Returns 0 on complete failure (all fonts failed to load), non-zero on success
// (at least one font loaded). On partial failure, individual fields may be invalid
// (texture.id == 0); callers must check before using a given role font.
int init_fonts(FontManager *fm);
// Unload all fonts held by a FontManager
void destroy_fonts(FontManager *fm);
// Render the map tiles // Render the map tiles
void render_map(const Map *map); void render_map(const Map *map);
@ -88,23 +112,24 @@ 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]); void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]);
// Render UI overlay // Render UI overlay
void render_ui(const Player *p, Font *font); void render_ui(const Player *p, const FontManager *fm);
// Render action log (bottom left corner) // Render action log (bottom left corner)
void render_action_log(const char log[5][128], int count, int head, Font *font); void render_action_log(const char log[5][128], int count, int head, const FontManager *fm);
// Render inventory selection overlay // Render inventory selection overlay
void render_inventory_overlay(const Player *p, int selected, Font *font); void render_inventory_overlay(const Player *p, int selected, const FontManager *fm);
// Render floating damage text // Render floating damage text
void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y); void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y);
// Render end screen (victory or death) with stats breakdown // Render end screen (victory or death) with stats breakdown
void render_end_screen(int is_victory, int kills, int items, int damage_dealt, int damage_taken, int crits, 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, Font *font); int times_hit, int potions, int floors, int turns, int score, unsigned int seed,
const FontManager *fm);
// Render a message popup // Render a message popup
void render_message(const char *message, Font *font); void render_message(const char *message, const FontManager *fm);
// Render seed display at top right of screen // Render seed display at top right of screen
void render_seed_display(unsigned int seed); void render_seed_display(unsigned int seed);