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

Author Topic: SFML 2.0 requires full file path...  (Read 8793 times)

0 Members and 1 Guest are viewing this topic.

GSfml

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
SFML 2.0 requires full file path...
« on: April 25, 2012, 12:52:40 pm »
Hello, I have noticed that whenever you specify something like a texture, sound, etc. You have
to specify the full file path for it to work. :) for example this will not work:
sf::Texture texture;
texture.loadFromFile("image.png");
 
This doesn't work, I made sure I put it in the same directory as the executable. It just says failed to load image.
Reason: could not find file. You have to do it this way for it to work:
sf::Texture texture;
texture.loadFromFile("C:/Users/Gwhiz/Documents/test/bin/Debug/image.png");
 
Is this a bug, or is it supposed to be like this? :-\

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: SFML 2.0 requires full file path...
« Reply #1 on: April 25, 2012, 01:07:06 pm »
The executable directory is not where relative paths are searched for. They are searched relatively to the current working directory. When you run your executable from your IDE (Code::Blocks, Visual Studio, ...), the working directory might be the project directory instead, for example.

A quick solution is to change the working directory in your project settings, to make it match the executable directory. But a more robust solution would be to retrieve the executable path programmatically and append resource paths to it so that you end up using well-defined absolute paths, and don't rely on the current working directory which might be anything.
Laurent Gomila - SFML developer

GSfml

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: SFML 2.0 requires full file path...
« Reply #2 on: April 25, 2012, 01:15:50 pm »
It works, thanks! ;D I didn't expect such a quick reply to my question! This is part
of the reason I stopped using SDL and chose SFML over it. :)