Skip to content

Commit

Permalink
SDL_GetClosestFullscreenDisplayMode() now takes a boolean whether to …
Browse files Browse the repository at this point in the history
…include high density modes

Also changed the match to prioritize resolution over refresh rate
  • Loading branch information
slouken committed May 18, 2023
1 parent 32ab118 commit a66cad7
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/SDL3/SDL_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ extern DECLSPEC const SDL_DisplayMode **SDLCALL SDL_GetFullscreenDisplayModes(SD
* \sa SDL_GetDisplays
* \sa SDL_GetFullscreenDisplayModes
*/
extern DECLSPEC const SDL_DisplayMode *SDLCALL SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate);
extern DECLSPEC const SDL_DisplayMode *SDLCALL SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate, SDL_bool include_high_density_modes);

/**
* Get information about the desktop's display mode.
Expand Down
2 changes: 1 addition & 1 deletion src/dynapi/SDL_dynapi_procs.h
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ SDL_DYNAPI_PROC(int,SDL_ConvertAudioSamples,(SDL_AudioFormat a, Uint8 b, int c,
SDL_DYNAPI_PROC(SDL_DisplayID*,SDL_GetDisplays,(int *a),(a),return)
SDL_DYNAPI_PROC(SDL_DisplayID,SDL_GetPrimaryDisplay,(void),(),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode**,SDL_GetFullscreenDisplayModes,(SDL_DisplayID a, int *b),(a,b),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetClosestFullscreenDisplayMode,(SDL_DisplayID a, int b, int c, float d),(a,b,c,d),return)
SDL_DYNAPI_PROC(const SDL_DisplayMode*,SDL_GetClosestFullscreenDisplayMode,(SDL_DisplayID a, int b, int c, float d, SDL_bool e),(a,b,c,d,e),return)
SDL_DYNAPI_PROC(int,SDL_GetRenderOutputSize,(SDL_Renderer *a, int *b, int *c),(a,b,c),return)
SDL_DYNAPI_PROC(int,SDL_ConvertEventToRenderCoordinates,(SDL_Renderer *a, SDL_Event *b),(a,b),return)
SDL_DYNAPI_PROC(int,SDL_SetRenderScale,(SDL_Renderer *a, float b, float c),(a,b,c),return)
Expand Down
18 changes: 14 additions & 4 deletions src/test/SDL_test_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1288,9 +1288,15 @@ SDLTest_CommonInit(SDLTest_CommonState *state)
}
}

fullscreen_mode = SDL_GetClosestFullscreenDisplayMode(state->displayID, state->window_w, state->window_h, state->refresh_rate);
if (fullscreen_mode) {
SDL_memcpy(&state->fullscreen_mode, fullscreen_mode, sizeof(state->fullscreen_mode));
{
SDL_bool include_high_density_modes = SDL_FALSE;
if (state->window_flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
include_high_density_modes = SDL_TRUE;
}
fullscreen_mode = SDL_GetClosestFullscreenDisplayMode(state->displayID, state->window_w, state->window_h, state->refresh_rate, include_high_density_modes);
if (fullscreen_mode) {
SDL_memcpy(&state->fullscreen_mode, fullscreen_mode, sizeof(state->fullscreen_mode));
}
}

state->windows =
Expand Down Expand Up @@ -1866,7 +1872,11 @@ static void FullscreenTo(SDLTest_CommonState *state, int index, int windowId)
new_mode.displayID = displays[index];
if (SDL_SetWindowFullscreenMode(window, &new_mode) < 0) {
/* Try again with a default mode */
mode = SDL_GetClosestFullscreenDisplayMode(displays[index], state->window_w, state->window_h, state->refresh_rate);
SDL_bool include_high_density_modes = SDL_FALSE;
if (state->window_flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
include_high_density_modes = SDL_TRUE;
}
mode = SDL_GetClosestFullscreenDisplayMode(displays[index], state->window_w, state->window_h, state->refresh_rate, include_high_density_modes);
SDL_SetWindowFullscreenMode(window, mode);
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/video/SDL_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ const SDL_DisplayMode **SDL_GetFullscreenDisplayModes(SDL_DisplayID displayID, i
return modes;
}

