diff options
Diffstat (limited to 'src/sdl/draw.c')
-rw-r--r-- | src/sdl/draw.c | 126 |
1 files changed, 32 insertions, 94 deletions
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,50 +190,17 @@ 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 |