May 2010

Core Animation Performance Tips

In my copious free time, I've been working on a videogame for the iPad. Friends and family may interject here that it seems like I'm always working on a videogame in my free time, but I've never actually finished one. This time is different. Really.

All of my personal projects are intended primarily to be interesting and fun for me. I gave myself a couple of technical constraints to keep things challenging:

  • All the code is well-factored idiomatic Objective-C. Unlike a lot of iPhone/iPad game programmers, I'm not writing all the guts in low-level C or C++ and then sprinkling a minimal amount of Cocoa on top to interface with the OS.
  • I'm using Core Animation as my "engine", rather than the OpenGL ES API or an off-the-shelf gaming engine. (Note: My game only needs a couple dozen sprites.)

So far, things have worked out well. I was worried that using Objective-C and Core Animation might lead to performance issues on the iPad, but that hasn't been the case. I have run into a couple of issues with Core Animation that were pretty easy to fix.

Layer Creation Is Expensive

My game's "sprites" are just Core Animation layers. When I initially implemented the game, I was creating layers and adding them to the view as needed, and then deleting them when they were no longer needed. This turned out to cause problems; a few frames would get dropped whenever a layer was created.

The fix for this was to create a pool of layers and add them all to the view at startup, and reuse those layers as needed. Unused layers get their hidden property set false.

Watch Out for Misaligned Images

If you run an app with Instruments with the Core Animation tool, it has an option to highlight "misaligned images." I couldn't find a lot of information on this, but apparently if you give layers positions that are not perfectly aligned to pixels, Core Animation does some anti-aliasing when rendering those layers, which degrades performance.

The easy fix is to just round all positions to whole pixels, via something like this:

- (void) setSpritePosition:(CGPoint)position {
    CGPoint alignedPosition;
    alignedPosition.x = floorf(0.5f + position.x);
    alignedPosition.y = floorf(0.5f + position.y);
    sprite.position = alignedPosition;
}

This got rid of most of my misaligned images, but I do still have some sprites that get rotated or scaled via a transform, and such images are always misaligned, unless they are rotated by some multiple of 90 degrees. Thankfully, I only have a few such sprites.

My iPad Review

So, I've had an iPad for about a month and a half. Here are my impressions:

Overall, it's really nice. It fills the need for a little Internet-connected device that lets me watch video, read books, read online news, and browse the web. I used to keep my old 13-inch MacBook next to the couch for these purposes, but that MacBook is now in a closet. I've also put away my Sony Reader that I kept on the nightstand.

I'm using it at work a lot more than I expected. My workstation has two monitors, but I find it handy to have programming documentation or technical books open on my iPad for reference. It essentially gives me a third monitor.

We take a lot of long car trips, and my wife really likes reading and web browsing with her 3G iPad in the passenger seat. It's a better experience than her MacBook Air.

The iPad is not a replacement for a laptop. When I need to do a lot of typing, or switch back and forth between applications, then I put down the iPad and pick up my laptop. But picking up a laptop is starting to seem like a chore. The iPad is not heavy, it's not hot, it doesn't have a whirring fan, it doesn't require me to assume an uncomfortable posture.

If you want a "computer," don't get an iPad: you'll be disappointed. Think of it more as a portable videogame console, or a Kindle on steroids. Saying that it is just a big iPhone is essentially correct, although many of the people who say that seemingly have no idea how useful a big iPhone could be.

The iPad is heavier than I expected. This is usually not a problem, but when playing any of those accelerometer-based games for an extended period, my arms get tired. On the plus side, I guess this gives me some exercise that I've been missing.

I use the Amazon Kindle app for iPad more than I use iBooks. I like the fact that with Kindle, I'm not locked in to Apple-only platforms. iBooks is prettier than the Kindle app, but the Kindle app is good enough. I need to stress here that I am talking about the Kindle application for iPad, not a Kindle device, which looks laughably archaic next to an iPad.

My only real complaint about the iPad is lack of multitasking, but that should be addressed in a few months with a new iPhone OS revision. (I have the iPhone OS 4 beta on my phone, and really, really miss the new features when using my iPad.)

In the month-and-a-half I've had the iPad, there has only been one instance where I went to a web page that didn't work due to lack of Flash. So that's not really a problem.

I've got the Apple iPad case. It is functional, but it is ugly.

My iPad has survived one fall from a countertop to the floor, without any ill effects.

I hope that some nice Android or Palm tablets appear soon. Competition and choice would be good, but right now, there is really no competitor to the iPad. It does what it does better than anything else can.

As a software developer, I'm excited by the new user-interface expectations being ushered in by the iPad. This will finally get us away from the 1984 Macintosh-style WIMP interfaces that we've been stuck with for so long. I don't like a lot of Apple's policies, but they are leading the way here, and other vendors will copy the good stuff, so we will all benefit.

Some must-try apps:

Looking for iPad Application Beta Testers

Some time in the next few weeks, I hope to have an iPad game ready for submission to the App Store. I am looking for people to help me test the app.

You need to have the following:

  • An iPad
  • Somebody with whom to play the game (it's a two-player game, but you only need one iPad)

To be a beta tester, you will have to give me your iPad's UDID (unique device identifier). To get that, follow these steps:

  1. Launch iTunes on your computer
  2. Plug your iPad into the USB port and wait for iTunes to indicate it is connected
  3. Select your iPad in the Devices list in iTunes and click the Summary tab
  4. Click the word "Serial Number" next to the picture of your iPad. It will change to "Identifier (UDID)" and there will be a long string of letters and digits after it
  5. From the Edit menu, choose the Copy command. (This will copy the letters and digits to the clipboard.)
  6. Paste the UDID into an email to me, saying that you want to be a beta tester. If you don't know my email address, use the Contact Me form.