SFML community forums

Help => Graphics => Topic started by: tnkr on April 27, 2012, 04:14:49 pm

Title: [SFML2.0] Not loading my image
Post by: tnkr on April 27, 2012, 04:14:49 pm
Hello,

the problem is that whenever I try to open and display an image, it says it failed to open the image and also crashes due to access violation errors. Note that SFML just works properly without the image loading and displaying part. My source code:

#include "stdafx.h"
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(300, 200), "SFML works!");
    sf::Text text("Hello SFML");

    sf::Texture texture;
    if (!texture.loadFromFile("image.jpg"))
                return EXIT_FAILURE;
    sf::Sprite sprite(texture);

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

        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}
 

My image.jpg is located in the same folder as the executable. I also tried using a full path but that didn't work out either. So whenever I try to run this I get these errors:

(http://i.imgur.com/9MWJP.png)

(http://i.imgur.com/sbl8o.png)

Thanks in advance

Title: Re: [SFML2.0] Not loading my image
Post by: Laurent on April 27, 2012, 04:24:45 pm
By default, when you run the program from Visual Studio, the working directory is the project directory, not the executable directory. So all your relative paths are wrong. You must change the working directory in your project settings (under "Debugging"; put "$(TargetDir)").
Title: Re: [SFML2.0] Not loading my image
Post by: tnkr on April 27, 2012, 04:27:59 pm
Alright it works now.
Thanks a lot!