aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2019-01-19 02:08:20 -0500
committerPaul Duncan <pabs@pablotron.org>2019-01-19 02:08:20 -0500
commitdf5ca740fa8e438e6bc67fe1ed078aa983ffeed5 (patch)
tree89a592a077d733c5d92e323f24611fb1ae1e1d83
parentfb0bdfed8ed7758e7f073ca85a5d1cefe4fea633 (diff)
downloadsok-df5ca740fa8e438e6bc67fe1ed078aa983ffeed5.tar.bz2
sok-df5ca740fa8e438e6bc67fe1ed078aa983ffeed5.zip
add fading, fix sounds
-rw-r--r--src/sdl/draw.c30
-rw-r--r--src/sdl/draw.h3
-rw-r--r--src/sdl/main.c58
3 files changed, 86 insertions, 5 deletions
diff --git a/src/sdl/draw.c b/src/sdl/draw.c
index d95e83f..e980131 100644
--- a/src/sdl/draw.c
+++ b/src/sdl/draw.c
@@ -11,6 +11,7 @@
#define M_2_PI (2.0 * 3.1415926)
#define BUMP_TIME 300
+#define FADE_TIME 200
static size_t
get_cell_size(
@@ -297,7 +298,6 @@ draw_solve_moves_text(
draw_text(draw_ctx->renderer, draw_ctx->font, style, buf);
}
-
static void
draw_bg(
draw_ctx_t * const draw_ctx
@@ -320,6 +320,31 @@ draw_bg(
SDL_RenderClear(draw_ctx->renderer);
}
+static void
+draw_fade(
+ draw_ctx_t * const draw_ctx
+) {
+ // check timestamp
+ if (SDL_TICKS_PASSED(draw_ctx->ticks, draw_ctx->fade_ticks + FADE_TIME)) {
+ return;
+ }
+
+ // calculate alpha mod
+ const Uint32 delta = (draw_ctx->ticks - draw_ctx->fade_ticks);
+ const Uint8 alpha = 255 - 255.0 * delta / FADE_TIME;
+
+ // set alpha mod
+ if (SDL_SetTextureAlphaMod(draw_ctx->fade_tex, alpha)) {
+ die("SDL_TextureSetAlphaMod(): %s", SDL_GetError());
+ }
+ // SDL_Log("delta = %u, alpha = %d", delta, alpha);
+
+ // draw texture
+ if (SDL_RenderCopy(draw_ctx->renderer, draw_ctx->fade_tex, NULL, NULL)) {
+ die("SDL_RenderCopy(): %s", SDL_GetError());
+ }
+}
+
void
draw(
draw_ctx_t * const draw_ctx
@@ -340,6 +365,9 @@ draw(
draw_solve_wait_text(draw_ctx);
draw_solve_moves_text(draw_ctx);
+ // draw fade overlay
+ draw_fade(draw_ctx);
+
// flip
SDL_RenderPresent(draw_ctx->renderer);
}
diff --git a/src/sdl/draw.h b/src/sdl/draw.h
index 919f362..b7e6caa 100644
--- a/src/sdl/draw.h
+++ b/src/sdl/draw.h
@@ -34,6 +34,9 @@ typedef struct {
game_state_t state;
solve_t *solve;
size_t solve_num_steps;
+
+ Uint32 fade_ticks;
+ SDL_Texture *fade_tex;
} draw_ctx_t;
void draw(draw_ctx_t * const);
diff --git a/src/sdl/main.c b/src/sdl/main.c
index 40896e2..0d492a5 100644
--- a/src/sdl/main.c
+++ b/src/sdl/main.c
@@ -17,6 +17,8 @@
#include "sounds.h"
#define WINDOW_TITLE "Pablotron Sokoban"
+#define WINDOW_WIDTH 800
+#define WINDOW_HEIGHT 600
static TTF_Font *
load_font(
@@ -45,11 +47,34 @@ load_font(
}
static void
+draw_to_fade_tex(
+ draw_ctx_t * const draw_ctx
+) {
+ // set render target
+ if (SDL_SetRenderTarget(draw_ctx->renderer, draw_ctx->fade_tex)) {
+ die("SDL_SetRenderTarget(): %s", SDL_GetError());
+ }
+
+ // draw
+ draw(draw_ctx);
+
+ // clear render target
+ if (SDL_SetRenderTarget(draw_ctx->renderer, NULL)) {
+ die("SDL_SetRenderTarget(): %s", SDL_GetError());
+ }
+}
+
+static void
set_level(
draw_ctx_t * const draw_ctx,
sok_ctx_t * const ctx,
const size_t level_num
) {
+ if (level_num) {
+ // draw current level to fade texture
+ draw_to_fade_tex(draw_ctx);
+ }
+
// get level data
draw_ctx->level = levels_get_level(level_num);
@@ -58,6 +83,11 @@ set_level(
die("Couldn't load level %d", (int) level_num);
}
+ if (level_num) {
+ // set fade ticks
+ draw_ctx->fade_ticks = SDL_GetTicks();
+ }
+
// log level title
SDL_Log(
"Loaded level \"%s: %s\" (#%d)",
@@ -167,8 +197,8 @@ int main(int argc, char *argv[]) {
WINDOW_TITLE,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
- 800,
- 600,
+ WINDOW_WIDTH,
+ WINDOW_HEIGHT,
SDL_WINDOW_RESIZABLE
);
@@ -198,8 +228,28 @@ int main(int argc, char *argv[]) {
.zoom = &zoom,
.theme = theme_get_default(),
.bump_ticks = SDL_GetTicks() - 5000,
+ .fade_ticks = SDL_GetTicks() - 5000,
};
+ // create fade texture
+ draw_ctx.fade_tex = SDL_CreateTexture(
+ renderer,
+ SDL_PIXELFORMAT_RGBA32,
+ SDL_TEXTUREACCESS_TARGET,
+ WINDOW_WIDTH,
+ WINDOW_HEIGHT
+ );
+
+ // set fade texture blend mode
+ if (SDL_SetTextureBlendMode(draw_ctx.fade_tex, SDL_BLENDMODE_BLEND)) {
+ die("SDl_SetTextureBlendMode(): %s", SDL_GetError());
+ }
+
+ // check for error
+ if (!draw_ctx.fade_tex) {
+ die("SDL_CreateTexture(): %s", SDL_GetError());
+ }
+
// set level
set_level(&draw_ctx, &ctx, level_num);
@@ -259,7 +309,7 @@ int main(int argc, char *argv[]) {
case ACTION_NEXT:
if (sok_ctx_is_done(&ctx)) {
// play sound
- sound_play(sounds, ASSET_SOUND_JUMP_0_WAV);
+ sound_play(sounds, ASSET_SOUND_POWERUP_1_WAV);
// advance level
level_num++;
@@ -285,7 +335,7 @@ int main(int argc, char *argv[]) {
case ACTION_WARP:
if (warp_buf_get(&warp_buf, &level_num)) {
// play sound
- sound_play(sounds, ASSET_SOUND_JUMP_0_WAV);
+ sound_play(sounds, ASSET_SOUND_POWERUP_1_WAV);
// load level
set_level(&draw_ctx, &ctx, level_num);