Handmade Hero»Forums»Code
107 posts
How can .exe use assets from another directory
Edited by C_Worm on Reason: Initial post
Hey!

Im wondering if there is a way for my executable in my \build directory to find assets like textures (.jpg, .png) etc
that are NOT in the same directory as the executable.

is there some compiler switch?

Or does the assets always have to be in the same directory as the executable?
Simon Anciaux
1341 posts
How can .exe use assets from another directory
To do that you generally use the working directory plus relative paths.

For example if you can launch the executable from a command line, you CD to the directory you want to be the working directory and launch your executable from there.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
rem Executable is in D:\bin\
rem I want to launch it from D:\temp\

C:\> D:
D:\> CD temp
rem "D:\temp" is now the working directory (from were the program will execute).
D:\temp> ..\bin\executable.exe

rem Using "xxx.png" in the code of the program will search for the file in "D:\temp\".
rem You can use "path\to\a\sub\folder\xxx.png" in the code to access a sub folder of the working directory.
rem You can use "..\" in the code to move up the path. For example "..\parent\path\xxx.png" to access "D:\parent\path\xxx.png".


If you use Visual Studio to launch you program you can set the working directory in the project settings.

If you create a Windows Shortcut you can set the working directory in the "Start in" field.