Kind of a noob question - finding artwork within my .app??

Nibbie
Posts: 1
Joined: 2010.03
Post: #1
Hi all,

I feel really stupid here - I have only recently started coding on OSX and I tried searching the forums (I'm sure the answer is already here, I'm just using the wrong words for my search) - but:

I'm storing my graphical assets within the .app package (I'm using SDL using one of their pre-built templates for a project within XCode) - I know I can use argv[0] and get the full path to the executable then do some work to strip back to where I store my artwork - I was wondering is there an easier way anyone knows of to get the full path up to the .app? so I could end up with a variable that has something like (this is an experiment using OpenGL for 2D work, hence the name 2dogltest):

/Users/nethfel/Documents/LearnSDL/SDL_NETtest/2dogltest/build/Debug/2dogltest.app

instead of what I get with argv[0]:

/Users/nethfel/Documents/LearnSDL/SDL_NETtest/2dogltest/build/Debug/2dogltest.app/Contents/MacOS/2dogltest

Thoughts appreciated - I'm sure there is a way. I could take getcwd and add my app name to it, but then arises a problem if someone renames the actual app for some odd reason or another...
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
There is a method in SDLMain.m that sets the working directory to the application's enclosing folder. The following snippet sets the working directory to the app's resources directory instead. Presumably that is where you are putting the game's assets in the bundle.

Code:
- (void) setupWorkingDirectory:(BOOL)shouldChdir
{
    if(shouldChdir){
        char parentdir[MAXPATHLEN];
        CFURLRef url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
        if(CFURLGetFileSystemRepresentation(url, true, (unsigned char *)parentdir, MAXPATHLEN))
            assert(chdir(parentdir) == 0);   /* chdir to the resources directory */
        
        CFRelease(url);
    }
}

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Moderator
Posts: 452
Joined: 2008.04
Post: #3
OneSadCookie usually points people to this blog post: http://blog.onesadcookie.com/2007/12/fin...files.html

Howling Moon Software - CrayonBall for Mac and iPhone, Contract Game Dev Work
Quote this message in a reply
Post Reply