const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate)
const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID displayID, int w, int h, float refresh_rate, SDL_bool include_high_density_modes)
{
const SDL_DisplayMode **modes;
const SDL_DisplayMode *mode, *closest = NULL;
Expand Down Expand Up @@ -1096,6 +1096,9 @@ const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID display
* This mode must be skipped, but closer modes may still follow */
continue;
}
if (mode->pixel_density > 1.0f && !include_high_density_modes) {
continue;
}
if (closest) {
float current_aspect_ratio = (float)mode->w / mode->h;
float closest_aspect_ratio = (float)closest->w / closest->h;
Expand All @@ -1104,7 +1107,8 @@ const SDL_DisplayMode *SDL_GetClosestFullscreenDisplayMode(SDL_DisplayID display
continue;
}

if (SDL_fabsf(closest->refresh_rate - refresh_rate) < SDL_fabsf(mode->refresh_rate - refresh_rate)) {
if (mode->w == closest->w && mode->h == closest->h &&
SDL_fabsf(closest->refresh_rate - refresh_rate) < SDL_fabsf(mode->refresh_rate - refresh_rate)) {
/* We already found a mode and the new mode is further from our
* refresh rate target */
continue;
Expand Down Expand Up @@ -3272,7 +3276,12 @@ void SDL_OnWindowDisplayChanged(SDL_Window *window)
const SDL_DisplayMode *new_mode = NULL;

if (window->requested_fullscreen_mode.w != 0 || window->requested_fullscreen_mode.h != 0) {
new_mode = SDL_GetClosestFullscreenDisplayMode(displayID, window->requested_fullscreen_mode.w, window->requested_fullscreen_mode.h, window->requested_fullscreen_mode.refresh_rate);
SDL_bool include_high_density_modes = SDL_FALSE;

if (window->requested_fullscreen_mode.pixel_density > 1.0f) {
include_high_density_modes = SDL_TRUE;
}
new_mode = SDL_GetClosestFullscreenDisplayMode(displayID, window->requested_fullscreen_mode.w, window->requested_fullscreen_mode.h, window->requested_fullscreen_mode.refresh_rate, include_high_density_modes);
}

if (new_mode) {
Expand Down
2 changes: 1 addition & 1 deletion src/video/kmsdrm/SDL_kmsdrmvideo.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ static drmModeModeInfo *KMSDRM_GetClosestDisplayMode(SDL_VideoDisplay *display,
const SDL_DisplayMode *closest;
drmModeModeInfo *drm_mode;

closest = SDL_GetClosestFullscreenDisplayMode(display->id, width, height, 0.0f);
closest = SDL_GetClosestFullscreenDisplayMode(display->id, width, height, 0.0f, SDL_FALSE);
if (closest) {
const SDL_DisplayModeData *modedata = (const SDL_DisplayModeData *)closest->driverdata;
drm_mode = &connector->modes[modedata->mode_index];
Expand Down
7 changes: 6 additions & 1 deletion src/video/uikit/SDL_uikitwindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ int UIKit_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window)
#if !TARGET_OS_TV
const CGSize origsize = data.uiscreen.currentMode.size;
if ((origsize.width == 0.0f) && (origsize.height == 0.0f)) {
const SDL_DisplayMode *bestmode = SDL_GetClosestFullscreenDisplayMode(display->id, window->w, window->h, 0.0f);
const SDL_DisplayMode *bestmode;
SDL_bool include_high_density_modes = SDL_FALSE;
if (window->flags & SDL_WINDOW_HIGH_PIXEL_DENSITY) {
include_high_density_modes = SDL_TRUE;
}
bestmode = SDL_GetClosestFullscreenDisplayMode(display->id, window->w, window->h, 0.0f, include_high_density_modes);
if (bestmode) {
SDL_UIKitDisplayModeData *modedata = (__bridge SDL_UIKitDisplayModeData *)bestmode->driverdata;
[data.uiscreen setCurrentMode:modedata.uiscreenmode];
Expand Down
9 changes: 2 additions & 7 deletions test/testautomation_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,7 @@ static int video_getClosestDisplayModeCurrentResolution(void *arg)
SDL_memcpy(&current, modes[0], sizeof(current));

/* Make call */
closest = SDL_GetClosestFullscreenDisplayMode(displays[i],
current.w,
current.h,
current.refresh_rate);
closest = SDL_GetClosestFullscreenDisplayMode(displays[i], current.w, current.h, current.refresh_rate, SDL_FALSE);
SDLTest_AssertPass("Call to SDL_GetClosestFullscreenDisplayMode(target=current)");
SDLTest_Assert(closest != NULL, "Verify returned value is not NULL");

Expand Down Expand Up @@ -356,9 +353,7 @@ static int video_getClosestDisplayModeRandomResolution(void *arg)
target.refresh_rate = (variation & 8) ? (float)SDLTest_RandomIntegerInRange(25, 120) : 0.0f;

/* Make call; may or may not find anything, so don't validate any further */
SDL_GetClosestFullscreenDisplayMode(displays[i], target.w,
target.h,
target.refresh_rate);
SDL_GetClosestFullscreenDisplayMode(displays[i], target.w, target.h, target.refresh_rate, SDL_FALSE);
SDLTest_AssertPass("Call to SDL_GetClosestFullscreenDisplayMode(target=random/variation%d)", variation);
}
}
Expand Down

0 comments on commit a66cad7

Please sign in to comment.