From e39f4552db8039cad4812668d5d9c3dfe24bd369 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Fri, 10 Apr 2026 17:07:49 -0400 Subject: [PATCH 01/11] font: fix the popup messages with the new font handling --- src/render.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/render.c b/src/render.c index 5a4c434..6a5882a 100644 --- a/src/render.c +++ b/src/render.c @@ -168,6 +168,7 @@ void render_ui(const Player *p, Font *font) { int bar_height = 16; // HP Label, above bar + // 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); // HP Bar background @@ -351,7 +352,8 @@ void render_action_log(const char log[5][128], int count, int head, Font *font) } else { text_color = (Color){120, 110, 100, 200}; // oldest: dim } - DrawTextEx(*font, log[idx], (Vector2){text_x, text_start_y + i * line_height}, 10, NAR_CHAR_SPACE, text_color); + DrawTextEx(*font, log[idx], (Vector2){text_x, text_start_y + i * line_height}, NORM_FONT, SMALL_CHAR_SPACE, + text_color); } } } @@ -367,7 +369,7 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) { // Title 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); DrawTextEx(*font, title, (Vector2){overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10}, HUGE_FONT, NORM_CHAR_SPACE, WHITE); @@ -401,17 +403,17 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) { 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}; - DrawTextEx(*font, name, (Vector2){overlay.x + 45, y_pos + 4}, 14, NORM_CHAR_SPACE, name_color); + DrawTextEx(*font, name, (Vector2){overlay.x + 45, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, name_color); } // Power snprintf(slot_text, sizeof(slot_text), "+%d", item->power); - DrawTextEx(*font, slot_text, (Vector2){overlay.x + 150, y_pos + 4}, 14, NORM_CHAR_SPACE, YELLOW); + DrawTextEx(*font, slot_text, (Vector2){overlay.x + 150, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, YELLOW); // Action if (item->type == ITEM_POTION) { - DrawTextEx(*font, "[U]se", (Vector2){overlay.x + 200, y_pos + 4}, 14, NORM_CHAR_SPACE, GREEN); + DrawTextEx(*font, "[U]se", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GREEN); } else { - DrawTextEx(*font, "[E]quip [D]rop", (Vector2){overlay.x + 200, y_pos + 4}, 14, NORM_CHAR_SPACE, GOLD); + DrawTextEx(*font, "[E]quip [D]rop", (Vector2){overlay.x + 200, y_pos + 4}, NORM_FONT, SMALL_CHAR_SPACE, GOLD); } } else { // Empty slot @@ -423,9 +425,9 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) { // Instructions at bottom const char *hint = "[1-0] Select [E] Equip [U] Use [D] Drop [I/ESC] Close"; - Vector2 hint_w = MeasureTextEx(*font, hint, NORM_FONT, NAR_CHAR_SPACE); - DrawTextEx(*font, hint, (Vector2){overlay.x + (overlay.width - hint_w.x) / 2, overlay.y + overlay.height - 22}, - NORM_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255}); + Vector2 hint_w = MeasureTextEx(*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}, + SMALL_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255}); } static Color label_color(FloatingText *ft, int alpha) { @@ -652,8 +654,8 @@ void render_message(const char *message, Font *font) { longest_line_width = current_line_width; // Measure full message - int total_msg_width = MeasureText(message, font_size); - int box_width = total_msg_width + (padding_x * 2); + Vector2 total_msg_width = MeasureTextEx(*font, message, font_size, NORM_CHAR_SPACE); + int box_width = total_msg_width.x + (padding_x * 2); // If message is too long, use wrapped width if (box_width > max_box_width) { @@ -677,7 +679,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}); // Draw text centered - int text_x = (SCREEN_WIDTH - total_msg_width) / 2; + int text_x = (SCREEN_WIDTH - total_msg_width.x) / 2; int text_y = (SCREEN_HEIGHT - font_size) / 2; // For wrapped text, draw at box center with padding From e00424a918d3a3fe244e5e9bccea90abfd56dba9 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 22 Apr 2026 02:58:39 +0300 Subject: [PATCH 02/11] render: clean up font management; account for differing container sizes Signed-off-by: NotAShelf Change-Id: Icd554815388ec44886245406ac9ea0be6a6a6964 --- src/main.c | 26 +++-- src/render.c | 318 ++++++++++++++++++++++++++++++++------------------- src/render.h | 37 +++++- 3 files changed, 249 insertions(+), 132 deletions(-) diff --git a/src/main.c b/src/main.c index 404eaa9..e9089a5 100644 --- a/src/main.c +++ b/src/main.c @@ -431,7 +431,6 @@ static int handle_movement_input(GameState *gs) { } } - Vec2 direction = {0, 0}; if (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) direction.y = -1; @@ -448,7 +447,6 @@ static int handle_movement_input(GameState *gs) { // Reset combat event before player acts combat_reset_event(); - int new_x = gs->player.position.x + direction.x; int new_y = gs->player.position.y + direction.y; @@ -519,7 +517,7 @@ void load_audio_assets(GameState *gs) { } // Main game loop -static void game_loop(unsigned int run_seed) { +static void game_loop(unsigned int run_seed, FontManager *fm) { GameState gs; memset(&gs, 0, sizeof(GameState)); gs.run_seed = run_seed; @@ -527,7 +525,7 @@ static void game_loop(unsigned int run_seed) { // sound load_audio_assets(&gs); // font - Font fontTTF = LoadFontEx("./assets/fonts/spartan_500.ttf", 36, NULL, 0); + init_fonts(fm); // Initialize first floor init_floor(&gs, 1); @@ -553,6 +551,7 @@ static void game_loop(unsigned int run_seed) { gs.game_over = 0; gs.game_won = 0; load_audio_assets(&gs); + init_fonts(fm); init_floor(&gs, 1); // Update window title with new seed char title[128]; @@ -585,19 +584,19 @@ static void game_loop(unsigned int run_seed) { // Floating texts follow world shake 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 - 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 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 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 @@ -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, 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(); @@ -621,6 +620,9 @@ static void game_loop(unsigned int run_seed) { // small delay for key repeat control WaitTime(0.08); } + + // Cleanup + destroy_fonts(fm); } // Check if a string is a valid unsigned integer @@ -683,11 +685,13 @@ int main(int argc, char **argv) { SetTargetFPS(60); // Run game - game_loop(run_seed); + FontManager fm; + init_fonts(&fm); + game_loop(run_seed, &fm); // Cleanup CloseWindow(); audio_close(); return 0; -} +} \ No newline at end of file diff --git a/src/render.c b/src/render.c index 6a5882a..4dfb602 100644 --- a/src/render.c +++ b/src/render.c @@ -5,6 +5,64 @@ #include #include +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) { for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { @@ -31,9 +89,9 @@ void render_map(const Map *map) { case TILE_STAIRS: DrawRectangleRec(rect, stairs_color); 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 - 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; } } @@ -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 const int hud_y = MAP_HEIGHT * TILE_SIZE; const int hud_height = 60; @@ -145,10 +203,10 @@ void render_ui(const Player *p, Font *font) { 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}); + 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}); + 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; @@ -169,7 +227,7 @@ void render_ui(const Player *p, Font *font) { // HP Label, above bar // 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 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 char hp_text[32]; snprintf(hp_text, sizeof(hp_text), "%d/%d", p->hp, p->max_hp); - int hp_text_w = MeasureText(hp_text, 12); - DrawTextEx(*font, hp_text, (Vector2){bar_x + (bar_width - hp_text_w) / 2.0f, bar_y + 2}, MEDIUM_FONT, - SMALL_CHAR_SPACE, WHITE); + 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; @@ -231,7 +289,7 @@ void render_ui(const Player *p, Font *font) { if (p->effects[i].duration > 0) { char eff_text[16]; 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; } } @@ -243,68 +301,76 @@ void render_ui(const Player *p, Font *font) { // Floor char floor_text[16]; 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); - DrawTextEx(*font, "Floor", (Vector2){stats_x, stats_y + 16}, 12, NAR_CHAR_SPACE, text_dim); + 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); - DrawTextEx(*font, atk_text, (Vector2){stats_x + stat_spacing, stats_y}, 16, NORM_CHAR_SPACE, YELLOW); - DrawTextEx(*font, "ATK", (Vector2){stats_x + stat_spacing, stats_y + 16}, 12, NAR_CHAR_SPACE, text_dim); + 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); - DrawTextEx(*font, def_text, (Vector2){stats_x + stat_spacing * 2, stats_y}, 16, 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_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 - 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) { 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)); - 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 { - 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 - 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) { 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); - 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 { - 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_y = hud_y + 14; - DrawTextEx(*font, "[WASD] Move [G] Pickup [I] Inventory [U] Use", (Vector2){ctrl_x, ctrl_y}, 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, - (Color){139, 119, 89, 255}); + 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, 10); - DrawTextEx(*font, inv_text, (Vector2){SCREEN_WIDTH - inv_width - 10, hud_y + 5}, 10, NAR_CHAR_SPACE, GREEN); + 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, Font *font) { +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; @@ -326,8 +392,8 @@ void render_action_log(const char log[5][128], int count, int head, Font *font) // Title bar 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, - (Color){180, 160, 130, 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); @@ -352,13 +418,13 @@ void render_action_log(const char log[5][128], int count, int head, Font *font) } else { 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, - text_color); + 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, Font *font) { +void render_inventory_overlay(const Player *p, int selected, const FontManager *fm) { // Overlay dimensions int ov_width = 360; int ov_height = 320; @@ -370,14 +436,14 @@ void render_inventory_overlay(const Player *p, int selected, Font *font) { // Title const char *title = "INVENTORY"; // int title_w = MeasureText(title, 24); - Vector2 t_w = MeasureTextEx(*font, title, 30, NORM_CHAR_SPACE); - DrawTextEx(*font, title, (Vector2){overlay.x + (overlay.width - t_w.x) / 2, overlay.y + 10}, HUGE_FONT, - NORM_CHAR_SPACE, WHITE); + 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 = overlay.y + 40; + int start_y = (int)overlay.y + 40; for (int i = 0; i < MAX_INVENTORY; i++) { 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 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, - (Color){80, 80, 80, 255}); + 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); @@ -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} : (item->type == ITEM_WEAPON) ? (Color){255, 255, 140, 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 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 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 { - 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 { // Empty slot 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, - (Color){40, 40, 40, 255}); + 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(*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}, - SMALL_FONT, NAR_CHAR_SPACE, (Color){80, 80, 80, 255}); + 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) { @@ -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}; char text[16]; snprintf(text, sizeof(text), "%d", texts[i].value); - int text_w = MeasureText(text, 18); - DrawText(text, x - text_w / 2, y, 18, color); + int text_w = MeasureText(text, FONT_SIZE_FLOAT_DMG); + 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, - 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 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 = 60; + int title_font_size = HUGE_FONT; Color title_color = is_victory ? GOLD : RED; 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, - title_color); + draw_text_body(fm->title_font, title, (float)(SCREEN_WIDTH - title_width) / 2.0f, 30.0f, title_font_size, + 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]; - int col1_x = box_x + 20; - int col2_x = box_x + 210; - int row_y = box_y + 20; int line_height = 24; + int label_value_gap = 10; + int col_padding = 40; Color label_color = LIGHTGRAY; Color value_color = WHITE; - // Column 1 - DrawTextEx(*font, "Kills:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", kills); - DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color); + // 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; - DrawTextEx(*font, "Items:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", items); - DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - DrawTextEx(*font, "Damage Dealt:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", damage_dealt); - DrawTextEx(*font, line, (Vector2){col1_x + 140, row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - DrawTextEx(*font, "Damage Taken:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", damage_taken); - DrawTextEx(*font, line, (Vector2){col1_x + 140, row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - DrawTextEx(*font, "Crits:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", crits); - DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - DrawTextEx(*font, "Times Hit:", (Vector2){col1_x, row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", times_hit); - DrawTextEx(*font, line, (Vector2){col1_x + 80, row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - // Column 2 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); - DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color); + + 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; - DrawTextEx(*font, "Floors:", (Vector2){col2_x, col2_row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", floors); - DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - DrawTextEx(*font, "Turns:", (Vector2){col2_x, col2_row_y}, 18, NORM_CHAR_SPACE, label_color); - snprintf(line, sizeof(line), "%d", turns); - DrawTextEx(*font, line, (Vector2){col2_x + 80, col2_row_y}, 18, NORM_CHAR_SPACE, value_color); + 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; - 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); - 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; // 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); - 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 if (is_victory) { const char *subtitle = "Press R to play again or Q to quit"; - int sub_width = MeasureText(subtitle, 20); - DrawTextEx(*font, subtitle, (Vector2){(SCREEN_WIDTH - sub_width) / 2.0f, SCREEN_HEIGHT - 50}, 20, NORM_CHAR_SPACE, - LIGHTGRAY); + 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, 20); - DrawTextEx(*font, subtitle, (Vector2){(SCREEN_WIDTH - sub_width) / 2.0f, SCREEN_HEIGHT - 50}, 20, NORM_CHAR_SPACE, - LIGHTGRAY); + 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, Font *font) { +void render_message(const char *message, const FontManager *fm) { if (message == NULL) return; - const int font_size = 20; + const int font_size = NORM_FONT; const int line_height = font_size + 4; const int padding_x = 20; const int padding_y = 15; @@ -654,8 +742,8 @@ void render_message(const char *message, Font *font) { longest_line_width = current_line_width; // Measure full message - Vector2 total_msg_width = MeasureTextEx(*font, message, font_size, NORM_CHAR_SPACE); - int box_width = total_msg_width.x + (padding_x * 2); + 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) { @@ -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}); // 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; // 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; } - 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) { char seed_text[64]; 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); // Position at top right with padding diff --git a/src/render.h b/src/render.h index 3dd376a..d29b273 100644 --- a/src/render.h +++ b/src/render.h @@ -1,4 +1,3 @@ - #ifndef RENDER_H #define RENDER_H @@ -75,6 +74,31 @@ // FIXME: remove when player sprites are available #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 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]); // 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) -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 -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 void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y); // 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, - 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 -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 void render_seed_display(unsigned int seed); From ceb657add8d5ab6d422dda5cc2eef9acd79f81ed Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Wed, 22 Apr 2026 20:03:23 +0300 Subject: [PATCH 03/11] tileset: initial GPU texture atlas w/ procedural sprite painting Signed-off-by: NotAShelf Change-Id: I7b6991f7342362033ad72ab4700fdb9f6a6a6964 --- build.zig | 23 + libs/tileset/tileset.c | 118 +++++ libs/tileset/tileset.h | 91 ++++ libs/tileset/tileset_paint.c | 873 +++++++++++++++++++++++++++++++++++ libs/tileset/tileset_paint.h | 66 +++ 5 files changed, 1171 insertions(+) create mode 100644 libs/tileset/tileset.c create mode 100644 libs/tileset/tileset.h create mode 100644 libs/tileset/tileset_paint.c create mode 100644 libs/tileset/tileset_paint.h diff --git a/build.zig b/build.zig index 76a85b1..e63af2c 100644 --- a/build.zig +++ b/build.zig @@ -49,6 +49,28 @@ pub fn build(b: *std.Build) void { // utils.h is co-located with map.c map_lib.addIncludePath(b.path("libs/map")); + // Tileset library + const tileset_obj = b.addObject(.{ + .name = "tileset", + .root_module = b.createModule(.{ + .target = target, + .optimize = optimize, + .link_libc = true, + }), + }); + tileset_obj.addCSourceFiles(.{ + .files = &[_][]const u8{ + "libs/tileset/tileset.c", + "libs/tileset/tileset_paint.c", + }, + .flags = &c_flags, + }); + // tileset.h includes settings.h which lives in src/ + tileset_obj.addIncludePath(b.path("src")); + // tileset.c includes tileset.h which is co-located + tileset_obj.addIncludePath(b.path("libs/tileset")); + tileset_obj.linkSystemLibrary("raylib"); + // Zig combat library. This must be compiled as an object and linked // directly to bypassing the archive step, or it yields a corrupt // archive that forces the user to clear the cache each time. @@ -98,6 +120,7 @@ pub fn build(b: *std.Build) void { exe.linkLibrary(rng_lib); exe.linkLibrary(map_lib); + exe.addObject(tileset_obj); exe.addObject(combat_obj); exe.linkSystemLibrary("raylib"); exe.linkSystemLibrary("m"); diff --git a/libs/tileset/tileset.c b/libs/tileset/tileset.c new file mode 100644 index 0000000..734899f --- /dev/null +++ b/libs/tileset/tileset.c @@ -0,0 +1,118 @@ +#include "tileset.h" +#include +#include +#include + +int tileset_init(Tileset *ts, int tile_w, int tile_h) { + if (ts == NULL) + return 0; + if (tile_w <= 0 || tile_h <= 0) + return 0; + + memset(ts, 0, sizeof(Tileset)); + + ts->tile_w = tile_w; + ts->tile_h = tile_h; + + // Compute grid dimensions to fit MAX_TILE_ID tiles + ts->atlas_cols = 4; // 4 columns + ts->atlas_rows = (MAX_TILE_ID + ts->atlas_cols - 1) / ts->atlas_cols; // round up + + int atlas_w = ts->atlas_cols * tile_w; + int atlas_h = ts->atlas_rows * tile_h; + + // Validate atlas dimensions are reasonable + if (atlas_w <= 0 || atlas_h <= 0 || atlas_w > 4096 || atlas_h > 4096) + return 0; + + ts->render_target = LoadRenderTexture(atlas_w, atlas_h); + if (!IsRenderTextureValid(ts->render_target)) + return 0; + + // Clear to transparent so unpainted regions don't show artifacts + BeginTextureMode(ts->render_target); + ClearBackground(BLANK); + EndTextureMode(); + + ts->finalized = 0; + ts->tile_count = 0; + return 1; +} + +int tileset_register(Tileset *ts, int id) { + if (ts == NULL) + return 0; + if (id < 0 || id >= MAX_TILE_ID) + return 0; + if (ts->render_target.id == 0) + return 0; + if (ts->finalized) + return 0; + if (ts->regions[id].width != 0) + return 0; // already registered + + int col = id % ts->atlas_cols; + int row = id / ts->atlas_cols; + + ts->regions[id] = + (Rectangle){(float)(col * ts->tile_w), (float)(row * ts->tile_h), (float)ts->tile_w, (float)ts->tile_h}; + ts->tile_count++; + return 1; +} + +int tileset_finalize(Tileset *ts) { + if (ts == NULL) + return 0; + if (ts->render_target.id == 0) + return 0; + if (ts->finalized) + return 1; // already finalized + + // Convert RenderTexture to regular Texture2D + // RenderTexture textures are flipped vertically in raylib, so we need to handle that + Texture2D old_texture = ts->render_target.texture; + + // Create a new texture from the render texture data + Image img = LoadImageFromTexture(old_texture); + if (img.data == NULL) { + return 0; + } + + // Flip image vertically because RenderTexture is upside-down + ImageFlipVertical(&img); + + Texture2D new_tex = LoadTextureFromImage(img); + UnloadImage(img); + + if (new_tex.id == 0) { + return 0; + } + + // Unload the old render texture and replace with the new regular texture + UnloadRenderTexture(ts->render_target); + ts->render_target.id = 0; + ts->atlas = new_tex; + ts->finalized = 1; + return 1; +} + +Rectangle tileset_get_region(const Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return (Rectangle){0, 0, 0, 0}; + if (!ts->finalized) + return (Rectangle){0, 0, 0, 0}; + return ts->regions[id]; +} + +void tileset_destroy(Tileset *ts) { + if (ts == NULL) + return; + if (ts->finalized) { + if (ts->atlas.id != 0) + UnloadTexture(ts->atlas); + } else { + if (ts->render_target.id != 0) + UnloadRenderTexture(ts->render_target); + } + memset(ts, 0, sizeof(Tileset)); +} diff --git a/libs/tileset/tileset.h b/libs/tileset/tileset.h new file mode 100644 index 0000000..9cbe4cc --- /dev/null +++ b/libs/tileset/tileset.h @@ -0,0 +1,91 @@ +#ifndef TILESET_H +#define TILESET_H + +#include "settings.h" +#include + +// Maximum number of tiles that can be registered in a single atlas +#define MAX_TILE_ID 32 + +// Tile IDs for map tiles (variants for visual variety) +#define TILE_WALL_0 0 +#define TILE_WALL_1 1 +#define TILE_FLOOR_0 2 +#define TILE_FLOOR_1 3 +#define TILE_FLOOR_2 4 +#define TILE_FLOOR_3 5 +#define TILE_STAIRS_SPRITE 6 + +// Sprite IDs for entities +#define SPRITE_PLAYER 7 +#define SPRITE_PLAYER_WALK_0 8 +#define SPRITE_PLAYER_WALK_1 9 +#define SPRITE_PLAYER_ATTACK 10 +#define SPRITE_ENEMY_GOBLIN 11 +#define SPRITE_ENEMY_GOBLIN_WALK_0 12 +#define SPRITE_ENEMY_GOBLIN_WALK_1 13 +#define SPRITE_ENEMY_GOBLIN_ATTACK 14 +#define SPRITE_ENEMY_SKELETON 15 +#define SPRITE_ENEMY_SKELETON_WALK_0 16 +#define SPRITE_ENEMY_SKELETON_WALK_1 17 +#define SPRITE_ENEMY_SKELETON_ATTACK 18 +#define SPRITE_ENEMY_ORC 19 +#define SPRITE_ENEMY_ORC_WALK_0 20 +#define SPRITE_ENEMY_ORC_WALK_1 21 +#define SPRITE_ENEMY_ORC_ATTACK 22 +#define SPRITE_ITEM_POTION 23 +#define SPRITE_ITEM_WEAPON 24 +#define SPRITE_ITEM_ARMOR 25 + +// Door tiles +#define TILE_DOOR_CLOSED_SPRITE 26 +#define TILE_DOOR_OPEN_SPRITE 27 + +// Effect/status sprites +#define SPRITE_EFFECT_BURN 28 +#define SPRITE_EFFECT_POISON 29 +#define SPRITE_EFFECT_BLOCK 30 +#define SPRITE_SLASH_EFFECT 31 + +// Total count of defined tiles +#define NUM_TILE_IDS 32 + +// Tileset encapsulates a GPU texture atlas with sub-rectangle regions per tile ID. +// The atlas is built at startup by painting into a RenderTexture, then finalized +// into a regular Texture2D for efficient drawing via DrawTexturePro. +typedef struct { + RenderTexture2D render_target; // RenderTexture for painting (valid before finalize) + Texture2D atlas; // GPU texture (valid after finalize) + int tile_w; // width of each tile in pixels + int tile_h; // height of each tile in pixels + Rectangle regions[MAX_TILE_ID]; // sub-rectangles within atlas for each tile ID + int tile_count; // number of registered tiles + int atlas_cols; // number of columns in the atlas grid + int atlas_rows; // number of rows in the atlas grid + int finalized; // 1 after tileset_finalize called, 0 otherwise +} Tileset; + +// Initialize a tileset with the given tile dimensions. +// Computes atlas grid size based on MAX_TILE_ID and allocates a RenderTexture. +// Returns 0 on failure (e.g., RenderTexture allocation failed), non-zero on success. +int tileset_init(Tileset *ts, int tile_w, int tile_h); + +// Register a tile ID with its atlas region. +// The region is computed automatically based on tile_w/tile_h and the ID index. +// Returns 0 if the ID is out of bounds or already registered, non-zero on success. +int tileset_register(Tileset *ts, int id); + +// Finalize the tileset: converts the internal RenderTexture into a regular Texture2D +// suitable for DrawTexturePro. After this call, painting functions must not be used. +// Returns 0 on failure, non-zero on success. +int tileset_finalize(Tileset *ts); + +// Get the atlas sub-rectangle for a given tile ID. +// Returns a zeroed Rectangle if the ID is invalid or not registered. +Rectangle tileset_get_region(const Tileset *ts, int id); + +// Destroy a tileset, unloading the atlas texture and zeroing the struct. +// Safe to call on a zero-initialized or already-destroyed tileset. +void tileset_destroy(Tileset *ts); + +#endif // TILESET_H diff --git a/libs/tileset/tileset_paint.c b/libs/tileset/tileset_paint.c new file mode 100644 index 0000000..0925345 --- /dev/null +++ b/libs/tileset/tileset_paint.c @@ -0,0 +1,873 @@ +#include "tileset_paint.h" +#include +#include + +// Simple LCG for deterministic noise (seeded by position) +static unsigned int lcg_seed = 0; +static void lcg_srand(unsigned int seed) { + lcg_seed = seed; +} +static unsigned int lcg_rand(void) { + lcg_seed = lcg_seed * 1103515245 + 12345; + return lcg_seed; +} + +static int lcg_rand_range(int min, int max) { + if (max <= min) + return min; + return min + (int)(lcg_rand() % (unsigned int)(max - min + 1)); +} + +// Get the RenderTexture target for painting a specific tile ID +static RenderTexture2D get_target(Tileset *ts) { + return ts->render_target; +} + +// Compute screen offset for a tile ID within the atlas +static Vector2 get_paint_offset(Tileset *ts, int id) { + int col = id % ts->atlas_cols; + int row = id / ts->atlas_cols; + return (Vector2){(float)(col * ts->tile_w), (float)(row * ts->tile_h)}; +} + +void paint_wall_tile(Tileset *ts, int id, int variant) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + + BeginTextureMode(ts->render_target); + + // Base color is much darker for better contrast against floor + Color base = variant == 0 ? (Color){45, 42, 38, 255} : (Color){35, 32, 28, 255}; + DrawRectangle((int)off.x, (int)off.y, w, h, base); + + // Brick pattern + int brick_h = h / 3; + int brick_w = w / 2; + Color mortar = (Color){25, 22, 18, 255}; + Color brick_light = variant == 0 ? (Color){70, 65, 60, 255} : (Color){50, 47, 43, 255}; + Color brick_dark = variant == 0 ? (Color){40, 37, 33, 255} : (Color){30, 27, 24, 255}; + + for (int row = 0; row < 3; row++) { + int y = (int)off.y + row * brick_h; + int offset_x = (row % 2 == 0) ? 0 : brick_w / 2; + for (int col = -1; col < 3; col++) { + int x = (int)off.x + offset_x + col * brick_w; + if (x >= (int)off.x + w) + break; + if (x + brick_w <= (int)off.x) + continue; + + // Clip to tile bounds + int draw_x = x < (int)off.x ? (int)off.x : x; + int draw_w = brick_w; + if (draw_x + draw_w > (int)off.x + w) + draw_w = (int)off.x + w - draw_x; + + Color c = ((col + row) % 2 == 0) ? brick_light : brick_dark; + DrawRectangle(draw_x, y, draw_w, brick_h - 1, c); + } + } + + // Mortar lines + for (int row = 1; row < 3; row++) { + int y = (int)off.y + row * brick_h; + DrawLine((int)off.x, y, (int)off.x + w - 1, y, mortar); + } + + EndTextureMode(); +} + +void paint_floor_tile(Tileset *ts, int id, int variant) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + + BeginTextureMode(ts->render_target); + + // Base stone color - lighter than walls for contrast + Color base = (Color){75, 72, 68, 255}; + DrawRectangle((int)off.x, (int)off.y, w, h, base); + + // Seeded noise based on variant + lcg_srand((unsigned int)(variant * 7919 + id * 104729)); + + // Dithered noise dots - lighter shades + int num_dots = 8 + variant * 4; + for (int i = 0; i < num_dots; i++) { + int px = (int)off.x + lcg_rand_range(1, w - 2); + int py = (int)off.y + lcg_rand_range(1, h - 2); + int shade = lcg_rand_range(0, 3); + Color c; + if (shade == 0) + c = (Color){85, 82, 78, 255}; + else if (shade == 1) + c = (Color){65, 62, 58, 255}; + else + c = (Color){90, 87, 83, 255}; + DrawPixel(px, py, c); + } + + // Occasional crack line + if (variant >= 2) { + int crack_x = (int)off.x + lcg_rand_range(2, w - 3); + int crack_y = (int)off.y + lcg_rand_range(2, h - 3); + int crack_len = lcg_rand_range(2, 4); + int crack_dir = lcg_rand_range(0, 1); // 0 = horizontal, 1 = vertical + Color crack_color = (Color){55, 52, 48, 255}; + for (int i = 0; i < crack_len; i++) { + if (crack_dir == 0) + DrawPixel(crack_x + i, crack_y, crack_color); + else + DrawPixel(crack_x, crack_y + i, crack_color); + } + } + + EndTextureMode(); +} + +void paint_stairs_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + + BeginTextureMode(ts->render_target); + + // Dark stone background + DrawRectangle((int)off.x, (int)off.y, w, h, (Color){40, 38, 35, 255}); + + // Stair steps (3 steps) + int step_h = h / 4; + Color step_light = (Color){100, 95, 90, 255}; + Color step_dark = (Color){60, 57, 53, 255}; + + for (int i = 0; i < 3; i++) { + int y = (int)off.y + h - (i + 1) * step_h; + int inset = i * 2; + int x = (int)off.x + inset; + DrawRectangle(x, y, w - inset * 2, step_h - 1, step_light); + DrawLine(x, y + step_h - 1, x + w - inset * 2 - 1, y + step_h - 1, step_dark); + } + + // ">" symbol on top step + int top_y = (int)off.y + 2; + int cx = (int)off.x + w / 2; + Color arrow = (Color){180, 175, 170, 255}; + DrawPixel(cx, top_y, arrow); + DrawPixel(cx - 1, top_y + 1, arrow); + DrawPixel(cx + 1, top_y + 1, arrow); + DrawPixel(cx - 2, top_y + 2, arrow); + DrawPixel(cx + 2, top_y + 2, arrow); + DrawPixel(cx, top_y + 3, arrow); + + EndTextureMode(); +} + +static void draw_player_base(Tileset *ts, int id, int leg_offset_left, int leg_offset_right, int arm_offset_left, + int arm_offset_right) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + + // Clear to transparent + DrawRectangle(ox, oy, w, h, BLANK); + + // Simple adventurer silhouette (16x16) + Color skin = (Color){230, 200, 160, 255}; + Color tunic = (Color){60, 100, 180, 255}; + Color pants = (Color){80, 60, 40, 255}; + Color boots = (Color){50, 40, 30, 255}; + Color hair = (Color){120, 80, 40, 255}; + + // Head (3x3) + DrawRectangle(ox + 6, oy + 2, 4, 3, skin); + // Hair + DrawRectangle(ox + 6, oy + 1, 4, 1, hair); + DrawPixel(ox + 5, oy + 2, hair); + DrawPixel(ox + 10, oy + 2, hair); + + // Body/tunic (4x5) + DrawRectangle(ox + 5, oy + 5, 6, 5, tunic); + // Belt + DrawRectangle(ox + 5, oy + 9, 6, 1, (Color){120, 80, 30, 255}); + + // Legs with offset for walking animation + DrawRectangle(ox + 6 + leg_offset_left, oy + 10, 2, 4, pants); + DrawRectangle(ox + 8 + leg_offset_right, oy + 10, 2, 4, pants); + + // Boots with offset + DrawRectangle(ox + 6 + leg_offset_left, oy + 14, 2, 2, boots); + DrawRectangle(ox + 8 + leg_offset_right, oy + 14, 2, 2, boots); + + // Arms with offset + DrawRectangle(ox + 3 + arm_offset_left, oy + 6, 2, 3, skin); + DrawRectangle(ox + 11 + arm_offset_right, oy + 6, 2, 3, skin); + + EndTextureMode(); +} + +void paint_player_tile(Tileset *ts, int id) { + // Idle pose - no offsets + draw_player_base(ts, id, 0, 0, 0, 0); +} + +void paint_player_walk_tile(Tileset *ts, int id, int frame) { + // Frame 0: left leg forward, right leg back + // Frame 1: right leg forward, left leg back + int leg_left = (frame == 0) ? -1 : 1; + int leg_right = (frame == 0) ? 1 : -1; + int arm_left = (frame == 0) ? 1 : -1; + int arm_right = (frame == 0) ? -1 : 1; + draw_player_base(ts, id, leg_left, leg_right, arm_left, arm_right); +} + +void paint_player_attack_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + + // Clear to transparent + DrawRectangle(ox, oy, w, h, BLANK); + + // Attack pose - lunging forward with sword arm extended + Color skin = (Color){230, 200, 160, 255}; + Color tunic = (Color){60, 100, 180, 255}; + Color pants = (Color){80, 60, 40, 255}; + Color boots = (Color){50, 40, 30, 255}; + Color hair = (Color){120, 80, 40, 255}; + Color steel = (Color){180, 180, 190, 255}; + + // Head (3x3) + DrawRectangle(ox + 7, oy + 2, 4, 3, skin); + // Hair + DrawRectangle(ox + 7, oy + 1, 4, 1, hair); + DrawPixel(ox + 6, oy + 2, hair); + DrawPixel(ox + 11, oy + 2, hair); + + // Body/tunic (4x5) - shifted right for lunge + DrawRectangle(ox + 6, oy + 5, 6, 5, tunic); + // Belt + DrawRectangle(ox + 6, oy + 9, 6, 1, (Color){120, 80, 30, 255}); + + // Legs - left forward, right back + DrawRectangle(ox + 5, oy + 10, 2, 4, pants); + DrawRectangle(ox + 9, oy + 10, 2, 4, pants); + + // Boots + DrawRectangle(ox + 5, oy + 14, 2, 2, boots); + DrawRectangle(ox + 9, oy + 14, 2, 2, boots); + + // Left arm (back) + DrawRectangle(ox + 4, oy + 6, 2, 3, skin); + + // Right arm extended forward with sword + DrawRectangle(ox + 12, oy + 6, 3, 2, skin); + // Sword blade + DrawRectangle(ox + 14, oy + 4, 1, 6, steel); + // Sword hilt + DrawRectangle(ox + 13, oy + 7, 3, 1, (Color){100, 80, 40, 255}); + + EndTextureMode(); +} + +// Draw goblin base with configurable leg/arm offsets +static void draw_goblin_base(Tileset *ts, int id, int leg_left, int leg_right, int arm_left, int arm_right) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + Color skin = (Color){80, 140, 60, 255}; + Color dark = (Color){50, 90, 35, 255}; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Head (large, hunched forward) + DrawRectangle(ox + 4, oy + 3, 8, 6, skin); + // Eyes (angry) + DrawPixel(ox + 5, oy + 5, (Color){200, 50, 50, 255}); + DrawPixel(ox + 10, oy + 5, (Color){200, 50, 50, 255}); + // Ears (pointy) + DrawPixel(ox + 3, oy + 4, skin); + DrawPixel(ox + 12, oy + 4, skin); + // Body (small, hunched) + DrawRectangle(ox + 5, oy + 9, 6, 4, dark); + // Legs with offsets + DrawRectangle(ox + 5 + leg_left, oy + 13, 2, 3, skin); + DrawRectangle(ox + 9 + leg_right, oy + 13, 2, 3, skin); + // Arms with offsets + DrawRectangle(ox + 3 + arm_left, oy + 10, 2, 2, skin); + DrawRectangle(ox + 11 + arm_right, oy + 10, 2, 2, skin); + + EndTextureMode(); +} + +// Draw skeleton base with configurable leg/arm offsets +static void draw_skeleton_base(Tileset *ts, int id, int leg_left, int leg_right, int arm_left, int arm_right) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + Color bone = (Color){220, 220, 210, 255}; + Color dark_bone = (Color){180, 180, 170, 255}; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Skull + DrawRectangle(ox + 5, oy + 2, 6, 5, bone); + // Eye sockets + DrawPixel(ox + 6, oy + 4, (Color){20, 20, 20, 255}); + DrawPixel(ox + 9, oy + 4, (Color){20, 20, 20, 255}); + // Ribs + for (int i = 0; i < 3; i++) { + DrawRectangle(ox + 4, oy + 8 + i * 2, 8, 1, bone); + } + // Spine + DrawRectangle(ox + 7, oy + 7, 2, 6, dark_bone); + // Legs with offsets + DrawRectangle(ox + 5 + leg_left, oy + 13, 2, 3, bone); + DrawRectangle(ox + 9 + leg_right, oy + 13, 2, 3, bone); + // Arms with offsets + DrawRectangle(ox + 3 + arm_left, oy + 8, 2, 3, bone); + DrawRectangle(ox + 11 + arm_right, oy + 8, 2, 3, bone); + + EndTextureMode(); +} + +// Draw orc base with configurable leg/arm offsets +static void draw_orc_base(Tileset *ts, int id, int leg_left, int leg_right, int arm_left, int arm_right) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + Color skin = (Color){60, 100, 45, 255}; + Color dark = (Color){40, 70, 30, 255}; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Large head + DrawRectangle(ox + 3, oy + 2, 10, 7, skin); + // Small angry eyes + DrawPixel(ox + 5, oy + 5, (Color){250, 250, 50, 255}); + DrawPixel(ox + 10, oy + 5, (Color){250, 250, 50, 255}); + // Tusks + DrawPixel(ox + 6, oy + 7, (Color){240, 240, 220, 255}); + DrawPixel(ox + 9, oy + 7, (Color){240, 240, 220, 255}); + // Broad body + DrawRectangle(ox + 3, oy + 9, 10, 5, dark); + // Thick legs with offsets + DrawRectangle(ox + 4 + leg_left, oy + 14, 3, 2, skin); + DrawRectangle(ox + 9 + leg_right, oy + 14, 3, 2, skin); + // Thick arms with offsets + DrawRectangle(ox + 1 + arm_left, oy + 10, 3, 3, skin); + DrawRectangle(ox + 12 + arm_right, oy + 10, 3, 3, skin); + + EndTextureMode(); +} + +void paint_enemy_tile(Tileset *ts, int id, int enemy_type) { + // Idle pose - no offsets + switch (enemy_type) { + case 0: + draw_goblin_base(ts, id, 0, 0, 0, 0); + break; + case 1: + draw_skeleton_base(ts, id, 0, 0, 0, 0); + break; + case 2: + draw_orc_base(ts, id, 0, 0, 0, 0); + break; + default: + break; + } +} + +void paint_enemy_walk_tile(Tileset *ts, int id, int enemy_type, int frame) { + // Frame 0: left leg forward, right leg back + // Frame 1: right leg forward, left leg back + int leg_left = (frame == 0) ? -1 : 1; + int leg_right = (frame == 0) ? 1 : -1; + int arm_left = (frame == 0) ? 1 : -1; + int arm_right = (frame == 0) ? -1 : 1; + + switch (enemy_type) { + case 0: + draw_goblin_base(ts, id, leg_left, leg_right, arm_left, arm_right); + break; + case 1: + draw_skeleton_base(ts, id, leg_left, leg_right, arm_left, arm_right); + break; + case 2: + draw_orc_base(ts, id, leg_left, leg_right, arm_left, arm_right); + break; + default: + break; + } +} + +void paint_enemy_attack_tile(Tileset *ts, int id, int enemy_type) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + switch (enemy_type) { + case 0: { + // Goblin attack - lunging with dagger + Color skin = (Color){80, 140, 60, 255}; + Color dark = (Color){50, 90, 35, 255}; + Color steel = (Color){180, 180, 190, 255}; + // Head (leaning forward) + DrawRectangle(ox + 6, oy + 3, 8, 6, skin); + DrawPixel(ox + 7, oy + 5, (Color){200, 50, 50, 255}); + DrawPixel(ox + 12, oy + 5, (Color){200, 50, 50, 255}); + // Body + DrawRectangle(ox + 7, oy + 9, 6, 4, dark); + // Left leg back + DrawRectangle(ox + 5, oy + 13, 2, 3, skin); + // Right leg forward + DrawRectangle(ox + 11, oy + 13, 2, 3, skin); + // Left arm back + DrawRectangle(ox + 4, oy + 10, 2, 2, skin); + // Right arm extended with dagger + DrawRectangle(ox + 14, oy + 9, 2, 2, skin); + DrawRectangle(ox + 15, oy + 7, 1, 4, steel); + break; + } + case 1: { + // Skeleton attack - swinging sword + Color bone = (Color){220, 220, 210, 255}; + Color dark_bone = (Color){180, 180, 170, 255}; + Color steel = (Color){180, 180, 190, 255}; + // Skull + DrawRectangle(ox + 6, oy + 2, 6, 5, bone); + DrawPixel(ox + 7, oy + 4, (Color){20, 20, 20, 255}); + DrawPixel(ox + 10, oy + 4, (Color){20, 20, 20, 255}); + // Ribs + for (int i = 0; i < 3; i++) { + DrawRectangle(ox + 5, oy + 8 + i * 2, 8, 1, bone); + } + // Spine + DrawRectangle(ox + 8, oy + 7, 2, 6, dark_bone); + // Legs + DrawRectangle(ox + 6, oy + 13, 2, 3, bone); + DrawRectangle(ox + 10, oy + 13, 2, 3, bone); + // Left arm back + DrawRectangle(ox + 4, oy + 8, 2, 3, bone); + // Right arm extended with sword + DrawRectangle(ox + 13, oy + 7, 3, 2, bone); + DrawRectangle(ox + 15, oy + 5, 1, 6, steel); + break; + } + case 2: { + // Orc attack - overhead smash + Color skin = (Color){60, 100, 45, 255}; + Color dark = (Color){40, 70, 30, 255}; + Color steel = (Color){180, 180, 190, 255}; + // Head + DrawRectangle(ox + 4, oy + 2, 10, 7, skin); + DrawPixel(ox + 6, oy + 5, (Color){250, 250, 50, 255}); + DrawPixel(ox + 11, oy + 5, (Color){250, 250, 50, 255}); + // Tusks + DrawPixel(ox + 7, oy + 7, (Color){240, 240, 220, 255}); + DrawPixel(ox + 10, oy + 7, (Color){240, 240, 220, 255}); + // Body + DrawRectangle(ox + 4, oy + 9, 10, 5, dark); + // Legs + DrawRectangle(ox + 5, oy + 14, 3, 2, skin); + DrawRectangle(ox + 10, oy + 14, 3, 2, skin); + // Left arm back + DrawRectangle(ox + 2, oy + 10, 3, 3, skin); + // Right arm raised with club + DrawRectangle(ox + 13, oy + 4, 3, 3, skin); + DrawRectangle(ox + 14, oy + 1, 2, 5, steel); + break; + } + default: + break; + } + + EndTextureMode(); +} + +void paint_item_tile(Tileset *ts, int id, int item_type) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int w = ts->tile_w; + int h = ts->tile_h; + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, w, h, BLANK); + + switch (item_type) { + case 0: { + // Flask shape + Color glass = (Color){200, 60, 60, 255}; + Color liquid = (Color){255, 80, 80, 255}; + Color highlight = (Color){255, 150, 150, 255}; + // Neck + DrawRectangle(ox + 6, oy + 2, 4, 3, glass); + // Body + DrawRectangle(ox + 4, oy + 5, 8, 8, liquid); + // Cork + DrawRectangle(ox + 6, oy + 1, 4, 1, (Color){160, 120, 60, 255}); + // Highlight + DrawPixel(ox + 5, oy + 6, highlight); + DrawPixel(ox + 5, oy + 7, highlight); + break; + } + case 1: { + // Sword + Color blade = (Color){220, 220, 230, 255}; + Color hilt = (Color){160, 120, 40, 255}; + Color guard = (Color){140, 140, 150, 255}; + // Blade + DrawRectangle(ox + 7, oy + 2, 2, 9, blade); + // Tip + DrawPixel(ox + 7, oy + 1, blade); + DrawPixel(ox + 8, oy + 1, blade); + // Guard + DrawRectangle(ox + 5, oy + 11, 6, 1, guard); + // Hilt + DrawRectangle(ox + 7, oy + 12, 2, 3, hilt); + // Pommel + DrawPixel(ox + 7, oy + 15, guard); + DrawPixel(ox + 8, oy + 15, guard); + break; + } + case 2: { + // Chestplate + Color metal = (Color){100, 120, 160, 255}; + Color dark_metal = (Color){70, 85, 115, 255}; + Color highlight = (Color){140, 160, 200, 255}; + // Main plate + DrawRectangle(ox + 4, oy + 3, 8, 9, metal); + // Collar + DrawRectangle(ox + 5, oy + 2, 6, 1, dark_metal); + // Vertical ridge + DrawRectangle(ox + 7, oy + 3, 2, 9, highlight); + // Bottom trim + DrawRectangle(ox + 4, oy + 11, 8, 1, dark_metal); + break; + } + default: + break; + } + + EndTextureMode(); +} + +void paint_door_closed_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + int w = ts->tile_w; + int h = ts->tile_h; + + BeginTextureMode(ts->render_target); + + // Wooden door frame + Color wood_dark = (Color){100, 70, 40, 255}; + Color wood_light = (Color){140, 100, 60, 255}; + Color wood_mid = (Color){120, 85, 50, 255}; + + // Frame + DrawRectangle(ox, oy, w, h, wood_dark); + // Panels + DrawRectangle(ox + 2, oy + 2, w - 4, h - 4, wood_mid); + // Inner panel + DrawRectangle(ox + 4, oy + 4, w - 8, h - 8, wood_light); + // Cross pattern + DrawRectangle(ox + 7, oy + 2, 2, h - 4, wood_dark); + DrawRectangle(ox + 2, oy + 7, w - 4, 2, wood_dark); + // Handle + DrawPixel(ox + 12, oy + 8, (Color){200, 180, 50, 255}); + DrawPixel(ox + 12, oy + 9, (Color){200, 180, 50, 255}); + + EndTextureMode(); +} + +void paint_door_open_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + int w = ts->tile_w; + int h = ts->tile_h; + + BeginTextureMode(ts->render_target); + + // Open door - shows floor with door frame on sides + Color floor = (Color){75, 72, 68, 255}; + Color wood_dark = (Color){100, 70, 40, 255}; + + // Floor + DrawRectangle(ox, oy, w, h, floor); + // Door frame on left side (open) + DrawRectangle(ox, oy, 3, h, wood_dark); + // Hinges + DrawPixel(ox + 1, oy + 4, (Color){80, 80, 80, 255}); + DrawPixel(ox + 1, oy + 12, (Color){80, 80, 80, 255}); + + EndTextureMode(); +} + +void paint_effect_burn_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Fire effect - orange/red flames + Color flame1 = (Color){255, 100, 20, 255}; + Color flame2 = (Color){255, 180, 40, 255}; + Color flame3 = (Color){255, 60, 10, 255}; + + // Flame shapes + DrawRectangle(ox + 4, oy + 8, 2, 6, flame1); + DrawRectangle(ox + 7, oy + 6, 2, 8, flame2); + DrawRectangle(ox + 10, oy + 9, 2, 5, flame3); + DrawPixel(ox + 5, oy + 5, flame2); + DrawPixel(ox + 8, oy + 4, flame1); + DrawPixel(ox + 11, oy + 7, flame2); + + EndTextureMode(); +} + +void paint_effect_poison_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Poison effect - green bubbles/drops + Color poison1 = (Color){50, 200, 50, 255}; + Color poison2 = (Color){30, 150, 30, 255}; + Color poison3 = (Color){80, 255, 80, 255}; + + // Bubbles + DrawRectangle(ox + 5, oy + 4, 3, 3, poison1); + DrawRectangle(ox + 9, oy + 7, 2, 2, poison2); + DrawRectangle(ox + 4, oy + 10, 2, 2, poison3); + DrawRectangle(ox + 8, oy + 11, 3, 2, poison1); + DrawPixel(ox + 11, oy + 5, poison3); + DrawPixel(ox + 6, oy + 13, poison2); + + EndTextureMode(); +} + +void paint_effect_block_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Block/shield effect - blue shield shape + Color shield = (Color){80, 130, 220, 255}; + Color shield_light = (Color){120, 170, 255, 255}; + Color shield_dark = (Color){50, 90, 180, 255}; + + // Shield outline + DrawRectangle(ox + 4, oy + 2, 8, 12, shield); + // Inner highlight + DrawRectangle(ox + 6, oy + 4, 4, 8, shield_light); + // Border + DrawRectangle(ox + 4, oy + 2, 8, 1, shield_dark); + DrawRectangle(ox + 4, oy + 13, 8, 1, shield_dark); + DrawRectangle(ox + 4, oy + 2, 1, 12, shield_dark); + DrawRectangle(ox + 11, oy + 2, 1, 12, shield_dark); + // Cross in center + DrawRectangle(ox + 7, oy + 6, 2, 4, shield_dark); + DrawRectangle(ox + 6, oy + 7, 4, 2, shield_dark); + + EndTextureMode(); +} + +void paint_slash_effect_tile(Tileset *ts, int id) { + if (ts == NULL || id < 0 || id >= MAX_TILE_ID) + return; + if (ts->render_target.id == 0 || ts->finalized) + return; + + Vector2 off = get_paint_offset(ts, id); + int ox = (int)off.x; + int oy = (int)off.y; + + BeginTextureMode(ts->render_target); + DrawRectangle(ox, oy, ts->tile_w, ts->tile_h, BLANK); + + // Slash effect - white/gray diagonal slash + Color slash = (Color){255, 255, 255, 255}; + Color slash_trail = (Color){200, 200, 220, 255}; + + // Main slash line (diagonal) + DrawRectangle(ox + 2, oy + 12, 3, 2, slash); + DrawRectangle(ox + 4, oy + 10, 3, 2, slash); + DrawRectangle(ox + 6, oy + 8, 3, 2, slash); + DrawRectangle(ox + 8, oy + 6, 3, 2, slash); + DrawRectangle(ox + 10, oy + 4, 3, 2, slash); + DrawRectangle(ox + 12, oy + 2, 2, 2, slash); + // Trail + DrawPixel(ox + 3, oy + 11, slash_trail); + DrawPixel(ox + 5, oy + 9, slash_trail); + DrawPixel(ox + 7, oy + 7, slash_trail); + DrawPixel(ox + 9, oy + 5, slash_trail); + DrawPixel(ox + 11, oy + 3, slash_trail); + + EndTextureMode(); +} + +int tileset_paint_all(Tileset *ts) { + if (ts == NULL) + return 0; + if (ts->render_target.id == 0 || ts->finalized) + return 0; + + // Register all tile IDs first + for (int id = 0; id < NUM_TILE_IDS; id++) { + if (!tileset_register(ts, id)) + return 0; + } + + // Paint map tiles + paint_wall_tile(ts, TILE_WALL_0, 0); + paint_wall_tile(ts, TILE_WALL_1, 1); + paint_floor_tile(ts, TILE_FLOOR_0, 0); + paint_floor_tile(ts, TILE_FLOOR_1, 1); + paint_floor_tile(ts, TILE_FLOOR_2, 2); + paint_floor_tile(ts, TILE_FLOOR_3, 3); + paint_stairs_tile(ts, TILE_STAIRS_SPRITE); + + // Paint entity sprites + paint_player_tile(ts, SPRITE_PLAYER); + paint_player_walk_tile(ts, SPRITE_PLAYER_WALK_0, 0); + paint_player_walk_tile(ts, SPRITE_PLAYER_WALK_1, 1); + paint_player_attack_tile(ts, SPRITE_PLAYER_ATTACK); + + // Enemy goblin sprites + paint_enemy_tile(ts, SPRITE_ENEMY_GOBLIN, 0); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_GOBLIN_WALK_0, 0, 0); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_GOBLIN_WALK_1, 0, 1); + paint_enemy_attack_tile(ts, SPRITE_ENEMY_GOBLIN_ATTACK, 0); + + // Enemy skeleton sprites + paint_enemy_tile(ts, SPRITE_ENEMY_SKELETON, 1); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_SKELETON_WALK_0, 1, 0); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_SKELETON_WALK_1, 1, 1); + paint_enemy_attack_tile(ts, SPRITE_ENEMY_SKELETON_ATTACK, 1); + + // Enemy orc sprites + paint_enemy_tile(ts, SPRITE_ENEMY_ORC, 2); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_ORC_WALK_0, 2, 0); + paint_enemy_walk_tile(ts, SPRITE_ENEMY_ORC_WALK_1, 2, 1); + paint_enemy_attack_tile(ts, SPRITE_ENEMY_ORC_ATTACK, 2); + + paint_item_tile(ts, SPRITE_ITEM_POTION, 0); + paint_item_tile(ts, SPRITE_ITEM_WEAPON, 1); + paint_item_tile(ts, SPRITE_ITEM_ARMOR, 2); + + // Door tiles + paint_door_closed_tile(ts, TILE_DOOR_CLOSED_SPRITE); + paint_door_open_tile(ts, TILE_DOOR_OPEN_SPRITE); + + // Effect sprites + paint_effect_burn_tile(ts, SPRITE_EFFECT_BURN); + paint_effect_poison_tile(ts, SPRITE_EFFECT_POISON); + paint_effect_block_tile(ts, SPRITE_EFFECT_BLOCK); + paint_slash_effect_tile(ts, SPRITE_SLASH_EFFECT); + + return 1; +} diff --git a/libs/tileset/tileset_paint.h b/libs/tileset/tileset_paint.h new file mode 100644 index 0000000..032b224 --- /dev/null +++ b/libs/tileset/tileset_paint.h @@ -0,0 +1,66 @@ +#ifndef TILESET_PAINT_H +#define TILESET_PAINT_H + +#include "tileset.h" + +// Forward declarations for types used in painting +typedef enum { ENEMY_GOBLIN_FWD, ENEMY_SKELETON_FWD, ENEMY_ORC_FWD } EnemyType_Paint; +typedef enum { ITEM_POTION_FWD, ITEM_WEAPON_FWD, ITEM_ARMOR_FWD } ItemType_Paint; + +// Paint a wall tile with brick-like pattern. +// variant: 0 or 1 for shade variation. +void paint_wall_tile(Tileset *ts, int id, int variant); + +// Paint a floor tile with stone/dithered pattern. +// variant: 0-3 for different noise patterns. +void paint_floor_tile(Tileset *ts, int id, int variant); + +// Paint a stairs tile with depth illusion. +void paint_stairs_tile(Tileset *ts, int id); + +// Paint the player sprite (adventurer silhouette). +void paint_player_tile(Tileset *ts, int id); + +// Paint a player walking animation frame. +// frame: 0 or 1 for the two walk frames. +void paint_player_walk_tile(Tileset *ts, int id, int frame); + +// Paint a player attacking animation frame. +void paint_player_attack_tile(Tileset *ts, int id); + +// Paint an enemy sprite based on type. +void paint_enemy_tile(Tileset *ts, int id, int enemy_type); + +// Paint an enemy walking animation frame. +// frame: 0 or 1 for the two walk frames. +void paint_enemy_walk_tile(Tileset *ts, int id, int enemy_type, int frame); + +// Paint an enemy attacking animation frame. +void paint_enemy_attack_tile(Tileset *ts, int id, int enemy_type); + +// Paint an item sprite based on type. +void paint_item_tile(Tileset *ts, int id, int item_type); + +// Paint a closed door tile. +void paint_door_closed_tile(Tileset *ts, int id); + +// Paint an open door tile. +void paint_door_open_tile(Tileset *ts, int id); + +// Paint a burning/fire effect sprite. +void paint_effect_burn_tile(Tileset *ts, int id); + +// Paint a poison effect sprite. +void paint_effect_poison_tile(Tileset *ts, int id); + +// Paint a block/shield effect sprite. +void paint_effect_block_tile(Tileset *ts, int id); + +// Paint a slash attack effect sprite. +void paint_slash_effect_tile(Tileset *ts, int id); + +// Convenience: paint and register all tiles in one call. +// Returns 0 on failure, non-zero on success. +int tileset_paint_all(Tileset *ts); + +#endif // TILESET_PAINT_H From 2f5c9595006cfd6a3d7a3c0703be80b79c11dcea Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 28 Apr 2026 15:32:15 +0300 Subject: [PATCH 04/11] map: expand tile system with doors Signed-off-by: NotAShelf Change-Id: I5704e8f954f6f935954c46ef8af40b836a6a6964 --- libs/map/map.c | 70 +++++++++++++++++++++++++++++- src/common.h | 114 +++++++++++++++++++++++++++++++++++++++++++++---- src/settings.h | 3 ++ 3 files changed, 176 insertions(+), 11 deletions(-) diff --git a/libs/map/map.c b/libs/map/map.c index 911d487..1e93d07 100644 --- a/libs/map/map.c +++ b/libs/map/map.c @@ -2,6 +2,7 @@ #include "rng/rng.h" #include "settings.h" #include "utils.h" +#include #include #include @@ -20,7 +21,8 @@ void map_init(Map *map) { int is_floor(const Map *map, int x, int y) { if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) return 0; - return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS; + return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS || map->tiles[y][x] == TILE_DOOR_OPEN || + map->tiles[y][x] == TILE_DOOR_RUINED; } void get_room_center(Room *room, int *cx, int *cy) { @@ -109,6 +111,32 @@ static int generate_rooms(Map *map, Room *rooms, int floor) { return room_count; } +// Check if a tile is at a room boundary (adjacent to wall but inside room) +static int is_room_boundary(Map *map, int x, int y) { + // Must be floor + if (map->tiles[y][x] != TILE_FLOOR) + return 0; + // Must have at least one adjacent wall + if (in_bounds(x - 1, y, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y][x - 1] == TILE_WALL) + return 1; + if (in_bounds(x + 1, y, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y][x + 1] == TILE_WALL) + return 1; + if (in_bounds(x, y - 1, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y - 1][x] == TILE_WALL) + return 1; + if (in_bounds(x, y + 1, MAP_WIDTH, MAP_HEIGHT) && map->tiles[y + 1][x] == TILE_WALL) + return 1; + return 0; +} + +// Place doors at corridor-room junctions +// DISABLED: Door placement removed per user request +static void place_doors(Map *map, Room *rooms, int room_count) { + (void)map; + (void)rooms; + (void)room_count; + // No-op: doors disabled +} + // Connect all rooms with corridors static void connect_rooms(Map *map, Room *rooms, int room_count) { for (int i = 0; i < room_count - 1; i++) { @@ -125,6 +153,9 @@ static void connect_rooms(Map *map, Room *rooms, int room_count) { carve_h_corridor(map, cx1, cx2, cy2); } } + + // Place doors after all corridors are carved + place_doors(map, rooms, room_count); } // Place stairs in the last room (furthest from start) @@ -134,8 +165,43 @@ static void place_stairs(Map *map, Room *rooms, int room_count) { int cx, cy; get_room_center(last_room, &cx, &cy); - // Place stairs at center of last room + // Ensure stairs are placed on a floor tile, not a wall + if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT) && map->tiles[cy][cx] == TILE_FLOOR) { + map->tiles[cy][cx] = TILE_STAIRS; + return; + } + + // 3x3 fallback + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + int nx = cx + dx; + int ny = cy + dy; + if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_FLOOR) { + map->tiles[ny][nx] = TILE_STAIRS; + return; + } + } + } + + // Expanded fallback: scan the room for any floor tile + for (int dy = 0; dy < last_room->h; dy++) { + for (int dx = 0; dx < last_room->w; dx++) { + int nx = last_room->x + dx; + int ny = last_room->y + dy; + if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_FLOOR) { + map->tiles[ny][nx] = TILE_STAIRS; + return; + } + } + } + + // Final fallback: force the center tile to stairs regardless of type + fprintf(stderr, "Warning: No floor tile found for stairs at room center (%d, %d). Forcing stairs placement.\n", cx, + cy); if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT)) { + if (map->tiles[cy][cx] == TILE_WALL) { + map->tiles[cy][cx] = TILE_FLOOR; + } map->tiles[cy][cx] = TILE_STAIRS; } } diff --git a/src/common.h b/src/common.h index fe87edf..f474781 100644 --- a/src/common.h +++ b/src/common.h @@ -9,7 +9,7 @@ typedef struct { } Vec2; // Tile types -typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS } TileType; +typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS, TILE_DOOR_CLOSED, TILE_DOOR_OPEN, TILE_DOOR_RUINED } TileType; // Status effect types typedef enum { EFFECT_NONE, EFFECT_POISON, EFFECT_STUN, EFFECT_BLEED, EFFECT_WEAKEN, EFFECT_BURN } StatusEffectType; @@ -49,69 +49,165 @@ typedef struct { typedef enum { ITEM_POTION, ITEM_WEAPON, ITEM_ARMOR } ItemType; // Item + typedef struct { int x, y; + ItemType type; + int power; + int floor; + int picked_up; + DamageClass dmg_class; + int crit_chance; + int crit_multiplier; + int status_chance; + + // rendering + + int sprite_tile_id; // tile ID for rendering + } Item; +// Player animation states +typedef enum { PLAYER_ANIM_IDLE, PLAYER_ANIM_WALK, PLAYER_ANIM_ATTACK } PlayerAnimState; + // Player + typedef struct { Vec2 position; + int hp, max_hp; + int attack; + int defense; + int floor; + int step_count; - int speed; // actions per 100 ticks (100 = 1 action per turn) + + int speed; // actions per 100 ticks (100 = 1 action per turn) + int cooldown; // countdown to next action (0 = can act) - int dodge; // dodge chance percentage - int block; // flat damage reduction on successful block roll + + int dodge; // dodge chance percentage + + int block; // flat damage reduction on successful block roll + Item equipped_weapon; + int has_weapon; + Item equipped_armor; + int has_armor; + Item inventory[MAX_INVENTORY]; + int inventory_count; + // status effects + StatusEffect effects[MAX_EFFECTS]; + int effect_count; + + // animation + + PlayerAnimState anim_state; + + int anim_frame; // current animation frame + + int anim_timer; // frames until next frame + + int facing_right; // 1 = facing right, 0 = facing left + + // rendering + + int sprite_tile_id; // tile ID for rendering + + // visual effects + + int flash_timer; // damage flash frames remaining + } Player; // Enemy types typedef enum { ENEMY_GOBLIN, ENEMY_SKELETON, ENEMY_ORC } EnemyType; +// Enemy animation states +typedef enum { ENEMY_ANIM_IDLE, ENEMY_ANIM_WALK, ENEMY_ANIM_ATTACK } EnemyAnimState; + // Enemy + typedef struct { Vec2 position; + int hp; + int max_hp; + int attack; + int alive; + EnemyType type; - int speed; // actions per 100 ticks + + int speed; // actions per 100 ticks + int cooldown; // countdown to next action - int dodge; // dodge chance percentage - int block; // flat damage reduction + + int dodge; // dodge chance percentage + + int block; // flat damage reduction + int resistance[NUM_DMG_CLASSES]; + DamageClass dmg_class; + int status_chance; + int crit_chance; // crit chance percentage (0-100) - int crit_mult; // crit damage multiplier percentage (e.g. 150 = 1.5x) + + int crit_mult; // crit damage multiplier percentage (e.g. 150 = 1.5x) + // vision + int vision_range; - int alert; // 1 = aware of player, searching + + int alert; // 1 = aware of player, searching + int last_known_x; // last position where enemy saw player + int last_known_y; + // status effects + StatusEffect effects[MAX_EFFECTS]; + int effect_count; + + // animation + + EnemyAnimState anim_state; + + int anim_frame; // current animation frame + + int anim_timer; // frames until next frame + + int facing_right; // 1 = facing right, 0 = facing left + + // rendering + + int sprite_tile_id; // tile ID for rendering + } Enemy; diff --git a/src/settings.h b/src/settings.h index 3408a13..2b700ac 100644 --- a/src/settings.h +++ b/src/settings.h @@ -83,4 +83,7 @@ #define ENEMY_VIEW_RANGE 6 #define ENEMY_PATROL_MOVE_CHANCE 30 +// Visual polish +#define DRAW_GRID_LINES 1 + #endif // SETTINGS_H From 5b640dcefd2e714392584b0e806dd20e489ecb54 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 28 Apr 2026 15:32:56 +0300 Subject: [PATCH 05/11] render: use tileset atlas for all entity and tile rendering; anims Signed-off-by: NotAShelf Change-Id: Idb42cff72368e26d8d44db79ba9c413a6a6a6964 --- src/enemy.c | 66 ++++++++ src/game_state.h | 7 + src/items.c | 17 ++ src/main.c | 107 ++++++++++-- src/player.c | 8 + src/render.c | 412 +++++++++++++++++++++++++++++++++++++++-------- src/render.h | 27 ++-- 7 files changed, 555 insertions(+), 89 deletions(-) diff --git a/src/enemy.c b/src/enemy.c index 6832386..084a954 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -5,6 +5,7 @@ #include "movement.h" #include "rng/rng.h" #include "settings.h" +#include "tileset/tileset.h" #include // Forward declaration @@ -25,6 +26,12 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { if (floor >= 4) max_type = 3; + // Get the player's starting room (first room) to exclude from enemy spawn + Room *start_room = NULL; + if (map->room_count > 0) { + start_room = &map->rooms[0]; + } + for (int i = 0; i < num_enemies; i++) { // Find random floor position int ex, ey; @@ -35,6 +42,14 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { continue; } + // Don't spawn in the starting room + if (start_room != NULL) { + if (ex >= start_room->x && ex < start_room->x + start_room->w && ey >= start_room->y && + ey < start_room->y + start_room->h) { + continue; + } + } + // Don't spawn on other enemies if (is_enemy_at(enemies, *count, ex, ey)) { continue; @@ -125,6 +140,27 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { } e.cooldown = e.speed; + // Initialize animation state + e.anim_state = ENEMY_ANIM_IDLE; + e.anim_frame = 0; + e.anim_timer = 0; + e.facing_right = (e.position.x < p->position.x) ? 1 : 0; + // Set sprite tile ID based on enemy type + switch (e.type) { + case ENEMY_GOBLIN: + e.sprite_tile_id = SPRITE_ENEMY_GOBLIN; + break; + case ENEMY_SKELETON: + e.sprite_tile_id = SPRITE_ENEMY_SKELETON; + break; + case ENEMY_ORC: + e.sprite_tile_id = SPRITE_ENEMY_ORC; + break; + default: + e.sprite_tile_id = SPRITE_ENEMY_GOBLIN; + break; + } + enemies[i] = e; (*count)++; } @@ -275,6 +311,9 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun // Attack if adjacent to player if (can_see && can_see_entity(map, e->position.x, e->position.y, p->position.x, p->position.y, 1)) { + e->anim_state = ENEMY_ANIM_ATTACK; + e->anim_timer = 12; + e->facing_right = (e->position.x < p->position.x) ? 1 : 0; combat_enemy_attack(e, p); propagate_alert(e, all_enemies, enemy_count); return; @@ -282,14 +321,30 @@ void enemy_act(Enemy *e, Player *p, Map *map, Enemy *all_enemies, int enemy_coun // Move toward player if visible if (can_see) { + int old_x = e->position.x; + int old_y = e->position.y; enemy_move_toward_player(e, p, map, all_enemies, enemy_count); + if (e->position.x != old_x || e->position.y != old_y) { + e->anim_state = ENEMY_ANIM_WALK; + e->anim_timer = 8; + e->facing_right = (e->position.x < p->position.x) ? 1 : 0; + } propagate_alert(e, all_enemies, enemy_count); return; } // If alert but can't see player, move toward last known position if (e->alert) { + int old_x = e->position.x; + int old_y = e->position.y; enemy_move_to_last_known(e, map, all_enemies, enemy_count); + if (e->position.x != old_x || e->position.y != old_y) { + e->anim_state = ENEMY_ANIM_WALK; + e->anim_timer = 8; + if (e->position.x != old_x) { + e->facing_right = (e->position.x < old_x) ? 0 : 1; + } + } return; } @@ -304,6 +359,17 @@ void enemy_update_all(Enemy enemies[], int count, Player *p, Map *map) { if (!e->alive) continue; + // Update animation timer + if (e->anim_timer > 0) { + e->anim_timer--; + if (e->anim_timer <= 0) { + e->anim_state = ENEMY_ANIM_IDLE; + e->anim_frame = 0; + } else if (e->anim_state == ENEMY_ANIM_WALK) { + e->anim_frame = (e->anim_timer / 4) % 2; + } + } + e->cooldown -= e->speed; if (e->cooldown <= 0) { enemy_act(e, p, map, enemies, count); diff --git a/src/game_state.h b/src/game_state.h index e0d7711..73cdac2 100644 --- a/src/game_state.h +++ b/src/game_state.h @@ -2,6 +2,7 @@ #define GAME_STATE_H #include "common.h" +#include "tileset/tileset.h" #include // Floating damage text @@ -65,6 +66,12 @@ typedef struct { int final_score; // Seed for this run unsigned int run_seed; + // Tileset atlas for rendering + Tileset tileset; + // Slash effect timer for attack animations + int slash_timer; // frames remaining for slash effect + int slash_x, slash_y; // position of slash effect + DamageClass slash_dmg_class; // damage type for slash visual } GameState; #endif // GAME_STATE_H diff --git a/src/items.c b/src/items.c index 27f343d..367a328 100644 --- a/src/items.c +++ b/src/items.c @@ -2,6 +2,7 @@ #include "map/map.h" #include "rng/rng.h" #include "settings.h" +#include "tileset/tileset.h" #include typedef struct { @@ -77,6 +78,22 @@ void item_spawn(Item items[], int *count, Map *map, int floor) { item.power = 1 + rng_int(0, floor / 2); } + // Set sprite tile ID based on item type + switch (item.type) { + case ITEM_POTION: + item.sprite_tile_id = SPRITE_ITEM_POTION; + break; + case ITEM_WEAPON: + item.sprite_tile_id = SPRITE_ITEM_WEAPON; + break; + case ITEM_ARMOR: + item.sprite_tile_id = SPRITE_ITEM_ARMOR; + break; + default: + item.sprite_tile_id = SPRITE_ITEM_POTION; + break; + } + items[*count] = item; (*count)++; } diff --git a/src/main.c b/src/main.c index e9089a5..e31dd99 100644 --- a/src/main.c +++ b/src/main.c @@ -4,11 +4,14 @@ #include "enemy.h" #include "items.h" #include "map/map.h" +#include "map/utils.h" #include "movement.h" #include "player.h" #include "render.h" #include "rng/rng.h" #include "settings.h" +#include "tileset/tileset.h" +#include "tileset/tileset_paint.h" #include #include #include @@ -182,19 +185,18 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { if (gs->game_over) return; - // Check if stepped on stairs - if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) { - gs->awaiting_descend = 1; - gs->last_message = "Descend to next floor? (Y/N)"; - gs->message_timer = 120; - return; - } - // combat feedback - player attacked an enemy this turn if (attacked_enemy != NULL) { int ex = attacked_enemy->position.x * TILE_SIZE + 8; int ey = attacked_enemy->position.y * TILE_SIZE; + // Trigger slash effect + gs->slash_timer = 8; + gs->slash_x = attacked_enemy->position.x; + gs->slash_y = attacked_enemy->position.y; + // Use player's equipped weapon damage class, or default to slash + gs->slash_dmg_class = gs->player.has_weapon ? gs->player.equipped_weapon.dmg_class : DMG_SLASH; + if (combat_was_dodged()) { spawn_floating_label(gs, ex, ey, LABEL_DODGE, EFFECT_NONE); audio_play_dodge(gs); @@ -237,6 +239,7 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { gs->screen_shake = SHAKE_PLAYER_DAMAGE_DURATION; gs->damage_taken += combat_get_last_damage(); gs->times_hit++; + gs->player.flash_timer = 4; // Trigger damage flash spawn_floating_text(gs, gs->player.position.x * TILE_SIZE + 8, gs->player.position.y * TILE_SIZE, combat_get_last_damage(), combat_was_critical()); } @@ -247,6 +250,13 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { if (gs->player.hp <= 0) gs->game_over = 1; + + // Check if stepped on stairs AFTER enemy turns + if (gs->map.tiles[gs->player.position.y][gs->player.position.x] == TILE_STAIRS) { + gs->awaiting_descend = 1; + gs->last_message = "Descend to next floor? (Y/N)"; + gs->message_timer = 120; + } } // If player is stunned, wait for any key then consume the turn @@ -457,11 +467,27 @@ static int handle_movement_input(GameState *gs) { try_move_entity(&gs->player.position, direction, &gs->map, &gs->player, gs->enemies, gs->enemy_count, true); if (result == MOVE_RESULT_MOVED) { player_on_move(&gs->player); + // Set walk animation + gs->player.anim_state = PLAYER_ANIM_WALK; + gs->player.anim_frame = 0; + gs->player.anim_timer = 8; // frames to show each walk frame + // Update facing direction + if (direction.x != 0) + gs->player.facing_right = (direction.x > 0); action = 1; } else if (result == MOVE_RESULT_BLOCKED_ENEMY) { target = player_find_enemy_at(gs->enemies, gs->enemy_count, new_x, new_y); if (target != NULL) { player_attack(&gs->player, target); + // Set attack animation + gs->player.anim_state = PLAYER_ANIM_ATTACK; + gs->player.anim_frame = 0; + gs->player.anim_timer = 12; // frames to show attack + // Face the enemy + if (target->position.x > gs->player.position.x) + gs->player.facing_right = 1; + else if (target->position.x < gs->player.position.x) + gs->player.facing_right = 0; action = 1; } } @@ -526,13 +552,35 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { load_audio_assets(&gs); // font init_fonts(fm); + // Initialize tileset atlas + if (!tileset_init(&gs.tileset, TILE_SIZE, TILE_SIZE)) { + fprintf(stderr, "Failed to initialize tileset\n"); + destroy_fonts(fm); + return; + } + if (!tileset_paint_all(&gs.tileset)) { + fprintf(stderr, "Failed to paint tiles\n"); + tileset_destroy(&gs.tileset); + destroy_fonts(fm); + return; + } + if (!tileset_finalize(&gs.tileset)) { + fprintf(stderr, "Failed to finalize tileset\n"); + tileset_destroy(&gs.tileset); + destroy_fonts(fm); + return; + } + // Initialize first floor init_floor(&gs, 1); // Disable esc to exit SetExitKey(0); + int frame_counter = 0; while (!WindowShouldClose()) { + frame_counter++; + // Handle input if (!gs.game_over) { // Tick status effects at the start of each frame where input is checked @@ -552,6 +600,12 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { gs.game_won = 0; load_audio_assets(&gs); init_fonts(fm); + // Re-initialize tileset for new run + if (!tileset_init(&gs.tileset, TILE_SIZE, TILE_SIZE) || !tileset_paint_all(&gs.tileset) || + !tileset_finalize(&gs.tileset)) { + fprintf(stderr, "Failed to re-initialize tileset\n"); + break; + } init_floor(&gs, 1); // Update window title with new seed char title[128]; @@ -567,6 +621,27 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { // Update effects update_effects(&gs); + // Update slash effect timer + if (gs.slash_timer > 0) + gs.slash_timer--; + + // Update player animation + if (gs.player.anim_timer > 0) { + gs.player.anim_timer--; + if (gs.player.anim_timer <= 0) { + // Animation finished, return to idle + gs.player.anim_state = PLAYER_ANIM_IDLE; + gs.player.anim_frame = 0; + } else if (gs.player.anim_state == PLAYER_ANIM_WALK) { + // Toggle walk frame every 4 frames + gs.player.anim_frame = (gs.player.anim_timer / 4) % 2; + } + } + + // Update player damage flash + if (gs.player.flash_timer > 0) + gs.player.flash_timer--; + // Render BeginDrawing(); ClearBackground(BLACK); @@ -576,15 +651,19 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { cam.zoom = 1.0f; cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y}; BeginMode2D(cam); - render_map(&gs.map); - render_items(gs.items, gs.item_count, gs.map.visible); - render_enemies(gs.enemies, gs.enemy_count, gs.map.visible); - render_player(&gs.player); + render_map(&gs.map, &gs.tileset); + render_items(gs.items, gs.item_count, gs.map.visible, &gs.tileset); + render_enemies(gs.enemies, gs.enemy_count, gs.map.visible, &gs.tileset, frame_counter); + render_player(&gs.player, &gs.tileset, frame_counter); + // Draw slash effect on top of entities + if (gs.slash_timer > 0) { + render_slash_effect(gs.slash_x, gs.slash_y, gs.slash_dmg_class, gs.slash_timer); + } EndMode2D(); // Floating texts follow world shake - render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y); - render_ui(&gs.player, fm); + render_floating_texts(gs.floating_texts, gs.floating_count, gs.shake_x, gs.shake_y, fm); + render_ui(&gs.player, &gs.tileset, fm); // Draw action log render_action_log(gs.action_log, gs.log_count, gs.log_head, fm); diff --git a/src/player.c b/src/player.c index ecc9967..f47becb 100644 --- a/src/player.c +++ b/src/player.c @@ -3,6 +3,7 @@ #include "common.h" #include "items.h" #include "settings.h" +#include "tileset/tileset.h" #include void player_init(Player *p, int x, int y) { @@ -28,6 +29,13 @@ void player_init(Player *p, int x, int y) { p->effect_count = 0; memset(p->effects, 0, sizeof(p->effects)); + // Initialize animation state + p->anim_state = PLAYER_ANIM_IDLE; + p->anim_frame = 0; + p->anim_timer = 0; + p->facing_right = 1; + p->sprite_tile_id = SPRITE_PLAYER; + // Initialize inventory to empty for (int i = 0; i < MAX_INVENTORY; i++) { p->inventory[i].picked_up = 1; // mark as invalid diff --git a/src/render.c b/src/render.c index 4dfb602..e7b4b27 100644 --- a/src/render.c +++ b/src/render.c @@ -1,6 +1,8 @@ #include "render.h" #include "items.h" #include "settings.h" +#include "map/utils.h" +#include #include #include #include @@ -63,78 +65,292 @@ static void draw_text_body(Font f, const char *text, float x, float y, int size, DrawTextEx(f, text, (Vector2){x, y}, (float)size, spacing, c); } -void render_map(const Map *map) { +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 rect = {(float)(x * TILE_SIZE), (float)(y * TILE_SIZE), (float)TILE_SIZE, (float)TILE_SIZE}; + 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]; if (!visible && !remembered) { - DrawRectangleRec(rect, (Color){5, 5, 10, 255}); + DrawRectangleRec(dst, (Color){5, 5, 10, 255}); continue; } + 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; + } + + 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 + tint = (Color){128, 128, 128, 255}; + } + DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, tint); + continue; + } + } + + // 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){100, 100, 100, 255} : (Color){40, 40, 45, 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}; switch (map->tiles[y][x]) { case TILE_WALL: - DrawRectangleRec(rect, wall_color); + DrawRectangleRec(dst, wall_color); break; case TILE_FLOOR: - DrawRectangleRec(rect, floor_color); + DrawRectangleRec(dst, floor_color); + // Torch flicker: warm tint on floor tiles adjacent to stairs + { + int is_adjacent_to_stairs = 0; + for (int dy = -1; dy <= 1 && !is_adjacent_to_stairs; dy++) { + for (int dx = -1; dx <= 1 && !is_adjacent_to_stairs; dx++) { + int nx = x + dx; + int ny = y + dy; + if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_STAIRS) { + is_adjacent_to_stairs = 1; + } + } + } + if (is_adjacent_to_stairs && visible) { + int flicker = (int)(sinf(GetTime() * 5.0f) * 15.0f); + DrawRectangleRec(dst, (Color){40 + flicker, 25, 10, 60}); + } + } + // Grid lines + if (DRAW_GRID_LINES && visible) { + DrawRectangleLines((int)dst.x, (int)dst.y, (int)dst.width, (int)dst.height, (Color){20, 20, 20, 80}); + } break; case TILE_STAIRS: - DrawRectangleRec(rect, stairs_color); - if (visible) - DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, NORM_FONT, WHITE); - else - DrawText(">", x * TILE_SIZE + 4, y * TILE_SIZE + 2, NORM_FONT, (Color){60, 60, 65, 255}); + DrawRectangleRec(dst, stairs_color); + // Make stairs very visible with bright symbol and bounce + { + int bounce = (int)(sinf(GetTime() * 3.0f) * 1.5f); + if (visible) + 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: + DrawRectangleRec(dst, door_color); + if (visible) { + 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}); + DrawText("+", x * TILE_SIZE + 5, y * TILE_SIZE + 1, NORM_FONT, WHITE); + } + break; + case TILE_DOOR_OPEN: + DrawRectangleRec(dst, floor_color); + if (visible) { + 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}); + DrawText("'", x * TILE_SIZE + 6, y * TILE_SIZE + 2, NORM_FONT, (Color){150, 140, 120, 255}); + } + break; + case TILE_DOOR_RUINED: + DrawRectangleRec(dst, (Color){60, 45, 30, 255}); + if (visible) { + 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; } } } } -void render_player(const Player *p) { - Rectangle rect = {(float)(p->position.x * TILE_SIZE), (float)(p->position.y * TILE_SIZE), (float)TILE_SIZE, - (float)TILE_SIZE}; - DrawRectangleRec(rect, BLUE); +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}; + + 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 unsigned char visible[MAP_HEIGHT][MAP_WIDTH]) { +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH], + 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]) continue; - Rectangle rect = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE), - (float)TILE_SIZE, (float)TILE_SIZE}; + Rectangle dst = {(float)(enemies[i].position.x * TILE_SIZE), (float)(enemies[i].position.y * TILE_SIZE), + (float)TILE_SIZE, (float)TILE_SIZE}; - // Different colors based on enemy type - Color enemy_color; - switch (enemies[i].type) { - case ENEMY_GOBLIN: - enemy_color = COLOR_ENEMY_GOBLIN; // dark red - break; - case ENEMY_SKELETON: - enemy_color = COLOR_ENEMY_SKELETON; // light gray - break; - case ENEMY_ORC: - enemy_color = COLOR_ENEMY_ORC; // dark green - break; - default: - enemy_color = RED; - break; + // 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; } - DrawRectangleRec(rect, enemy_color); + 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].hp * TILE_SIZE) / enemies[i].max_hp; + 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; @@ -151,38 +367,47 @@ 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], + const Tileset *tileset) { for (int i = 0; i < count; i++) { if (items[i].picked_up) continue; if (!visible[items[i].y][items[i].x]) continue; - Rectangle rect = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE, - (float)TILE_SIZE}; + Rectangle dst = {(float)(items[i].x * TILE_SIZE), (float)(items[i].y * TILE_SIZE), (float)TILE_SIZE, + (float)TILE_SIZE}; - // Different colors based on item type - Color item_color; - switch (items[i].type) { - case ITEM_POTION: - item_color = COLOR_ITEM_POTION; // red/pink - break; - case ITEM_WEAPON: - item_color = COLOR_ITEM_WEAPON; // yellow - break; - case ITEM_ARMOR: - item_color = COLOR_ITEM_ARMOR; // blue - break; - default: - item_color = GREEN; - break; + 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); } - - DrawRectangleRec(rect, item_color); } } -void render_ui(const Player *p, const FontManager *fm) { +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; @@ -213,10 +438,18 @@ void render_ui(const Player *p, const FontManager *fm) { int portrait_y = hud_y + 8; int portrait_size = 44; - // FIXME: for now this is just a blue square indicating the player. Once we - // model the player, add classes, sprites, etc. this will need to be revisited. - DrawRectangle(portrait_x, portrait_y, portrait_size, portrait_size, (Color){30, 30, 45, 255}); - DrawRectangle(portrait_x + 2, portrait_y + 2, portrait_size - 4, portrait_size - 4, BLUE); + // 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 @@ -552,7 +785,54 @@ static int label_font_size(FloatingLabel label) { return (label == LABEL_CRIT) ? FONT_SIZE_FLOAT_CRIT : FONT_SIZE_FLOAT_LABEL; } -void render_floating_texts(FloatingText *texts, int count, int shake_x, int shake_y) { +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; @@ -568,15 +848,17 @@ void render_floating_texts(FloatingText *texts, int count, int shake_x, int shak int font_size = label_font_size(texts[i].label); Color color = label_color(&texts[i], a); const char *text = label_text(texts[i].label); - int text_w = MeasureText(text, font_size); - DrawText(text, x - text_w / 2, y, font_size, color); + 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); - int text_w = MeasureText(text, FONT_SIZE_FLOAT_DMG); - DrawText(text, x - text_w / 2, y, FONT_SIZE_FLOAT_DMG, color); + 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); } } } diff --git a/src/render.h b/src/render.h index d29b273..e36bcdb 100644 --- a/src/render.h +++ b/src/render.h @@ -99,20 +99,24 @@ int init_fonts(FontManager *fm); // Unload all fonts held by a FontManager void destroy_fonts(FontManager *fm); -// Render the map tiles -void render_map(const Map *map); +// Render the map tiles using tileset atlas +void render_map(const Map *map, const Tileset *tileset); -// Render the player -void render_player(const Player *p); +// Render the player using tileset atlas +// frame_counter is used for idle breathing animation +void render_player(const Player *p, const Tileset *tileset, int frame_counter); -// Render all enemies -void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); +// Render all enemies using tileset atlas +// frame_counter is used for idle breathing animation +void render_enemies(const Enemy *enemies, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH], + const Tileset *tileset, int frame_counter); -// Render all items -void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH]); +// Render all items using tileset atlas +void render_items(const Item *items, int count, const unsigned char visible[MAP_HEIGHT][MAP_WIDTH], + const Tileset *tileset); // Render UI overlay -void render_ui(const Player *p, const FontManager *fm); +void render_ui(const Player *p, const Tileset *tileset, const FontManager *fm); // Render action log (bottom left corner) void render_action_log(const char log[5][128], int count, int head, const FontManager *fm); @@ -121,7 +125,10 @@ void render_action_log(const char log[5][128], int count, int head, const FontMa void render_inventory_overlay(const Player *p, int selected, const FontManager *fm); // 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, const FontManager *fm); + +// Render slash effect during attacks +void render_slash_effect(int x, int y, DamageClass dmg_class, int timer); // 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, From 00b3798ae07c016cb6796fed6eb5ec1a43805806 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 28 Apr 2026 15:57:17 +0300 Subject: [PATCH 06/11] various: sub-tile lighting; nicer visibility calculations Signed-off-by: NotAShelf Change-Id: I0f0a0c12db76cc8e0f4c8ccc72ca4b826a6a6964 --- libs/map/map.c | 147 +++++++++++++++++++++++++++++++++++++++++++---- libs/map/map.h | 4 +- src/common.h | 9 ++- src/game_state.h | 3 + src/main.c | 26 +++++++-- src/render.c | 45 ++++++++------- src/render.h | 6 +- src/settings.h | 13 ++++- 8 files changed, 206 insertions(+), 47 deletions(-) diff --git a/libs/map/map.c b/libs/map/map.c index 1e93d07..74f1c80 100644 --- a/libs/map/map.c +++ b/libs/map/map.c @@ -2,18 +2,18 @@ #include "rng/rng.h" #include "settings.h" #include "utils.h" +#include #include #include #include void map_init(Map *map) { - // Fill entire map with walls for (int y = 0; y < MAP_HEIGHT; y++) { for (int x = 0; x < MAP_WIDTH; x++) { map->tiles[y][x] = TILE_WALL; } } - memset(map->visible, 0, sizeof(map->visible)); + memset(map->light_map, 0, sizeof(map->light_map)); memset(map->remembered, 0, sizeof(map->remembered)); map->room_count = 0; } @@ -300,20 +300,145 @@ int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) { int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range) { if (!is_in_view_range(to_x, to_y, from_x, from_y, range)) return 0; - return has_line_of_sight(map, from_x, from_y, to_x, to_y); + if (!has_line_of_sight(map, from_x, from_y, to_x, to_y)) + return 0; + return tile_brightness(map, to_x, to_y) > LIGHT_SIGHT_THRESHOLD; } -void calculate_visibility(Map *map, int x, int y) { - memset(map->visible, 0, sizeof(map->visible)); +static int is_solid(const Map *map, int sub_x, int sub_y) { + int map_w = MAP_WIDTH; + int map_h = MAP_HEIGHT; + int tx = sub_x / SUB_TILE_RES; + int ty = sub_y / SUB_TILE_RES; + if (tx < 0 || tx >= map_w || ty < 0 || ty >= map_h) + return 1; + TileType t = map->tiles[ty][tx]; + return t == TILE_WALL || t == TILE_DOOR_CLOSED; +} - for (int ty = 0; ty < MAP_HEIGHT; ty++) { - for (int tx = 0; tx < MAP_WIDTH; tx++) { - if (is_in_view_range(tx, ty, x, y, PLAYER_VIEW_RANGE)) { - if (has_line_of_sight(map, x, y, tx, ty)) { - map->visible[ty][tx] = 1; - map->remembered[ty][tx] = 1; +static float smoothstep_light(float edge0, float edge1, float x) { + float t = (x - edge0) / (edge1 - edge0); + if (t < 0.0f) + return 0.0f; + if (t > 1.0f) + return 1.0f; + return t * t * (3.0f - 2.0f * t); +} + +static int trace_sub_los(const Map *map, int sx, int sy, int tx, int ty) { + int dx = abs(tx - sx); + int dy = abs(ty - sy); + int step_x = (sx < tx) ? 1 : -1; + int step_y = (sy < ty) ? 1 : -1; + int err = dx - dy; + int x = sx, y = sy; + + while (1) { + if (x == tx && y == ty) + return 1; + if (is_solid(map, x, y) && !(x == sx && y == sy)) + return 0; + int e2 = 2 * err; + if (e2 > -dy) { + err -= dy; + x += step_x; + } + if (e2 < dx) { + err += dx; + y += step_y; + } + } +} + +void compute_lighting(Map *map, const LightSource *sources, int num_sources) { + memset(map->light_map, 0, sizeof(map->light_map)); + + int map_sub_w = MAP_WIDTH * SUB_TILE_RES; + int map_sub_h = MAP_HEIGHT * SUB_TILE_RES; + + for (int si = 0; si < num_sources; si++) { + int cx = sources[si].x * SUB_TILE_RES + SUB_TILE_RES / 2; + int cy = sources[si].y * SUB_TILE_RES + SUB_TILE_RES / 2; + int range_sub = sources[si].range * SUB_TILE_RES; + int intensity = sources[si].intensity; + + for (int dy = -range_sub; dy <= range_sub; dy++) { + for (int dx = -range_sub; dx <= range_sub; dx++) { + int sub_x = cx + dx; + int sub_y = cy + dy; + + if (sub_x < 0 || sub_x >= map_sub_w || sub_y < 0 || sub_y >= map_sub_h) + continue; + + int dist_sq = dx * dx + dy * dy; + if (dist_sq > range_sub * range_sub) + continue; + + if (!trace_sub_los(map, cx, cy, sub_x, sub_y)) + continue; + + float dist = sqrtf((float)dist_sq); + float t = dist / (float)range_sub; + float brightness = 1.0f - smoothstep_light(0.35f, 1.0f, t); + int val = (int)(brightness * (float)intensity); + + if (val > map->light_map[sub_y][sub_x]) + map->light_map[sub_y][sub_x] = (unsigned char)val; + } + } + + int src_x = sources[si].x; + int src_y = sources[si].y; + int src_range = sources[si].range; + int src_intensity = sources[si].intensity; + + for (int ty = 0; ty < MAP_HEIGHT; ty++) { + for (int tx = 0; tx < MAP_WIDTH; tx++) { + if (map->tiles[ty][tx] != TILE_WALL && map->tiles[ty][tx] != TILE_DOOR_CLOSED) + continue; + if (!is_in_view_range(tx, ty, src_x, src_y, src_range)) + continue; + if (!has_line_of_sight(map, src_x, src_y, tx, ty)) + continue; + + int dx = tx - src_x; + int dy = ty - src_y; + float t = sqrtf((float)(dx * dx + dy * dy)) / (float)src_range; + float fb = 1.0f - smoothstep_light(0.35f, 1.0f, t); + int val = (int)(fb * (float)src_intensity); + + int base_x = tx * SUB_TILE_RES; + int base_y = ty * SUB_TILE_RES; + for (int cy2 = 0; cy2 < SUB_TILE_RES; cy2++) { + for (int cx2 = 0; cx2 < SUB_TILE_RES; cx2++) { + if (val > map->light_map[base_y + cy2][base_x + cx2]) + map->light_map[base_y + cy2][base_x + cx2] = (unsigned char)val; + } } } } } + + for (int ty = 0; ty < MAP_HEIGHT; ty++) { + for (int tx = 0; tx < MAP_WIDTH; tx++) { + if (tile_brightness(map, tx, ty) > LIGHT_SIGHT_THRESHOLD) + map->remembered[ty][tx] = 1; + } + } +} + +int tile_brightness(const Map *map, int tx, int ty) { + int sum = 0; + int base_x = tx * SUB_TILE_RES; + int base_y = ty * SUB_TILE_RES; + for (int dy = 0; dy < SUB_TILE_RES; dy++) { + for (int dx = 0; dx < SUB_TILE_RES; dx++) { + sum += map->light_map[base_y + dy][base_x + dx]; + } + } + return sum / (SUB_TILE_RES * SUB_TILE_RES); +} + +int is_tile_revealed(const Map *map, int tx, int ty) { + return tile_brightness(map, tx, ty) > LIGHT_SIGHT_THRESHOLD; } diff --git a/libs/map/map.h b/libs/map/map.h index 84f1119..765a28d 100644 --- a/libs/map/map.h +++ b/libs/map/map.h @@ -21,7 +21,9 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts); // Visibility / Fog of War int is_in_view_range(int x, int y, int view_x, int view_y, int range); int has_line_of_sight(const Map *map, int x1, int y1, int x2, int y2); -void calculate_visibility(Map *map, int x, int y); +void compute_lighting(Map *map, const LightSource *sources, int num_sources); +int tile_brightness(const Map *map, int tx, int ty); +int is_tile_revealed(const Map *map, int tx, int ty); int can_see_entity(const Map *map, int from_x, int from_y, int to_x, int to_y, int range); #endif // MAP_H diff --git a/src/common.h b/src/common.h index f474781..2db9859 100644 --- a/src/common.h +++ b/src/common.h @@ -29,12 +29,19 @@ typedef struct { int x, y, w, h; } Room; +// Light source for sub-tile lighting system +typedef struct { + int x, y; + int intensity; + int range; +} LightSource; + // Map typedef struct { TileType tiles[MAP_HEIGHT][MAP_WIDTH]; Room rooms[MAX_ROOMS]; int room_count; - unsigned char visible[MAP_HEIGHT][MAP_WIDTH]; + unsigned char light_map[MAP_HEIGHT * SUB_TILE_RES][MAP_WIDTH * SUB_TILE_RES]; unsigned char remembered[MAP_HEIGHT][MAP_WIDTH]; } Map; diff --git a/src/game_state.h b/src/game_state.h index 73cdac2..40cde3f 100644 --- a/src/game_state.h +++ b/src/game_state.h @@ -68,6 +68,9 @@ typedef struct { unsigned int run_seed; // Tileset atlas for rendering Tileset tileset; + // Sub-tile lighting + LightSource static_lights[32]; + int static_light_count; // Slash effect timer for attack animations int slash_timer; // frames remaining for slash effect int slash_x, slash_y; // position of slash effect diff --git a/src/main.c b/src/main.c index e31dd99..83007cd 100644 --- a/src/main.c +++ b/src/main.c @@ -131,8 +131,12 @@ static void init_floor(GameState *gs, int floor_num) { } gs->player.floor = floor_num; - // Calculate initial visibility - calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); + // Set initial player light and compute visibility + LightSource player_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; + LightSource sources[1 + 32]; + sources[0] = player_light; + memcpy(sources + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource)); + compute_lighting(&gs->map, sources, 1 + gs->static_light_count); // Spawn enemies enemy_spawn(gs->enemies, &gs->enemy_count, &gs->map, &gs->player, floor_num); @@ -228,7 +232,11 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { } // Update visibility based on player's new position - calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); + LightSource p_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; + LightSource srcs[1 + 32]; + srcs[0] = p_light; + memcpy(srcs + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource)); + compute_lighting(&gs->map, srcs, 1 + gs->static_light_count); // Enemy turns - uses speed/cooldown system enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); @@ -269,7 +277,13 @@ static int handle_stun_turn(GameState *gs) { if (gs->game_over) return 1; enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); - calculate_visibility(&gs->map, gs->player.position.x, gs->player.position.y); + { + LightSource l = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; + LightSource s[1 + 32]; + s[0] = l; + memcpy(s + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource)); + compute_lighting(&gs->map, s, 1 + gs->static_light_count); + } if (gs->player.hp <= 0) gs->game_over = 1; gs->last_message = "You are stunned!"; @@ -652,8 +666,8 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { cam.offset = (Vector2){(float)gs.shake_x, (float)gs.shake_y}; BeginMode2D(cam); render_map(&gs.map, &gs.tileset); - render_items(gs.items, gs.item_count, gs.map.visible, &gs.tileset); - render_enemies(gs.enemies, gs.enemy_count, gs.map.visible, &gs.tileset, frame_counter); + render_items(gs.items, gs.item_count, &gs.map, &gs.tileset); + render_enemies(gs.enemies, gs.enemy_count, &gs.map, &gs.tileset, frame_counter); render_player(&gs.player, &gs.tileset, frame_counter); // Draw slash effect on top of entities if (gs.slash_timer > 0) { diff --git a/src/render.c b/src/render.c index e7b4b27..f18254b 100644 --- a/src/render.c +++ b/src/render.c @@ -1,6 +1,7 @@ #include "render.h" #include "items.h" #include "settings.h" +#include "map/map.h" #include "map/utils.h" #include #include @@ -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, diff --git a/src/render.h b/src/render.h index e36bcdb..6408f62 100644 --- a/src/render.h +++ b/src/render.h @@ -108,12 +108,10 @@ void render_player(const Player *p, const Tileset *tileset, int frame_counter); // Render all enemies using tileset atlas // frame_counter is used for idle breathing animation -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); // Render all items using tileset atlas -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); // Render UI overlay void render_ui(const Player *p, const Tileset *tileset, const FontManager *fm); diff --git a/src/settings.h b/src/settings.h index 2b700ac..19ecf14 100644 --- a/src/settings.h +++ b/src/settings.h @@ -79,10 +79,19 @@ #define MESSAGE_TIMER_DURATION 60 // Visibility / Fog of War -#define PLAYER_VIEW_RANGE 8 -#define ENEMY_VIEW_RANGE 6 #define ENEMY_PATROL_MOVE_CHANCE 30 +// Sub-tile lighting +#define SUB_TILE_RES 8 +#define LIGHT_SIGHT_THRESHOLD 40 + +// Player light source parameters +#define PLAYER_LIGHT_RANGE 8 +#define PLAYER_LIGHT_INTENSITY 255 + +// Enemy vision (default fallback for spawn) +#define ENEMY_VIEW_RANGE 6 + // Visual polish #define DRAW_GRID_LINES 1 From d8b49054d5903dda21aa7b9b0af6fba74792c7bc Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Tue, 28 Apr 2026 16:31:57 +0300 Subject: [PATCH 07/11] various: fixup doors Signed-off-by: NotAShelf Change-Id: Id81f32d86f70a7df99c2ad3d478646416a6a6964 --- libs/map/map.c | 78 +++++++++++-- src/common.h | 3 + src/main.c | 38 ++++++- src/movement.c | 23 ++++ src/render.c | 290 +++++++++++++++++++++++++++++++++++-------------- src/settings.h | 4 + 6 files changed, 347 insertions(+), 89 deletions(-) diff --git a/libs/map/map.c b/libs/map/map.c index 74f1c80..518b89c 100644 --- a/libs/map/map.c +++ b/libs/map/map.c @@ -15,6 +15,9 @@ void map_init(Map *map) { } memset(map->light_map, 0, sizeof(map->light_map)); memset(map->remembered, 0, sizeof(map->remembered)); + memset(map->door_open_from, 255, sizeof(map->door_open_from)); + memset(map->door_anim_timer, 0, sizeof(map->door_anim_timer)); + memset(map->door_anim_target, 0, sizeof(map->door_anim_target)); map->room_count = 0; } @@ -22,7 +25,7 @@ int is_floor(const Map *map, int x, int y) { if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) return 0; return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS || map->tiles[y][x] == TILE_DOOR_OPEN || - map->tiles[y][x] == TILE_DOOR_RUINED; + map->tiles[y][x] == TILE_DOOR_RUINED || map->tiles[y][x] == TILE_DOOR_CLOSED; } void get_room_center(Room *room, int *cx, int *cy) { @@ -128,13 +131,59 @@ static int is_room_boundary(Map *map, int x, int y) { return 0; } -// Place doors at corridor-room junctions -// DISABLED: Door placement removed per user request +// A corridor tile is a narrow floor passage with walls on at least 2 sides. +static int is_corridor_tile(const Map *map, int x, int y) { + if (map->tiles[y][x] != TILE_FLOOR) + return 0; + int walls = 0; + if (y > 0 && map->tiles[y - 1][x] == TILE_WALL) + walls++; + if (y < MAP_HEIGHT - 1 && map->tiles[y + 1][x] == TILE_WALL) + walls++; + if (x > 0 && map->tiles[y][x - 1] == TILE_WALL) + walls++; + if (x < MAP_WIDTH - 1 && map->tiles[y][x + 1] == TILE_WALL) + walls++; + return walls >= 2; +} + +static int tile_in_any_room(int x, int y, Room *rooms, int room_count) { + for (int i = 0; i < room_count; i++) { + if (x >= rooms[i].x && x < rooms[i].x + rooms[i].w && y >= rooms[i].y && y < rooms[i].y + rooms[i].h) + return 1; + } + return 0; +} + +// Place doors at corridor-room junctions. +// Doors sit on corridor tiles, not room tiles, so they occupy the actual doorway. static void place_doors(Map *map, Room *rooms, int room_count) { - (void)map; - (void)rooms; - (void)room_count; - // No-op: doors disabled + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + if (map->tiles[y][x] != TILE_FLOOR) + continue; + if (!is_corridor_tile(map, x, y)) + continue; + // Don't place doors inside rooms — corridors are between rooms + if (tile_in_any_room(x, y, rooms, room_count)) + continue; + // Corridor must be adjacent to a room floor tile + const int dx[4] = {0, 0, 1, -1}; + const int dy[4] = {1, -1, 0, 0}; + for (int i = 0; i < 4; i++) { + int nx = x + dx[i]; + int ny = y + dy[i]; + if (!in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT)) + continue; + if (map->tiles[ny][nx] != TILE_FLOOR) + continue; + if (tile_in_any_room(nx, ny, rooms, room_count)) { + map->tiles[y][x] = TILE_DOOR_CLOSED; + break; + } + } + } + } } // Connect all rooms with corridors @@ -276,7 +325,8 @@ static int trace_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) { if (x == x2 && y == y2) return 1; - if (map->tiles[y][x] == TILE_WALL && !(x == x1 && y == y1)) + TileType t = map->tiles[y][x]; + if ((t == TILE_WALL || t == TILE_DOOR_CLOSED) && !(x == x1 && y == y1)) return 0; int e2 = 2 * err; @@ -421,7 +471,17 @@ void compute_lighting(Map *map, const LightSource *sources, int num_sources) { for (int ty = 0; ty < MAP_HEIGHT; ty++) { for (int tx = 0; tx < MAP_WIDTH; tx++) { - if (tile_brightness(map, tx, ty) > LIGHT_SIGHT_THRESHOLD) + int max_bright = 0; + int bx = tx * SUB_TILE_RES; + int by = ty * SUB_TILE_RES; + for (int dy = 0; dy < SUB_TILE_RES; dy++) { + for (int dx = 0; dx < SUB_TILE_RES; dx++) { + int v = map->light_map[by + dy][bx + dx]; + if (v > max_bright) + max_bright = v; + } + } + if (max_bright > LIGHT_SIGHT_THRESHOLD) map->remembered[ty][tx] = 1; } } diff --git a/src/common.h b/src/common.h index 2db9859..8df2f72 100644 --- a/src/common.h +++ b/src/common.h @@ -43,6 +43,9 @@ typedef struct { int room_count; unsigned char light_map[MAP_HEIGHT * SUB_TILE_RES][MAP_WIDTH * SUB_TILE_RES]; unsigned char remembered[MAP_HEIGHT][MAP_WIDTH]; + unsigned char door_open_from[MAP_HEIGHT][MAP_WIDTH]; // 0=N,1=S,2=E,3=W,255=closed + unsigned char door_anim_timer[MAP_HEIGHT][MAP_WIDTH]; + unsigned char door_anim_target[MAP_HEIGHT][MAP_WIDTH]; // 0=closed, 1=open } Map; // Dungeon diff --git a/src/main.c b/src/main.c index 83007cd..1b8383c 100644 --- a/src/main.c +++ b/src/main.c @@ -231,6 +231,19 @@ static void post_action(GameState *gs, Enemy *attacked_enemy) { } } + // Close doors that the player moved away from before recomputing lighting. + for (int dy = 0; dy < MAP_HEIGHT; dy++) { + for (int dx = 0; dx < MAP_WIDTH; dx++) { + if (gs->map.tiles[dy][dx] == TILE_DOOR_OPEN) { + if (gs->player.position.x != dx || gs->player.position.y != dy) { + gs->map.tiles[dy][dx] = TILE_DOOR_CLOSED; + gs->map.door_anim_target[dy][dx] = 0; + gs->map.door_anim_timer[dy][dx] = DOOR_ANIM_FRAMES; + } + } + } + } + // Update visibility based on player's new position LightSource p_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; LightSource srcs[1 + 32]; @@ -277,6 +290,18 @@ static int handle_stun_turn(GameState *gs) { if (gs->game_over) return 1; enemy_update_all(gs->enemies, gs->enemy_count, &gs->player, &gs->map); + // Close doors that the player moved away from before recomputing lighting. + for (int dy = 0; dy < MAP_HEIGHT; dy++) { + for (int dx = 0; dx < MAP_WIDTH; dx++) { + if (gs->map.tiles[dy][dx] == TILE_DOOR_OPEN) { + if (gs->player.position.x != dx || gs->player.position.y != dy) { + gs->map.tiles[dy][dx] = TILE_DOOR_CLOSED; + gs->map.door_anim_target[dy][dx] = 0; + gs->map.door_anim_timer[dy][dx] = DOOR_ANIM_FRAMES; + } + } + } + } { LightSource l = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; LightSource s[1 + 32]; @@ -639,6 +664,17 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { if (gs.slash_timer > 0) gs.slash_timer--; + // Door animations are visual, so they tick every rendered frame. + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + if (gs.map.door_anim_timer[y][x] > 0) { + gs.map.door_anim_timer[y][x]--; + if (gs.map.door_anim_timer[y][x] == 0 && gs.map.door_anim_target[y][x] == 0) + gs.map.door_open_from[y][x] = 255; + } + } + } + // Update player animation if (gs.player.anim_timer > 0) { gs.player.anim_timer--; @@ -787,4 +823,4 @@ int main(int argc, char **argv) { audio_close(); return 0; -} \ No newline at end of file +} diff --git a/src/movement.c b/src/movement.c index 89e74e1..23afd5c 100644 --- a/src/movement.c +++ b/src/movement.c @@ -1,6 +1,7 @@ #include "movement.h" #include "enemy.h" #include "map/map.h" +#include "map/utils.h" #include // Check if position is occupied by player @@ -8,11 +9,33 @@ static int is_player_at(Player *p, int x, int y) { return (p->position.x == x && p->position.y == y); } +static int direction_to_door_open_from(Vec2 dir) { + if (dir.y == -1) + return 0; // N + if (dir.y == 1) + return 1; // S + if (dir.x == 1) + return 2; // E + if (dir.x == -1) + return 3; // W + return 255; +} + MoveResult try_move_entity(Vec2 *p, Vec2 direction, Map *map, Player *player, Enemy *enemies, int enemy_count, bool moving_is_player) { int new_x = p->x + direction.x; int new_y = p->y + direction.y; + if (!in_bounds(new_x, new_y, MAP_WIDTH, MAP_HEIGHT)) + return MOVE_RESULT_BLOCKED_WALL; + + if (map->tiles[new_y][new_x] == TILE_DOOR_CLOSED) { + map->tiles[new_y][new_x] = TILE_DOOR_OPEN; + map->door_open_from[new_y][new_x] = direction_to_door_open_from(direction); + map->door_anim_target[new_y][new_x] = 1; + map->door_anim_timer[new_y][new_x] = DOOR_ANIM_FRAMES; + } + if (!is_floor(map, new_x, new_y)) return MOVE_RESULT_BLOCKED_WALL; diff --git a/src/render.c b/src/render.c index f18254b..c88584e 100644 --- a/src/render.c +++ b/src/render.c @@ -66,6 +66,43 @@ static void draw_text_body(Font f, const char *text, float x, float y, int size, 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))}; +} + 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++) { @@ -78,6 +115,11 @@ void render_map(const Map *map, const Tileset *tileset) { continue; } + float base_light = brightness > 0 ? AMBIENT_LIGHT_FACTOR : REMEMBERED_LIGHT_FACTOR; + float light_factor = base_light + (1.0f - base_light) * powf((float)brightness / 255.0f, LIGHT_EXPONENT); + if (is_deep_corridor(map, x, y)) + light_factor *= 0.82f; + int tile_id = -1; switch (map->tiles[y][x]) { case TILE_WALL: @@ -100,94 +142,175 @@ void render_map(const Map *map, const Tileset *tileset) { break; } - if (tile_id >= 0 && tileset != NULL && tileset->finalized) { - Rectangle src = tileset_get_region(tileset, tile_id); - if (src.width > 0) { - 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); - continue; + int is_door = (map->tiles[y][x] == TILE_DOOR_CLOSED || map->tiles[y][x] == TILE_DOOR_OPEN); + int tile_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) { + int fv = (int)(255.0f * light_factor); + if (fv > 255) + fv = 255; + Color ftint = (Color){(unsigned char)fv, (unsigned char)fv, (unsigned char)fv, 255}; + DrawTexturePro(tileset->atlas, floor_src, dst, (Vector2){0, 0}, 0.0f, ftint); + tile_drawn = 1; } } - 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}; + if (tile_id >= 0 && tileset != NULL && tileset->finalized && !is_door) { + Rectangle src = tileset_get_region(tileset, tile_id); + if (src.width > 0) { + int is_opaque = (map->tiles[y][x] == TILE_WALL); + float wall_dim = is_opaque ? (AMBIENT_LIGHT_FACTOR + 0.92f * light_factor) : light_factor; + int tv = (int)(255.0f * wall_dim); + if (tv > 255) + tv = 255; + Color tint = (Color){(unsigned char)tv, (unsigned char)tv, (unsigned char)tv, 255}; + DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, tint); + tile_drawn = 1; + } + } - switch (map->tiles[y][x]) { - case TILE_WALL: - DrawRectangleRec(dst, wall_color); - break; - case TILE_FLOOR: - DrawRectangleRec(dst, floor_color); - // Torch flicker: warm tint on floor tiles adjacent to stairs - { - int is_adjacent_to_stairs = 0; - for (int dy = -1; dy <= 1 && !is_adjacent_to_stairs; dy++) { - for (int dx = -1; dx <= 1 && !is_adjacent_to_stairs; dx++) { - int nx = x + dx; - int ny = y + dy; - if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_STAIRS) { - is_adjacent_to_stairs = 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); + + switch (map->tiles[y][x]) { + case TILE_WALL: + DrawRectangleRec(dst, wall_color); + break; + case TILE_FLOOR: + DrawRectangleRec(dst, floor_color); + // Torch flicker: warm tint on floor tiles adjacent to stairs + { + int is_adjacent_to_stairs = 0; + for (int dy = -1; dy <= 1 && !is_adjacent_to_stairs; dy++) { + for (int dx = -1; dx <= 1 && !is_adjacent_to_stairs; dx++) { + int nx = x + dx; + int ny = y + dy; + if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_STAIRS) { + is_adjacent_to_stairs = 1; + } } } + if (is_adjacent_to_stairs && light_factor > 0.05f) { + int flicker = (int)(sinf(GetTime() * 5.0f) * 15.0f); + DrawRectangleRec(dst, (Color){40 + flicker, 25, 10, 60}); + } } - 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 && 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)); + 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 && light_factor > 0.05f) { + 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)); + 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 && light_factor > 0.05f) { + 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; } - // Grid lines - if (DRAW_GRID_LINES && brightness > 0) { - DrawRectangleLines((int)dst.x, (int)dst.y, (int)dst.width, (int)dst.height, (Color){20, 20, 20, 80}); + 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; } - 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 (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}); - } - break; - case TILE_DOOR_CLOSED: - DrawRectangleRec(dst, door_color); - 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}); - DrawText("+", x * TILE_SIZE + 5, y * TILE_SIZE + 1, NORM_FONT, WHITE); - } - break; - case TILE_DOOR_OPEN: - DrawRectangleRec(dst, floor_color); - 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}); - DrawText("'", x * TILE_SIZE + 6, y * TILE_SIZE + 2, NORM_FONT, (Color){150, 140, 120, 255}); - } - break; - case TILE_DOOR_RUINED: - DrawRectangleRec(dst, (Color){60, 45, 30, 255}); - 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}); - 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; + } + + // 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}); } } } @@ -197,6 +320,13 @@ 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}; + // Soft radial glow under the player + int cx = p->position.x * TILE_SIZE + TILE_SIZE / 2; + int cy = p->position.y * TILE_SIZE + TILE_SIZE / 2; + DrawCircle(cx, cy, 14.0f, (Color){255, 220, 100, 20}); + DrawCircle(cx, cy, 10.0f, (Color){255, 230, 150, 35}); + DrawCircle(cx, cy, 6.0f, (Color){255, 240, 180, 55}); + if (tileset != NULL && tileset->finalized) { int tile_id = p->sprite_tile_id; switch (p->anim_state) { @@ -412,7 +542,7 @@ 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 = {25, 20, 15, 255}; // dark parchment + 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 @@ -420,6 +550,8 @@ void render_ui(const Player *p, const Tileset *tileset, const FontManager *fm) { // 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}); diff --git a/src/settings.h b/src/settings.h index 19ecf14..246843f 100644 --- a/src/settings.h +++ b/src/settings.h @@ -84,6 +84,9 @@ // Sub-tile lighting #define SUB_TILE_RES 8 #define LIGHT_SIGHT_THRESHOLD 40 +#define AMBIENT_LIGHT_FACTOR 0.08f +#define REMEMBERED_LIGHT_FACTOR 0.18f +#define LIGHT_EXPONENT 1.7f // Player light source parameters #define PLAYER_LIGHT_RANGE 8 @@ -94,5 +97,6 @@ // Visual polish #define DRAW_GRID_LINES 1 +#define DOOR_ANIM_FRAMES 8 #endif // SETTINGS_H From 514a9560a25095ae89bcd10b2f50af4b08eb39ea Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Mon, 11 May 2026 20:35:13 +0300 Subject: [PATCH 08/11] various: add admin build; various layout improvements Signed-off-by: NotAShelf Change-Id: Ieaa99fa0a32b42b1e97aada611d809b96a6a6964 --- Justfile | 2 +- build.zig | 19 +- libs/map/map.c | 416 +++++++++++++++++++++++++++++++---- libs/map/map.h | 4 + libs/tileset/tileset_paint.c | 231 ++++++++++--------- src/common.h | 25 ++- src/enemy.c | 16 +- src/main.c | 162 +++++++++++++- src/render.c | 178 ++++++++++++--- 9 files changed, 854 insertions(+), 199 deletions(-) diff --git a/Justfile b/Justfile index a425cb3..86415cf 100644 --- a/Justfile +++ b/Justfile @@ -4,7 +4,7 @@ build: # Build and run dev: - zig build run + zig build -Dadmin-controls=true run # Clean build artifacts clean: diff --git a/build.zig b/build.zig index e63af2c..f819593 100644 --- a/build.zig +++ b/build.zig @@ -3,13 +3,22 @@ const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); + const admin_controls = b.option(bool, "admin-controls", "Enable in-game development admin controls") orelse false; - const c_flags = [_][]const u8{ + const base_c_flags = [_][]const u8{ "-std=c99", "-Wall", "-Wextra", "-O2", }; + const admin_c_flags = [_][]const u8{ + "-std=c99", + "-Wall", + "-Wextra", + "-O2", + "-DROGGED_ADMIN_CONTROLS=1", + }; + const c_flags = if (admin_controls) &admin_c_flags else &base_c_flags; // RNG library const rng_lib = b.addLibrary(.{ @@ -22,7 +31,7 @@ pub fn build(b: *std.Build) void { }); rng_lib.addCSourceFiles(.{ .files = &[_][]const u8{"libs/rng/rng.c"}, - .flags = &c_flags, + .flags = c_flags, }); rng_lib.addIncludePath(b.path("libs/rng")); @@ -40,7 +49,7 @@ pub fn build(b: *std.Build) void { "libs/map/map.c", "libs/map/utils.c", }, - .flags = &c_flags, + .flags = c_flags, }); // map.h includes common.h and settings.h which live in src/ map_lib.addIncludePath(b.path("src")); @@ -63,7 +72,7 @@ pub fn build(b: *std.Build) void { "libs/tileset/tileset.c", "libs/tileset/tileset_paint.c", }, - .flags = &c_flags, + .flags = c_flags, }); // tileset.h includes settings.h which lives in src/ tileset_obj.addIncludePath(b.path("src")); @@ -111,7 +120,7 @@ pub fn build(b: *std.Build) void { exe.addCSourceFiles(.{ .files = &c_sources, - .flags = &c_flags, + .flags = c_flags, }); // src/ for own headers; libs/ so "rng/rng.h" and "map/map.h" resolve diff --git a/libs/map/map.c b/libs/map/map.c index 518b89c..791aadf 100644 --- a/libs/map/map.c +++ b/libs/map/map.c @@ -25,7 +25,8 @@ int is_floor(const Map *map, int x, int y) { if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) return 0; return map->tiles[y][x] == TILE_FLOOR || map->tiles[y][x] == TILE_STAIRS || map->tiles[y][x] == TILE_DOOR_OPEN || - map->tiles[y][x] == TILE_DOOR_RUINED || map->tiles[y][x] == TILE_DOOR_CLOSED; + map->tiles[y][x] == TILE_DOOR_RUINED || map->tiles[y][x] == TILE_DOOR_CLOSED || + map->tiles[y][x] == TILE_RUBBLE || map->tiles[y][x] == TILE_SHALLOW_WATER; } void get_room_center(Room *room, int *cx, int *cy) { @@ -37,9 +38,25 @@ void get_room_center(Room *room, int *cx, int *cy) { static void carve_room(Map *map, Room *room) { for (int y = room->y; y < room->y + room->h; y++) { for (int x = room->x; x < room->x + room->w; x++) { - if (in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) { - map->tiles[y][x] = TILE_FLOOR; + if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT)) + continue; + + int local_x = x - room->x; + int local_y = y - room->y; + int carved = 1; + + if ((room->type == ROOM_SHRINE || room->type == ROOM_VAULT) && + ((local_x == 0 && local_y == 0) || (local_x == room->w - 1 && local_y == 0) || + (local_x == 0 && local_y == room->h - 1) || (local_x == room->w - 1 && local_y == room->h - 1))) { + carved = 0; } + + if (room->type == ROOM_CRYPT && + ((local_x == 0 || local_x == room->w - 1) && (local_y == 0 || local_y == room->h - 1))) + carved = 0; + + if (carved) + map->tiles[y][x] = TILE_FLOOR; } } } @@ -71,39 +88,125 @@ static int room_overlaps(Room *rooms, int count, Room *new_room) { // Add padding to prevent rooms from touching for (int i = 0; i < count; i++) { Room *r = &rooms[i]; - if (!(new_room->x > r->x + r->w || new_room->x + new_room->w < r->x || new_room->y > r->y + r->h || - new_room->y + new_room->h < r->y)) { + if (!(new_room->x > r->x + r->w + 2 || new_room->x + new_room->w + 2 < r->x || new_room->y > r->y + r->h + 2 || + new_room->y + new_room->h + 2 < r->y)) { return 1; } } return 0; } +static RoomType pick_room_type(int floor, int room_index) { + if (room_index == 0) + return ROOM_START; + + int roll = rng_int(0, 99); + if (floor <= 1) { + if (roll < 35) + return ROOM_GUARD; + if (roll < 55) + return ROOM_ARMORY; + if (roll < 75) + return ROOM_SHRINE; + return ROOM_CRYPT; + } + if (floor == 2) { + if (roll < 40) + return ROOM_CISTERN; + if (roll < 60) + return ROOM_GUARD; + if (roll < 80) + return ROOM_SHRINE; + return ROOM_CRYPT; + } + if (floor == 3) { + if (roll < 35) + return ROOM_CRYPT; + if (roll < 60) + return ROOM_LIBRARY; + if (roll < 80) + return ROOM_CISTERN; + return ROOM_VAULT; + } + if (floor == 4) { + if (roll < 35) + return ROOM_FORGE; + if (roll < 55) + return ROOM_ARMORY; + if (roll < 75) + return ROOM_CRYPT; + return ROOM_VAULT; + } + if (roll < 30) + return ROOM_VAULT; + if (roll < 55) + return ROOM_CRYPT; + if (roll < 75) + return ROOM_FORGE; + return ROOM_SHRINE; +} + +static void room_size_for_type(RoomType type, int *w, int *h) { + switch (type) { + case ROOM_START: + *w = rng_int(7, 10); + *h = rng_int(6, 8); + break; + case ROOM_GUARD: + case ROOM_ARMORY: + *w = rng_int(6, 11); + *h = rng_int(5, 8); + break; + case ROOM_SHRINE: + case ROOM_VAULT: + *w = rng_int(7, 11); + *h = rng_int(7, 11); + break; + case ROOM_CISTERN: + *w = rng_int(8, 14); + *h = rng_int(6, 10); + break; + case ROOM_CRYPT: + case ROOM_LIBRARY: + *w = rng_int(8, 13); + *h = rng_int(5, 9); + break; + case ROOM_FORGE: + *w = rng_int(7, 12); + *h = rng_int(6, 9); + break; + } +} + // Generate rooms for this floor static int generate_rooms(Map *map, Room *rooms, int floor) { int room_count = 0; int attempts = 0; - int max_attempts = 100; + int max_attempts = 250; // Room count varies by floor, but capped at max_rooms - int target_rooms = 5 + (floor % 3) + rng_int(0, 3); + int target_rooms = 8 + (floor % 3) + rng_int(0, 3); if (target_rooms > MAX_ROOMS) target_rooms = MAX_ROOMS; while (room_count < target_rooms && attempts < max_attempts) { attempts++; - // Random room dimensions - int w = rng_int(5, 12); - int h = rng_int(5, 10); + RoomType type = pick_room_type(floor, room_count); + int w, h; + room_size_for_type(type, &w, &h); - // Random position (within map bounds with 1-tile border) - int x = rng_int(2, MAP_WIDTH - w - 2); - int y = rng_int(2, MAP_HEIGHT - h - 2); + int x, y; + if (room_count == 0) { + x = rng_int(3, 8); + y = rng_int(3, MAP_HEIGHT - h - 4); + } else { + x = rng_int(2, MAP_WIDTH - w - 3); + y = rng_int(2, MAP_HEIGHT - h - 3); + } - Room new_room = {x, y, w, h}; + Room new_room = {x, y, w, h, type}; - // Check for overlap if (!room_overlaps(rooms, room_count, &new_room)) { rooms[room_count] = new_room; carve_room(map, &new_room); @@ -155,6 +258,103 @@ static int tile_in_any_room(int x, int y, Room *rooms, int room_count) { return 0; } +static int tile_in_room(const Room *room, int x, int y) { + return x >= room->x && x < room->x + room->w && y >= room->y && y < room->y + room->h; +} + +Room *map_room_at(Map *map, int x, int y) { + for (int i = 0; i < map->room_count; i++) { + if (tile_in_room(&map->rooms[i], x, y)) + return &map->rooms[i]; + } + return NULL; +} + +static int protected_room_tile(const Room *room, int x, int y) { + int cx, cy; + get_room_center((Room *)room, &cx, &cy); + return (abs(x - cx) <= 1 && abs(y - cy) <= 1) || x == room->x || y == room->y || x == room->x + room->w - 1 || + y == room->y + room->h - 1; +} + +static void set_room_tile(Map *map, const Room *room, int x, int y, TileType tile) { + if (!in_bounds(x, y, MAP_WIDTH, MAP_HEIGHT) || !tile_in_room(room, x, y) || protected_room_tile(room, x, y)) + return; + if (map->tiles[y][x] == TILE_FLOOR) + map->tiles[y][x] = tile; +} + +static void decorate_room(Map *map, const Room *room, int floor) { + int cx, cy; + get_room_center((Room *)room, &cx, &cy); + + switch (room->type) { + case ROOM_START: + if (room->w >= 8 && room->h >= 6) { + set_room_tile(map, room, room->x + 2, room->y + 2, TILE_RUBBLE); + set_room_tile(map, room, room->x + room->w - 3, room->y + room->h - 3, TILE_RUBBLE); + } + break; + case ROOM_GUARD: + for (int y = room->y + 2; y < room->y + room->h - 2; y += 3) { + set_room_tile(map, room, room->x + 2, y, TILE_STATUE); + set_room_tile(map, room, room->x + room->w - 3, y, TILE_STATUE); + } + break; + case ROOM_SHRINE: + set_room_tile(map, room, cx - 2, cy, TILE_STATUE); + set_room_tile(map, room, cx + 2, cy, TILE_STATUE); + set_room_tile(map, room, cx, cy - 2, TILE_RUBBLE); + set_room_tile(map, room, cx, cy + 2, TILE_RUBBLE); + break; + case ROOM_CISTERN: + for (int y = cy - 1; y <= cy + 1; y++) { + for (int x = cx - 2; x <= cx + 2; x++) { + if ((x + y + floor) % 5 != 0) + set_room_tile(map, room, x, y, TILE_SHALLOW_WATER); + } + } + break; + case ROOM_ARMORY: + for (int x = room->x + 2; x < room->x + room->w - 2; x += 3) { + set_room_tile(map, room, x, room->y + 2, TILE_RUBBLE); + set_room_tile(map, room, x, room->y + room->h - 3, TILE_RUBBLE); + } + break; + case ROOM_CRYPT: + for (int x = room->x + 2; x < room->x + room->w - 2; x += 3) { + set_room_tile(map, room, x, cy - 1, TILE_STATUE); + set_room_tile(map, room, x, cy + 1, TILE_RUBBLE); + } + break; + case ROOM_LIBRARY: + for (int y = room->y + 2; y < room->y + room->h - 2; y += 2) { + set_room_tile(map, room, room->x + 2, y, TILE_STATUE); + set_room_tile(map, room, room->x + room->w - 3, y, TILE_RUBBLE); + } + break; + case ROOM_FORGE: + for (int y = cy - 1; y <= cy + 1; y++) { + set_room_tile(map, room, cx - 2, y, TILE_RUBBLE); + set_room_tile(map, room, cx + 2, y, TILE_RUBBLE); + } + set_room_tile(map, room, cx, cy - 2, TILE_STATUE); + break; + case ROOM_VAULT: + set_room_tile(map, room, cx - 2, cy - 2, TILE_STATUE); + set_room_tile(map, room, cx + 2, cy - 2, TILE_STATUE); + set_room_tile(map, room, cx - 2, cy + 2, TILE_STATUE); + set_room_tile(map, room, cx + 2, cy + 2, TILE_STATUE); + break; + } +} + +static void decorate_rooms(Map *map, Room *rooms, int room_count, int floor) { + for (int i = 0; i < room_count; i++) { + decorate_room(map, &rooms[i], floor); + } +} + // Place doors at corridor-room junctions. // Doors sit on corridor tiles, not room tiles, so they occupy the actual doorway. static void place_doors(Map *map, Room *rooms, int room_count) { @@ -187,23 +387,56 @@ static void place_doors(Map *map, Room *rooms, int room_count) { } // Connect all rooms with corridors -static void connect_rooms(Map *map, Room *rooms, int room_count) { - for (int i = 0; i < room_count - 1; i++) { - int cx1, cy1, cx2, cy2; - get_room_center(&rooms[i], &cx1, &cy1); - get_room_center(&rooms[i + 1], &cx2, &cy2); +static int room_distance2(Room *a, Room *b) { + int ax, ay, bx, by; + get_room_center(a, &ax, &ay); + get_room_center(b, &bx, &by); + int dx = ax - bx; + int dy = ay - by; + return dx * dx + dy * dy; +} - // Carve L-shaped corridor between rooms - if (rng_int(0, 1) == 0) { - carve_h_corridor(map, cx1, cx2, cy1); - carve_v_corridor(map, cx2, cy1, cy2); - } else { - carve_v_corridor(map, cx1, cy1, cy2); - carve_h_corridor(map, cx1, cx2, cy2); +static void carve_connection(Map *map, Room *a, Room *b) { + int cx1, cy1, cx2, cy2; + get_room_center(a, &cx1, &cy1); + get_room_center(b, &cx2, &cy2); + + if (rng_int(0, 2) == 0) { + int mid_x = (cx1 + cx2) / 2 + rng_int(-3, 3); + carve_h_corridor(map, cx1, mid_x, cy1); + carve_v_corridor(map, mid_x, cy1, cy2); + carve_h_corridor(map, mid_x, cx2, cy2); + } else if (rng_int(0, 1) == 0) { + carve_h_corridor(map, cx1, cx2, cy1); + carve_v_corridor(map, cx2, cy1, cy2); + } else { + carve_v_corridor(map, cx1, cy1, cy2); + carve_h_corridor(map, cx1, cx2, cy2); + } +} + +static void connect_rooms(Map *map, Room *rooms, int room_count) { + for (int i = 1; i < room_count; i++) { + int nearest = 0; + int best = room_distance2(&rooms[i], &rooms[0]); + for (int j = 1; j < i; j++) { + int dist = room_distance2(&rooms[i], &rooms[j]); + if (dist < best) { + best = dist; + nearest = j; + } } + carve_connection(map, &rooms[i], &rooms[nearest]); + } + + for (int i = 0; i < room_count; i++) { + if (rng_int(0, 99) >= 28) + continue; + int j = rng_int(0, room_count - 1); + if (i != j) + carve_connection(map, &rooms[i], &rooms[j]); } - // Place doors after all corridors are carved place_doors(map, rooms, room_count); } @@ -284,18 +517,121 @@ void get_random_floor_tile(Map *map, int *x, int *y, int attempts) { } } +int get_room_floor_tile(const Map *map, const Room *room, int *x, int *y) { + *x = -1; + *y = -1; + + int cx = room->x + room->w / 2; + int cy = room->y + room->h / 2; + if (in_bounds(cx, cy, MAP_WIDTH, MAP_HEIGHT) && map->tiles[cy][cx] == TILE_FLOOR) { + *x = cx; + *y = cy; + return 1; + } + + for (int radius = 1; radius < room->w + room->h; radius++) { + for (int yy = cy - radius; yy <= cy + radius; yy++) { + for (int xx = cx - radius; xx <= cx + radius; xx++) { + if (!tile_in_room(room, xx, yy) || !in_bounds(xx, yy, MAP_WIDTH, MAP_HEIGHT)) + continue; + if (map->tiles[yy][xx] == TILE_FLOOR) { + *x = xx; + *y = yy; + return 1; + } + } + } + } + + return 0; +} + +int get_random_floor_tile_excluding_room(Map *map, const Room *excluded, int *x, int *y, int attempts) { + *x = -1; + *y = -1; + + for (int i = 0; i < attempts; i++) { + int tx = rng_int(1, MAP_WIDTH - 2); + int ty = rng_int(1, MAP_HEIGHT - 2); + + if (map->tiles[ty][tx] == TILE_FLOOR && (excluded == NULL || !tile_in_room(excluded, tx, ty))) { + *x = tx; + *y = ty; + return 1; + } + } + + for (int ty = 1; ty < MAP_HEIGHT - 1; ty++) { + for (int tx = 1; tx < MAP_WIDTH - 1; tx++) { + if (map->tiles[ty][tx] == TILE_FLOOR && (excluded == NULL || !tile_in_room(excluded, tx, ty))) { + *x = tx; + *y = ty; + return 1; + } + } + } + + return 0; +} + +int map_validate_layout(const Map *map) { + if (map->room_count <= 0) + return 0; + + int sx, sy; + if (!get_room_floor_tile(map, &map->rooms[0], &sx, &sy)) + return 0; + + unsigned char visited[MAP_HEIGHT][MAP_WIDTH]; + Vec2 queue[MAP_HEIGHT * MAP_WIDTH]; + memset(visited, 0, sizeof(visited)); + + int head = 0; + int tail = 0; + queue[tail++] = (Vec2){sx, sy}; + visited[sy][sx] = 1; + + while (head < tail) { + Vec2 p = queue[head++]; + const int dx[4] = {0, 0, 1, -1}; + const int dy[4] = {1, -1, 0, 0}; + for (int i = 0; i < 4; i++) { + int nx = p.x + dx[i]; + int ny = p.y + dy[i]; + if (!in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) || visited[ny][nx] || !is_floor(map, nx, ny)) + continue; + visited[ny][nx] = 1; + queue[tail++] = (Vec2){nx, ny}; + } + } + + int stairs = 0; + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + if (is_floor(map, x, y) && !visited[y][x]) + return 0; + if (map->tiles[y][x] == TILE_STAIRS) { + if (!visited[y][x]) + return 0; + stairs++; + } + } + } + + return stairs == 1; +} + void dungeon_generate(Dungeon *d, Map *map, int floor_num) { - // Initialize map to all walls - map_init(map); + for (int attempt = 0; attempt < 4; attempt++) { + map_init(map); + map->room_count = generate_rooms(map, map->rooms, floor_num); + connect_rooms(map, map->rooms, map->room_count); + decorate_rooms(map, map->rooms, map->room_count, floor_num); + place_stairs(map, map->rooms, map->room_count); - // Generate rooms - map->room_count = generate_rooms(map, map->rooms, floor_num); - - // Connect rooms with corridors - connect_rooms(map, map->rooms, map->room_count); - - // Place stairs in last room - place_stairs(map, map->rooms, map->room_count); + if (map_validate_layout(map)) + break; + } // Store dungeon state d->current_floor = floor_num; @@ -326,7 +662,7 @@ static int trace_line_of_sight(const Map *map, int x1, int y1, int x2, int y2) { return 1; TileType t = map->tiles[y][x]; - if ((t == TILE_WALL || t == TILE_DOOR_CLOSED) && !(x == x1 && y == y1)) + if ((t == TILE_WALL || t == TILE_DOOR_CLOSED || t == TILE_STATUE) && !(x == x1 && y == y1)) return 0; int e2 = 2 * err; @@ -363,7 +699,7 @@ static int is_solid(const Map *map, int sub_x, int sub_y) { if (tx < 0 || tx >= map_w || ty < 0 || ty >= map_h) return 1; TileType t = map->tiles[ty][tx]; - return t == TILE_WALL || t == TILE_DOOR_CLOSED; + return t == TILE_WALL || t == TILE_DOOR_CLOSED || t == TILE_STATUE; } static float smoothstep_light(float edge0, float edge1, float x) { diff --git a/libs/map/map.h b/libs/map/map.h index 765a28d..4472a6c 100644 --- a/libs/map/map.h +++ b/libs/map/map.h @@ -17,6 +17,10 @@ void map_init(Map *map); // Get a random floor tile position void get_random_floor_tile(Map *map, int *x, int *y, int attempts); +int get_room_floor_tile(const Map *map, const Room *room, int *x, int *y); +int get_random_floor_tile_excluding_room(Map *map, const Room *excluded, int *x, int *y, int attempts); +Room *map_room_at(Map *map, int x, int y); +int map_validate_layout(const Map *map); // Visibility / Fog of War int is_in_view_range(int x, int y, int view_x, int view_y, int range); diff --git a/libs/tileset/tileset_paint.c b/libs/tileset/tileset_paint.c index 0925345..8e39a09 100644 --- a/libs/tileset/tileset_paint.c +++ b/libs/tileset/tileset_paint.c @@ -42,44 +42,67 @@ void paint_wall_tile(Tileset *ts, int id, int variant) { BeginTextureMode(ts->render_target); - // Base color is much darker for better contrast against floor - Color base = variant == 0 ? (Color){45, 42, 38, 255} : (Color){35, 32, 28, 255}; - DrawRectangle((int)off.x, (int)off.y, w, h, base); + int ox = (int)off.x; + int oy = (int)off.y; + Color base = variant == 0 ? (Color){38, 37, 39, 255} : (Color){33, 32, 35, 255}; + Color edge = variant == 0 ? (Color){48, 46, 46, 255} : (Color){42, 40, 41, 255}; + Color shadow = variant == 0 ? (Color){24, 23, 25, 255} : (Color){20, 19, 21, 255}; + Color highlight = variant == 0 ? (Color){56, 54, 52, 255} : (Color){49, 47, 46, 255}; + Color moss = variant == 0 ? (Color){38, 58, 42, 255} : (Color){28, 45, 34, 255}; - // Brick pattern - int brick_h = h / 3; - int brick_w = w / 2; - Color mortar = (Color){25, 22, 18, 255}; - Color brick_light = variant == 0 ? (Color){70, 65, 60, 255} : (Color){50, 47, 43, 255}; - Color brick_dark = variant == 0 ? (Color){40, 37, 33, 255} : (Color){30, 27, 24, 255}; + DrawRectangle(ox, oy, w, h, base); + lcg_srand((unsigned int)(variant * 4099 + id * 9176)); + int row_y = 0; for (int row = 0; row < 3; row++) { - int y = (int)off.y + row * brick_h; - int offset_x = (row % 2 == 0) ? 0 : brick_w / 2; - for (int col = -1; col < 3; col++) { - int x = (int)off.x + offset_x + col * brick_w; - if (x >= (int)off.x + w) - break; - if (x + brick_w <= (int)off.x) - continue; - - // Clip to tile bounds - int draw_x = x < (int)off.x ? (int)off.x : x; - int draw_w = brick_w; - if (draw_x + draw_w > (int)off.x + w) - draw_w = (int)off.x + w - draw_x; - - Color c = ((col + row) % 2 == 0) ? brick_light : brick_dark; - DrawRectangle(draw_x, y, draw_w, brick_h - 1, c); + int block_h = row == 2 ? h - row_y : lcg_rand_range(5, 6); + int x = 0; + int stagger = (row % 2) ? lcg_rand_range(-4, 0) : lcg_rand_range(-1, 2); + while (x < w) { + int block_w = lcg_rand_range(7, 12); + int bx = ox + x + stagger; + int by = oy + row_y; + int bw = block_w; + if (bx < ox) { + bw -= ox - bx; + bx = ox; + } + if (bx + bw > ox + w) + bw = ox + w - bx; + if (bw > 0) { + Color face = ((x + row + variant) % 3 == 0) ? edge : base; + DrawRectangle(bx, by, bw, block_h, face); + if (row == 0) + DrawLine(bx, by, bx + bw - 1, by, highlight); + DrawLine(bx, by + block_h - 1, bx + bw - 1, by + block_h - 1, shadow); + if (bw > 5 && block_h > 4 && lcg_rand_range(0, 99) < 18) + DrawPixel(bx + lcg_rand_range(1, bw - 2), by + lcg_rand_range(1, block_h - 2), shadow); + } + x += block_w; } + row_y += block_h; } - // Mortar lines - for (int row = 1; row < 3; row++) { - int y = (int)off.y + row * brick_h; - DrawLine((int)off.x, y, (int)off.x + w - 1, y, mortar); + for (int i = 0; i < 3 + variant; i++) { + int px = ox + lcg_rand_range(1, w - 2); + int py = oy + lcg_rand_range(1, h - 2); + DrawPixel(px, py, lcg_rand_range(0, 1) ? highlight : shadow); } + if (variant == 1) { + DrawLine(ox + 3, oy + 1, ox + 5, oy + 5, shadow); + DrawLine(ox + 5, oy + 5, ox + 4, oy + 8, shadow); + DrawLine(ox + 11, oy + 4, ox + 8, oy + 7, shadow); + } else { + DrawLine(ox + 9, oy + 2, ox + 12, oy + 5, shadow); + DrawLine(ox + 12, oy + 5, ox + 10, oy + 9, shadow); + DrawPixel(ox + 3, oy + 13, moss); + DrawPixel(ox + 4, oy + 13, moss); + DrawPixel(ox + 4, oy + 14, moss); + } + + DrawRectangle(ox, oy + h - 2, w, 2, (Color){16, 15, 17, 255}); + EndTextureMode(); } @@ -95,42 +118,44 @@ void paint_floor_tile(Tileset *ts, int id, int variant) { BeginTextureMode(ts->render_target); - // Base stone color - lighter than walls for contrast - Color base = (Color){75, 72, 68, 255}; - DrawRectangle((int)off.x, (int)off.y, w, h, base); + int ox = (int)off.x; + int oy = (int)off.y; + Color base = variant == 0 ? (Color){61, 58, 55, 255} + : variant == 1 ? (Color){67, 63, 58, 255} + : variant == 2 ? (Color){56, 55, 58, 255} + : (Color){64, 59, 53, 255}; + Color bevel = (Color){86, 82, 75, 255}; + Color seam = (Color){34, 32, 31, 255}; + Color chip = (Color){43, 41, 40, 255}; - // Seeded noise based on variant + DrawRectangle(ox, oy, w, h, base); lcg_srand((unsigned int)(variant * 7919 + id * 104729)); - // Dithered noise dots - lighter shades - int num_dots = 8 + variant * 4; + int split_x = lcg_rand_range(6, 10); + int split_y = lcg_rand_range(6, 10); + DrawLine(ox + split_x, oy + 1, ox + split_x, oy + h - 2, seam); + DrawLine(ox + 1, oy + split_y, ox + w - 2, oy + split_y, seam); + DrawLine(ox + 1, oy + 1, ox + w - 2, oy + 1, bevel); + DrawLine(ox + 1, oy + 1, ox + 1, oy + h - 2, bevel); + DrawLine(ox + 1, oy + h - 2, ox + w - 2, oy + h - 2, seam); + DrawLine(ox + w - 2, oy + 1, ox + w - 2, oy + h - 2, seam); + + int num_dots = 14 + variant * 5; for (int i = 0; i < num_dots; i++) { - int px = (int)off.x + lcg_rand_range(1, w - 2); - int py = (int)off.y + lcg_rand_range(1, h - 2); - int shade = lcg_rand_range(0, 3); - Color c; - if (shade == 0) - c = (Color){85, 82, 78, 255}; - else if (shade == 1) - c = (Color){65, 62, 58, 255}; - else - c = (Color){90, 87, 83, 255}; + int px = ox + lcg_rand_range(1, w - 2); + int py = oy + lcg_rand_range(1, h - 2); + Color c = lcg_rand_range(0, 2) == 0 ? bevel : chip; DrawPixel(px, py, c); } - // Occasional crack line - if (variant >= 2) { - int crack_x = (int)off.x + lcg_rand_range(2, w - 3); - int crack_y = (int)off.y + lcg_rand_range(2, h - 3); - int crack_len = lcg_rand_range(2, 4); - int crack_dir = lcg_rand_range(0, 1); // 0 = horizontal, 1 = vertical - Color crack_color = (Color){55, 52, 48, 255}; - for (int i = 0; i < crack_len; i++) { - if (crack_dir == 0) - DrawPixel(crack_x + i, crack_y, crack_color); - else - DrawPixel(crack_x, crack_y + i, crack_color); - } + if (variant >= 1) { + int crack_x = ox + lcg_rand_range(3, w - 4); + int crack_y = oy + lcg_rand_range(3, h - 4); + DrawPixel(crack_x, crack_y, seam); + DrawPixel(crack_x + 1, crack_y, seam); + DrawPixel(crack_x + 1, crack_y + 1, seam); + if (variant >= 2) + DrawPixel(crack_x + 2, crack_y + 2, seam); } EndTextureMode(); @@ -148,32 +173,25 @@ void paint_stairs_tile(Tileset *ts, int id) { BeginTextureMode(ts->render_target); - // Dark stone background - DrawRectangle((int)off.x, (int)off.y, w, h, (Color){40, 38, 35, 255}); + int ox = (int)off.x; + int oy = (int)off.y; + DrawRectangle(ox, oy, w, h, (Color){31, 29, 27, 255}); - // Stair steps (3 steps) - int step_h = h / 4; - Color step_light = (Color){100, 95, 90, 255}; - Color step_dark = (Color){60, 57, 53, 255}; - - for (int i = 0; i < 3; i++) { - int y = (int)off.y + h - (i + 1) * step_h; - int inset = i * 2; - int x = (int)off.x + inset; - DrawRectangle(x, y, w - inset * 2, step_h - 1, step_light); - DrawLine(x, y + step_h - 1, x + w - inset * 2 - 1, y + step_h - 1, step_dark); + Color face = (Color){82, 76, 68, 255}; + Color lip = (Color){118, 106, 84, 255}; + Color drop = (Color){28, 25, 23, 255}; + for (int i = 0; i < 4; i++) { + int y = oy + 3 + i * 3; + int inset = i + 1; + DrawRectangle(ox + inset, y, w - inset * 2, 2, face); + DrawLine(ox + inset, y, ox + w - inset - 1, y, lip); + DrawLine(ox + inset, y + 2, ox + w - inset - 1, y + 2, drop); } - // ">" symbol on top step - int top_y = (int)off.y + 2; - int cx = (int)off.x + w / 2; - Color arrow = (Color){180, 175, 170, 255}; - DrawPixel(cx, top_y, arrow); - DrawPixel(cx - 1, top_y + 1, arrow); - DrawPixel(cx + 1, top_y + 1, arrow); - DrawPixel(cx - 2, top_y + 2, arrow); - DrawPixel(cx + 2, top_y + 2, arrow); - DrawPixel(cx, top_y + 3, arrow); + DrawLine(ox + 4, oy + 2, ox + 11, oy + 2, (Color){120, 111, 93, 255}); + DrawPixel(ox + 7, oy + 1, (Color){142, 134, 114, 255}); + DrawPixel(ox + 8, oy + 1, (Color){142, 134, 114, 255}); + DrawRectangle(ox + 6, oy + 13, 4, 1, (Color){12, 10, 9, 255}); EndTextureMode(); } @@ -634,23 +652,22 @@ void paint_door_closed_tile(Tileset *ts, int id) { BeginTextureMode(ts->render_target); - // Wooden door frame - Color wood_dark = (Color){100, 70, 40, 255}; - Color wood_light = (Color){140, 100, 60, 255}; - Color wood_mid = (Color){120, 85, 50, 255}; + Color iron = (Color){35, 32, 30, 255}; + Color wood_dark = (Color){72, 47, 30, 255}; + Color wood_mid = (Color){111, 73, 42, 255}; + Color wood_light = (Color){151, 103, 58, 255}; - // Frame - DrawRectangle(ox, oy, w, h, wood_dark); - // Panels - DrawRectangle(ox + 2, oy + 2, w - 4, h - 4, wood_mid); - // Inner panel - DrawRectangle(ox + 4, oy + 4, w - 8, h - 8, wood_light); - // Cross pattern - DrawRectangle(ox + 7, oy + 2, 2, h - 4, wood_dark); - DrawRectangle(ox + 2, oy + 7, w - 4, 2, wood_dark); - // Handle - DrawPixel(ox + 12, oy + 8, (Color){200, 180, 50, 255}); - DrawPixel(ox + 12, oy + 9, (Color){200, 180, 50, 255}); + DrawRectangle(ox, oy, w, h, iron); + DrawRectangle(ox + 2, oy + 1, w - 4, h - 2, wood_dark); + DrawRectangle(ox + 3, oy + 2, 3, h - 4, wood_mid); + DrawRectangle(ox + 7, oy + 2, 3, h - 4, wood_light); + DrawRectangle(ox + 11, oy + 2, 2, h - 4, wood_mid); + DrawRectangle(ox + 2, oy + 5, w - 4, 2, iron); + DrawRectangle(ox + 2, oy + 11, w - 4, 2, iron); + DrawPixel(ox + 4, oy + 3, (Color){180, 129, 72, 255}); + DrawPixel(ox + 8, oy + 9, (Color){84, 53, 32, 255}); + DrawPixel(ox + 12, oy + 8, (Color){208, 169, 69, 255}); + DrawPixel(ox + 12, oy + 9, (Color){166, 124, 51, 255}); EndTextureMode(); } @@ -669,17 +686,19 @@ void paint_door_open_tile(Tileset *ts, int id) { BeginTextureMode(ts->render_target); - // Open door - shows floor with door frame on sides - Color floor = (Color){75, 72, 68, 255}; - Color wood_dark = (Color){100, 70, 40, 255}; + Color floor = (Color){58, 55, 52, 255}; + Color seam = (Color){32, 30, 29, 255}; + Color wood_dark = (Color){72, 47, 30, 255}; + Color wood_light = (Color){131, 88, 49, 255}; - // Floor DrawRectangle(ox, oy, w, h, floor); - // Door frame on left side (open) + DrawLine(ox + 7, oy + 1, ox + 7, oy + h - 2, seam); + DrawLine(ox + 1, oy + 8, ox + w - 2, oy + 8, seam); DrawRectangle(ox, oy, 3, h, wood_dark); - // Hinges - DrawPixel(ox + 1, oy + 4, (Color){80, 80, 80, 255}); - DrawPixel(ox + 1, oy + 12, (Color){80, 80, 80, 255}); + DrawRectangle(ox + 2, oy + 2, 2, h - 4, wood_light); + DrawPixel(ox + 1, oy + 4, (Color){72, 72, 72, 255}); + DrawPixel(ox + 1, oy + 11, (Color){72, 72, 72, 255}); + DrawRectangle(ox + 4, oy + 13, 6, 1, (Color){25, 20, 17, 255}); EndTextureMode(); } diff --git a/src/common.h b/src/common.h index 8df2f72..3de5ecf 100644 --- a/src/common.h +++ b/src/common.h @@ -9,7 +9,17 @@ typedef struct { } Vec2; // Tile types -typedef enum { TILE_WALL, TILE_FLOOR, TILE_STAIRS, TILE_DOOR_CLOSED, TILE_DOOR_OPEN, TILE_DOOR_RUINED } TileType; +typedef enum { + TILE_WALL, + TILE_FLOOR, + TILE_STAIRS, + TILE_DOOR_CLOSED, + TILE_DOOR_OPEN, + TILE_DOOR_RUINED, + TILE_RUBBLE, + TILE_SHALLOW_WATER, + TILE_STATUE, +} TileType; // Status effect types typedef enum { EFFECT_NONE, EFFECT_POISON, EFFECT_STUN, EFFECT_BLEED, EFFECT_WEAKEN, EFFECT_BURN } StatusEffectType; @@ -25,8 +35,21 @@ typedef struct { } StatusEffect; // Room +typedef enum { + ROOM_START, + ROOM_GUARD, + ROOM_SHRINE, + ROOM_CISTERN, + ROOM_ARMORY, + ROOM_CRYPT, + ROOM_LIBRARY, + ROOM_FORGE, + ROOM_VAULT, +} RoomType; + typedef struct { int x, y, w, h; + RoomType type; } Room; // Light source for sub-tile lighting system diff --git a/src/enemy.c b/src/enemy.c index 084a954..ffa59d1 100644 --- a/src/enemy.c +++ b/src/enemy.c @@ -26,16 +26,14 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { if (floor >= 4) max_type = 3; - // Get the player's starting room (first room) to exclude from enemy spawn - Room *start_room = NULL; - if (map->room_count > 0) { - start_room = &map->rooms[0]; - } + Room *start_room = map_room_at(map, p->position.x, p->position.y); - for (int i = 0; i < num_enemies; i++) { - // Find random floor position + int attempts = 0; + while (*count < num_enemies && attempts < num_enemies * 40) { + attempts++; int ex, ey; - get_random_floor_tile(map, &ex, &ey, 50); + if (!get_random_floor_tile_excluding_room(map, start_room, &ex, &ey, 80)) + break; // Don't spawn on player position if (ex == p->position.x && ey == p->position.y) { @@ -161,7 +159,7 @@ void enemy_spawn(Enemy enemies[], int *count, Map *map, Player *p, int floor) { break; } - enemies[i] = e; + enemies[*count] = e; (*count)++; } } diff --git a/src/main.c b/src/main.c index 1b8383c..66abfd8 100644 --- a/src/main.c +++ b/src/main.c @@ -118,7 +118,9 @@ static void init_floor(GameState *gs, int floor_num) { // Find spawn position int start_x, start_y; - get_random_floor_tile(&gs->map, &start_x, &start_y, 100); + if (gs->map.room_count <= 0 || !get_room_floor_tile(&gs->map, &gs->map.rooms[0], &start_x, &start_y)) { + get_random_floor_tile(&gs->map, &start_x, &start_y, 100); + } // Initialize player position if first floor if (floor_num == 1) { @@ -148,6 +150,151 @@ static void init_floor(GameState *gs, int floor_num) { gs->turn_count = 0; } +#ifdef ROGGED_ADMIN_CONTROLS +static void admin_recompute_lighting(GameState *gs) { + LightSource player_light = {gs->player.position.x, gs->player.position.y, PLAYER_LIGHT_INTENSITY, PLAYER_LIGHT_RANGE}; + LightSource sources[1 + 32]; + sources[0] = player_light; + memcpy(sources + 1, gs->static_lights, gs->static_light_count * sizeof(LightSource)); + compute_lighting(&gs->map, sources, 1 + gs->static_light_count); +} + +static void admin_reveal_map(GameState *gs) { + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + gs->map.remembered[y][x] = 1; + } + } +} + +static void admin_apply_fullbright(GameState *gs) { + memset(gs->map.light_map, 255, sizeof(gs->map.light_map)); + admin_reveal_map(gs); +} + +static void admin_kill_enemies(GameState *gs) { + for (int i = 0; i < gs->enemy_count; i++) { + if (gs->enemies[i].alive) { + gs->enemies[i].alive = 0; + gs->enemies[i].hp = 0; + } + } +} + +static void admin_teleport_to_stairs(GameState *gs) { + for (int y = 0; y < MAP_HEIGHT; y++) { + for (int x = 0; x < MAP_WIDTH; x++) { + if (gs->map.tiles[y][x] == TILE_STAIRS) { + gs->player.position.x = x; + gs->player.position.y = y; + gs->awaiting_descend = 1; + gs->last_message = "Admin: teleported to stairs"; + gs->message_timer = 90; + admin_recompute_lighting(gs); + return; + } + } + } +} + +static int admin_button(Rectangle rect, const char *label) { + Vector2 mouse = GetMousePosition(); + int hovered = CheckCollisionPointRec(mouse, rect); + DrawRectangleRec(rect, hovered ? (Color){72, 62, 48, 235} : (Color){44, 42, 46, 235}); + DrawRectangleLines((int)rect.x, (int)rect.y, (int)rect.width, (int)rect.height, + hovered ? (Color){210, 180, 110, 255} : (Color){112, 104, 92, 255}); + DrawText(label, (int)rect.x + 8, (int)rect.y + 5, SMALL_FONT, (Color){235, 228, 210, 255}); + return hovered && IsMouseButtonPressed(MOUSE_LEFT_BUTTON); +} + +static void admin_controls(GameState *gs, int *visible, int *fullbright) { + if (IsKeyPressed(KEY_F1)) + *visible = !*visible; + + DrawRectangle(8, 8, 142, 22, (Color){10, 10, 12, 190}); + DrawText("F1 Admin", 16, 13, SMALL_FONT, (Color){220, 190, 110, 255}); + + if (!*visible) + return; + + const int x = 20; + const int start_y = 84; + const int w = 166; + const int h = 24; + const int gap = 8; + const int button_count = 10; + const int panel_padding_bottom = 16; + Rectangle panel = {8, 34, 190, + (float)(start_y - 34 + button_count * h + (button_count - 1) * gap + panel_padding_bottom)}; + DrawRectangleRec(panel, (Color){12, 12, 15, 225}); + DrawRectangleLines((int)panel.x, (int)panel.y, (int)panel.width, (int)panel.height, (Color){170, 145, 90, 255}); + DrawText("Admin Controls", 20, 46, NORM_FONT, (Color){230, 205, 130, 255}); + DrawText("development build only", 20, 62, TINY_FONT, (Color){170, 164, 150, 255}); + + int y = start_y; + + if (admin_button((Rectangle){x, y, w, h}, "Heal full")) { + gs->player.hp = gs->player.max_hp; + gs->game_over = 0; + gs->last_message = "Admin: healed"; + gs->message_timer = 60; + } + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "+10 max HP")) { + gs->player.max_hp += 10; + gs->player.hp = gs->player.max_hp; + } + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "+2 attack")) + gs->player.attack += 2; + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "+2 defense")) + gs->player.defense += 2; + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "Reveal map")) + admin_reveal_map(gs); + y += h + gap; + + char fullbright_label[32]; + snprintf(fullbright_label, sizeof(fullbright_label), "Fullbright: %s", *fullbright ? "on" : "off"); + if (admin_button((Rectangle){x, y, w, h}, fullbright_label)) { + *fullbright = !*fullbright; + if (*fullbright) { + admin_apply_fullbright(gs); + } else { + admin_recompute_lighting(gs); + } + } + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "Kill enemies")) + admin_kill_enemies(gs); + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "Teleport stairs")) + admin_teleport_to_stairs(gs); + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "Reroll floor")) { + init_floor(gs, gs->player.floor); + gs->last_message = "Admin: rerolled floor"; + gs->message_timer = 60; + } + y += h + gap; + + if (admin_button((Rectangle){x, y, w, h}, "Next floor")) { + int next_floor = gs->player.floor < NUM_FLOORS ? gs->player.floor + 1 : gs->player.floor; + init_floor(gs, next_floor); + gs->last_message = "Admin: advanced floor"; + gs->message_timer = 60; + } +} +#endif + // Tick all status effects at the start of a turn static void tick_all_effects(GameState *gs) { // Player effects @@ -617,6 +764,10 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { SetExitKey(0); int frame_counter = 0; +#ifdef ROGGED_ADMIN_CONTROLS + int admin_visible = 1; + int admin_fullbright = 0; +#endif while (!WindowShouldClose()) { frame_counter++; @@ -692,6 +843,11 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { if (gs.player.flash_timer > 0) gs.player.flash_timer--; +#ifdef ROGGED_ADMIN_CONTROLS + if (admin_fullbright) + admin_apply_fullbright(&gs); +#endif + // Render BeginDrawing(); ClearBackground(BLACK); @@ -744,6 +900,10 @@ static void game_loop(unsigned int run_seed, FontManager *fm) { gs.final_score, gs.run_seed, fm); } +#ifdef ROGGED_ADMIN_CONTROLS + admin_controls(&gs, &admin_visible, &admin_fullbright); +#endif + EndDrawing(); // small delay for key repeat control diff --git a/src/render.c b/src/render.c index c88584e..07b4d9c 100644 --- a/src/render.c +++ b/src/render.c @@ -103,6 +103,57 @@ static Color color_lerp(Color a, Color b, float 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++) { @@ -115,8 +166,7 @@ void render_map(const Map *map, const Tileset *tileset) { continue; } - float base_light = brightness > 0 ? AMBIENT_LIGHT_FACTOR : REMEMBERED_LIGHT_FACTOR; - float light_factor = base_light + (1.0f - base_light) * powf((float)brightness / 255.0f, LIGHT_EXPONENT); + float light_factor = light_factor_from_brightness(brightness); if (is_deep_corridor(map, x, y)) light_factor *= 0.82f; @@ -140,10 +190,18 @@ void render_map(const Map *map, const Tileset *tileset) { 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 @@ -151,29 +209,31 @@ void render_map(const Map *map, const Tileset *tileset) { 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) { - int fv = (int)(255.0f * light_factor); - if (fv > 255) - fv = 255; - Color ftint = (Color){(unsigned char)fv, (unsigned char)fv, (unsigned char)fv, 255}; - DrawTexturePro(tileset->atlas, floor_src, dst, (Vector2){0, 0}, 0.0f, ftint); + draw_lit_texture_tile(map, tileset, floor_src, dst, x, y, 0, 1.0f); tile_drawn = 1; } } - if (tile_id >= 0 && tileset != NULL && tileset->finalized && !is_door) { + 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); - float wall_dim = is_opaque ? (AMBIENT_LIGHT_FACTOR + 0.92f * light_factor) : light_factor; - int tv = (int)(255.0f * wall_dim); - if (tv > 255) - tv = 255; - Color tint = (Color){(unsigned char)tv, (unsigned char)tv, (unsigned char)tv, 255}; - DrawTexturePro(tileset->atlas, src, dst, (Vector2){0, 0}, 0.0f, tint); + 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); @@ -187,23 +247,6 @@ void render_map(const Map *map, const Tileset *tileset) { break; case TILE_FLOOR: DrawRectangleRec(dst, floor_color); - // Torch flicker: warm tint on floor tiles adjacent to stairs - { - int is_adjacent_to_stairs = 0; - for (int dy = -1; dy <= 1 && !is_adjacent_to_stairs; dy++) { - for (int dx = -1; dx <= 1 && !is_adjacent_to_stairs; dx++) { - int nx = x + dx; - int ny = y + dy; - if (in_bounds(nx, ny, MAP_WIDTH, MAP_HEIGHT) && map->tiles[ny][nx] == TILE_STAIRS) { - is_adjacent_to_stairs = 1; - } - } - } - if (is_adjacent_to_stairs && light_factor > 0.05f) { - int flicker = (int)(sinf(GetTime() * 5.0f) * 15.0f); - DrawRectangleRec(dst, (Color){40 + flicker, 25, 10, 60}); - } - } // 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}); @@ -304,6 +347,72 @@ void render_map(const Map *map, const Tileset *tileset) { (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; } } @@ -320,12 +429,9 @@ 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}; - // Soft radial glow under the player int cx = p->position.x * TILE_SIZE + TILE_SIZE / 2; int cy = p->position.y * TILE_SIZE + TILE_SIZE / 2; - DrawCircle(cx, cy, 14.0f, (Color){255, 220, 100, 20}); - DrawCircle(cx, cy, 10.0f, (Color){255, 230, 150, 35}); - DrawCircle(cx, cy, 6.0f, (Color){255, 240, 180, 55}); + 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; From 4bcacb59ac7f808aef085772ad55bc123aa59d8b Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 11 Jun 2026 10:28:28 +0300 Subject: [PATCH 09/11] nix: bump nixpkgs Signed-off-by: NotAShelf Change-Id: Ib47bdebc8354ef9b894512fdc93a683a6a6a6964 --- flake.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flake.lock b/flake.lock index 4b75299..14e3cb3 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1775036866, - "narHash": "sha256-ZojAnPuCdy657PbTq5V0Y+AHKhZAIwSIT2cb8UgAz/U=", + "lastModified": 1780749050, + "narHash": "sha256-3av0pIjlOWQ6rDbNOmpUSvbNnJkGORQKKjb4LtCZsIY=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "6201e203d09599479a3b3450ed24fa81537ebc4e", + "rev": "a799d3e3886da994fa307f817a6bc705ae538eeb", "type": "github" }, "original": { From bb10fb88f0ba60d76c541f771172e34140f5e1a3 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 11 Jun 2026 10:35:06 +0300 Subject: [PATCH 10/11] various: upgrade Zig version Signed-off-by: NotAShelf Change-Id: Ia97044bd7d44c776f217f2223e35ae3b6a6a6964 --- build.zig | 48 ++++++++++++++++++++--------------------- build.zig.zon | 2 +- docs/README.md | 4 ++-- libs/combat/effects.zig | 12 +++++------ nix/package.nix | 1 + src/render.c | 13 +++++++++-- 6 files changed, 45 insertions(+), 35 deletions(-) diff --git a/build.zig b/build.zig index f819593..2a334b6 100644 --- a/build.zig +++ b/build.zig @@ -29,11 +29,11 @@ pub fn build(b: *std.Build) void { .link_libc = true, }), }); - rng_lib.addCSourceFiles(.{ + rng_lib.root_module.addCSourceFiles(.{ .files = &[_][]const u8{"libs/rng/rng.c"}, .flags = c_flags, }); - rng_lib.addIncludePath(b.path("libs/rng")); + rng_lib.root_module.addIncludePath(b.path("libs/rng")); // Map library const map_lib = b.addLibrary(.{ @@ -44,7 +44,7 @@ pub fn build(b: *std.Build) void { .link_libc = true, }), }); - map_lib.addCSourceFiles(.{ + map_lib.root_module.addCSourceFiles(.{ .files = &[_][]const u8{ "libs/map/map.c", "libs/map/utils.c", @@ -52,11 +52,11 @@ pub fn build(b: *std.Build) void { .flags = c_flags, }); // map.h includes common.h and settings.h which live in src/ - map_lib.addIncludePath(b.path("src")); + map_lib.root_module.addIncludePath(b.path("src")); // map.c includes rng/rng.h via libs/ root - map_lib.addIncludePath(b.path("libs")); + map_lib.root_module.addIncludePath(b.path("libs")); // utils.h is co-located with map.c - map_lib.addIncludePath(b.path("libs/map")); + map_lib.root_module.addIncludePath(b.path("libs/map")); // Tileset library const tileset_obj = b.addObject(.{ @@ -67,7 +67,7 @@ pub fn build(b: *std.Build) void { .link_libc = true, }), }); - tileset_obj.addCSourceFiles(.{ + tileset_obj.root_module.addCSourceFiles(.{ .files = &[_][]const u8{ "libs/tileset/tileset.c", "libs/tileset/tileset_paint.c", @@ -75,10 +75,10 @@ pub fn build(b: *std.Build) void { .flags = c_flags, }); // tileset.h includes settings.h which lives in src/ - tileset_obj.addIncludePath(b.path("src")); + tileset_obj.root_module.addIncludePath(b.path("src")); // tileset.c includes tileset.h which is co-located - tileset_obj.addIncludePath(b.path("libs/tileset")); - tileset_obj.linkSystemLibrary("raylib"); + tileset_obj.root_module.addIncludePath(b.path("libs/tileset")); + tileset_obj.root_module.linkSystemLibrary("raylib", .{}); // Zig combat library. This must be compiled as an object and linked // directly to bypassing the archive step, or it yields a corrupt @@ -93,8 +93,8 @@ pub fn build(b: *std.Build) void { }), }); // common.h and settings.h live in src/; rng.h exposed bare from libs/rng - combat_obj.addIncludePath(b.path("src")); - combat_obj.addIncludePath(b.path("libs/rng")); + combat_obj.root_module.addIncludePath(b.path("src")); + combat_obj.root_module.addIncludePath(b.path("libs/rng")); // C sources remaining in src/ const c_sources = [_][]const u8{ @@ -118,24 +118,24 @@ pub fn build(b: *std.Build) void { }), }); - exe.addCSourceFiles(.{ + exe.root_module.addCSourceFiles(.{ .files = &c_sources, .flags = c_flags, }); // src/ for own headers; libs/ so "rng/rng.h" and "map/map.h" resolve - exe.addIncludePath(b.path("src")); - exe.addIncludePath(b.path("libs")); + exe.root_module.addIncludePath(b.path("src")); + exe.root_module.addIncludePath(b.path("libs")); - exe.linkLibrary(rng_lib); - exe.linkLibrary(map_lib); - exe.addObject(tileset_obj); - exe.addObject(combat_obj); - exe.linkSystemLibrary("raylib"); - exe.linkSystemLibrary("m"); - exe.linkSystemLibrary("pthread"); - exe.linkSystemLibrary("dl"); - exe.linkSystemLibrary("rt"); + exe.root_module.linkLibrary(rng_lib); + exe.root_module.linkLibrary(map_lib); + exe.root_module.addObject(tileset_obj); + exe.root_module.addObject(combat_obj); + exe.root_module.linkSystemLibrary("raylib", .{}); + exe.root_module.linkSystemLibrary("m", .{}); + exe.root_module.linkSystemLibrary("pthread", .{}); + exe.root_module.linkSystemLibrary("dl", .{}); + exe.root_module.linkSystemLibrary("rt", .{}); b.installArtifact(exe); diff --git a/build.zig.zon b/build.zig.zon index 61fe03c..96295f2 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -28,7 +28,7 @@ // Tracks the earliest Zig version that the package considers to be a // supported use case. - .minimum_zig_version = "0.15.2", + .minimum_zig_version = "0.16.0", // This field is optional. // Each dependency must either provide a `url` and `hash`, or a `path`. diff --git a/docs/README.md b/docs/README.md index d9d9ba9..722da5e 100644 --- a/docs/README.md +++ b/docs/README.md @@ -28,7 +28,7 @@ to be viable. For a semi-complete list of things that need to be done, see the Rogged is built on a relatively simple stack. It uses C99 for the main game logic, and Zig for the combat library. Besides `raylib` and `pkg-config`, you -only need the core Zig tooling. For now the required Zig version is 0.15.2, but +only need the core Zig tooling. For now the required Zig version is 0.16.0, but this might change in the future. Additionally, you will need `clang-format` and `just` for common development tasks in the case you plan to contribute. For building, Zig is enough. @@ -53,7 +53,7 @@ $ just dev ### Manual Build If you are allergic to good tooling and would rather use your system Zig, you -may simply invoke `zig build` after acquiring Zig 0.15.2. +may simply invoke `zig build` after acquiring Zig 0.16.0. ```sh # Full build diff --git a/libs/combat/effects.zig b/libs/combat/effects.zig index a665422..3e105b0 100644 --- a/libs/combat/effects.zig +++ b/libs/combat/effects.zig @@ -101,18 +101,18 @@ fn compact(effects: [*c]c.StatusEffect, count: [*c]c_int) void { count[0] = @intCast(write); } -fn tickOne(eff: *c.StatusEffect, hp: *c_int) c_int { - if (eff.duration <= 0) return 0; +fn tickOne(eff: [*c]c.StatusEffect, hp: [*c]c_int) c_int { + if (eff[0].duration <= 0) return 0; var dmg: c_int = 0; - switch (eff.type) { + switch (eff[0].type) { c.EFFECT_POISON, c.EFFECT_BLEED, c.EFFECT_BURN => { - dmg = eff.intensity; - hp.* -= dmg; + dmg = eff[0].intensity; + hp[0] -= dmg; }, else => {}, } - eff.duration -= 1; + eff[0].duration -= 1; return dmg; } diff --git a/nix/package.nix b/nix/package.nix index 0177a10..03498a3 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -20,6 +20,7 @@ stdenv.mkDerivation { (s + /libs) (s + /src) (s + /build.zig) + (s + /build.zig.zon) ]; }; diff --git a/src/render.c b/src/render.c index 07b4d9c..5c627a1 100644 --- a/src/render.c +++ b/src/render.c @@ -240,6 +240,11 @@ void render_map(const Map *map, const Tileset *tileset) { 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: @@ -299,11 +304,13 @@ void render_map(const Map *map, const Tileset *tileset) { 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 && light_factor > 0.05f) { + 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; @@ -324,11 +331,13 @@ void render_map(const Map *map, const Tileset *tileset) { 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 && light_factor > 0.05f) { + 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; From efd8ae04a878d3a2090b6ed0502c3c0eb12a29a3 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Thu, 11 Jun 2026 10:35:45 +0300 Subject: [PATCH 11/11] meta: ignore zig-out dir and object files Signed-off-by: NotAShelf Change-Id: I239d69f962600dffe1bb3cf48511b53a6a6a6964 --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index a53104b..c9a23f9 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ roguelike # Zig .zig-cache -.zig-out +zig-out +*.o