In my game you can take screenshots using Shift+F12, and I was
thinking making it store the files in the folder that is normally used
to store the user’s pictures. Then I realized that such a thing may be
useful for programs in general, so I wonder: can we make SDL return
the relevant paths to them?
Here is the set of functions I propose. They’d be similar to
SDL_GetBasePath and SDL_GetPrefPath.------------------------------------------------------
SDL_GetHomePath()
Returns the path to the current user’s home directory. If such a thing
doesn’t exist, it returns whatever directory would be most suitable
for that job. If it’s impossible to even access those user files in
the first place, it returns NULL.
SDL_GetPicturesPath()
Returns the path to the directory intended to store the user’s
pictures. If such a thing doesn’t exist, it behaves like
SDL_GetHomePath.
SDL_GetMusicPath()
Returns the path to the directory intended to store the user’s music.
If such a thing doesn’t exist, it behaves like SDL_GetHomePath.
SDL_GetVideoPath()
Returns the path to the directory intended to store the user’s videos.
If such a thing doesn’t exist, it behaves like SDL_GetHomePath.
SDL_GetDocumentsPath()
Returns the path to the directory intended to store the user’s
documents. If such a thing doesn’t exist, it behaves like
SDL_GetHomePath.
SDL_GetDownloadsPath()
Returns the path to the directory intended to store the files the user
downloaded. If such a thing doesn’t exist, it behaves like
SDL_GetHomePath.
With Windows you could use SHGetFolderPath for that (yes, I know it’s
deprecated, but SDL2 still works on Windows XP so may as well keep
that around). The home directory would be My Documents I suppose (and
yes I know that the last two functions would have to return that as
well).
With Linux you can get the home directory with getenv(“HOME”). As for
the other directories, they’re in $HOME/.config/user-dirs.dirs (it’s a
text file), and its format should be self-explanatory once you open
the file. If the file is not present it’s probably safer to just
fallback on the home directory.
No idea how it’d work with other OSes but I suppose there are similar
APIs. Going for the home directory first is probably a good idea since
all other functions will fallback to it in case of failure. Also
making the functions return NULL when you can’t access the user
directories (e.g. because it’s a mobile app without enough privileges)
is OK I suppose.
Comments?