Tuesday, December 11, 2007

Changing SDL's Working Directory on Mac OS X

The Mac OS X version of SDL sets the working directory to the directory containing the application bundle. This default behavior makes loading images and sounds more difficult because the images and sounds most likely reside in the application bundle's Resources folder. What would be nice would be to set the working directory to the Resources folder. How do you do this?

You change the working directory by modifying the method -setupWorkingDirectory: in the file SDLMain.m. You'll want to change the code in SDLMain.m in SDL's Xcode templates so your changes take effect for any new SDL projects you create. Just to be safe, you should comment out the original code or save a copy of it so you can go back to the original if things go wrong. Enter the following code in the -setupWorkingDirectory: method:


NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
[[NSFileManager defaultManager]
changeCurrentDirectoryPath
:resourcePath];


Thanks to Keith Bauer, aka OneSadCookie, for the source code to change the working directory.