Home > other >  c sfml writes to the console: Failed to load image "". Reason: Unable to open file
c sfml writes to the console: Failed to load image "". Reason: Unable to open file

Time:01-20

I tried to upload the image via sfml probably in all possible ways, but I got an error in the console
My code:

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    sf::Texture texture;
    int i=0;
    if (!texture.loadFromFile("box.jpg"))
    {
        std::cout << "Error!";
        window.close();
        return 1;
    }
    
    sf::Sprite sprite;
    sprite.setTexture(texture);
    sf::Event event=sf::Event();
    while (window.isOpen())
    {   
        
        if (event.type == sf::Event::Closed)
            window.close();
        window.draw(sprite);
        window.clear();
        window.display();
    }

    return 0;
}

Maybe I'm doing something wrong, I'm a beginner c and sfml developer.
My settings:
https://i.stack.imgur.com/OF9FA.png
https://i.stack.imgur.com/u2ZSC.png
The file with the picture is in all folders starting from the repos

I dragged the file over all folders in the solution folder, starting from the very first folder ending with the x64 folder, all without success, I searched the question on the Internet, I did not find. \

I tried to move the image to the C folder and specified the path to the image C:\\box.jpg, I got an error in the console:Failed to load image "╨°│╕;☻C:\box.jpg ‼b√⌂Г0b√⌂9Ц ☻ў⌂ ♥X☻ ў⌂Я0b√⌂И·/WJИ·/WJ¶·/WJж╬▀pp↓ut ☻ў⌂)w ☻ў⌂☺p↓│▼√⌂╜О ☻ў⌂☺P?Ь╡;☻└ЙЪ╡;☻`t ☻ў⌂╬u ☻ў⌂Ё ☻ў⌂ Є ☻ў⌂аK♥ў⌂ИK♥ў⌂Оt ☻ў⌂╛w ☻ў⌂¶v╞р√⌂@¶WJб&╢с√⌂0√  ш♦0√  ╨♦↓

CodePudding user response:

You can start from here.

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
using namespace std;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");
    sf::Texture texture;
    int i = 0;
    if (!texture.loadFromFile("box.jpg")) // check file path or you can execute the program from command line to make sure
    {
        std::cout << "Error!";
        window.close();
        return 1;
    }

    sf::Sprite sprite;
    sprite.setTexture(texture);

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

        window.clear(); // clear first
        window.draw(sprite);
        window.display();
    }

    return 0;
}

If you don't use the absolute file path the folder tree is as follows:

C:\...\SFML
├───SFML // the image file needs to be here to run from IDE
│   └───x64
│       └───Debug
│
└───x64
    └───Debug // the image file needs to be here to run from command line
    └───Release  // the image file needs to be here to run from command line

CodePudding user response:

Who has the same problem go to the project -> properties -> configuration properties -> advanced -> use debugging libraries, by default it is yes, you need to set it to default or "no". Translated through a translator, the translation may not be accurate

(Sorry for my English)

  • Related