diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-11-27 11:17:23 -0800 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-11-27 11:52:53 -0800 |
commit | 880eee92ca294c5367c859ab5e14338aa6838637 (patch) | |
tree | 33edd456535c2dc638d4b89612e6c60869dc3e70 /src/sars.c | |
parent | 9c29781b182ddb911cb7ce07e42ea7b9f2f3628b (diff) |
sars: chdir() to executable's base path
On MacOS in particular it seems when executing sars via gui from
the downloaded .zip the cwd isn't being set to the containing
directory beforehand. This prevents the assets from being found
at the correct location.
With this commit SDL_GetBasePath() is used to try find the
executable's containing directory. When successful, a chdir() is
performed to this path in an effort to ensure the assets/ dir is
where expected in relative terms.
These are all done in a non-fatal best-effort fashion, simply
warning when failed.
An unfortunate side effect of this is when you're explicitly
running sars from somewhere else with a different relative
assets/ directory for development/testing purposes, it no longer
uses the inherited cwd. Instead it will always move itself to
wherever the executable resides. Maybe I'll add a flag for
controlling this behavior.
It's also unclear to me how safe assuming chdir() works will be
vs. platforms supported by SDL2. Sure, chdir() is POSIX, but um,
SDL2 speaks to supporting platforms like PSP and Android. I
doubt chdir() is available everywhere, and I wonder why there's
no SDL_chdir() in SDL_filesystem.h to hide this potential detail
alongside the SDL_GetBasePath() abstraction.
Diffstat (limited to 'src/sars.c')
-rw-r--r-- | src/sars.c | 9 |
1 files changed, 9 insertions, 0 deletions
@@ -15,6 +15,7 @@ */ #include <SDL.h> +#include <unistd.h> /* for chdir() */ #include <play.h> #include <stage.h> @@ -228,6 +229,14 @@ sars_winmode_t sars_winmode_set(sars_t *sars, sars_winmode_t winmode) static void * sars_init(play_t *play, int argc, char *argv[]) { sars_t *sars; + char *base; + + /* in case we're executed outside our dir, try chdir to it for assets/ */ + warn_if(!(base = SDL_GetBasePath()), "unable to get base path"); + if (base) { + warn_if(chdir(base) < 0, "unable to chdir(\"%s\")", base); + SDL_free(base); + } sars = calloc(1, sizeof(sars_t)); fatal_if(!sars, "Unable to allocate sars_t"); |