简体   繁体   中英

Asset folder path for c++ program [windows]

The structure of my Release version of my c++program is

 | 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. What am I doing wrong?

Extra info: I am using SLD2 as a supporting library.

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.

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