SFML community forums

Help => General => Topic started by: Jaged on July 04, 2021, 05:53:47 am

Title: Reason: Unable to open file
Post by: Jaged on July 04, 2021, 05:53:47 am
I keep getting this error when I try to use "IncludeFromFile" for a sprite texture. I've tried to change the directory from shortened to full and change the picture just in case but it doesn't load the image.

I feel it is a mistake on my end like a mistake in calling the file or linking as I'm relatively new to both sfml and C++.

I'm using Visual studio 2019, and running a debug

   
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Game"); //calls a window
    window.setVerticalSyncEnabled(true);
    sf::Texture t1;
    t1.loadFromFile("Images/whitePawn.png");
    sf::Sprite board(t1);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
   
        }

        window.clear(); //set black
        window.draw(board); //draw whats needed
        window.display(); //reset for new frame

    }
 

I hope someone can help  :D, Thanks in advance
Title: Re: Reason: Unable to open file
Post by: Kvaz1r on July 04, 2021, 08:46:38 am
Can you check does example shader work for you?
Title: Re: Reason: Unable to open file
Post by: Nexus on July 04, 2021, 02:20:33 pm
Probably your working directory is not correct.

IIRC, by default Visual Studio uses the output directory (where the .exe is created) and not the solution directory.
You can change the working directory in the project settings.
Title: Re: Reason: Unable to open file
Post by: kojack on July 04, 2021, 03:59:42 pm
The default working directory for Visual Studio is $(ProjectDir), which is the directory where the .vcxproj file is located.
The setting for this is in the project properties under Debugging / Working Directory.

Common ones you can include in the setting:
$(ProjectDir) - Where the .vcxproj file is.
$(SolutionDir) - Where the .sln file is.
$(TargetDir) - Where the .exe is put during a build.
(Note: all of these expand to an absolute path with a slash on the end)

I tend to do all of my settings (intermediate, output, etc) relative to the .sln. So I use: $(SolutionDir)bin\
But $(TargetDir) would work too.
Title: Re: Reason: Unable to open file
Post by: Jaged on July 05, 2021, 07:55:29 am
I don't know if the working directory is the issue because I've tried the full location of the file too and it gave the same error, and I am sure that's the file's
    sf::RenderWindow window(sf::VideoMode(800, 800), "Chess"); //calls a window
    window.setVerticalSyncEnabled(true);
    sf::Texture t1;
    t1.loadFromFile("C:/Users/jagdr/Downloads/Code/SFML Chess/Debug/Images/board.jpg");
    sf::Sprite board(t1);
 
name.

But if that is the issue how can I change the working directory?