CCDirector Class Reference
| Inherits from | CC_VIEWCONTROLLER |
|---|---|
| Declared in | CCDirector.h |
Overview
The director creates and handles the main Window and the Cocos2D view. It also presents Scenes and initiates scene updates and drawing.
CCDirector inherits from CC_VIEWCONTROLLER which is equivalent to UIViewController on iOS, and NSObject on OS X.
Since the CCDirector is a singleton, the standard way to use its methods and properties is:
[[CCDirector sharedDirector] methodName];[CCDirector sharedDirector].aProperty;
The CCDirector is responsible for:
- initializing the OpenGL ES / Metal context
- setting the pixel format (default on is RGB565)
- setting the buffer depth (default one is 0-bit)
- setting the projection (default one is 3D)
The CCDirector also sets the default OpenGL context:
GL_TEXTURE_2Dis enabledGL_VERTEX_ARRAYis enabledGL_COLOR_ARRAYis enabledGL_TEXTURE_COORD_ARRAYis enabled
Singleton Accessor
+ sharedDirector
The shared director instance.
+ (CCDirector *)sharedDirectorReturn Value
The shared director instance.
Declared In
CCDirector.h
Accessing OpenGL Thread
runningThread
If you want to run any Cocos2D task, run it in this thread. Any task that modifies Cocos2D’s OpenGL state must be executed on this thread due to OpenGL state changes only being allowed on the OpenGL thread.
@property (weak, readonly, nonatomic) NSThread *runningThreadReturn Value
The Cocos2D thread, typically this will be the main thread.
Declared In
CCDirector.h
View Scale
contentScaleFactor
Content scaling factor. Sets the ratio of points to pixels. Default value is initalized from the content scale of the GL view used by the director.
@property (nonatomic, assign) CGFloat contentScaleFactorDeclared In
CCDirector.h
UIScaleFactor
UI scaling factor, default value is 1. Positions and content sizes are scale by this factor if the position type is set to scale.
@property (nonatomic, readwrite, assign) float UIScaleFactorSee Also
Declared In
CCDirector.h
designSize
User definable value that is used for default contentSizes of many node types (CCScene, CCNodeColor, etc). Defaults to the view size.
@property (nonatomic, assign) CGSize designSizeDeclared In
CCDirector.h
Working with View and Projection
view
View used by the director for rendering. The CC_VIEW macro equals UIView on iOS, NSOpenGLView on OS X and CCGLView.
@property (nonatomic, strong) CC_VIEW<CCDirectorView> *viewSee Also
Declared In
CCDirector.h
projection
Sets an OpenGL projection
@property (nonatomic, readwrite) CCDirectorProjection projectionDeclared In
CCDirector.h
projectionMatrix
Projection matrix used for rendering.
@property (nonatomic, readonly) GLKMatrix4 projectionMatrixSee Also
Declared In
CCDirector.h
viewportRect
Rect of the visible screen area in GL coordinates.
@property (nonatomic, readonly) CGRect viewportRectDeclared In
CCDirector.h
globalShaderUniforms
The current global shader values values.
@property (nonatomic, readonly) NSMutableDictionary *globalShaderUniformsDeclared In
CCDirector.h
displayStats
Whether or not to display statistics in the view’s lower left corner. From top to bottom the numbers are: number of draw calls, time per frame (in seconds), framerate (average over most recent frames).
@property (nonatomic, readwrite, assign) BOOL displayStatsDeclared In
CCDirector.h
– viewSize
– viewSizeInPixels
– reshapeProjection:
Changes the projection size.
- (void)reshapeProjection:(CGSize)newViewSizeParameters
newViewSize |
New projection size. |
|---|
Declared In
CCDirector.h
– convertToGL:
Converts a UIKit coordinate to an OpenGL coordinate.
- (CGPoint)convertToGL:(CGPoint)pParameters
p |
Point to convert. |
|---|
Return Value
Converted point.
Discussion
Useful to convert (multi) touch coordinates to the current layout (portrait or landscape).
Declared In
CCDirector.h
– convertToUI:
Converts an OpenGL coordinate to a UIKit coordinate.
- (CGPoint)convertToUI:(CGPoint)pParameters
p |
Point to convert. |
|---|
Return Value
Converted point.
Discussion
Useful to convert node points to window points for calls such as glScissor.
Declared In
CCDirector.h
Presenting Scenes
runningScene
The current running Scene. Director can only run one Scene at a time.
@property (nonatomic, readonly) CCScene *runningSceneSee Also
Declared In
CCDirector.h
– presentScene:
Presents a new scene.
- (void)presentScene:(CCScene *)sceneParameters
scene |
Scene to start. |
|---|
Discussion
If no scene is currently running, the scene will be started.
If another scene is currently running, this scene will be stopped, and the new scene started.
See Also
Declared In
CCDirector.h
– presentScene:withTransition:
Presents a new scene, with a transition.
- (void)presentScene:(CCScene *)scene withTransition:(CCTransition *)transitionParameters
scene |
Scene to start. |
|---|---|
transition |
Transition to use. Can be nil. |
Discussion
If no scene is currently running, the new scene will be started without a transition.
If another scene is currently running, this scene will be stopped, and the new scene started, according to the provided transition.
See Also
Declared In
CCDirector.h
– pushScene:
Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
- (void)pushScene:(CCScene *)sceneParameters
scene |
New scene to start. |
|---|
Discussion
The new scene will be executed, the previous scene remains in memory. Try to avoid big stacks of pushed scenes to reduce memory allocation.
Warning: ONLY call it if there is already a running scene.
Declared In
CCDirector.h
– popScene
Pops out a scene from the queue. This scene will replace the running one. The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
- (void)popSceneDiscussion
Warning: ONLY call it if there is a running scene.
Declared In
CCDirector.h
– popToRootScene
Pops out all scenes from the queue until the root scene in the queue.
- (void)popToRootSceneDiscussion
This scene will replace the running one.
Internally it will call popToSceneStackLevel:1
See Also
Declared In
CCDirector.h
– popToRootSceneWithTransition:
Pops out all scenes from the queue until the root scene in the queue, using a transition
- (void)popToRootSceneWithTransition:(CCTransition *)transitionParameters
transition |
The transition to play. |
|---|
Discussion
This scene will replace the running one. Internally it will call popToRootScene
See Also
Declared In
CCDirector.h
– pushScene:withTransition:
Pushes the running scene onto the scene stack, and presents the incoming scene, using a transition
- (void)pushScene:(CCScene *)scene withTransition:(CCTransition *)transitionParameters
scene |
The scene to present |
|---|---|
transition |
The transition to use |
See Also
Declared In
CCDirector.h
– popSceneWithTransition:
Replaces the running scene, with the last scene pushed to the stack, using a transition
- (void)popSceneWithTransition:(CCTransition *)transitionParameters
transition |
The transition to use |
|---|
See Also
Declared In
CCDirector.h
Animating the Active Scene
animationInterval
The animation interval is the time per frame. Typically specified as 1.0 / 60.0 where the latter number defines
the framerate. The lowest value is 0.0166 (1/60).
@property (nonatomic, readwrite, assign) CCTime animationIntervalSee Also
Declared In
CCDirector.h
fixedUpdateInterval
The fixed animation interval is used to run “fixed updates” at a fixed rate, independently of the framerate. Used primarly by the physics engine.
@property (nonatomic, readwrite, assign) CCTime fixedUpdateIntervalSee Also
Declared In
CCDirector.h
nextDeltaTimeZero
whether or not the next delta time will be zero
@property (nonatomic, readwrite, assign, getter=isNextDeltaTimeZero) BOOL nextDeltaTimeZeroDeclared In
CCDirector.h
paused
Whether or not the Director is paused.
@property (nonatomic, readonly, getter=isPaused) BOOL pausedSee Also
Declared In
CCDirector.h
animating
Whether or not the Director is active (animating).
@property (nonatomic, readonly, getter=isAnimating) BOOL animatingDeclared In
CCDirector.h
totalFrames
How many frames were called since the director started
@property (nonatomic, readonly) NSUInteger totalFramesDeclared In
CCDirector.h
secondsPerFrame
Time it took to render the most recent frames, in seconds per frame.
@property (nonatomic, readonly) CCTime secondsPerFrameDeclared In
CCDirector.h
– resume
– stopAnimation
Stops the animation. All scheduled updates and actions are effectively paused.
- (void)stopAnimationDiscussion
When not animating, the director doesn’t redraw the view at all. It is best to hide the view when not animating the director. If you need to keep showing the director’s view use pause instead.
See Also
Declared In
CCDirector.h
– startAnimation
Begins drawing the screen. Scheduled timers and actions will run.
- (void)startAnimationDiscussion
Warning: Don’t call this function to start the main loop. To run the main loop call presentScene:
See Also
Declared In
CCDirector.h
Purging Caches
– purgeCachedData
Removes all the cocos2d resources that have been previously loaded and automatically cached, textures for instance.
- (void)purgeCachedDataDeclared In
CCDirector.h
– rendererFromPool
Get a renderer object to use for rendering. This method is thread safe.
- (CCRenderer *)rendererFromPoolDeclared In
CCDirector.h
– poolRenderer:
Return a renderer to a pool after rendering. This method is thread safe.
- (void)poolRenderer:(CCRenderer *)rendererDeclared In
CCDirector.h
– addFrameCompletionHandler:
Add a block to be called when the GPU finishes rendering a frame. This is used to pool rendering resources (renderers, buffers, textures, etc) without stalling the GPU pipeline.
- (void)addFrameCompletionHandler:(dispatch_block_t)handlerDeclared In
CCDirector.h