summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2018-05-24 01:03:44 -0700
committerVito Caputo <vcaputo@pengaru.com>2018-05-24 01:06:11 -0700
commit29e7e9c928bd0b2918091a56013d3c3435ebed5e (patch)
treeedcebc7c0203b1a61e3219515d130d70d31be68d
parentbcf1646fe89b2e55e8282ea5bed7633ba4d2b8be (diff)
libstage: decouple stage_fit() from stage_t
It's desirable to be able to fit dimensions to an aspect ratio prior to stage creation. Since stage creation requires an SDL2 Renderer, it must already have the window etc. Turn this into a lower level helper for fitting so the window can be sized before creating the renderer from it. Should probably name it differently, but this is fine for now.
-rw-r--r--src/stage.c10
-rw-r--r--src/stage.h2
2 files changed, 6 insertions, 6 deletions
diff --git a/src/stage.c b/src/stage.c
index 7344104..064c739 100644
--- a/src/stage.c
+++ b/src/stage.c
@@ -307,9 +307,9 @@ stage_t * stage_free(stage_t *stage)
/* fit a stage to the supplied dimensions, returns the fitted dimensions in the result pointers */
/* the idea is this can be used in a window resize hook to enforce the stage's aspect ratio */
-void stage_fit(const stage_t *stage, int width, int height, int *res_width, int *res_height)
+void stage_fit(float aspect_ratio, int width, int height, int *res_width, int *res_height)
{
- float full_width = stage->aspect_ratio * ((float)height);
+ float full_width = aspect_ratio * ((float)height);
if (full_width == width) {
/* perfect fit */
@@ -317,7 +317,7 @@ void stage_fit(const stage_t *stage, int width, int height, int *res_width, int
*res_height = height;
} else if (full_width > width) {
/* height is too large */
- *res_height = (1.0f / stage->aspect_ratio) * ((float)width);
+ *res_height = (1.0f / aspect_ratio) * ((float)width);
*res_width = width;
} else {
/* width is too large */
@@ -422,7 +422,7 @@ void stage_render(const stage_t *stage)
/* XXX TODO: investigate renderer viewports and scale factors */
SDL_GetRendererOutputSize(stage->renderer, &width, &height);
- stage_fit(stage, width, height, &rect.w, &rect.h);
+ stage_fit(stage->aspect_ratio, width, height, &rect.w, &rect.h);
rect.x = (width - rect.w) / 2;
rect.y = (height - rect.h) / 2;
@@ -460,7 +460,7 @@ stage_node_t * stage_node_lookup_cartesian(const stage_t *stage, int x, int y)
/* FIXME: copy-pasta with render, factor out */
SDL_GetRendererOutputSize(stage->renderer, &width, &height);
- stage_fit(stage, width, height, &rect.w, &rect.h);
+ stage_fit(stage->aspect_ratio, width, height, &rect.w, &rect.h);
rect.x = (width - rect.w) / 2;
rect.y = (height - rect.h) / 2;
diff --git a/src/stage.h b/src/stage.h
index 6d73753..dd053f8 100644
--- a/src/stage.h
+++ b/src/stage.h
@@ -32,7 +32,7 @@ typedef void (*stage_free_func_t)(void *object);
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_fit(float aspect_ratio, int width, int height, int *res_width, int *res_height);
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);
© All Rights Reserved