相关文章推荐
跑龙套的皮蛋  ·  JSON Java 8 ...·  1 年前    · 
想出国的碗  ·  使用 GitHub Codespaces ...·  1 年前    · 
听话的煎饼  ·  python - "Could not ...·  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

Am trying to load an image with SFML using Xcode, but everytime I run the program, the window (which has been created using the code) flashes and disappears...

Here's the code I am using:

#include <SFML/Graphics.hpp>
#include <iostream>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 1024
int main()
    sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Orsome Game");
    while(window.isOpen())
        sf::Event e;
        while(window.pollEvent(e))
            switch (e.type)
                case sf::Event::Closed:
                    window.close();
                    break;
        sf::Image image;
        if(!image.loadFromFile("sprite.png")){
            return -1;
        window.clear(sf::Color(255,255,255));
        window.display();
    return EXIT_SUCCESS;

Have also put the image file where the c++ file is, but it still doesn't work!

Configure the working directory in your IDE to be the directory containing the image file. – nada May 9, 2020 at 8:43 Or try if(!image.loadFromFile("C:/path/to/image/file/sprite.png")){ 9 times out of 10 this is because the program is looking for the file somewhere different from what the poster expected. – john May 9, 2020 at 8:46 @Transformer Well I'm no expert on SFML but you don't seem to be doing anything to display the image, you just load it. – john May 9, 2020 at 10:26

I would put an sf::Style::Default in the RenderWindow arguments, straight after "Orsome Game" window name. Because you are using the pollEvent, that would allow you to exit the window by pressing the x button in the top right corner, like a default application layout.

Is the picture in the same directory as your solution file? Or your main.cpp? It should be, of course. And, maybe, try loading your image with Texture and Sprite, like this:

sf::Texture texture;
texture.loadFromFile("picturename.png");
sf::Sprite sprite;
sprite.setTexture(texture);

inside the while window is open loop.

To draw it:

// after window.clear() ...
//... your code
window.draw(sprite);
//... other things to draw
// and then, your window.display();

But, probably, your sf::Image should also work. Tell me how it goes

EDIT:

Try this code and see if the window opens. Otherwise, that might be the problem of setting up the SFML on your computer and you will have to find some good tutorial on how to set it up properly! But let's see:

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
int main()
    RenderWindow window(VideoMode(640, 640), "Test", Style::Default);
    while (window.isOpen()) {
        Event _event;
        while (window.pollEvent(_event)) {
            switch (_event.type) {
            case Event::Closed:
                window.close();
                break;
                It should've but didn't work...the image file is in the  same directory as  the xcode file.
– Transformer
                May 9, 2020 at 9:15
                Maybe the problem is in your declaration of screen sizes. Re-write them with int screenwidth = 1024;  and int screenheight = 1024. And try returning with 0, instead of EXIT_SUCCESS. And, perhaps, that would work
– RockOGOlic
                May 9, 2020 at 9:23
                Starts flashing again with this in output: Failed to load image "/Users/myname/Desktop/SFML/sprite.png". Reason: Unable to open file Program ended with exit code: 255
– Transformer
                May 9, 2020 at 9:26
                Your installation folder with SFML must contain the .dll files that you would have to copy to your working directory of your project.  I am not sure how the installation is done on MacOS, but I can definitely say that something is not installed either correctly, or completely.  You can try and see, if the window draws, without your image. I have updated my answer, so you can check the code that must work. If not - look for tutorials on how to set it up properly.
– RockOGOlic
                May 9, 2020 at 9:39
        

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.