Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SFML2.0] Not loading my image  (Read 1712 times)

0 Members and 1 Guest are viewing this topic.

tnkr

  • Newbie
  • *
  • Posts: 4
    • View Profile
[SFML2.0] Not loading my image
« 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:





Thanks in advance

« Last Edit: April 27, 2012, 04:22:48 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: [SFML2.0] Not loading my image
« Reply #1 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)").
Laurent Gomila - SFML developer

tnkr

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [SFML2.0] Not loading my image
« Reply #2 on: April 27, 2012, 04:27:59 pm »
Alright it works now.
Thanks a lot!