简体   繁体   中英

C++ Cross-platform solution to finding a data directory

This is my first stackoverflow post.

I am making a game, and I need a directory to put its resources in to, such as images, sounds, and configuration files. (Note that the game needs to be able to modify its own config files.) I was thinking I could use /usr/share/mygame/ on Linux, and C:/Program Files/MyGame/ on Windows, but problems arise, because the game may or may not be installed in either of those places, as well as for purposes of development.

I could check to find out what folder the executable is in and look there, but as I understand it that's bad practice, and difficult to implement cross-platform. I clearly couldn't use the working directory because the game could be run from shell scripts, the command line, other processes, etc. I could also search through the whole filesystem, maybe in order of sensibility, for a certain "marker" file indicating that this is where the game's files are, but seriously? Combing through the filesystem isn't exactly a great way to start off an application, and there is a dilemma of multiple installations, multiple marker files, or even no marker file at all, which would have the undesirable result of forcing the user to sit through a long wait while the game searches the disk and eventually fails to start because it came up empty.

So, I give up! It's time to consult the Internet directly about this issue. How should I figure out where my files are stored?

I think you can store your data in your project directory, when you want to get the absolute path of your data, you can get your project's absolute work directory firstly using:

getcwd()

and add the Relative path of your data,you will get the absolute path of your data!

So you can define a function that will set the environment of your project and get the absolute path of your data!

Since you don't want to use the working directory in case the game is running from shell scripts, command line, etc.. you could always use the respective user folders for the operating systems.

For Windows

%APPDATA%\<YourGame>

For Linux I believe the standard location is

~/.local/share/<YourGame>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM