From: Ingo Ruhnke Date: Sun, 21 Mar 2004 15:05:21 +0000 (+0000) Subject: - added detection magic for the datadir X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=08a697dcba687557de6dce27a1736edc75ce48d3;p=supertux.git - added detection magic for the datadir SVN-Revision: 292 --- diff --git a/src/globals.cpp b/src/globals.cpp index 20ad5e620..228185521 100644 --- a/src/globals.cpp +++ b/src/globals.cpp @@ -12,6 +12,9 @@ #include "globals.h" +/** The datadir prefix prepended when loading game data file */ +std::string datadir; + SDL_Surface * screen; text_type black_text, gold_text, blue_text, red_text, yellow_nums, white_text, white_small_text, white_big_text; diff --git a/src/globals.h b/src/globals.h index 502aa685c..da08624ad 100644 --- a/src/globals.h +++ b/src/globals.h @@ -14,10 +14,13 @@ #ifndef SUPERTUX_GLOBALS_H #define SUPERTUX_GLOBALS_H +#include #include #include "text.h" #include "menu.h" +extern std::string datadir; + extern SDL_Surface * screen; extern text_type black_text, gold_text, white_text, white_small_text, white_big_text, blue_text, red_text, yellow_nums; diff --git a/src/setup.cpp b/src/setup.cpp index e932d80e0..dc79fd069 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -10,6 +10,7 @@ April 11, 2000 - March 15, 2004 */ +#include #include #include #include @@ -26,6 +27,7 @@ #include #include #include +#include #include #endif @@ -253,7 +255,7 @@ void st_directory_setup(void) home = "."; st_dir = (char *) malloc(sizeof(char) * (strlen(home) + - strlen("/.supertux") + 1)); + strlen("/.supertux") + 1)); strcpy(st_dir, home); strcat(st_dir, "/.supertux"); @@ -283,6 +285,31 @@ void st_directory_setup(void) mkdir(str); #endif + // User has not that a datadir, so we try some magic + if (datadir.empty()) + { + // Detect datadir + char exe_file[PATH_MAX]; + if (readlink("/proc/self/exe", exe_file, PATH_MAX) < 0) + { + puts("Couldn't read /proc/self/exe, using default path: " DATA_PREFIX); + } + else + { + std::string exedir = std::string(dirname(exe_file)) + "/"; + + datadir = exedir + "../data/"; // SuperTux run from source dir + if (access(datadir.c_str(), F_OK) != 0) + { + datadir = exedir + "../share/supertux/"; // SuperTux run from PATH + if (access(datadir.c_str(), F_OK) != 0) + { // If all fails, fall back to compiled path + datadir = DATA_PREFIX; + } + } + } + } + printf("Datadir: %s\n", datadir.c_str()); } /* Create and setup menus. */ @@ -853,6 +880,12 @@ void parseargs(int argc, char * argv[]) { launch_worldmap_mode = true; } + else if (strcmp(argv[i], "--datadir") == 0 + || strcmp(argv[i], "-d") == 0 ) + { + assert(i+1 < argc); + datadir = argv[i+1]; + } else if (strcmp(argv[i], "--show-fps") == 0) { /* Use full screen: */ @@ -929,6 +962,7 @@ void parseargs(int argc, char * argv[]) "\n" "Misc Options:\n" " --worldmap Start in worldmap-mode (EXPERIMENTAL)\n" + " -d, --datadir DIR Load Game data from DIR (default: automatic)\n" " --debug-mode Enables the debug-mode, which is useful for developers.\n" " --help Display a help message summarizing command-line\n" " options, license and game controls.\n"