C ++用于查找数据目录的跨平台解决方案

时间:2023-01-29 12:02:23

This is my first * 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.

我正在制作游戏,我需要一个目录来放置其资源,例如图像,声音和配置文件。 (请注意,游戏需要能够修改自己的配置文件。)我以为我可以在Linux上使用/ usr / share / mygame /,在Windows上使用C:/ Program Files / MyGame /,但是会出现问题,因为游戏可以安装在这两个地方,也可以不安装,也可以用于开发目的。

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.

我可以查看可执行文件所在的文件夹并查看,但据我所知,这是不好的做法,并且很难实现跨平台。我显然无法使用工作目录,因为游戏可以从shell脚本,命令行,其他进程等运行。我也可以搜索整个文件系统,可能是为了敏感,为某个“标记”文件表明这是游戏文件的位置,但是认真对待?梳理文件系统并不是启动应用程序的好方法,而且存在多个安装,多个标记文件甚至根本没有标记文件的困境,这会导致强制用户坐下来的不良后果通过漫长的等待,当游戏搜索磁盘并最终无法启动,因为它是空的。

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?

所以,我放弃了!是时候直接就这个问题咨询互联网了。我应该如何确定我的文件存储位置?

2 个解决方案

#1


1  

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!

因此,您可以定义一个函数来设置项目的环境并获取数据的绝对路径!

#2


0  

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.

由于您不想使用工作目录,以防游戏从shell脚本,命令行等运行。您可以始终使用操作系统的相应用户文件夹。

For Windows

%APPDATA%\<YourGame>

For Linux I believe the standard location is

对于Linux我相信标准位置是

~/.local/share/<YourGame>

#1


1  

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!

因此,您可以定义一个函数来设置项目的环境并获取数据的绝对路径!

#2


0  

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.

由于您不想使用工作目录,以防游戏从shell脚本,命令行等运行。您可以始终使用操作系统的相应用户文件夹。

For Windows

%APPDATA%\<YourGame>

For Linux I believe the standard location is

对于Linux我相信标准位置是

~/.local/share/<YourGame>