Phil Hassey - game dev blog
Phil Hassey as Snidely Whiplash
"You can't buy awesomeness.
You're born that way."

Transitioning to Xcode 4.0.x

I’m working on a new game project that I’m hoping to release sometime in the next month or so! I’m taking a break from Dynamite to do something a bit more wacky 🙂 For these posts I’m going to be referring to my project as “mygame” since I’m not ready to announce the name of this super-amazing project yet.

Anyway, I downloaded Xcode 4.0.2 the other day so I could get going with the latest iOS tools. I’m going to be using more iOS features in this game than ever before, so I want to be sure I’m up to date with how things are supposed to be done. Xcode, however, has quite a few changes, so here’s the documentation of some of the common things I do as an Xcode user that changed.

1. Adding CFLAGS to my project “-DMYGAME_IOS_BUILD, etc”

– Click on “mygame” at the top left of the project tree hierarchy.
– Click on the “Build Settings” tab in the main frame.
– Click on the “All” button to reveal all settings.

From there you can find the “Other C Flags” setting. Apparently Xcode 4 has “Schemes” (the name of your project) as well as “Builds” (Debug / Release) as well as “Destinations” (iPhone / iPad / Simulator).

2. Adding more Frameworks to the project

I always have a handful of frameworks I need to add. In Xcode 3, you could right-click and click on “Add Existing Frameworks” to pick from a list. This is no longer the case. But doing the same is pretty easy:

– Expand the “Frameworks” from the project tree.
– Right-click on one of the frameworks.
– Select “Show in Finder”
– Finder will open, and will show the folder where all the frameworks are.
– Drag and drop a framework from Finder to the Frameworks in your project

3. Working with the new OpenGLES iOS template

Every few releases of Xcode, Apple changes up the OpenGLES template. I do wonder if I should just copy over it with my own stuff? Maybe, but I figure Apple knows best or whatever, and I always adapt to the latest template for new projects. This one has me a bit more confused than usual since it seems to use more Interface Builder type stuff and has a mygameViewController object in it as well (and corresponding .m and .h files.)

– At this point I knew since I was going to be using GameCenter and IAP in this game I might as well learn a bit more ObjectiveC. I’ve thus far managed to NOT learn it in the 3 years of iOS coding I’ve been doing. So, to kick things off I figured I’d check out what the iOS development videos might have for me. Unfortunately, the newest videos are from Apple’s 2009 tour. Kind of sad, I mean, 2009 was back before iPads even existed. So those videos are certainly not going to help me deal with the new fancy in Xcode 4 and building Universal apps.

– Instead I found that the typed documentation was much more helpful. I clicked into “Tools for iOS Development” to see what they’d have to say about Xcode 4.

– From perusing a few documents I got the feeling that to use GameCenter, etc, I was going to need a ViewController. Since the new OpenGLES template includes one, I guess that is good. If there are any iOS devs reading this who want to point me towards some helpful tutorials so I can understand what I’m doing better, that would be great. As-is, I am sort of continuing in my usual blind-cut-and-paste-and-hope type stupor with this stuff.

– I hacked out all the GLES2 code, I still write all my code using GLES1 .. the advantage that gives me is that my code will work simply on pretty much any platform. And I finally after 3 years understand GLES1 pretty well. I suppose I might get into ES2 eventually, but not yet 🙂 I also like that GLES1 code can work on the desktop with almost no changes. I hear shaders can add complication to my life on the desktop. Again, if anyone wants to link me to useful commentary on that, I wouldn’t mind!

4. Hacking my game onto the iOS app

I always code my games in C/C++. What I’ve found is the easiest way to get it to work with iOS is to rename all the .m files to .mm so that they are C++ aware ObjectiveC. Then I have a set of wrapper functions for my game that the ObjectiveC code can call.

Okay, so this is really cool, Xcode 4 now detects if you type in an invalid function name, and suggests a replacement function name 🙂 And it worked!

5. Making the game Universal

Xcode 4 seems to make this pretty easy. I just went into the Summary tab and switched mygame over to Universal and it all seemed to work pretty well. I only had to add these lines to the top of my drawFrame method in my ViewController:

CGRect screenFrame = [[UIScreen mainScreen] applicationFrame];
game_resize(screenFrame.size.width,screenFrame.size.height);

Here’s the blog post that helped me with that.

6. Making the game Retina enabled

I just used this code snippet at the end of my EAGLView initWithCoder (I believe I got it from the Fargoal folks):

      float scale = 1;

      if([[UIScreen mainScreen] respondsToSelector:
              NSSelectorFromString(@"scale")])
      {
              if([self respondsToSelector:
                      NSSelectorFromString(@"contentScaleFactor")])
              {
                      scale = [[UIScreen mainScreen] scale];
                      self.contentScaleFactor = scale;
              }
      }

Towards the end of the day I spent some time mucking with orientation stuff. That always confuses me! And that’s all for today!

-Phil

P.S. Changing where the builds are made:

By default the builds are put in ~/Library/Developer/Xcode/DerivedData .. they used to be placed within my project which is what I prefer. This can be changed by going into Xcode > Preferences > Locations > Build Location > Place build products in locations specified by targets

2 Responses to “Transitioning to Xcode 4.0.x”

  1. Arun Says:

    Hi,
    I am trying to create a Document-based application in Lion OS. In the .nib file i am using NSBrowser, i set the maximum column as 3 and Autosizing in all the direction. When i run the application i am getting only 2 column but after selecting or resizing the window i am able to see all the 3 columns.

    Please let me know that it is a Lion OS issue or Xcode issue.

    Thanks in Advance,
    Arun

  2. philhassey Says:

    Hi, I don’t know 🙂 I really don’t understand the .nib stuff myself …