Sunday, July 5, 2015

SDL 2 (and extensions) on XCode 6 Tutorial

Hey all,

So I ran into an issue recently involving using SDL on Mac OSX and XCode, and would like to provide a tutorial showing my findings.

A Summary:

 XCode is not the best IDE in terms of telling you exactly what's going on. Apple forces you to create an app using their frameworks that aren't exactly practical in terms of multi-platform game creation, circumventing this is a real pain and causes a lot of headache.

I'm going to assume that you have some familiarity with XCode, I may skip over some details I consider trivial, like: "how to navigate a project". There are enough tutorials as it is on that subject matter.

Long story short, finding out all of this on my own:


With that out of the way, let's dig in.

Getting Started:

1. Download XCode from the Mac App Store, it's not difficult, I'll wait here.
2. Install said XCode (lol this doesn't need to be said.)
3. In XCode, Go to File -> New -> Project, Application -> New Cocoa Application

4. Enter a name for your project, and create it! Yay! Step one!
5. See the files highlighted in the picture below? See them? Yeah? Delete them, XCode generates those and we don't need them. If you want test cases go ahead, but I'm not keeping them.

Delete these files.

Handling SDL:

1. Download SDL 2 here: SDL 2
2. Download any SDL 2 extensions that you plan to use.
Examples include: SDL_image, SDL_mixer, SDL_ttf
3. Drag and drop the .framework files from each of these downloads to /Library/Frameworks, congratulations, you just installed SDL 2!

This is what my /Library/Frameworks directory looks like.

XCode and SDL 2:

Now that you have XCode, SDL 2, and your project started, we can now configure the project itself! THE FUN PART! (For many definitions of fun.)

1. In XCode, click on your project file, navigate to "Build Phases", and add your SDL 2 libraries as such:

 You want to copy the SDL 2 libraries with your executable, else you user will need to have SDL 2 installed on their local machine, not ideal.
2. Next, go to "Build Settings" and set up you "Search Path" parameters as such:

3. Set LLVM as such: (Also in build settings)
Note the part about Prefix Headers, we're not using one.

4. Next, we need to kill the test build step, because who needs that?

Click that area, select the Test build phase and delete it!

5. Set "Enable Modules (C and Objective-C)" to No.
6. Boom! Done! That wasn't so bad right?

The Stupid Part

So, there's an interesting bug in XCode, where if you try to link a library with unsigned code, XCode will crash. Hard crash. As in, the app forcefully closes. It sucks, really bad. Unfortunately, SDL 2 and its extensions are all unsigned! So I will help you fix that.

The issue is referenced here: XCode crash

1. Open a Terminal and navigate to /Library/Frameworks (that's the 'cd' command for those who don't use the Terminal.)

Yes, my Terminal is black and blue like that.

2. Code Sign all of your SDL 2 frameworks (You may need to have XCode tools installed, there are tutorials on that as well.)

3. The command that you are looking for looks as such:

Modify as needed for other frameworks

Clapping Time!

Congratulations! You now have a project ready to write SDL related code in! You can reference SDL just like this:

Give yourself a pat on the back,  you just saved yourself a crap ton of time! If you have any questions let me know in the comments, and I'll adjust the tutorial accordingly! This is my first time writing a publicly accessible tutorial about project set up.

If you want to actually draws things, there are MANY tutorials on that subject matter, and would be another tutorial in its own right, for now, I'm gonna leave it to the setting up part.

Anyway, I hope I helped you out somehow, and thanks for reading!

No comments:

Post a Comment