From db56f2e024f5c5247b7af63125f5f8640f5e8d9a Mon Sep 17 00:00:00 2001 From: turbocat Date: Sat, 3 Apr 2021 00:16:38 +0000 Subject: [PATCH] Wolfenstein 3D: - Now it searches for resources only in the directory where the binary itself is located. git-svn-id: svn://kolibrios.org@8664 a494cfbc-eb01-0410-851d-a64ba20cac60 --- contrib/games/wolf3d/kolibri.cpp | 60 ++++++++++++++++++++++++++++++++ contrib/games/wolf3d/wl_main.cpp | 6 ++++ 2 files changed, 66 insertions(+) diff --git a/contrib/games/wolf3d/kolibri.cpp b/contrib/games/wolf3d/kolibri.cpp index ee1abb72bb..d6be883582 100644 --- a/contrib/games/wolf3d/kolibri.cpp +++ b/contrib/games/wolf3d/kolibri.cpp @@ -1,6 +1,15 @@ #include #include #include "../../kolibri-libc/source/include/ksys.h" +#include + +void *memrchr(const void *m, int c, size_t n) +{ + const unsigned char *s = (const unsigned char*)m; + c = (unsigned char)c; + while (n--) if (s[n]==c) return (void *)(s+n); + return 0; +} void kolibri_set_win_center() { @@ -19,3 +28,54 @@ int mkdir(const char * path, unsigned) return _ksys_mkdir(path); } +char *dirname (char *path) +{ + static const char dot[] = "."; + char *last_slash; + /* Find last '/'. */ + last_slash = path != NULL ? strrchr (path, '/') : NULL; + if (last_slash != NULL && last_slash != path && last_slash[1] == '\0') + { + /* Determine whether all remaining characters are slashes. */ + char *runp; + for (runp = last_slash; runp != path; --runp) + if (runp[-1] != '/') + break; + /* The '/' is the last character, we have to look further. */ + if (runp != path) + last_slash = (char*)memrchr((void*)path, '/', runp - path); + } + if (last_slash != NULL) + { + /* Determine whether all remaining characters are slashes. */ + char *runp; + for (runp = last_slash; runp != path; --runp) + if (runp[-1] != '/') + break; + /* Terminate the path. */ + if (runp == path) + { + /* The last slash is the first character in the string. We have to + return "/". As a special case we have to return "//" if there + are exactly two slashes at the beginning of the string. See + XBD 4.10 Path Name Resolution for more information. */ + if (last_slash == path + 1) + ++last_slash; + else + last_slash = path + 1; + } + else + last_slash = runp; + last_slash[0] = '\0'; + } + else + /* This assignment is ill-designed but the XPG specs require to + return a string containing "." in any case no directory part is + found and so a static and constant string is required. */ + path = (char *) dot; + return path; +} + +void setcwd(char* path){ + _ksys_setcwd(path); +} diff --git a/contrib/games/wolf3d/wl_main.cpp b/contrib/games/wolf3d/wl_main.cpp index f0ac5c561c..91f4c2fdc7 100755 --- a/contrib/games/wolf3d/wl_main.cpp +++ b/contrib/games/wolf3d/wl_main.cpp @@ -25,6 +25,8 @@ extern byte signon[]; extern void kolibri_set_win_center(); +extern char* dirname(char* path); +extern void setcwd(char* path); /* ============================================================================= @@ -1959,6 +1961,10 @@ void CheckParameters(int argc, char *argv[]) int main (int argc, char *argv[]) { +#ifdef _KOLIBRI + setcwd(dirname(argv[0])); +#endif + #if defined(_arch_dreamcast) DC_Init(); #else