From d6cc129bed4a58031ff8f560b34ac6b6ddcf948b Mon Sep 17 00:00:00 2001 From: IllusionMan1212 Date: Sat, 18 Nov 2023 03:29:00 +0200 Subject: [PATCH] calculate help overlay width from text instead of hardcoding it --- cudoku.c | 7 ++++--- helper.c | 24 ++++++++++++++++-------- helper.h | 1 + 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cudoku.c b/cudoku.c index f33b046..0a958bb 100644 --- a/cudoku.c +++ b/cudoku.c @@ -13,7 +13,6 @@ #include "audio.h" #define HELP_TEXT_SIZE 13 -#define HELP_OVERLAY_WIDTH 700 static const float quad_vertices[] = { // positions // texture coords @@ -623,12 +622,14 @@ void draw_help_overlay(Shader overlay_shader, Shader font_shader, unsigned int o int const text_padding = 10; float const text_scale = 0.3f; int overlay_height = 0; + int overlay_width = 0; for (int i = 0; i < HELP_TEXT_SIZE; i++) { Size text_size = calculate_text_size(help_texts[i], text_scale); overlay_height += text_size.height + text_padding; + overlay_width = max(overlay_width, text_size.width + text_padding * 2); } -Size size = {.width = HELP_OVERLAY_WIDTH, .height = overlay_height + 20}; + Size size = {.width = overlay_width, .height = overlay_height + text_padding * 2}; Vec2 pos = { .x = 0, .y = window_height - size.height @@ -656,7 +657,7 @@ Size size = {.width = HELP_OVERLAY_WIDTH, .height = overlay_height + 20}; snprintf(closing_in_text, sizeof(closing_in_text), "Hiding in %ds", (int)timer_remaining(timer) + 1); Size closing_in_text_size = calculate_text_size(closing_in_text, text_scale / 2.0); Vec2 closing_in_text_pos = { - .x = HELP_OVERLAY_WIDTH - closing_in_text_size.width, + .x = overlay_width - closing_in_text_size.width, .y = window_height - closing_in_text_size.height - text_padding, }; Color closing_in_text_color = { diff --git a/helper.c b/helper.c index bfea911..7e3e4d2 100644 --- a/helper.c +++ b/helper.c @@ -1,14 +1,18 @@ #include "helper.h" -Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, float top) { - Matrix4x4 result; +Matrix4x4 identity() { + Matrix4x4 result = {0}; - // Identity matrix - for (int i = 0; i < 4; i++) { - for (int j = 0; j < 4; j++) { - result.m[i][j] = 0.0f; - } - } + result.m[0][0] = 1.0f; + result.m[1][1] = 1.0f; + result.m[2][2] = 1.0f; + result.m[3][3] = 1.0f; + + return result; +} + +Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, float top) { + Matrix4x4 result = identity(); result.m[0][0] = 2.0f / (right - left); result.m[3][0] = -(right + left) / (right - left); @@ -19,3 +23,7 @@ Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, floa return result; } + +int max(int a, int b) { + return a > b ? a : b; +} diff --git a/helper.h b/helper.h index bebcdee..c2c0f9d 100644 --- a/helper.h +++ b/helper.h @@ -5,3 +5,4 @@ typedef struct { } Matrix4x4; Matrix4x4 orthographic_projection_2d(float left, float right, float bottom, float top); +int max(int a, int b);