简体   繁体   English

C++ 程序的资产文件夹路径 [windows]

[英]Asset folder path for c++ program [windows]

The structure of my Release version of my c++program is我的 c++ 程序的 Release 版本的结构是

 | bin
 | | game.exe
 | content
 | | sprites
 | | | asset.png

Within the code I pass the path to the asset as "../content/sprites/asset.png", but it is not found by the program.在代码中,我将资产的路径作为“../content/sprites/asset.png”传递,但程序找不到它。 What am I doing wrong?我究竟做错了什么?

Extra info: I am using SLD2 as a supporting library.额外信息:我使用 SLD2 作为支持库。

You are relying on the current directory being the directory containing your binary, but this is not always true.您依赖当前目录作为包含二进制文件的目录,但这并不总是正确的。 Instead, you can do this (error and range checking omitted for brevity):相反,您可以这样做(为简洁起见省略了错误和范围检查):

WCHAR buf = new WCHAR [32768];   // allow for long path names
GetModuleFileNameW (NULL, buf, 32768);
WCHAR sep = wcsrchr (buf, '\\');
wcscpy (sep + 1, L"..\\content\\sprites\\asset.png");
...
delete [] buf;

Edit:编辑:

If you are calling code that depends on the current directory being the one containing your binary, then you can do this instead somewhere in your initialisation code:如果您调用的代码依赖于包含二进制文件的当前目录,那么您可以在初始化代码中的某处执行此操作:

WCHAR buf = new WCHAR [32768];   // allow for long path names
GetModuleFileNameW (NULL, buf, 32768);
WCHAR sep = wcsrchr (buf, '\\');
*sep = 0;
SetCurrentDirectoryW (buf);
delete [] buf;

Again, I have omitted error and range checking for brevity.为简洁起见,我再次省略了错误和范围检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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