Maelstrom: Fixed video mode detection on the original iPhone, where the desktop resolution is in portrait mode and too small for Maelstrom

https://github.com/libsdl-org/Maelstrom/commit/3fc36b8a7b27edf233117f32a822cfbdc8e478f8

From 3fc36b8a7b27edf233117f32a822cfbdc8e478f8 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 2 Oct 2012 22:24:57 -0700
Subject: [PATCH] Fixed video mode detection on the original iPhone, where the
 desktop resolution is in portrait mode and too small for Maelstrom

---
 game/init.cpp | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/game/init.cpp b/game/init.cpp
index 05d32324..d73e0dc5 100644
--- a/game/init.cpp
+++ b/game/init.cpp
@@ -179,17 +179,28 @@ static bool InitResolutions(int &w, int &h)
 		}
 	}
 
-	// Try to get our desktop size and use the closest supported resolution
-	screen->GetDesktopSize(w, h);
-	if (!w || !h) {
-		w = GAME_WIDTH;
-		h = GAME_HEIGHT;
-	}
-	gResolutionIndex = FindResolution(w, h);
-	if (gResolutionIndex >= 0) {
-		w = gResolutions[gResolutionIndex].w;
-		h = gResolutions[gResolutionIndex].h;
-		return true;
+	// Look for the best mode in two passes, first check to see if any of
+	// our supported modes are available, and if not just grab the first mode
+	// that's bigger than any of our supported modes and stretch to that.
+	SDL_DisplayMode mode;
+	int displayIndex = 0;
+	for (int pass = 0; pass < 2; ++pass) {
+		bool exact = (pass == 0);
+		for (int i = 0; i < SDL_GetNumDisplayModes(displayIndex); ++i) {
+			if (SDL_GetDisplayMode(displayIndex, i, &mode) < 0) {
+				continue;
+			}
+			gResolutionIndex = FindResolution(mode.w, mode.h);
+			if (gResolutionIndex >= 0) {
+				// Note that we're going to request our best supported resolution here.
+				w = gResolutions[gResolutionIndex].w;
+				h = gResolutions[gResolutionIndex].h;
+				if (exact && (mode.w != w || mode.h != h)) {
+					continue;
+				}
+				return true;
+			}
+		}
 	}
 
 	error("Couldn't find any supported resolutions\n");