From bcf1646fe89b2e55e8282ea5bed7633ba4d2b8be Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Mon, 14 May 2018 23:34:31 -0700 Subject: stage: add stage_[sg]et_alpha() Convenient for whole-stage fades... --- src/stage.c | 23 +++++++++++++++++++---- src/stage.h | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/stage.c b/src/stage.c index b50bc22..7344104 100644 --- a/src/stage.c +++ b/src/stage.c @@ -52,6 +52,7 @@ struct stage_t { list_head_t layers[STAGE_LAYERS_MAX]; SDL_Renderer *renderer; /* target renderer */ float aspect_ratio; /* width/height ratio of stage, -1.0...1.0 range of node coordinates map to this ratio. */ + float alpha; /* alpha to apply to all nodes */ }; @@ -326,6 +327,20 @@ void stage_fit(const stage_t *stage, int width, int height, int *res_width, int } +/* set the global alpha factor for the stage, the individual node alphas are multiplied by this value @ render time. */ +void stage_set_alpha(stage_t *stage, float alpha) +{ + stage->alpha = alpha; +} + + +/* get the global alpha factor for the stage */ +void stage_get_alpha(stage_t *stage, float *res_alpha) +{ + *res_alpha = stage->alpha; +} + + static void aabb_to_rect(const aabb_t *aabb, const v2f_t *pos, const SDL_Rect *stage_rect, SDL_Rect *res_rect) { float half_w = ((float)stage_rect->w) * .5f, half_h = ((float)stage_rect->h) * .5f; /* FIXME silly to recompute this repeatedly... */ @@ -340,7 +355,7 @@ static void aabb_to_rect(const aabb_t *aabb, const v2f_t *pos, const SDL_Rect *s } -static void render_nodes(list_head_t *head, SDL_Renderer *renderer, SDL_Rect *dest_rect) +static void render_nodes(const stage_t *stage, const list_head_t *head, SDL_Renderer *renderer, SDL_Rect *dest_rect) { stage_node_t *node; @@ -372,7 +387,7 @@ static void render_nodes(list_head_t *head, SDL_Renderer *renderer, SDL_Rect *de node->render(renderer, node->object, node_rect.w, node_rect.h, &node->cached.texture); node->cached.width = node_rect.w; node->cached.height = node_rect.h; - SDL_SetTextureAlphaMod(node->cached.texture, ((float)node->alpha * 255.0f)); + SDL_SetTextureAlphaMod(node->cached.texture, stage->alpha * node->alpha * 255.0f); /* copy the texture to renderer */ if (node->angle == 0) @@ -399,7 +414,7 @@ void stage_get_output_size(stage_t *stage, int *res_width, int *res_height) * will center the rendering and leave blank borders on the edges left empty. * The renderer may be sized precisely by first calling stage_fit(). */ -void stage_render(stage_t *stage) +void stage_render(const stage_t *stage) { SDL_Rect rect; int i, width, height; @@ -414,7 +429,7 @@ void stage_render(stage_t *stage) SDL_RenderClear(stage->renderer); /* TODO: handle -1 on error */ for (i = 0; i < STAGE_LAYERS_MAX; i++) - render_nodes(&stage->layers[i], stage->renderer, &rect); + render_nodes(stage, &stage->layers[i], stage->renderer, &rect); } diff --git a/src/stage.h b/src/stage.h index ff43847..6d73753 100644 --- a/src/stage.h +++ b/src/stage.h @@ -33,7 +33,9 @@ stage_t * stage_new(SDL_Renderer *renderer, float aspect_ratio); void stage_clear(stage_t *stage); stage_t * stage_free(stage_t *stage); void stage_fit(const stage_t *stage, int width, int height, int *res_width, int *res_height); -void stage_render(stage_t *stage); +void stage_set_alpha(stage_t *stage, float alpha); +void stage_get_alpha(stage_t *stage, float *res_alpha); +void stage_render(const stage_t *stage); stage_node_t * stage_node_new(stage_t *stage, int layer, const char *name, void *object, stage_render_func_t render_func, stage_free_func_t free_func); void stage_get_output_size(stage_t *stage, int *res_width, int *res_height); -- cgit v1.2.3