Calvin's

Icon

designs and hacks. people and products.

iOS dev launcher: Session #1 SaveThePrincess

save the princess

Participated in Subhb’s iOS dev launcher workshop today and it was really fun to chill out and mess with code with a whole bunch of like-minded people.

Stuff for my Objective C Muscle Memory

Besides the social aspect of these coding workshops, it was also a great opportunity for me to get a refresher on Objective C since I last worked on it 6 months ago.  Here’s the gist of what session #1 covered, introduced in a cute and friendly way using a “Princess” class:-

  • Anatomy of an Objective C class
  • Creating our own custom class
  • Memory allocation when instantiating a class
  • Messaging Syntax (essentially methods in an Objective C class, whether it is a class method or an instance method)
  • Writing @property in a class in the header file (.h) and making them available in the implementation (.m) file with @synthesize
  • And @property of course is represented by this – @property (<attributes>) <type> <name>;
  • The differences between readonly, assign, copy and retain attributes in the @property declaration

Practice assignment for session #1

  • replicate “SaveThePrincess” Exercise
  • Add a “School” property to the princess class
  • Create a new initializer method to create a princess instance with name, age and school
  • Change sing method and print “Myra loves singing, she is 5 years old and she goes to Bugis Primary School”
  • Change description method to print school name for debugging

And so, giving the name, age and school to my princess object, my princess Kait-lyn is 6 years old and goes to school at Phyllis Riccia.

What about your princess? :-)

Source code right here – https://github.com/calvinchengx/SaveThePrincess

Texture Dimensions

Texture mapping is  a standard technique used to display images in iOS games and apps.  In iOS devices, textures are constrained to dimensions with a power of two.  In other words, the width or the height of a texture can only be in terms of 2, 4, 8, 16, 32, 64, 128, 256, 512 or 1024 (pixels).  From iOS 4 onwards, textures can be as wide or as high as 2048 pixels.

A texture comes into play when we create a sprite from an image file.  For example, we use the cocos2d CCSprite class to instantiate a sprite in a game using an image file:-

CCSprite *target = [CCSprite spriteWithFile:@"Target.png"
rect:CGRectMake(0, 0, 27, 40)];

If Target.png in the above example is 260 x 260 pixels in dimension with 32-bit colors, it should in theory use only 270 KB.  But if we attempt this in iOS, we will discover that it actually uses 1 MB!

This unexpected increase is due to the constraint that texture need to be powers of 2 in its width and its height.  iOS has created the next best texture of 512 x 512 (since 260 x 260 just missed 256 x 256 by 4 pixels in each attribute) and this uses 1 MB of memory.

The simple solution is to create image files that takes the powers-of-two rule into account.  Taking care of this little detail goes a long way towards solving memory and performance related issues when rendering images in your iOS game or app.

distribute versus setuptools

Following my previous post about switching to use python distribute when upgrading to Python 2.7 via MacPorts, I am curious about the difference between python setuptools and python distribute.  Well, with some googling, distribute is apparently a fork of setuptools and distribute looks very community friendly.

For people unfamiliar with the whacky world of Python Packaging, here are two very good articles which will help you understand the back story with these two packages.

And for the impatient who just want to get things done, this quotable quote sums it all up for you… :-)

In case you heard of both setuptools and distribute: distribute fully replaces setuptools. Just use distribute. Setuptools is “maintained” (for various historically dubious values of “maintain”) by one person (whom all should applaud for creating the darn thing in the first place, btw!). Distribute is maintained by a lot of people, so bugs actually get fixed. And “bugs” meaning “it doesn’t break with subversion 1.6 because patches that fix it don’t get applied for half a year”. Be sure to use the latest versions of distribute (and buildout, if applicable).

Looks like I am going to be sticking to python distribute from now on.