From dec30a82819f93903004a3e9d46bf22f352942b4 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Tue, 15 Jan 2019 00:10:46 -0500 Subject: add theme.[hc] and refactor draw.c --- meson.build | 1 + src/sdl/draw-text.h | 22 +--------------- src/sdl/draw.c | 71 ++++++++++++++-------------------------------------- src/sdl/draw.h | 5 ++++ src/sdl/main.c | 1 + src/sdl/sprites.c | 1 + src/sdl/text-style.h | 26 +++++++++++++++++++ src/sdl/theme.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/sdl/theme.h | 37 +++++++++++++++++++++++++++ 9 files changed, 162 insertions(+), 73 deletions(-) create mode 100644 src/sdl/text-style.h create mode 100644 src/sdl/theme.c create mode 100644 src/sdl/theme.h diff --git a/meson.build b/meson.build index 341054b..b3204dd 100644 --- a/meson.build +++ b/meson.build @@ -32,6 +32,7 @@ executable('sok-sdl', sources + [ 'src/sdl/draw.c', 'src/sdl/assets.c', 'src/sdl/main.c', + 'src/sdl/theme.c', 'src/sdl/log-renderer-info.c', ], dependencies: [ dependency('SDL2'), diff --git a/src/sdl/draw-text.h b/src/sdl/draw-text.h index b58c1c1..f69d46f 100644 --- a/src/sdl/draw-text.h +++ b/src/sdl/draw-text.h @@ -1,29 +1,9 @@ #ifndef DRAW_TEXT_H #define DRAW_TEXT_H -#include "../libsok/sok.h" // sok_pos_t -#include // SDL_Color +#include "text-style.h" #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, diff --git a/src/sdl/draw.c b/src/sdl/draw.c index 99faa19..0d2b17a 100644 --- a/src/sdl/draw.c +++ b/src/sdl/draw.c @@ -171,20 +171,13 @@ DRAW_CBS = { .on_box = draw_on_box, }; -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_title_text( draw_ctx_t * const draw_ctx ) { + // get text style + const text_style_t *style = theme_get_text_style(draw_ctx->theme, TEXT_STYLE_TITLE); + // build text char buf[256]; snprintf( @@ -196,23 +189,16 @@ draw_title_text( ); // draw text - draw_text(draw_ctx->renderer, draw_ctx->font, &TITLE_STYLE, buf); + draw_text(draw_ctx->renderer, draw_ctx->font, 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 ) { + // get text style + const text_style_t *style = theme_get_text_style(draw_ctx->theme, TEXT_STYLE_MOVES); + // build text char buf[256]; snprintf( @@ -223,23 +209,16 @@ draw_moves_text( ); // draw text - draw_text(draw_ctx->renderer, draw_ctx->font, &MOVES_STYLE, buf); + draw_text(draw_ctx->renderer, draw_ctx->font, 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 }, - }, -}; - static void draw_help_text( draw_ctx_t * const draw_ctx ) { + // get text style + const text_style_t *style = theme_get_text_style(draw_ctx->theme, TEXT_STYLE_HELP); + // build text char buf[256]; #define D " " @@ -251,34 +230,22 @@ draw_help_text( #undef D // draw text - draw_text(draw_ctx->renderer, draw_ctx->font, &HELP_STYLE, buf); + draw_text(draw_ctx->renderer, draw_ctx->font, style, buf); } -static const bg_style_t -BG_STYLES[] = {{ - // normal style - .r = { .base = 0x00 }, - .g = { .base = 0x00 }, - .b = { .base = 0x00 }, - .a = { .base = 0xFF }, -}, { - // won style - .r = { .base = 0x66, .scale = 0x33, .phase = 1000, .period = 2000 }, - .g = { .base = 0x66, .scale = 0x33, .phase = 3000, .period = 5000 }, - .b = { .base = 0x66, .scale = 0x33, .phase = 5000, .period = 7000 }, - .a = { .base = 0xFF }, -}}; - static void draw_bg( draw_ctx_t * const draw_ctx ) { - // get color - const SDL_Color c = bg_style_get_color( - BG_STYLES + (sok_ctx_is_done(draw_ctx->ctx) ? 1 : 0), - draw_ctx->ticks + // get bg style + const bg_style_t *style = theme_get_bg_style( + draw_ctx->theme, + sok_ctx_is_done(draw_ctx->ctx) ? BG_STYLE_WON : BG_STYLE_NORMAL ); + // get color + const SDL_Color c = bg_style_get_color(style, draw_ctx->ticks); + // set color if (SDL_SetRenderDrawColor(draw_ctx->renderer, c.r, c.g, c.b, c.a)) { die("SDL_SetRenderDrawColor(): %s", SDL_GetError()); diff --git a/src/sdl/draw.h b/src/sdl/draw.h index 19d8837..1c44320 100644 --- a/src/sdl/draw.h +++ b/src/sdl/draw.h @@ -6,12 +6,15 @@ #include #include "../libsok/sok.h" #include "../text/levels.h" +#include "theme.h" // arbitrary #define MAX_SPRITES 32 typedef struct { SDL_Renderer * const renderer; + + // FIXME: move to theme eventually SDL_Texture *sprites[MAX_SPRITES]; TTF_Font *font; @@ -23,6 +26,8 @@ typedef struct { // render offset int render_ofs_x, render_ofs_y; Uint32 ticks; + + const theme_t * const theme; } draw_ctx_t; void draw(draw_ctx_t * const); diff --git a/src/sdl/main.c b/src/sdl/main.c index 3a20720..1d0b11b 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -152,6 +152,7 @@ int main(int argc, char *argv[]) { .ctx = &ctx, .renderer = renderer, .zoom = &zoom, + .theme = theme_get_default(), }; // set level diff --git a/src/sdl/sprites.c b/src/sdl/sprites.c index 0aa96fc..51824d8 100644 --- a/src/sdl/sprites.c +++ b/src/sdl/sprites.c @@ -1,4 +1,5 @@ #include +#define STB_NO_STDIO #define STB_IMAGE_IMPLEMENTATION #define STB_ASSERT(x) #define STB_ONLY_PNG diff --git a/src/sdl/text-style.h b/src/sdl/text-style.h new file mode 100644 index 0000000..71d9a91 --- /dev/null +++ b/src/sdl/text-style.h @@ -0,0 +1,26 @@ +#ifndef TEXT_STYLE_H +#define TEXT_STYLE_H + +#include "../libsok/sok.h" +#include + +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; + +#endif /* TEXT_STYLE_H */ diff --git a/src/sdl/theme.c b/src/sdl/theme.c new file mode 100644 index 0000000..93b7950 --- /dev/null +++ b/src/sdl/theme.c @@ -0,0 +1,71 @@ +#include "util.h" +#include "theme.h" + +static const theme_t +DEFAULT_THEME = { + .text_styles = {{ + .align = TEXT_ALIGN_TOP_CENTER, + .pad = { 0, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, + }, { + .align = TEXT_ALIGN_BOTTOM_LEFT, + .pad = { 10, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, + }, { + .align = TEXT_ALIGN_BOTTOM_RIGHT, + .pad = { 10, 10 }, + .colors = { + { 0xff, 0xff, 0xff, 0xff }, + { 0x00, 0x00, 0x00, 0xff }, + }, + }}, + + .bg_styles = {{ + // normal style + .r = { .base = 0x00 }, + .g = { .base = 0x00 }, + .b = { .base = 0x00 }, + .a = { .base = 0xFF }, + }, { + // won style + .r = { .base = 0x66, .scale = 0x33, .phase = 1000, .period = 2000 }, + .g = { .base = 0x66, .scale = 0x33, .phase = 3000, .period = 5000 }, + .b = { .base = 0x66, .scale = 0x33, .phase = 5000, .period = 7000 }, + .a = { .base = 0xFF }, + }}, +}; + +const theme_t * +theme_get_default(void) { + return &DEFAULT_THEME; +} + +const text_style_t * +theme_get_text_style( + const theme_t * const theme, + const text_style_id_t id +) { + if (id >= TEXT_STYLE_LAST) { + die("invalid text style ID"); + } + return theme->text_styles + id; +} + +const bg_style_t * +theme_get_bg_style( + const theme_t * const theme, + const bg_style_id_t id +) { + if (id >= BG_STYLE_LAST) { + die("invalid BG style ID"); + } + + return theme->bg_styles + id; +} + diff --git a/src/sdl/theme.h b/src/sdl/theme.h new file mode 100644 index 0000000..562df3f --- /dev/null +++ b/src/sdl/theme.h @@ -0,0 +1,37 @@ +#ifndef THEME_H +#define THEME_H + +#include "text-style.h" +#include "bg-style.h" + +typedef enum { + TEXT_STYLE_TITLE, + TEXT_STYLE_MOVES, + TEXT_STYLE_HELP, + TEXT_STYLE_LAST, +} text_style_id_t; + +typedef enum { + BG_STYLE_NORMAL, + BG_STYLE_WON, + BG_STYLE_LAST, +} bg_style_id_t; + +typedef struct { + text_style_t text_styles[TEXT_STYLE_LAST]; + bg_style_t bg_styles[BG_STYLE_LAST]; +} theme_t; + +const theme_t *theme_get_default(); + +const text_style_t *theme_get_text_style( + const theme_t * const, + const text_style_id_t +); + +const bg_style_t *theme_get_bg_style( + const theme_t * const, + const bg_style_id_t +); + +#endif /* THEME_H */ -- cgit v1.2.3