Need some help: cross platform resource loading(Windows/Linux/OSX)

Need some help: cross platform resource loading(Windows/Linux/OSX)…

Hi,

I wrote an SDL based game for Windows.

I have a friend working on the Linux and Mac OSX version now.
He is having some difficulty in the porting to these platforms.

Here is a sample of my code:
*Spritebmp[0] = SDL_LoadBMP(“Graphics/Title Screen
Graphics/SimpleDirectmediaLayer_Logo.bmp”); *

The above code is the Windows implementation.
(on Windows platform, the resources are stored in a folder in the
executables folder)

How would we load resources on Linux and Mac OSX platforms ?
(solution must work on Windows, Linux, and Max OSX platforms)

Thanks in advanced!

PS - This is the game:
http://www.silentheroproductions.com/games_pc_windows_lettersfall_100percent.htm

Jesse "JeZ+Lee"
slnthero at aol.com
Silent Hero Productions®
http://www.SilentHeroProductions.com

Hi Jesse,

My primary experience is with Mac OS X, so let me comment on that.

On Mac OS X, executables and their related resources are typically
grouped together into application “bundles”. These bundles are just
special folders, but they appear as applications to users (who can
double click them to launch them). In XCode you can add a resource to
the application bundle by placing it under the “resources” grouping in
the side bar. After compilation, XCode will place the resource into
the Contents/Resources directory of the application bundle.

To locate the resources within the bundle you can use the Objective-C
class NSBundle or you can use CFBundle if you’re working in straight
C. I guess you could also locate the resources manually within your
bundle, if you were so inclined.

To make your app look like a “real” Mac OS X application, you should
keep the executable in an application bundle, but if you’d prefer, you
could still keep your resources in external folders in the same way
that you are doing on windows. Just keep in mind that when you launch
your Mac OS X application, the current working directory will be the
executable’s directory (which is located within the application
bundle) rather than the directory of the application bundle. So on
Mac OS X you’ll have to go through the extra step of changing the
working directory. This can be done with chdir or by the following
method using CFURL and CFBundle:

 CFURLRef resourceURL =   

CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
char resourcePath[4096];
CFURLGetFileSystemRepresentation((__CFURL*)resourceURL, TRUE,
(UInt8*)resourcePath, 4096);
chdir(resourcePath);
CFRelease(resourceURL);

  • HolmesOn May 22, 2008, at 4:27 AM, Jesse P. wrote:

Need some help: cross platform resource loading(Windows/Linux/OSX)…

Hi,

I wrote an SDL based game for Windows.

I have a friend working on the Linux and Mac OSX version now.
He is having some difficulty in the porting to these platforms.

Here is a sample of my code:
*Spritebmp[0] = SDL_LoadBMP(“Graphics/Title Screen Graphics/
SimpleDirectmediaLayer_Logo.bmp”); *

The above code is the Windows implementation.
(on Windows platform, the resources are stored in a folder in the
executables folder)

How would we load resources on Linux and Mac OSX platforms ?
(solution must work on Windows, Linux, and Max OSX platforms)

Thanks in advanced!

PS - This is the game:
http://www.silentheroproductions.com/games_pc_windows_lettersfall_100percent.htm

Jesse "JeZ+Lee"
slnthero at aol.com
Silent Hero Productions®
http://www.SilentHeroProductions.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Hi Jesse,

Oops, my code segment was not totally right. The code I gave you was
for changing the current working directory to the the resources
directory of the bundle. This is what you need to do if you decide to
keep your resources in the bundle, along with the executable.

In the case that you’d like to keep your resources in an external
folder, you can do what I sent you, but then add the line
chdir("…/…/…/…/"); to set the working directory a few directories
higher. I don’t know if there is a better way than this.

  • HolmesOn May 22, 2008, at 10:45 AM, Holmes Futrell wrote:

Hi Jesse,

My primary experience is with Mac OS X, so let me comment on that.

On Mac OS X, executables and their related resources are typically
grouped together into application “bundles”. These bundles are just
special folders, but they appear as applications to users (who can
double click them to launch them). In XCode you can add a resource
to the application bundle by placing it under the "resources"
grouping in the side bar. After compilation, XCode will place the
resource into the Contents/Resources directory of the application
bundle.

