相关文章推荐
酷酷的酱牛肉  ·  soapenv:Server.userExc ...·  3 月前    · 
彷徨的马铃薯  ·  PyQt5 ...·  10 月前    · 
酒量大的火龙果  ·  Vue.js ...·  11 月前    · 
快乐的哑铃  ·  Could not find ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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√  ╨♦↓

Is box.jpg located in the same directory from which your executable is launched? It is common mistake to place the image files in the source folders. – Martin Sand Jan 19 at 16:14 If in doubt, just change loadFromFile("box.jpg") to the absolute path to your image and see if that works, something like loadFromFile("C:\\thisfolder\\thatfolder\\box.jpg") – user20716902 Jan 19 at 16:25 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

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)

You shouldn't set "use debbuging libraries" to no if there isn't an actual reason for that. You just simply have the wrong linker properties. It is important to link to the libraries that match the configuration: "sfml-xxx-d.lib" for Debug, and "sfml-xxx.lib" for Release. A bad mix may result in crashes. Quote from sfml-dev.org/tutorials/2.5/start-vc.php – Dnafiqvss Jan 21 at 21:20

Make sure the image you want to load is in the same folder as the c++ project file. it doesn't need to be anywhere else but there.

Also make sure your file is named as it is in your program (box.jpg).

You should also move window.clear(); to above window.draw(sprite); or the image wont be displayed.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.