Page 1 of 2

Need help: Loading images from certain paths

Posted: Sat May 14, 2011 7:26 pm
by StoveBacon
I'm getting back into programming so instead of using Allegro, because I know of its limited capabilities, I switched to SFML. I want to know how to load an image from my graphics folder which is in the projects folder. Heres the structure. Documents>Project>Project>Graphics|debug etc... This is what I have tried..

Code: Select all

Image.LoadFromFile("Graphics/test.png");
Any help would be appreciated... I know it's a very stupid question.

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 8:04 pm
by PaperDuckyFTW
Place the folder which holds the graphics inside the same folder as the .exe, much like you need to place the .dll in the same directory as the .exe

So for example, cause im bored
Documents > Projects > Awesome Game > Debug > game.exe
-------> makefile
-------> neededlibs.dll
------->Graphics
----->test.png

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 8:10 pm
by StoveBacon
So then I would do the

Code: Select all

Image.LoadFromFile("Graphics/test.png");
? That still didn't work, the command prompt said unable to open file.

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 8:50 pm
by short
KeithStoffel wrote:So then I would do the

Code: Select all

Image.LoadFromFile("Graphics/test.png");
? That still didn't work, the command prompt said unable to open file.
Does

Code: Select all

Image.LoadFromFile("./test.png");
work if your image is in the same directory as your executable?

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 8:51 pm
by StoveBacon
I'm still getting the unable to open file error

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 9:26 pm
by like80ninjas
If the location is project/project/graphics/file.png
then this should work "graphics/file.png", assuming you are running the game from your compiler. If you are running the exe from the release or debug folder (i'm assuming you are using VC ) you should add your graphics folder to those folders as well.

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 9:45 pm
by short
What does

Code: Select all

char* buf = (char*) malloc( sizeof(char) * 1000);
getcwd(buf, 1000);
fprintf(stdout, "cdir %s \n", buf);
display?

edit: with microsoft compilers I think you need to use _getcwd instead of getcwd

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:00 pm
by StoveBacon
getcwd/_getcwd identifier not found.

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:26 pm
by short
what OS are you on? If your windows I think you need to include direct.h, on unix/osx I think you need unistd.h

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:42 pm
by StoveBacon
cdir c:\users\user\documents\project\project\project

That is the output in the console.

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:46 pm
by short
KeithStoffel wrote:cdir c:\users\user\documents\project\project\project

That is the output in the console.
Is that the same directory your executable is in?

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:53 pm
by StoveBacon
exe is in: Documents>project>project>debug

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 10:56 pm
by short
KeithStoffel wrote:exe is in: Documents>project>project>debug
Try putting your image file in Documents>projects>projects>resources>test.png

Code: Select all

Image.LoadFromFile("../resources/test.png");
is that working for you?

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 11:00 pm
by StoveBacon
Well, there is no more unable to load file error, but I can't see the image, but that is more or less something probably wrong in my code that I will be able to figure out myself. Thanks for your help!!

Re: Need help: Loading images from certain paths

Posted: Sat May 14, 2011 11:01 pm
by StoveBacon
Ah yes, clearing the buffer before drawing it tends to not show anything.