To locate the resources within the bundle you can use the Objective-
C class NSBundle or you can use CFBundle if you’re working in
straight C. I guess you could also locate the resources manually
within your bundle, if you were so inclined.

To make your app look like a “real” Mac OS X application, you should
keep the executable in an application bundle, but if you’d prefer,
you could still keep your resources in external folders in the same
way that you are doing on windows. Just keep in mind that when you
launch your Mac OS X application, the current working directory will
be the executable’s directory (which is located within the
application bundle) rather than the directory of the application
bundle. So on Mac OS X you’ll have to go through the extra step of
changing the working directory. This can be done with chdir or by
the following method using CFURL and CFBundle:

CFURLRef resourceURL =   

CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
char resourcePath[4096];
CFURLGetFileSystemRepresentation((__CFURL*)resourceURL, TRUE,
(UInt8*)resourcePath, 4096);
chdir(resourcePath);
CFRelease(resourceURL);

  • Holmes

On May 22, 2008, at 4:27 AM, Jesse P. wrote:

Need some help: cross platform resource loading(Windows/Linux/OSX)…

Hi,

I wrote an SDL based game for Windows.

I have a friend working on the Linux and Mac OSX version now.
He is having some difficulty in the porting to these platforms.

Here is a sample of my code:
*Spritebmp[0] = SDL_LoadBMP(“Graphics/Title Screen Graphics/
SimpleDirectmediaLayer_Logo.bmp”); *

The above code is the Windows implementation.
(on Windows platform, the resources are stored in a folder in the
executables folder)

How would we load resources on Linux and Mac OSX platforms ?
(solution must work on Windows, Linux, and Max OSX platforms)

Thanks in advanced!

PS - This is the game:
http://www.silentheroproductions.com/games_pc_windows_lettersfall_100percent.htm

Jesse "JeZ+Lee"
slnthero at aol.com
Silent Hero Productions®
http://www.SilentHeroProductions.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

how can this be done with auto tools. making the *nix build seems easy.
not sure how to make a ppc|x86|univeral build with autotools. i was
assuming i could add osx support to the makefile so i could run
./configure && make bundle

thanks

mattOn Thu, 22 May 2008, Holmes Futrell wrote:

Hi Jesse,

My primary experience is with Mac OS X, so let me comment on that.

On Mac OS X, executables and their related resources are typically grouped
together into application “bundles”. These bundles are just special folders,
but they appear as applications to users (who can double click them to launch
them). In XCode you can add a resource to the application bundle by placing
it under the “resources” grouping in the side bar. After compilation, XCode
will place the resource into the Contents/Resources directory of the
application bundle.

To locate the resources within the bundle you can use the Objective-C class
NSBundle or you can use CFBundle if you’re working in straight C. I guess
you could also locate the resources manually within your bundle, if you were
so inclined.

To make your app look like a “real” Mac OS X application, you should keep the
executable in an application bundle, but if you’d prefer, you could still
keep your resources in external folders in the same way that you are doing on
windows. Just keep in mind that when you launch your Mac OS X application,
the current working directory will be the executable’s directory (which is
located within the application bundle) rather than the directory of the
application bundle. So on Mac OS X you’ll have to go through the extra step
of changing the working directory. This can be done with chdir or by the
following method using CFURL and

In the case that you’d like to keep your resources in an external folder,
you can do what I sent you, but then add the line chdir("…/…/…/…/"); to
set the working directory a few directories higher. I don’t know if there
is a better way than this.

What I found to be the most portable was to have functions to return
path prefixes. The thing is, there can be many different places to put
things, it might not be just one place. For example, there is per-user
or system-wide stuff, platform-independent or platform-specific stuff,
etc… It helps to have a concept of “paths” (an ordered list of
different prefixes to try).

Then, on a platform like Mac OS X, you might have preferences in
~/Library/Preferences, saved games in ~/Library/Application\ Support,
image resources in /Content/Resources/… (which needs to
be obtained with a bit of code like that the previous poster gave)…
On Linux, the first two might both be ~/.mygamename (with a mkdir()
slipped in when files are opened for writing, to make things things
are all right), and the images be in /usr/share/mygamename (with the
"/usr/share" part taken from autoconf’s datadir macro at build time).

