Added SDL_SetWindowAspectRatio() and SDL_GetWindowAspectRatio()

Fixes https://github.com/libsdl-org/SDL/issues/1573
This commit is contained in:
Sam Lantinga 2024-05-27 15:23:04 -07:00
parent aacafd6233
commit c74886ab00
15 changed files with 364 additions and 36 deletions

View file

@ -35,6 +35,7 @@ static const char *common_usage[] = {
static const char *video_usage[] = {
"[--always-on-top]",
"[--aspect min-max]",
"[--auto-scale-content]",
"[--center | --position X,Y]",
"[--confine-cursor X,Y,W,H]",
@ -373,12 +374,7 @@ int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
return 2;
}
if (SDL_strcasecmp(argv[index], "--usable-bounds") == 0) {
/* !!! FIXME: this is a bit of a hack, but I don't want to add a
!!! FIXME: flag to the public structure in 2.0.x */
state->window_x = -1;
state->window_y = -1;
state->window_w = -1;
state->window_h = -1;
state->fill_usable_bounds = SDL_TRUE;
return 1;
}
if (SDL_strcasecmp(argv[index], "--geometry") == 0) {
@ -438,6 +434,26 @@ int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
state->window_maxH = SDL_atoi(h);
return 2;
}
if (SDL_strcasecmp(argv[index], "--aspect") == 0) {
char *min_aspect, *max_aspect;
++index;
if (!argv[index]) {
return -1;
}
min_aspect = argv[index];
max_aspect = argv[index];
while (*max_aspect && *max_aspect != '-') {
++max_aspect;
}
if (*max_aspect) {
*max_aspect++ = '\0';
} else {
max_aspect = min_aspect;
}
state->window_min_aspect = SDL_atof(min_aspect);
state->window_max_aspect = SDL_atof(max_aspect);
return 2;
}
if (SDL_strcasecmp(argv[index], "--logical") == 0) {
char *w, *h;
++index;
@ -1308,19 +1324,18 @@ SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
SDL_Rect r;
SDL_PropertiesID props;
r.x = state->window_x;
r.y = state->window_y;
r.w = state->window_w;
r.h = state->window_h;
if (state->auto_scale_content) {
float scale = SDL_GetDisplayContentScale(state->displayID);
r.w = (int)SDL_ceilf(r.w * scale);
r.h = (int)SDL_ceilf(r.h * scale);
}
/* !!! FIXME: hack to make --usable-bounds work for now. */
if ((r.x == -1) && (r.y == -1) && (r.w == -1) && (r.h == -1)) {
if (state->fill_usable_bounds) {
SDL_GetDisplayUsableBounds(state->displayID, &r);
} else {
r.x = state->window_x;
r.y = state->window_y;
r.w = state->window_w;
r.h = state->window_h;
if (state->auto_scale_content) {
float scale = SDL_GetDisplayContentScale(state->displayID);
r.w = (int)SDL_ceilf(r.w * scale);
r.h = (int)SDL_ceilf(r.h * scale);
}
}
if (state->num_windows > 1) {
@ -1349,6 +1364,9 @@ SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
if (state->window_maxW || state->window_maxH) {
SDL_SetWindowMaximumSize(state->windows[i], state->window_maxW, state->window_maxH);
}
if (state->window_min_aspect || state->window_max_aspect) {
SDL_SetWindowAspectRatio(state->windows[i], state->window_min_aspect, state->window_max_aspect);
}
SDL_GetWindowSize(state->windows[i], &w, &h);
if (!(state->window_flags & SDL_WINDOW_RESIZABLE) && (w != r.w || h != r.h)) {
SDL_Log("Window requested size %dx%d, got %dx%d\n", r.w, r.h, w, h);
@ -2376,15 +2394,21 @@ int SDLTest_CommonEventMainCallbacks(SDLTest_CommonState *state, const SDL_Event
break;
case SDLK_a:
if (withControl) {
/* Ctrl-A reports absolute mouse position. */
float x, y;
const SDL_MouseButtonFlags mask = SDL_GetGlobalMouseState(&x, &y);
SDL_Log("ABSOLUTE MOUSE: (%g, %g)%s%s%s%s%s\n", x, y,
(mask & SDL_BUTTON_LMASK) ? " [LBUTTON]" : "",
(mask & SDL_BUTTON_MMASK) ? " [MBUTTON]" : "",
(mask & SDL_BUTTON_RMASK) ? " [RBUTTON]" : "",
(mask & SDL_BUTTON_X1MASK) ? " [X2BUTTON]" : "",
(mask & SDL_BUTTON_X2MASK) ? " [X2BUTTON]" : "");
/* Ctrl-A toggle aspect ratio */
SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
if (window) {
float min_aspect = 0.0f, max_aspect = 0.0f;
SDL_GetWindowAspectRatio(window, &min_aspect, &max_aspect);
if (min_aspect > 0.0f || max_aspect > 0.0f) {
min_aspect = 0.0f;
max_aspect = 0.0f;
} else {
min_aspect = 1.0f;
max_aspect = 1.0f;
}
SDL_SetWindowAspectRatio(window, min_aspect, max_aspect);
}
}
break;
case SDLK_0: