summaryrefslogtreecommitdiff
path: root/src/font.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/font.c')
-rw-r--r--src/font.c32
1 files changed, 31 insertions, 1 deletions
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
});