فهرست منبع

gdk: GetBasePath should be a UTF8 version of Win32 GetBasePath

Ethan Lee 1 سال پیش
والد
کامیت
6f8a6a31ca
1فایلهای تغییر یافته به همراه45 افزوده شده و 1 حذف شده
  1. 45 1
      src/filesystem/gdk/SDL_sysfilesystem.cpp

+ 45 - 1
src/filesystem/gdk/SDL_sysfilesystem.cpp

@@ -34,7 +34,51 @@
 char *
 SDL_GetBasePath(void)
 {
-    return SDL_strdup("G:\\");
+    /* NOTE: This function is a UTF8 version of the Win32 SDL_GetBasePath()!
+     * The GDK actually _recommends_ the 'A' functions over the 'W' functions :o
+     */
+    DWORD buflen = 128;
+    CHAR *path = NULL;
+    DWORD len = 0;
+    int i;
+
+    while (SDL_TRUE) {
+        void *ptr = SDL_realloc(path, buflen * sizeof(CHAR));
+        if (ptr == NULL) {
+            SDL_free(path);
+            SDL_OutOfMemory();
+            return NULL;
+        }
+
+        path = (CHAR *)ptr;
+
+        len = GetModuleFileNameA(NULL, path, buflen);
+        /* if it truncated, then len >= buflen - 1 */
+        /* if there was enough room (or failure), len < buflen - 1 */
+        if (len < buflen - 1) {
+            break;
+        }
+
+        /* buffer too small? Try again. */
+        buflen *= 2;
+    }
+
+    if (len == 0) {
+        SDL_free(path);
+        WIN_SetError("Couldn't locate our .exe");
+        return NULL;
+    }
+
+    for (i = len - 1; i > 0; i--) {
+        if (path[i] == '\\') {
+            break;
+        }
+    }
+
+    SDL_assert(i > 0);  /* Should have been an absolute path. */
+    path[i + 1] = '\0'; /* chop off filename. */
+
+    return path;
 }
 
 char *