OpenGL ES for iPhone tutorial Basic - 2 : Hello EAGL, C++ Style

Kongfu.Yang kongfu.yang(a)gmail.com http://www.play3d.net

Warning: these article were written in iPhone SDK beta3, example doesn't work with final SDK yet!
Thanks Muhammad Ishaq (http://blogs.kahaf.com/ishaq) for notify. I will update these article to final SDK when I have time.


The concept has proven by the project template for separating Coco Touch and pure OpenGL ES code. Next step is to create a C++ version, because I have written some useful class in C++ when I was playing Windows Mobile.

The idea is simple, just convert the callbacks to a class and use one global c function getApp() to retrieve its instance for Coco Touch part. All system dependent functions, especially I/O, are wrapped in C functions or C++ class. To avoid the headache of name mangling, the whole project is written in C++, even the funcitons in C mentioned just a moment ago.

I think it will be better if Apple provides an empty project wizard in new project dialog. Now we have to new a Coco Touch project and remove everything except the precompiled header file. If you are not familiar with the project info of Xcode, then leave the Info.plist too but edit it to remove the reference to mainWindow.xib.

I created the project as two part, the callback class glApp is the main functionality (say hello) and put in the project folder, the re-usable parts of this template project are placed in ../mylib/iPhone. Graphics classes will be in ../mylib.

The Coco Touch parts of this C++ style template project are modified from the C style version. By rename them from .m to .mm, they became objective c++ and the content need not change. Only modification is to convert callbacks from c function call to getApp()->functionName(). Just that easy!

The I/O wrapper is still naive. It will grow up while I write more code. The interesting thing is the current path when the App running. It is root directory of the whole system, "/". The document said the sandboxing will jail the App in its own directory, but the truth is you still have accessing to any corner at this moment (beta 3).

In the prove of concept, I didn't pay attension to the event processing. And I have no interesting to create yet another app framework. Here a few callback is designed, the accelerating, the resizing and timer. Sure we'd better add mouse and keyboard, but I have planned to create a cool moving controller, let's ignore mouse at this moment. The keyboard is another story, I have some cool idea too, but most likely I have no time to implement it. The offical SDK hides almost everthing about keyboard, obviously Apple doesn't like us care it too much :-).

After all, here is the skeleton of this project:
class of iPhone 2.0 OpenGL ES Application Skeleton
The library is just for illustration, I don't have most of them yet. But I do plan to write them and share with you, if I have time.

To package resource files in the App, we need add extra action in target of the project. By default, the resource is placed at same directory of executable (Info.plist).

Another fun is the Default.png in same directory of executable. We may use Organizer to take a screen shot and assign it as default image of the App. If Organizer is not running, bring it out by Window | Organizer menu in Xcode. But as my feeling, the screen shot as default image just confuse the user, to put logo and credit information in it is much better. Following is mine (powered by GIMP):


Here are files:
  all in one zip
project folder:
helloeagl++/helloeagl___Prefix.pch
helloeagl++/helloeagl.cpp
helloeagl++/Info.plist
helloeagl++/helloMesh.h

library folder:

mylib/iPhone/main.m
mylib/iPhone/eaglAppDelegate.mm
mylib/iPhone/eaglAppDelegate.h
mylib/iPhone/eglSetup.h
mylib/iPhone/eglSetup.cpp
mylib/iPhone/callbacks.h
mylib/iPhone/IO.h
mylib/iPhone/IO.mm
mylib/iPhone/eglView.h
mylib/iPhone/eglView.mm
mylib/iPhone/Info.plist

Screen Shot:

If you feel this article is helpful, please find an AD interesting for you to click, it is helpful for author :-)