diff options
Diffstat (limited to 'src/sdl/draw-text.c')
-rw-r--r-- | src/sdl/draw-text.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/src/sdl/draw-text.c b/src/sdl/draw-text.c new file mode 100644 index 0000000..0e58949 --- /dev/null +++ b/src/sdl/draw-text.c @@ -0,0 +1,130 @@ +#include "draw-text.h" +#include "util.h" + +static SDL_Rect +text_get_rect( + SDL_Renderer * const renderer, + TTF_Font * const font, + const text_style_t * const style, + const char * const buf +) { + // get renderer size + int renderer_w, renderer_h; + if (SDL_GetRendererOutputSize(renderer, &renderer_w, &renderer_h)) { + die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); + } + + // get text size + int text_w, text_h; + if (TTF_SizeText(font, buf, &text_w, &text_h)) { + die("TTF_SizeText(): %s", TTF_GetError()); + } + + // build rect + switch (style->align) { + case TEXT_ALIGN_TOP_LEFT: + return (SDL_Rect) { + .x = style->pad.x, + .y = style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_TOP_CENTER: + return (SDL_Rect) { + .x = (renderer_w - text_w) / 2 + style->pad.x, + .y = style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_TOP_RIGHT: + return (SDL_Rect) { + .x = renderer_w - text_w - style->pad.x, + .y = style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_CENTER_LEFT: + return (SDL_Rect) { + .x = style->pad.x, + .y = (renderer_h - text_h) / 2 + style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_CENTER_CENTER: + return (SDL_Rect) { + .x = (renderer_w - text_w) / 2 + style->pad.x, + .y = (renderer_h - text_h) / 2 + style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_CENTER_RIGHT: + return (SDL_Rect) { + .x = renderer_w - text_w - style->pad.x, + .y = (renderer_h - text_h) / 2 + style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_BOTTOM_LEFT: + return (SDL_Rect) { + .x = style->pad.x, + .y = renderer_h - text_h - style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_BOTTOM_CENTER: + return (SDL_Rect) { + .x = (renderer_w - text_w) / 2 + style->pad.x, + .y = renderer_h - text_h - style->pad.y, + .w = text_w, + .h = text_h, + }; + case TEXT_ALIGN_BOTTOM_RIGHT: + return (SDL_Rect) { + .x = renderer_w - text_w - style->pad.x, + .y = renderer_h - text_h - style->pad.y, + .w = text_w, + .h = text_h, + }; + default: + return (SDL_Rect) { + .x = style->pad.x, + .y = style->pad.y, + .w = text_w, + .h = text_h, + }; + } +} + +void +draw_text( + SDL_Renderer * const renderer, + TTF_Font * const font, + const text_style_t * const style, + const char * const buf +) { + // get rect + const SDL_Rect rect = text_get_rect(renderer, font, style, buf); + + // create surface, check for error + SDL_Surface *surface = TTF_RenderText_Shaded(font, buf, style->colors[0], style->colors[1]); + if (!surface) { + die("TTF_RenderText_Blended(): %s", TTF_GetError()); + } + + // create texture, check for error + SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); + if (!texture) { + die("SDL_CreateTextureFromSurface(): %s", SDL_GetError()); + } + + // free surface + SDL_FreeSurface(surface); + + // render text + if (SDL_RenderCopy(renderer, texture, NULL, &rect)) { + die("SDL_RenderCopy(): %s", SDL_GetError()); + } + + // free texture + SDL_DestroyTexture(texture); +} |