sdl2-compat: render: match SDL2's handling of bogus values in SDL_HINT_RENDER_DRIVER.

From e4332ceb5b48129be126a819743b850576376f4f Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Tue, 4 Feb 2025 13:23:26 -0500
Subject: [PATCH] render: match SDL2's handling of bogus values in
 SDL_HINT_RENDER_DRIVER.

Fixes #305.
---
 src/sdl2_compat.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/src/sdl2_compat.c b/src/sdl2_compat.c
index bbfd64c..84c4c5c 100644
--- a/src/sdl2_compat.c
+++ b/src/sdl2_compat.c
@@ -4556,8 +4556,31 @@ SDL_CreateRenderer(SDL_Window *window, int idx, Uint32 flags)
     SDL_PropertiesID props;
     SDL_Renderer *renderer;
     const char *name = NULL;
+    char *namecpy = NULL;
 
-    if (idx != -1) {
+    if (idx == -1) {
+        /* SDL2, if given a bogus render driver name in a hint, would find no match, and then try everything.
+           SDL3 reports failure in this case. PCem tries to create a renderer named "auto" so we need to deal
+           with this. */
+        name = SDL3_GetHint(SDL_HINT_RENDER_DRIVER);
+        if (name) {
+            const int total = SDL3_GetNumRenderDrivers();
+            int i;
+            for (i = 0; i < total; i++) {
+                if (SDL3_strcasecmp(name, SDL3_GetRenderDriver(i)) == 0) {
+                    break;
+                }
+            }
+            if (i == total) {
+                namecpy = SDL3_strdup(name);
+                if (!namecpy) {
+                    return NULL;
+                }
+                name = NULL;  /* set it to NULL so SDL3 will choose a default. */
+                SDL3_SetHint(SDL_HINT_RENDER_DRIVER, NULL);
+            }
+        }
+    } else {
         name = SDL3_GetRenderDriver(idx);
         if (!name) {
             return NULL;  /* assume SDL3_GetRenderDriver set the SDL error. */
@@ -4588,6 +4611,12 @@ SDL_CreateRenderer(SDL_Window *window, int idx, Uint32 flags)
         SDL3_SetRenderVSync(renderer, 1);
     }
 
+    /* restore original hint. */
+    if (namecpy) {
+        SDL3_SetHint(SDL_HINT_RENDER_DRIVER, namecpy);
+        SDL3_free(namecpy);
+    }
+
     return renderer;
 }