diff --git a/src/lol/sys/init.h b/src/lol/sys/init.h index 27ab6808..539bc9a7 100644 --- a/src/lol/sys/init.h +++ b/src/lol/sys/init.h @@ -34,12 +34,17 @@ namespace lol # define LOL_CONFIG_SOLUTIONDIR "" #endif +/* + * System namespace. The platform-specific stuff in there makes the API + * not as clean as the rest of the framework. + */ + namespace System { extern void Init(int argc, char *argv[], - char const *projectdir = LOL_CONFIG_PROJECTDIR, - char const *solutiondir = LOL_CONFIG_SOLUTIONDIR); + String const projectdir = LOL_CONFIG_PROJECTDIR, + String const solutiondir = LOL_CONFIG_SOLUTIONDIR); extern void SetDataDir(char const *dir); extern char const *GetDataDir(); diff --git a/src/sys/init.cpp b/src/sys/init.cpp index 1e1cd856..853f32dc 100644 --- a/src/sys/init.cpp +++ b/src/sys/init.cpp @@ -12,6 +12,11 @@ # include "config.h" #endif +#ifdef _WIN32 +# define WIN32_LEAN_AND_MEAN +# include +#endif + #include "core.h" namespace lol @@ -27,11 +32,31 @@ namespace System #endif void Init(int argc, char *argv[], - char const *projectdir, char const *solutiondir) + String const projectdir, String const solutiondir) { - bool got_rootdir = false; + /* + * Retrieve binary directory, defaulting to current dir. + */ + +#if defined _WIN32 + char const *cwd = _getcwd(NULL, 0); + String binarydir = String(cwd ? cwd : ".") + SEPARATOR; + free((void *)cwd); +#else + String binarydir = String(getcwd()) + SEPARATOR; +#endif + if (argc > 0) + { + char const *last_sep = strchr(argv[0], SEPARATOR); + if (last_sep) + binarydir = String(argv[0], last_sep - argv[0] + 1); + } - /* Find the common prefix between project dir and solution dir. */ + /* + * Find the common prefix between project dir and solution dir. + */ + + bool got_rootdir = false; for (int i = 0; ; i++) { if (projectdir[i] != solutiondir[i] || projectdir[i] == '\0') @@ -51,17 +76,11 @@ void Init(int argc, char *argv[], } } - /* Try to guess the data directory from the executable location. */ - if (!got_rootdir && argc > 0) + /* If no rootdir found, use the executable location */ + if (!got_rootdir) { - char const *last_slash = strrchr(argv[0], SEPARATOR); - - if (last_slash) - { - String dir(argv[0], last_slash - argv[0] + 1); - SetDataDir(&dir[0]); - got_rootdir = true; - } + SetDataDir(&binarydir[0]); + got_rootdir = true; } }