aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/draw-text.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-14 10:13:39 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-14 10:13:39 -0500
commit969c64da6a63e6c0efa1b79a317ef953cb3aaaa9 (patch)
tree3cb0d32988e6231886a0f1e9c88b97805ab92d4d /src/sdl/draw-text.c
parentbb9cf9472cad42ad80a53ab4523cabba71978583 (diff)
downloadsok-969c64da6a63e6c0efa1b79a317ef953cb3aaaa9.tar.bz2
sok-969c64da6a63e6c0efa1b79a317ef953cb3aaaa9.zip
add text_style_t, draw-text.c
Diffstat (limited to 'src/sdl/draw-text.c')
-rw-r--r--src/sdl/draw-text.c130
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);
+}