Changing SDL’s Working Directory on Mac OS X

December 11th, 2007

Filed under: Game Development, Mac Development, SDL | 3 comments

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.


3 thoughts on “Changing SDL’s Working Directory on Mac OS X

  1. kaolin fire says:

    It took me following the advice on a couple of other sites and get stuff working that way… to realize the complete brilliance of the above code change. Thanks! 😀

  2. ?ukasz says:

    Thanks! It works!

    I added images to my app manualy using finder – option “Show Package Contents”. Works fine.

    But howto add images to xcode project? I don’t want to add images manualy after every compilation, but drag and drop image doesn’t work.

  3. Mark says:

    Lukasz,

    The easiest way to add an image to your Xcode project is to select the Resources folder from the Groups and Files list and right-click. A contextual menu will open. Choose Add > Existing Files to add the files. Files added to the Resources folder will get copied to the application bundle when you build the project.

    If you are adding a folder of images to your project, make sure you choose the Create Folder References for any added folders radio button.

Leave a Reply

Your email address will not be published. Required fields are marked *