SDL Tips for Mac OS X
SDL’s most compelling feature is its cross-platform compatibility. You can use the same source code to write a game for Linux, Mac OS X, Windows, and any other system SDL supports. When people write a game with SDL on Linux or Windows and try to build a Mac version of your game, they learn that Mac OS X has some subtle differences from Linux and Windows. The three tips in this post should help people create Mac versions of their SDL games.
Use the Xcode Project Templates
If you haven’t done so already, go to the SDL website and download it. At the SDL download site, you will see both runtime and development libraries for Mac OS X. Download both libraries. The runtime libraries contain the SDL framework. The development libraries contain documentation and Xcode project templates.
The Xcode project templates do two nice things for you when you build the project. First, the templates create a proper Mac application bundle for you. Second, the templates copy the SDL framework to the application bundle. Including the SDL framework with your game lets people play your game without having to install SDL.
Adding Files to the Game
Games contain many external files, such as graphics, sound, and data files. Mac games store these files in the Resources folder inside the game’s application bundle. If you add graphics, sound, and data files to your project’s Resources folder, they will be copied to the application bundle’s Resources folder when you build your project.
Go to the Groups and Files list on the left side of Xcode’s project window. The top entry should be the name of your project. Click the disclosure triangle next to the project name to see a series of folders. Right-click the Resources folder and choose Add > Existing Files. Select the files you want to add and click the Add button.
A second sheet will open. If you are adding individual files, click the Add button. If you’re adding a folder of files, click the Create Folder References for any added folders radio button before clicking the Add button. Creating a folder reference copies the folder to the Resources folder inside the application bundle when Xcode builds your project.
Changing the Working Directory
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 usually reside in the application bundle’s Resources folder. You can make file loading simpler by changing the working directory to the Resources folder.
Open the file SDLMain.m and modify the setupWorkingDirectory: method. The following code changes the working directory to the application bundle’s Resources folder:
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
[[NSFileManager defaultManager] changeCurrentDirectoryPath:resourcePath];
By changing the working directory to the application’s Resources folder, you should be able to use the same code to load files on Linux, Mac OS X, and Windows.
Thanks for the tips – they have helped me alot in learning on how to port to the Mac OSX. I have managed to get my files copied into the ‘resources’ part of the app bundle, but it throws them all into a singe directory instead of the directory structure for my data. Any hints on how to do this?
Phil,
As a test I tried adding two folders, one named Images and one named Sounds, to an SDL project. After building the project I checked the application package. Both folders were in the Resources directory.
The first thing I did was move the Images and Sounds folders inside the Xcode project’s folder. Next, I added the folders to the project. After clicking the Add button, a sheet opened. I clicked the Create Folder References for any added folders radio button and clicked the Add button. In the Groups and Files list on the left side of Xcode’s project window were two blue folders: Images and Sounds. That’s how I got the folders to be added to the Resources folder inside the application bundle.
Examine your project’s Copy Bundles Resources build phase to see how the files are being copied to the Resources folder. In the Groups and Files list, there should be a Targets entry with a disclosure triangle. Click it and you will see the name of your target with another disclosure triangle. Click that disclosure triangle. Now you will see a collection of build phases. There should be one named Copy Bundle Resources. Click the disclosure triangle next to Copy Bundle Resources. Make sure there is a blue folder for each folder you’ve added to the project.
I hope this helps.
Hi Mark,
Found my problem. I didn’t click the “Create Folder References” option so my data folders were displaying as yellow folders (groups) instead of real folders (blue).
Thanks so much for the speedy response, your post lead me in the right direction.
Cheers,
Phil.