From a03a982c026a185d617d25e6a37ce939a481e946 Mon Sep 17 00:00:00 2001 From: Menase Teshale Date: Mon, 7 Sep 2026 03:14:22 +0300 Subject: Started work on menus --- src/font.c | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'src/font.c') diff --git a/src/font.c b/src/font.c index b04c0df..93a2cfe 100644 --- a/src/font.c +++ b/src/font.c @@ -326,6 +326,30 @@ bool load_sprite_font(SDL_Renderer* renderer, const char* filename, sprite_font* return true; } +void measure_text +( + sprite_font* font, + const unsigned char* text, + float scale, + float* width, float* height +) { + *width = 0; + *height = 0; + + while (*text) { + const font_char* fc = &font->characters[*text-font->smallest_character]; + if (!fc->active) { + text++; + continue; + } + + *width += fc->x_advance * scale; + *height = SDL_max(*height, (fc->y_offset+fc->height) * scale); + + text++; + } +} + void draw_text_with_spritefont_extended ( SDL_Renderer* renderer, @@ -334,6 +358,12 @@ void draw_text_with_spritefont_extended float x_position, float y_position, float scale ) { + float text_column_length, text_column_height; + measure_text(font, text, scale, &text_column_length, &text_column_height); + + x_position -= text_column_length / 2; + y_position -= text_column_height / 2; + while (*text) { const font_char* fc = &font->characters[*text-font->smallest_character]; if (!fc->active) { @@ -351,7 +381,7 @@ void draw_text_with_spritefont_extended }, &(SDL_FRect){ .x = x_position + fc->x_offset*scale, - .y = (y_position + fc->y_offset) * scale, + .y = y_position + fc->y_offset * scale, .w = fc->width * scale, .h = fc->height * scale }); -- cgit v1.2.3