aboutsummaryrefslogtreecommitdiff
path: root/src/sdl/draw.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sdl/draw.c')
-rw-r--r--src/sdl/draw.c140
1 files changed, 139 insertions, 1 deletions
diff --git a/src/sdl/draw.c b/src/sdl/draw.c
index 4449b52..aa16bca 100644
--- a/src/sdl/draw.c
+++ b/src/sdl/draw.c
@@ -1,4 +1,5 @@
#include <stdbool.h> // bool
+#include <math.h> // sinf()
#include "util.h" // warn()/die()
#include "color.h" // set_color()
#include "draw.h"
@@ -251,17 +252,154 @@ DRAW_CBS = {
};
#endif /* DRAW_SPRITES */
+static const SDL_Color
+TEXT_COLORS[] = {
+ { 0xff, 0xff, 0xff, 0xff }, // fg
+ { 0x00, 0x00, 0x00, 0xff }, // bg
+};
+
+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_level_text(
+ draw_ctx_t * const draw_ctx
+) {
+ // build text
+ char buf[256];
+ snprintf(
+ buf, sizeof(buf),
+ "%s: %s (#%zu)",
+ draw_ctx->level->pack,
+ draw_ctx->level->name,
+ *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);
+}
+
+static void
+draw_moves_text(
+ draw_ctx_t * const draw_ctx
+) {
+ // build text
+ char buf[256];
+ snprintf(buf, sizeof(buf), "Moves: %zu", draw_ctx->ctx->num_moves);
+
+ // 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);
+}
+
+static void
+set_bg_won_color(
+ draw_ctx_t * const draw_ctx
+) {
+ const Uint32 ticks = SDL_GetTicks();
+
+ // gen color
+ const SDL_Color c = {
+ .r = 0x66 + 0x33 * sinf((1000 + ticks) * 2.0 * 3.14159 / 2000.0),
+ .g = 0x66 + 0x33 * sinf((3000 + ticks) * 2.0 * 3.14159 / 5000.0),
+ .b = 0x66 + 0x33 * sinf((5000 + ticks) * 2.0 * 3.14159 / 7000.0),
+ .a = 0xFF,
+ };
+
+ // set color
+ if (SDL_SetRenderDrawColor(draw_ctx->renderer, c.r, c.g, c.b, c.a)) {
+ die("SDL_SetRenderDrawColor(): %s", SDL_GetError());
+ }
+}
+
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
- set_color(draw_ctx->renderer, sok_ctx_is_done(draw_ctx->ctx) ? COLOR_BG_WON : COLOR_BG);
SDL_RenderClear(draw_ctx->renderer);
// render
sok_ctx_walk(draw_ctx->ctx, &DRAW_CBS, draw_ctx);
+ // render text
+ draw_level_text(draw_ctx);
+ draw_moves_text(draw_ctx);
+
// flip
SDL_RenderPresent(draw_ctx->renderer);
}