Tuesday, July 28, 2009

Ramblings about the Cocos2d Director class

All Cocos2d based applications use 2 core classes above all others, Director and CocosNode(Sprite,Scene,Etc..). I want to give these 2 objects some special attention up front as you will interact with them most often in your initial Cocos2d projects. In this post we will focus in on the Director class.

Director is the class that acts as a binding agent linking the Cocos2d Scene management system to the UIKit framework. It takes over control of application flow, most commonly, in the UIApplicationDelegate applicationDidFinishLaunching function override. There are 2 different "types" of Director. The normal director and a higher performance fast director. The director maintains the applications main run loop, runs the initial scene and houses the functions that start and stop the main run and animation loops.

Encapsulated in the Director is the one and only EAGLView instance. This our rendering surface and is where all of the drawing for our Cocos2d based application happens. The EAGLView is backed by CAEAGLLayer which is an especially talented core animation layer that has an OpenGL ES rendering context. As I stated in the previous post this is not an OpenGL ES tutorial. Being satisfied in the knowledge that Cocos2d will be handling all of the OpenGL ES kung fu for you will allow you to focus on the specifics of your particular project. After a while using Cocos2d for IPhone go back and optimize the parts that require special attention for usage in your application. The whole point is not to reinvent the wheel.

The simple sample code below sets up a director for an application in landscape mode, that shows the frames per second and runs with a Scene. Also observe the main loop control functions being called in the application delegates applicationWillTerminate, applicationWillResignActive, and applicationDidBecomeActive functions.

Next post I will be focusing on the CocosNode class and Node inheritance hierarchy. I will also discuss scene graphs and how the Cocos2d Nodes realize the composite design pattern.

No comments: