Compare commits

..

2 commits

Author SHA1 Message Date
6ea1e084db
render: revert redundant ternary expression
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ica6dc2015e573cc71b1882e69fef351e6a6a6964
2026-04-08 12:04:52 +03:00
1f53d1d40f
render: implement dynamic box sizing & line count calculation in render_message
Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: Ibb30f7ff6fbff55f253397619e2208c76a6a6964
2026-04-08 12:04:51 +03:00

View file

@ -359,7 +359,7 @@ void render_end_screen(int is_victory, int kills, int items, int damage_dealt, i
// Title // Title
const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER"; const char *title = is_victory ? "YOU ESCAPED!" : "GAME OVER";
int title_font_size = is_victory ? 60 : 60; int title_font_size = 60;
Color title_color = is_victory ? GOLD : RED; Color title_color = is_victory ? GOLD : RED;
int title_width = MeasureText(title, title_font_size); int title_width = MeasureText(title, title_font_size);
DrawText(title, (SCREEN_WIDTH - title_width) / 2, 30, title_font_size, title_color); DrawText(title, (SCREEN_WIDTH - title_width) / 2, 30, title_font_size, title_color);
@ -450,18 +450,70 @@ void render_message(const char *message) {
if (message == NULL) if (message == NULL)
return; return;
int msg_len = strlen(message); const int font_size = 20;
float msg_ratio = 13.5; const int line_height = font_size + 4;
const int padding_x = 20;
const int padding_y = 15;
const int max_box_width = (int)(SCREEN_WIDTH * 0.75f);
const int max_line_width = max_box_width - (padding_x * 2);
// Draw message box // Calculate line breaks by iterating through message
// TODO: Separate out the calculation of the x/y and width/height so that if a message takes up more than, say, int line_count = 1;
// 75% of the screen width, we add a line break and increase the height. That would then require calculating the int current_line_width = 0;
// width based on the longest line. int longest_line_width = 0;
Rectangle msg_bg = {(float)(SCREEN_WIDTH / 2.0f - ((msg_ratio / 2.03f) * msg_len)),
(float)(SCREEN_HEIGHT / 2.0f - 30.0f), msg_ratio * msg_len, 60}; const char *msg_ptr = message;
while (*msg_ptr && line_count <= 10) {
// Estimate character width (average ~10px for 20pt font)
int char_width = 10;
current_line_width += char_width;
if (current_line_width > max_line_width && *msg_ptr == ' ') {
if (current_line_width > longest_line_width)
longest_line_width = current_line_width;
line_count++;
current_line_width = 0;
}
msg_ptr++;
}
if (current_line_width > longest_line_width)
longest_line_width = current_line_width;
// Measure full message
int total_msg_width = MeasureText(message, font_size);
int box_width = total_msg_width + (padding_x * 2);
// If message is too long, use wrapped width
if (box_width > max_box_width) {
box_width = max_box_width;
}
// Ensure minimum width
if (box_width < 200)
box_width = 200;
// Calculate box height based on line count
int box_height = (line_count * line_height) + (padding_y * 2);
// Center the box
float box_x = (SCREEN_WIDTH - box_width) / 2.0f;
float box_y = (SCREEN_HEIGHT - box_height) / 2.0f;
// Draw message box background
Rectangle msg_bg = {box_x, box_y, (float)box_width, (float)box_height};
DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235}); DrawRectangleRec(msg_bg, (Color){45, 45, 45, 235});
DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255}); DrawRectangleLines((int)msg_bg.x, (int)msg_bg.y, (int)msg_bg.width, (int)msg_bg.height, (Color){180, 180, 180, 255});
int msg_width = MeasureText(message, 20); // Draw text centered
DrawText(message, (SCREEN_WIDTH - msg_width) / 2, SCREEN_HEIGHT / 2 - 10, 20, WHITE); int text_x = (SCREEN_WIDTH - total_msg_width) / 2;
int text_y = (SCREEN_HEIGHT - font_size) / 2;
// For wrapped text, draw at box center with padding
if (line_count > 1) {
text_x = (int)box_x + padding_x;
text_y = (int)box_y + padding_y;
}
DrawText(message, text_x, text_y, font_size, WHITE);
} }