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,478
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: 445
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 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  finding a training center - Heeeelp Glauter 1 2,463 Dec 26, 2011 12:30 PM
Last Post: zenkimoto
  link redeclared as different kind of symbol in [...] works in simulator, not device sefiroths 8 7,398 Jul 7, 2011 12:43 AM
Last Post: sefiroths
  Finding an object by name markhula 2 3,911 Mar 29, 2011 08:08 AM
Last Post: markhula
  Finding the closest point to (x1,y1) in array of [x1,y1,z1,x2... mikey 17 7,886 Aug 28, 2009 08:12 AM
Last Post: mikey
  Noob: Accessing Structures from Cocoa Classes MikeC 15 6,575 Oct 19, 2007 02:42 PM
Last Post: MikeC