From 29e7e9c928bd0b2918091a56013d3c3435ebed5e Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 24 May 2018 01:03:44 -0700 Subject: 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. --- src/stage.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/stage.c') 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; -- cgit v1.2.3