sdl file open paths

Feb 21, 2012 at 12:35am
closed account (zwA4jE8b)
Hi,
I cannot figure out how to load files (using sdl) that are in a different directory. I am currently building in windows 7.

So my .exe is in the root folder, in here are 3 more folders, image, sound, and ttf.

img_original = IMG_Load("\\image\\Robo-spritesheet-x95-y120.bmp");

as well as

img_original = IMG_Load("/image/Robo-spritesheet-x95-y120.bmp");

does not work. same for the sound and font.

How does one load files in subdirectories?

thank you.
Feb 21, 2012 at 12:43am
IDEs often use a different working directory than the directory of the executable file.
Feb 21, 2012 at 12:45am
closed account (zwA4jE8b)
Do you by chance know how to specify (in code) the directory? Or specify that the folder containing the exe is the working directory? I know plenty of games have their resources in separate folders.

I am using eclipse c++ btw.
Last edited on Feb 21, 2012 at 12:53am
Feb 21, 2012 at 12:56am
closed account (zwA4jE8b)
Thank you for the hint about IDE's I found it in eclipse how to set the working directory for the .exe
Feb 21, 2012 at 1:05am
closed account (zwA4jE8b)
Is there a variable that is for 'this' directory. I see the ones for program files, windir, system, and such, but cannot find what environment variable will set the CWD to the directory of the .exe
Feb 21, 2012 at 1:17am
The "current directory" is whatever directory the program is run from on the commandline. When you double-click on an exe to run it in Windows Explorer, this puts the current directory as the exe directory.

It is usually be safe to assume that the current directory will be the exe directory at startup. Many programs expect to run that way, and if you don't run them that way they will usually have lots of failures at startup.

The big exception is when you run a program from an IDE's debugger. For some reason (which I never understood), IDEs like to launch the program from a directory other than the exe directory. Which leads to the troubles you're having.

As for solutions:

1: Change your project settings in your IDE so that it launches the debugger from the exe directory. I have no idea how to do this in Eclipse since I never use it, but I'm sure it can be done.

2: When developing, move all of your necessary files to the current directory, rather than putting them in the exe directory. When you release the game, you can throw all the files back in the exe directory (remember that when you run from explorer, the exe directory will be the current directory -- the problem is only when you run it from the IDE, which users won't be doing)

3: Put code in your program so that it looks for files in the exe directory, rather than the current directory. I don't recommend this approach at all since it might conflict with what your users want or expect, is non-portable, and is unnecessary additional code.



On Windows there are 3 functions which help with this:

1) GetCurrentDirectory http://msdn.microsoft.com/en-us/library/windows/desktop/aa364934%28v=vs.85%29.aspx
2) SetCurrentDirectory http://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx
3) GetModuleFileName http://msdn.microsoft.com/en-us/library/windows/desktop/ms683197%28v=vs.85%29.aspx


GetModuleFileName gives you the exe directory if you give it NULL for the module handle.

Note that all 3 are Windows only and are nonportable. And again you shouldn't really need these because running from the current directory is pretty normal behavior for all programs (hence why the concept of a current directory exists)


--------------------------------------
When using directories in code:

- Prefer '/' over '\\'. '/' works on Windows and pretty much everything else. '\\' works only on Windows and on nothing else. Also '/' tends to be less error prone because \ is the escape character.

- Do not start directories with a '/' character. If you try to open "/path/file", on Unix, that looks in the root directory which is not relative to the current directory. If you want to look in a directory that's in the current directory, use "path/file".
Last edited on Feb 21, 2012 at 1:19am
Feb 21, 2012 at 1:56am
closed account (zwA4jE8b)
I am not using a the debugger, I am building a release. When I move the exe into a folder with the above said subdirectories, it fails to load. I found that eclipse will let you specify the environmental vars for your build but Im not sure which, if any, are the correct ones

${ProgramFiles}\Robo-venger\
like this will set the directory to the machines Program Files\Robo-venger folder, but I want it to be more vague than that and find a variable that just sets it to whatever directory the exe is in.

It is usually be safe to assume that the current directory will be the exe directory at startup.
I also believed this but when code img_original = IMG_Load("/image/Robo-spritesheet-x95-y120.bmp"); into my game, the exe never finds the images.
Feb 21, 2012 at 2:01am
I found that eclipse will let you specify the environmental vars for your build but Im not sure which, if any, are the correct ones


Do not mess with environment variables. That is sooooooooo the wrong path to head down.

I also believed this but when code img_original = IMG_Load("/image/Robo-spritesheet-x95-y120.bmp"); into my game, the exe never finds the images.


See the end of my post:

I wrote:
Do not start directories with a '/' character.


ditch the '/' prefix: IMG_Load("image/Robo-spritesheet-x95-y120.bmp");
Feb 21, 2012 at 2:06am
closed account (zwA4jE8b)
Awesome, that works. I thought I tried that before but i guess not.
Topic archived. No new replies allowed.