I would really like to keep all my images and sounds etc all wrapped up
in my .app. Is there a good way of doing this with SDL? I noticed that
SDL seems to, perhaps for portability reasons, have its root directory
set to the same as that of the executable.
Yes. The standard place to put application data is in
MyApp.app/Contents/Resources/. We use the .app’s parent as the default
because that is more in line with other platforms.
You can instruct SDL to make this the working directory of the program
by editing the chdir() call in SDLMain.m.
Where can I keep my
media/resources in my project folder (I’m using Project Builder btw) so
that they get automatically inserted into the .app’s package?
You can put your files here as part of compiling your app by creating a
"Copy Files" build phase or “Shell Script” build phase for these items.
See the Project Builder docs for how this is done.
do I do the pathing then in the actual code?
Reference the files as if MyApp.app/Contents/Resources is the working
directory. For example, use “myimage.png” to reference
While I’m writing all my code on Mac OS X right now, I’d like to make
it as portable as possible, so it would be easy to recompile for
Windows/Linux. I’d appreciate suggestions on how I should organise the
file paths so that I can keep my media withing the .app for Mac OS X,
but within a single folder in the same folder as the executable for
That’s easy. Just make the change to SDLMain.m, and your app will “just
work” with the exact same file paths in the code that work in Mac OS X.
That’s because the working directory for windows will be the directory
that contains the executable file.
For Linux, the working directory is not necessarily the same as the
executable’s directory. So, for this target, you should probably use a
path prefix which is prepended to all paths before opening files (so
you can use the same paths everywhere, but the prefix is changeable).
You can hard-code this using a PATH_PREFIX macro:
#ifndef PATH_PREFIX // let people compiling your app specify the
if LINUX // made up macro
define PATH_PREFIX “/usr/local/share/myapp/”
define PATH_PREFIX “”
// note: C compiler will append the macro string with the specified
fopen (PATH_PREFIX “myimage.png”, “r”);
OR you can make this a run-time option. In this case, you want to
fallback to the hard-coded path if the prefix is not specified (for
example, through a command-line argument and/or environment variable).
gPathPrefix=getenv(“PATH_PREFIX”); // configurable
gPathPrefix=PATH_PREFIX; // hardcoded
// prepend prefix before opening each file
sprintf (path, “%s%s”, gPathPrefix, “myimage.png”);
fopen (path, “r”);
And, as usual, check for errors when opening files, and present clear
error messages. As a plus, on Linux print out the path prefix used and
tell the user how to change it.
Well, that’s how I think it should be/could be done. Others may have a
different opinion on the matter. I’ve seen many apps that use a system
like the ones I’ve described.
DarrellOn Wednesday, March 26, 2003, at 03:02 PM, joseph at mansefield.net wrote:
Date: Tue, 25 Mar 2003 20:34:32 +0000
From: Joseph Humfrey
To: sdl at libsdl.org
Subject: [SDL] Media in the .app package resources
Reply-To: sdl at libsdl.org