SDL: Prefer display modes that exactly match the desired format or refresh rate

From 6c6b497f89f52071753505b86d77f1542f09bb21 Mon Sep 17 00:00:00 2001
From: Cameron Cawley <[EMAIL REDACTED]>
Date: Fri, 12 Apr 2024 22:01:04 +0100
Subject: [PATCH] Prefer display modes that exactly match the desired format or
 refresh rate

---
 src/video/SDL_video.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index d3052aa2dd635..a841239052010 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -994,7 +994,7 @@ static SDL_DisplayMode *SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay *di
             match = current;
             continue;
         }
-        if (current->format != match->format) {
+        if (current->format != match->format && match->format != target_format) {
             /* Sorted highest depth to lowest */
             if (current->format == target_format ||
                 (SDL_BITSPERPIXEL(current->format) >=
@@ -1005,7 +1005,7 @@ static SDL_DisplayMode *SDL_GetClosestDisplayModeForDisplay(SDL_VideoDisplay *di
             }
             continue;
         }
-        if (current->refresh_rate != match->refresh_rate) {
+        if (current->refresh_rate != match->refresh_rate && match->refresh_rate != target_refresh_rate) {
             /* Sorted highest refresh to lowest */
             if (current->refresh_rate >= target_refresh_rate) {
                 match = current;