SDL_image: showimage/showanim: only use the base path if the file exists there

From 14d374baa5d76cd0263791f5d3ab289dfdfadf85 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Mon, 5 Jan 2026 13:53:34 -0800
Subject: [PATCH] showimage/showanim: only use the base path if the file exists
 there

---
 examples/showanim.c  | 23 ++++++++++++--------
 examples/showimage.c | 51 ++++++++++++++------------------------------
 2 files changed, 30 insertions(+), 44 deletions(-)

diff --git a/examples/showanim.c b/examples/showanim.c
index ecb62a48..0fe85956 100644
--- a/examples/showanim.c
+++ b/examples/showanim.c
@@ -52,6 +52,19 @@ static void draw_background(SDL_Renderer *renderer)
     }
 }
 
+static const char *get_file_path(const char *file)
+{
+    static char path[4096];
+
+    if (*file != '/' && !SDL_GetPathInfo(file, NULL)) {
+        SDL_snprintf(path, sizeof(path), "%s%s", SDL_GetBasePath(), file);
+        if (SDL_GetPathInfo(path, NULL)) {
+            return path;
+        }
+    }
+    return file;
+}
+
 int main(int argc, char *argv[])
 {
     SDL_Window *window;
@@ -108,15 +121,7 @@ int main(int argc, char *argv[])
         }
 
         /* Open the image file */
-        anim = IMG_LoadAnimation(argv[i]);
-        if (!anim) {
-            char *path = NULL;
-            SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), argv[i]);
-            if (path) {
-                anim = IMG_LoadAnimation(path);
-                SDL_free(path);
-            }
-        }
+        anim = IMG_LoadAnimation(get_file_path(argv[i]));
         if (!anim) {
             SDL_Log("Couldn't load %s: %s\n", argv[i], SDL_GetError());
             continue;
diff --git a/examples/showimage.c b/examples/showimage.c
index 9e00d5f8..f8b0bb15 100644
--- a/examples/showimage.c
+++ b/examples/showimage.c
@@ -52,17 +52,22 @@ static void draw_background(SDL_Renderer *renderer)
     }
 }
 
-static void set_cursor(const char *cursor_file)
+static const char *get_file_path(const char *file)
 {
-    IMG_Animation *anim = IMG_LoadAnimation(cursor_file);
-    if (!anim) {
-        char *path = NULL;
-        SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), cursor_file);
-        if (path) {
-            anim = IMG_LoadAnimation(path);
-            SDL_free(path);
+    static char path[4096];
+
+    if (*file != '/' && !SDL_GetPathInfo(file, NULL)) {
+        SDL_snprintf(path, sizeof(path), "%s%s", SDL_GetBasePath(), file);
+        if (SDL_GetPathInfo(path, NULL)) {
+            return path;
         }
     }
+    return file;
+}
+
+static void set_cursor(const char *cursor_file)
+{
+    IMG_Animation *anim = IMG_LoadAnimation(get_file_path(cursor_file));
     if (anim) {
         SDL_Cursor *cursor = IMG_CreateAnimatedCursor(anim, 0, 0);
         if (cursor) {
@@ -172,15 +177,7 @@ int main(int argc, char *argv[])
         if (tonemap) {
             SDL_Surface *surface, *temp;
 
-            surface = IMG_Load(argv[i]);
-            if (!surface) {
-                char *path = NULL;
-                SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), argv[i]);
-                if (path) {
-                    surface = IMG_Load(path);
-                    SDL_free(path);
-                }
-            }
+            surface = IMG_Load(get_file_path(argv[i]));
             if (!surface) {
                 SDL_Log("Couldn't load %s: %s\n", argv[i], SDL_GetError());
                 continue;
@@ -202,15 +199,7 @@ int main(int argc, char *argv[])
                 continue;
             }
         } else {
-            texture = IMG_LoadTexture(renderer, argv[i]);
-            if (!texture) {
-                char *path = NULL;
-                SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), argv[i]);
-                if (path) {
-                    texture = IMG_LoadTexture(renderer, path);
-                    SDL_free(path);
-                }
-            }
+            texture = IMG_LoadTexture(renderer, get_file_path(argv[i]));
             if (!texture) {
                 SDL_Log("Couldn't load %s: %s\n", argv[i], SDL_GetError());
                 continue;
@@ -220,15 +209,7 @@ int main(int argc, char *argv[])
 
         /* Save the image file, if desired */
         if (saveFile) {
-            SDL_Surface *surface = IMG_Load(argv[i]);
-            if (!surface) {
-                char *path = NULL;
-                SDL_asprintf(&path, "%s%s", SDL_GetBasePath(), argv[i]);
-                if (path) {
-                    surface = IMG_Load(path);
-                    SDL_free(path);
-                }
-            }
+            SDL_Surface *surface = IMG_Load(get_file_path(argv[i]));
             if (surface) {
                 if (!IMG_Save(surface, saveFile)) {
                     SDL_Log("Couldn't save %s: %s\n", saveFile, SDL_GetError());