Posts tagged with cocos2d

Building Flockwork: Retina iPad graphics in cocos2d

This is the first in a series of blog articles explaining some of the interesting technical hurdles we encountered while building our new iPad puzzle game Flockwork.

Shortly before our app launched, Apple announced the iPad 3 with retina display. We knew it would be important to support retina graphics in our app. Luckily, it didn't take too much additional effort to get our game ready for the "resolutionary" display!

We're using the cocos2d framework to build Flockwork, and it does have some built-in support for retina graphics. So first we had to:

1. Enable retina display mode

  1. if( ! [director enableRetinaDisplay:YES] ) {
  2. CCLOG(@"Retina Display Not supported");
  3. }

First, set enableRetinaDisplay to YES in your AppDelegate. If you build immediately after enabling this, your app will look a complete mess! You now need to provide alternative HD assets for all the static assets in your games: images, spritessheets, fonts, particle effects, and so on.

2. Upgrade your image assets

Every PNG file used in your app will now need an HD version, double the width and height of the original. If your original source files are in vector format, for example Adobe Illustrator files, this shouldn't be too hard. If you made bitmaps in Photoshop, there may be some extra work.

Cocos2d uses the convention of a -hd suffix, for example the hi-res version of a file called button.png (20x24 pixels) would be button-hd.png (40x48 pixels).

After adding the new file to your project, cocos will automatically load the HD version on a retina display. This is handled by the CCFileUtils class.

If your project has a large number of image assets, it can be easy to lose track of which files have been upgraded to HD. It's also very important to ensure that the -hd version is exactly double the size of the standard version.  A cautionary tale: in Flockwork, we were using the pixel size of some assets to determine the size of Box2d physics bodies. Because some of our -hd assets were off by a few pixels, the physics of the objects changed in Retina mode, causing some very obscure bugs!

To avoid this, we wrote a little Python script to scan through our assets folders.

  1. import subprocess,re,glob
  2. def dimensions(path):
  3. #get dimensions of an image from sips
  4. dimensionArgs = ['/usr/bin/sips', '-g', 'pixelHeight', '-g', 'pixelWidth', path]
  5. sips = subprocess.Popen(dimensionArgs, stdout=subprocess.PIPE)
  6. dimensions = sips.stdout.read()
  7. dimensions = re.findall(r'pixel(Height|Width): ([0-9]+)', dimensions)
  8. if len(dimensions) == 2:
  9. label, height = dimensions[0]
  10. label, width = dimensions[1]
  11. height = int(height)
  12. width = int(width)
  13. return width, height
  14.  
  15. def parse(path):
  16. #parse all files and check that theres an HD version with the right dimensions
  17. files = glob.glob(path)
  18. for f in files:
  19. if "-hd" in f:
  20. w2,h2 = dimensions(f)
  21. w1,h1 = dimensions(f.replace("-hd",""))
  22. if w1*2==w2 and h1*2==h2:
  23. pass
  24. else:
  25. print f+" is not double the size of "+f.replace("-hd","")
  26. ratiow=float(w2)/float(w1)
  27. ratioh=float(h2)/float(h1)
  28. print (ratiow,ratioh)
  29.  
  30. #parse everything in the specified folder
  31. parse('Resources/images/*.png')

Note that Flockwork is iPad-only. If you're trying to make a Universal app (iPhone and iPad) you may have to provide up to four sets of images for each asset!)

3. Upgrade your spritesheets

Spritesheets work similarly to images. First, you need to make HD versions of each of the frames within the spritesheet. Then, create a spritesheet double in size to the original (for example, a 2048x2048 sheet instead of 1024x1024). We used the excellent Zwoptex. In your HD spritesheet, you then need to add all the HD assets instead of the SD ones, save and publish. All being well you'll now have files named something like:

sprites_campaign_4.png (1024x1024)
sprites_campaign_4.plist
sprites_campaign_4-hd.png (2048x2048)
sprites_campaign_4-hd.plist

Add the new .png and .plist to your project, and you're good to go.

4. Upgrade your fonts

If you're using any bitmap-based fonts, you'll also need HD versions there. We use GlyphDesigner to create our fonts files, which are then used by CCLabelBMFont. Make a copy of your GlyphDesigner project, and double the font size, for example a 35-point font would now be 70 points.

5. Test, test, test

Once all your images and fonts are in place, it's time to test. You can do testing in the iOS Simulator in Retina mode, but its likely you'll get a very slow frame rate. There's no substitute for getting a real iPad 3 to test on (even if that's a major challenge in China!).

Test all the screens and functionality of your app. It's likely you'll find some glitches or areas you've missed. For example, Flockwork uses particle effects: we needed to create HD assets of those too. We also found we'd got a few filenames for HD assets switched - this is something only a human is likely to spot!

Let us know your cocos2d retina war stories in the comments - and of course, if you haven't already, download Flockwork!

Adding iOS5 native Twitter support to your cocos2d app

In iOS5, Apple introduced native Twitter integration via the "Tweet Sheet" (aka TWTweetComposeViewController), allowing you to easily implement Twitter in your iOS app without worrying about all the heavy lifting. If you tried implementing Twitter support in the past and found yourself lost in a sea of OAuth source files, trying to handle authentication yourself, and banging your head over what to do with various responses, you'll know how much of a relief it is that this now "just works."

This is great for UIKit, but what if you want to implement the same simple Twitter sharing functionality in your cocos2d game? Luckily, it's nearly as easy!

Continue reading...