I am trying to load textures for a chessboard and chess pieces in my chess game using SFML. However, the loadFromFile function fails and returns the error: Unable to open file.
What I am trying to do:
Load textures from the files images/board.png and images/figures.png.
What works:
I checked the working directory using std::filesystem::current_path() and it points to the correct location: C:/Users/Ola/Desktop/Studia/c++/Szachy/x64/Debug.
The texture files exist in the images folder.
void ChessGame::loadTextures() {
if (!boardTexture.loadFromFile("images/board.png")) {
std::cerr << "Failed to load board texture (images/board.png)" << std::endl;
}
if (!pieceTexture.loadFromFile("images/figures.png")) {
std::cerr << "Failed to load piece textures (images/figures.png)" << std::endl;
}
boardSprite.setTexture(boardTexture);
for (int i = 0; i < 32; i++) {
pieces[i].setTexture(pieceTexture);
}
}
Folder structure:
x64
└── Debug
├── Szachy.exe
├── images
│ ├── board.png
│ └── figures.png
Error messages in the console:
Failed to load image "". Reason: Unable to open file
Failed to load board texture
Failed to load piece textures
What could be causing the issue, and how can I resolve it?
system("cd")at the first line of yourmainto see your current working directory printed, you should use absolute paths relative to your executable path instead of using relative paths"". The missing file name to me could represent a situation where you have more than 1 version of the standard library being used in your code and that caused the file name to fail when 2 different std::string implementations were used.