Monday, February 9, 2009

Writing an iPhone app not child's play

After this blog broke the story of Ding Wen's remarkable achievement, the news spread like wildfire around the world.

Let’s get one thing straight — Lim Ding Wen, the 9-year-old boy who wrote an iPhone app, did not download the iPhone SDK one day, learn Objective-C on his own, write Doodle Kids, and then post the app to Apple for approval.

The story is much longer and more complicated than that. It’s a story of a smart kid with an interest and aptitude for programming, a supportive father who is himself a highly skilled programmer, and many, many hours of hard work on the part of the boy to master different programming languages. It also involves, of all things, an Apple IIGS.

The story begins in 2006, back when Ding Wen was in primary 1 (first grade). He was then six going on seven. His father, Lim Thye Chean, remembers that Ding Wen wanted to write a game so he promised to help his son if the boy did well in his exams.

Thye Chean is eminently qualified to help, being the CTO of a high-tech company and a hardcore techie himself. One of his favourite languages is Forth, where you think and write in reverse polish notation.

More importantly, Thye Chean was, and still is, also a true Apple IIGS fan. He had been programming on the Apple II ever since he was a teenager and he lusted after the IIGS from the moment it was released in 1986. Thye Chean bought his first machine, a secondhand IIGS, in 1990 for the outrageous price of $2,000.

When his son first said that he wanted to write a game, Thye Chean researched various languages to find one that was suitable for his son. He could not find anything he was happy with, so he settled on an interim solution, Applesoft BASIC on the IIGS.

Back to BASICs

Thye Chean first taught his son Applesoft BASIC on a Mac running the Sweet16 IIGS emulator. Ding Wen learned to write simple programs but BASIC is obviously a hard program to write a
graphics-heavy game on.

They lucked out when they found an old version of Complete Pascal designed for the IIGS. Complete Pascal's graphical IDE, ease of use, and ease of compilation, combined with Apple's QuickDraw API, made it much better platform for learning to write a game.

With the discovery of Pascal for the IIGS, the father-son project became more intensive. Recalls Thye Chean: "We spent days going through old bookstores to find old Pascal books. We even used the Borland Delphi book to learn about Pascal."

Because of his background in BASIC, Ding Wen picked up Pascal quickly and he wrote his first program in a few days. Eventually, he wrote Doodle Kids, which was completed at the end of May 2008. The application was Ding Wen's first event-driven program.

Doodle Kids was written over a few days, with some help from dad. Ding Wen studied the source code of his dad's Pascal painting program Simple Paint. Thye Chean also helped his son debug the colour cycling part of the program. According to Thye Chean, his son knew how to use hex numbers to do the RGB calculation, but he had trouble understanding part of the loop code used in colour cycling. (This part of the Pascal program did not make it over to the iPhone version).

Porting to the iPhone

A few months later, Ding Wen decided to port Doodle Kids over to the iPhone, not surprising as Ding Wen's favourite game device is his dad's iPhone which has over 100 games on it.

By this time, Ding Wen had learned Javascript so he rewrote Doodle Kids in Javascript, with the help of a drawing library called GScript that his dad had developed. GScript is a drawing routine for JavaScript simulating QuickDraw commands. It uses the Canvas feature available in all web browsers (except for IE). "With the QuickDraw-like commands, it made it easier for Ding Wen to port, so that he could concentrate on language changes and less on the API," says Thye Chean.

Javascript applications can be turned into iPhone apps relatively easily. What's needed is a WebKit browser (UIWebView) to host a web application. All iPhone technologies, including multi-touch, canvas and animation, can be used in the application, according to Thye Chean. (Full details are available here.)

He taught his son some Objective-C so that Ding Wen could write the wrapper for WebKit. After debugging, Doodle Kids for the iPhone was sent to Apple for approval in mid January 2009. Two weeks later, Apple gave the OK.

Ding Wen is now learning Objective-C, his seventh language, and is porting his game Invader War to the iPhone. Invader War is Ding Wen's first object oriented program, and was written in Object Pascal.

Anyone can write a program

Thye Chean insists that his son is not a whiz kid, though he allows that his son has above average intelligence. His son's experience has convinced him that anyone can learn to program. As Thye Chean notes in a blog post, it used to be that anyone could write a simple program. Not so today. “Programming has become a science, where you need to be a rocket scientist to just to do something simple!”

In order to bring back the good old days when programming was easily accessible, Thye Chean developed GScript BASIC, a modern Applesoft BASIC-inspired language. "It is what I think Applesoft BASIC would be if it evolved, and ran on a super Apple IIGS," he says.

GScript BASIC running on Mobile Safari on the iPhone.

GScript BASIC will create a 640 by 400 Super Hires screen in Safari, Firefox, Chrome and Mobile Safari. You can run GScript BASIC scripts over the web or you can download GScript BASIC and run it off your local machine. It is not a clone of Applesoft BASIC though as it has new commands for drawing shapes, playing sounds and doing animation.

GScript BASIC turned out this way because Thye Chean had always dreamed of having a super Apple IIGS with a 640 by 400 resolution (the IIGS could only handle resolutions of 320 by 200 and 640 by 200).

"Unfortunately, such a product never existed so I designed an imaginary Applesoft BASIC-like language (based on JavaScript), to run on an imaginary Apple IIGS. The result? You can do programming like you can on a super Apple IIGS, but on a modern computer."

Thye Chean originally wrote GScript BASIC as a simple programming language for his 5-year-old daugher Xin Quan. She still hasn’t quite decided if she wants to follow in her brother’s footsteps yet, but if she does, watch out Ding Wen. Younger sisters seldom play fair.

No comments: