SFML community forums

Help => Graphics => Topic started by: notpedro on February 15, 2015, 09:37:33 am

Title: Loading a file from a folder in the current directory.
Post by: notpedro on February 15, 2015, 09:37:33 am
I would like to include an image that is in a folder in the current folder without including the full path using the loadFromFile() function.

How can I achieve this?
Thank you.
Title: Re: Loading a file from a folder in the current directory.
Post by: Jesper Juhl on February 15, 2015, 10:00:48 am
Use a relative path like "./filename".
Title: Re: Loading a file from a folder in the current directory.
Post by: excentio on February 15, 2015, 09:23:39 pm
On windows, you can use this

#include <windows.h>

string getPath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}
 

then write

Image img;
img.loadFromFile(getPath() + "\\myImg.png");
 
Title: Re: Loading a file from a folder in the current directory.
Post by: Jesper Juhl on February 15, 2015, 09:26:41 pm
Note that '/' works as a path seperator on both Windows and Unix and does not need to be escaped as "\\" (not to mention that '\' does not work in Unix, so annoying in platform agnostic code)...
Also, if you know that the resources are in your cwd (current working directory) (as returned by the standard getcwd()), then just using "./filename" is a lot simpler.
Title: Re: Loading a file from a folder in the current directory.
Post by: excentio on February 15, 2015, 09:28:56 pm
Note that '/' works as a path seperator on both Windows and Unix and does not need to be escaped as "\\"...

works too, but there's no sense in this, because this code is windows-only, he'll need to search another piece of code for another OS  :)
btw, that's strange, but on Visual Studio 12 thing like "./myImg.png" doesn't work, just tested this
Title: AW: Loading a file from a folder in the current directory.
Post by: eXpl0it3r on February 16, 2015, 01:56:07 am
It does work, it's just that VS default working directory is set to the directory that contains the project file.

Also "./" itself is not needed, "myimg.png" will work just as well.
Title: Re: Loading a file from a folder in the current directory.
Post by: notpedro on February 22, 2015, 02:26:41 am
Use a relative path like "./filename".

This does work for me unfortunately.
Title: Re: Loading a file from a folder in the current directory.
Post by: notpedro on February 22, 2015, 02:27:41 am
On windows, you can use this

#include <windows.h>

string getPath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}
 

then write

Image img;
img.loadFromFile(getPath() + "\\myImg.png");
 

Thanks for this answer, although Im slightly hesitant as I would have thought there was a more concise way of doing it.
Title: Re: Loading a file from a folder in the current directory.
Post by: Jesper Juhl on February 22, 2015, 09:19:52 am
You could do
 
chdir("dir/holding/resources");
img.loadFromFild("myfile.png");