Alex
Alex

Reputation: 21

SFML cannot load textures from the images folder - Unable to open file

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?

Upvotes: 0

Views: 63

Answers (1)

Lukas
Lukas

Reputation: 2613

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

Upvotes: 0

Related Questions