From 969c64da6a63e6c0efa1b79a317ef953cb3aaaa9 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 14 Jan 2019 10:13:39 -0500 Subject: add text_style_t, draw-text.c --- meson.build | 1 + src/sdl/draw-text.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sdl/draw-text.h | 35 ++++++++++++++ src/sdl/draw.c | 126 +++++++++++++------------------------------------- 4 files changed, 198 insertions(+), 94 deletions(-) create mode 100644 src/sdl/draw-text.c create mode 100644 src/sdl/draw-text.h diff --git a/meson.build b/meson.build index 5c826a7..226054c 100644 --- a/meson.build +++ b/meson.build @@ -27,6 +27,7 @@ executable('sok-sdl', sources + [ 'src/sdl/color.c', 'src/sdl/action.c', 'src/sdl/sprites.c', + 'src/sdl/draw-text.c', 'src/sdl/draw.c', 'src/sdl/main.c', ], dependencies: [ 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); +} diff --git a/src/sdl/draw-text.h b/src/sdl/draw-text.h new file mode 100644 index 0000000..b58c1c1 --- /dev/null +++ b/src/sdl/draw-text.h @@ -0,0 +1,35 @@ +#ifndef DRAW_TEXT_H +#define DRAW_TEXT_H + +#include "../libsok/sok.h" // sok_pos_t +#include // SDL_Color +#include // TTF_Font + +typedef enum { + TEXT_ALIGN_TOP_LEFT, + TEXT_ALIGN_TOP_CENTER, + TEXT_ALIGN_TOP_RIGHT, + TEXT_ALIGN_CENTER_LEFT, + TEXT_ALIGN_CENTER_CENTER, + TEXT_ALIGN_CENTER_RIGHT, + TEXT_ALIGN_BOTTOM_LEFT, + TEXT_ALIGN_BOTTOM_RIGHT, + TEXT_ALIGN_BOTTOM_CENTER, + TEXT_ALIGN_LAST, +} text_align_t; + +typedef struct { + const text_align_t align; + const sok_pos_t pad; + const SDL_Color colors[2]; +} text_style_t; + +void +draw_text( + SDL_Renderer * const, + TTF_Font * const, + const text_style_t * const, + const char * const +); + +#endif /* DRAW_TEXT_H */ diff --git a/src/sdl/draw.c b/src/sdl/draw.c index 2dab2ee..418ddd2 100644 --- a/src/sdl/draw.c +++ b/src/sdl/draw.c @@ -4,6 +4,7 @@ #include "color.h" // set_color() #include "draw.h" #include "sprites.h" +#include "draw-text.h" #define M_2_PI (2.0 * 3.1415926) @@ -189,49 +190,16 @@ DRAW_CBS = { .on_box = draw_sprites_on_box, }; -static const SDL_Color -TEXT_COLORS[] = { - { 0xff, 0xff, 0xff, 0xff }, // fg - { 0x00, 0x00, 0x00, 0xff }, // bg +static const text_style_t +TITLE_STYLE = { + .align = TEXT_ALIGN_TOP_CENTER, + .pad = { 0, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, }; -static void -draw_text( - draw_ctx_t * const draw_ctx, - const char * const buf, - const SDL_Rect * const rect -) { - // create surface - SDL_Surface *surface = TTF_RenderText_Shaded( - draw_ctx->font, - buf, - TEXT_COLORS[0], - TEXT_COLORS[1] - ); - - // check for error - if (!surface) { - die("TTF_RenderText_Blended(): %s", TTF_GetError()); - } - - // create texture - SDL_Texture *texture = SDL_CreateTextureFromSurface(draw_ctx->renderer, surface); - if (!texture) { - die("SDL_CreateTextureFromSurface(): %s", SDL_GetError()); - } - - // free surface - SDL_FreeSurface(surface); - - // render text - if (SDL_RenderCopy(draw_ctx->renderer, texture, NULL, rect)) { - die("SDL_RenderCopy(): %s", SDL_GetError()); - } - - // free texture - SDL_DestroyTexture(texture); -} - static void draw_title_text( draw_ctx_t * const draw_ctx @@ -246,25 +214,20 @@ draw_title_text( *draw_ctx->level_num ); - // get renderer width - int renderer_w; - if (SDL_GetRendererOutputSize(draw_ctx->renderer, &renderer_w, NULL)) { - die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); - } - - // get text size - int text_w, text_h; - if (TTF_SizeText(draw_ctx->font, buf, &text_w, &text_h)) { - die("TTF_SizeText(): %s", TTF_GetError()); - } - - // build rect - const SDL_Rect rect = { (renderer_w - text_w) / 2, 10, text_w, text_h }; - // draw text - draw_text(draw_ctx, buf, &rect); + draw_text(draw_ctx->renderer, draw_ctx->font, &TITLE_STYLE, buf); } +static const text_style_t +MOVES_STYLE = { + .align = TEXT_ALIGN_BOTTOM_LEFT, + .pad = { 10, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, +}; + static void draw_moves_text( draw_ctx_t * const draw_ctx @@ -278,25 +241,20 @@ draw_moves_text( sok_ctx_is_done(draw_ctx->ctx) ? " (Won!)" : "" ); - // get renderer height - int renderer_h; - if (SDL_GetRendererOutputSize(draw_ctx->renderer, NULL, &renderer_h)) { - die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); - } - - // get text size - int text_w, text_h; - if (TTF_SizeText(draw_ctx->font, buf, &text_w, &text_h)) { - die("TTF_SizeText(): %s", TTF_GetError()); - } - - // build rect - const SDL_Rect rect = { 10, renderer_h - text_h - 10, text_w, text_h }; - // draw text - draw_text(draw_ctx, buf, &rect); + draw_text(draw_ctx->renderer, draw_ctx->font, &MOVES_STYLE, buf); } +static const text_style_t +HELP_STYLE = { + .align = TEXT_ALIGN_BOTTOM_RIGHT, + .pad = { 10, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, +}; + #define DELIM " " static void @@ -311,28 +269,8 @@ draw_help_text( sok_ctx_is_done(draw_ctx->ctx) ? "Space: Next Level" DELIM : "" ); - // get renderer height - int renderer_w, renderer_h; - if (SDL_GetRendererOutputSize(draw_ctx->renderer, &renderer_w, &renderer_h)) { - die("SDL_GetRendererOutputSize(): %s", SDL_GetError()); - } - - // get text size - int text_w, text_h; - if (TTF_SizeText(draw_ctx->font, buf, &text_w, &text_h)) { - die("TTF_SizeText(): %s", TTF_GetError()); - } - - // build rect - const SDL_Rect rect = { - renderer_w - text_w - 10, - renderer_h - text_h - 10, - text_w, - text_h, - }; - // draw text - draw_text(draw_ctx, buf, &rect); + draw_text(draw_ctx->renderer, draw_ctx->font, &HELP_STYLE, buf); } static void -- cgit v1.2.3