0

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?

7
  • 2
    A guess is that your CWD (Current Working Directory) is not where you think it is when running the program. I find it to usually be more robust to obtain the path to the running executable at runtime and then construct a relative path to resources from there (or you could change the CWD at runtime, to a known location or use absolute paths). Commented Dec 11, 2024 at 20:25
  • Have you checked that you have read access to the files in question (and to the directory they are in)? Commented Dec 11, 2024 at 20:45
  • add system("cd") at the first line of your main to see your current working directory printed, you should use absolute paths relative to your executable path instead of using relative paths Commented Dec 11, 2024 at 21:02
  • 1
    @Ahmed or just current-path or getcwd. Seems better to me than opening a shell and running some random system command. Commented Dec 11, 2024 at 22:15
  • 1
    Failed to load image "". Reason: Unable to open file This error is suspicious to me. If that is the exact error message my guess is you are using a version of sfml that was compiled with a compiler that is not compatible with the one you are using in your project. The reason for this guess is the file name should have been displayed instead of "". 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. Commented Dec 11, 2024 at 23:55

1 Answer 1

0

When you end up with an empty string or random characters as path in the error message, it's usually an indication of broken runtime compatibility.

In most cases people link SFML release libraries in debug mode, or debug libraries in release mode and thus end up with different, incompatible runtimes between their application code and SFML.

Make sure that you link the library files with the -d suffix only in the debug mode configuration and the one without suffix in release mode.

Note: If you use static libraries, the suffixes would be -s-d for debug and -s for release

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.