summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-03 16:50:26 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-03 16:50:26 (EDT)
commit8bab7926c95c0a785e05ee9197401332a70af92b (patch)
treeeb267a3ae9b51e17244f32cb57440629c9c6ee7a
parentf75620d8d873af217fffd3c574463a1931ae54c1 (diff)
downloadmazefight-8bab7926c95c0a785e05ee9197401332a70af92b.zip
mazefight-8bab7926c95c0a785e05ee9197401332a70af92b.tar.gz
mazefight-8bab7926c95c0a785e05ee9197401332a70af92b.tar.bz2
ttf: Center text within line skip height
And free text surface.
-rw-r--r--src/ttf.c7
1 files changed, 5 insertions, 2 deletions
diff --git a/src/ttf.c b/src/ttf.c
index 9cfaab0..2bc24cf 100644
--- a/src/ttf.c
+++ b/src/ttf.c
@@ -28,6 +28,7 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color,
{
SDL_Surface *surface;
SDL_Texture *texture;
+ int line_skip;
int ascent;
int max_y;
const char *c;
@@ -46,10 +47,12 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color,
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't create texture: %s",
SDL_GetError());
+ SDL_FreeSurface(surface);
return NULL;
}
- ascent = TTF_FontAscent(font);
+ line_skip = TTF_FontLineSkip(font);
+ ascent = TTF_FontAscent (font);
max_y = 0;
for (c = text; *c != '\0'; ++c) {
if (TTF_GlyphMetrics(font, *c, NULL, NULL, NULL, &glyph_max_y,
@@ -64,7 +67,7 @@ mf_ttf_render(TTF_Font *font, const char *text, SDL_Color *color,
}
}
rect->x = 0;
- rect->y = ascent - max_y;
+ rect->y = (line_skip - ascent) / 2 + (ascent - max_y);
rect->w = surface->w;
rect->h = surface->h;