I like to have a small family of functions that return something like
an SDL_RWops*, say OpenDataFile(), OpenResourceFile(), etc… The
configuration file is something I often end up treating specially,
with a LoadConfig()/SaveConfig() pair that returns and takes a struct
with the configuration, and keep all the magic in there, since it’s
fairly specific from one platform to the other, if you want to play
nice (save as a property list on Mac OS X, store in the registry on
Windows, etc), but you can make it more generic too.

Not rocket science, but needs to be done…On Thu, May 22, 2008 at 1:58 PM, Holmes Futrell wrote:


http://pphaneuf.livejournal.com/

how can this be done with auto tools. making the *nix build seems easy. not
sure how to make a ppc|x86|univeral build with autotools. i was assuming i
could add osx support to the makefile so i could run ./configure && make
bundle

It’s definitely possible to do it all in the Makefile (it’s all just a
bunch of mkdirs and cps, nothing magical about bundles), although it
might involve some heavy lifting to get it going at first. One tip:
you can have the property list generated for you by the configure
script (to put things like the version number and stuff), and then you
can just copy the generated file into place when you build the bundle.On Thu, May 22, 2008 at 2:30 PM, wrote:


http://pphaneuf.livejournal.com/

how can this be done with auto tools. making the *nix build seems easy. not
sure how to make a ppc|x86|univeral build with autotools. i was assuming i
could add osx support to the makefile so i could run ./configure && make
bundle

Oh, another tip: for the implementation of the functions I suggested,
Windows is pretty easy to distinguish from Linux and Mac OS X, but to
distinguish between the two latter, have an autoconf test for the
CFBundleGetMainBundle function, and use that in your source code to
switch between one behaviour and the other.On Thu, May 22, 2008 at 2:30 PM, wrote:


http://pphaneuf.livejournal.com/

You might check out CMake. It has some built-in support for bundles on
Mac OS X. It also knows how to build universal binaries.

-EricOn 5/22/08, Pierre Phaneuf wrote:

On Thu, May 22, 2008 at 2:30 PM, wrote:

how can this be done with auto tools. making the *nix build seems easy.
not
sure how to make a ppc|x86|univeral build with autotools. i was assuming
i
could add osx support to the makefile so i could run ./configure && make
bundle

While it doesn’t entirely answer your question, PhysFS was designed to
mitigate this kind of problem.

Of course, you’re still gonna have your main files in different places
on different OS, but once you establish a base directory, PhysFS makes
everything else more simple.

Eddy

Jesse P. wrote:> Need some help: cross platform resource loading(Windows/Linux/OSX)…

Hi,

I wrote an SDL based game for Windows.

I have a friend working on the Linux and Mac OSX version now.
He is having some difficulty in the porting to these platforms.

Here is a sample of my code:
*Spritebmp[0] = SDL_LoadBMP(“Graphics/Title Screen
Graphics/SimpleDirectmediaLayer_Logo.bmp”); *

The above code is the Windows implementation.
(on Windows platform, the resources are stored in a folder in the
executables folder)

How would we load resources on Linux and Mac OSX platforms ?
(solution must work on Windows, Linux, and Max OSX platforms)

Thanks in advanced!

PS - This is the game:
http://www.silentheroproductions.com/games_pc_windows_lettersfall_100percent.htm

Jesse "JeZ+Lee"
slnthero at aol.com
Silent Hero Productions®
http://www.SilentHeroProductions.com


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

how can this be done with auto tools.
makin/Developer/SDKs/MacOSX10.4u.sdkg the *nix build seems easy. not sure
how to make a ppc|x86|univeral build with autotools. i was assuming i could
add osx support to the makefile so i could run ./configure && make bundle

export CC="gcc -arch i386 -arch ppc -isysroot
/Developer/SDKs/MacOSX10.4u.sdk"
export CXX=“g++ -arch i386 -arch ppc -isysroot
/Developer/SDKs/MacOSX10.4u.sdk”
./configure
make

It could fail if configure tries to figure endianness or if it does some
strange dependency check.

About the “base path” problem:

  1. Use binreloc on linux: http://autopackage.org/docs/binreloc/
  2. Look inside SDLMain.m on OSX to set the path it uses in the chdir() call
    to point inside your application bundle in the directory where you place
    your datafiles.On Thu, May 22, 2008 at 8:30 PM, wrote:


Bye,
Gabry