diff options
author | Paul Duncan <pabs@pablotron.org> | 2019-01-14 12:44:32 -0500 |
---|---|---|
committer | Paul Duncan <pabs@pablotron.org> | 2019-01-14 12:44:32 -0500 |
commit | 280b84ed9122d6b96555498d8672b5de98286c46 (patch) | |
tree | 684700fcf0fe93adb74a5dd6040c459d46cc40ad /src/sdl | |
parent | 969c64da6a63e6c0efa1b79a317ef953cb3aaaa9 (diff) | |
download | sok-280b84ed9122d6b96555498d8672b5de98286c46.tar.bz2 sok-280b84ed9122d6b96555498d8672b5de98286c46.zip |
add bg-style.[hc]
Diffstat (limited to 'src/sdl')
-rw-r--r-- | src/sdl/bg-style.c | 21 | ||||
-rw-r--r-- | src/sdl/bg-style.h | 21 | ||||
-rw-r--r-- | src/sdl/draw.c | 79 |
3 files changed, 94 insertions, 27 deletions
diff --git a/src/sdl/bg-style.c b/src/sdl/bg-style.c new file mode 100644 index 0000000..02bc759 --- /dev/null +++ b/src/sdl/bg-style.c @@ -0,0 +1,21 @@ +#include <math.h> // sin() +#include "bg-style.h" + +#define M_2_PI (2.0 * 3.1415926) + +#define BG_STYLE_GET_CHANNEL(c, time) ((c).base + (((c).period) ? ( \ + (c).amplitude * sin(((c).phase + (time)) * M_2_PI / (1.0 * (c).period)) \ +) : 0)) + +SDL_Color +bg_style_get_color( + const bg_style_t * const style, + const Uint32 ticks +) { + return (SDL_Color) { + .r = BG_STYLE_GET_CHANNEL(style->r, ticks), + .g = BG_STYLE_GET_CHANNEL(style->g, ticks), + .b = BG_STYLE_GET_CHANNEL(style->b, ticks), + .a = BG_STYLE_GET_CHANNEL(style->a, ticks), + }; +} diff --git a/src/sdl/bg-style.h b/src/sdl/bg-style.h new file mode 100644 index 0000000..c4ca9a5 --- /dev/null +++ b/src/sdl/bg-style.h @@ -0,0 +1,21 @@ +#ifndef BG_STYLE_H +#define BG_STYLE_H + +#include <SDL.h> // SDL_Color + +typedef struct { + struct { + int base, + amplitude, + phase, + period; + } r, g, b, a; +} bg_style_t; + +SDL_Color +bg_style_get_color( + const bg_style_t * const, + const Uint32 +); + +#endif /* BG_STYLE_H */ diff --git a/src/sdl/draw.c b/src/sdl/draw.c index 418ddd2..482bfb2 100644 --- a/src/sdl/draw.c +++ b/src/sdl/draw.c @@ -5,6 +5,7 @@ #include "draw.h" #include "sprites.h" #include "draw-text.h" +#include "bg-style.h" #define M_2_PI (2.0 * 3.1415926) @@ -255,7 +256,6 @@ HELP_STYLE = { }, }; -#define DELIM " " static void draw_help_text( @@ -263,55 +263,80 @@ draw_help_text( ) { // build text char buf[256]; +#define D " " snprintf( buf, sizeof(buf), - " %sU: Undo" DELIM "R: Reset" DELIM "S: Solve" DELIM "Q: Quit ", - sok_ctx_is_done(draw_ctx->ctx) ? "Space: Next Level" DELIM : "" + " %sU: Undo" D "R: Reset" D "S: Solve" D "Q: Quit ", + sok_ctx_is_done(draw_ctx->ctx) ? "Space: Next Level" D : "" ); +#undef D // draw text draw_text(draw_ctx->renderer, draw_ctx->font, &HELP_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, + .amplitude = 0x33, + .phase = 1000, + .period = 2000, + }, + + .g = { + .base = 0x66, + .amplitude = 0x33, + .phase = 3000, + .period = 5000, + }, + + .b = { + .base = 0x66, + .amplitude = 0x33, + .phase = 5000, + .period = 7000, + }, + + .a = { + .base = 0xFF, + }, +}}; + static void -set_bg_won_color( +draw_bg( draw_ctx_t * const draw_ctx ) { - const Uint32 ticks = SDL_GetTicks(); - - // gen color - const SDL_Color c = { - .r = 0x66 + 0x33 * sinf((1000 + ticks) * M_2_PI / 2000.0), - .g = 0x66 + 0x33 * sinf((3000 + ticks) * M_2_PI / 5000.0), - .b = 0x66 + 0x33 * sinf((5000 + ticks) * M_2_PI / 7000.0), - .a = 0xFF, - }; + // get color + const SDL_Color c = bg_style_get_color( + BG_STYLES + (sok_ctx_is_done(draw_ctx->ctx) ? 1 : 0), + SDL_GetTicks() + ); // set color if (SDL_SetRenderDrawColor(draw_ctx->renderer, c.r, c.g, c.b, c.a)) { die("SDL_SetRenderDrawColor(): %s", SDL_GetError()); } + + // clear background + SDL_RenderClear(draw_ctx->renderer); } void draw( draw_ctx_t * const draw_ctx ) { - const bool is_done = sok_ctx_is_done(draw_ctx->ctx); - - // set bg color - if (sok_ctx_is_done(draw_ctx->ctx)) { - // set bg won color - set_bg_won_color(draw_ctx); - } else { - // set normal bg color - set_color(draw_ctx->renderer, is_done ? COLOR_BG_WON : COLOR_BG); - } - // clear background - SDL_RenderClear(draw_ctx->renderer); + draw_bg(draw_ctx); - // render + // render level sok_ctx_walk(draw_ctx->ctx, &DRAW_CBS, draw_ctx); // render text @@ -319,6 +344,6 @@ draw( draw_moves_text(draw_ctx); draw_help_text(draw_ctx); - // flip + // flip SDL_RenderPresent(draw_ctx->renderer); } |