jueves, 29 de agosto de 2013

Towards Game Development in iOS

I've been curious for a long time about how to program for iPhone and iPad (in general, for iOS) and about Objective-C, the language under which to develop for this operating system, so I finally decided yesterday that it was time to delve a little bit into the details of this programming language. 

If you can program in C/C++ and you know the fundamentals of Object-Oriented programming (knowing Java also helps), the leap to Objective-C seems pretty straightforward. I started reading the book Learning Objective-C 2.0: A Hands-on Guide to Objective-C for Mac and iOS by Robert Clair. I found this book very illustrative and well-written, so I can openly advice it. 

Objective-C is an extension to plain C and very often it presents subtle differences with C++, such as the fact that you cannot create objects in the stack (i.e. you must always hold pointers to objects allocated in the heap), there's no syntax support for abstract classes, there are some new reserved keywords like nil (a pointer to no object) and id (a pointer to an object of any class), etc. 

Possibly, the biggest change is that of the 'messaging system' used in place of function calls or method invocation. In Objective-C, you use the syntax [receiver message] where receiver is an object of a class and the message is the name of a method of that object. Technically, it seems another way of the same idea of method invocation, but the difference comes from the fact that this statement is not resolved  at compile time but at run-time, adding more flexibility. Of course you can nest expressions and the result would be used in the next level (e.g. [[receiver message] message] would first evaluate the inner [receiver message] part and then the result would be used as the receiver of the outer [receiver message]).

The book also shows you how this messaging system (and other high level stuff) works under the hood (in the end, all is translated into plain C) and how to bypass the runtime (the runtime translates from Objective-C to plain C and fills in certain structures with class information) to gain efficiency when required, given that a function call, if very simple, is more than twice faster than the receiver-message scheme.

Regarding memory management, the language traditionally required the developer to manually take care of releasing allocated memory or retaining it, using manual reference counting. However, Apple included recently Automatic Reference Counting, which means that the runtime is in charge of doing this dirty work in an efficient way. In fact, Apple encourages developers to use this feature as it can give an important performance boost to applications and prevent memory leaks. 

I've read more than half of the book and I'm enjoying it. My goal is to be able to develop games for iPhone and iPad, probably using existing and reputable frameworks such as Cocos2D. If your goal is the same, I highly advice you to learn the fundamentals of Objective-C and I think that this book is a perfect starting point.

See you!

2 comentarios: