CCDirector Class Reference
Inherits from | CC_VIEWCONTROLLER |
Declared in | CCDirector.h |
Overview
Class that creates and handle the main Window and manages how and when to execute the Scenes.
The CCDirector is also responsible for: – initializing the OpenGL ES context – setting the OpenGL pixel format (default on is RGB565) – setting the OpenGL buffer depth (default one is 0-bit) – setting the projection (default one is 3D)
Since the CCDirector is a singleton, the standard way to use it is by calling: – [[CCDirector sharedDirector] methodName];
The CCDirector also sets the default OpenGL context: – GL_TEXTURE_2D is enabled – GL_VERTEX_ARRAY is enabled – GL_COLOR_ARRAY is enabled – GL_TEXTURE_COORD_ARRAY is enabled
extends CCDirector CCDirector extensions for iPhone
If the window is resized, it will be autoscaled (default behavior)
Tasks
Other Methods
-
runningThread
returns the cocos2d thread. If you want to run any cocos2d task, run it in this thread. On iOS usually it is the main thread.
property -
runningScene
The current running Scene. Director can only run one Scene at the time
property -
animationInterval
The FPS value
property -
displayStats
Whether or not to display director statistics
property -
nextDeltaTimeZero
whether or not the next delta time will be zero
property -
paused
Whether or not the Director is paused
property -
isAnimating
Whether or not the Director is active (animating)
property -
projection
Sets an OpenGL projection
property -
totalFrames
How many frames were called since the director started
property -
secondsPerFrame
seconds per frame
property -
sendCleanupToScene
Whether or not the replaced scene will receive the cleanup message. If the new scene is pushed, then the old scene won’t receive the “cleanup” message. If the new scene replaces the old one, the it will receive the “cleanup” message.
property -
notificationNode
This object will be visited after the main scene is visited. This object MUST implement the “visit” selector. Useful to hook a notification object, like CCNotifications (http://github.com/manucorporat/CCNotifications)
property -
delegate
CCDirector delegate. It shall implement the CCDirectorDelegate protocol
property -
scheduler
CCScheduler associated with this director
property -
actionManager
CCActionManager associated with this director
property -
+ sharedDirector
returns a shared instance of the director
-
– winSize
returns the size of the OpenGL view in points
-
– winSizeInPixels
returns the size of the OpenGL view in pixels. On Mac winSize and winSizeInPixels return the same value.
-
– reshapeProjection:
changes the projection size
-
– setViewport
Sets the glViewport
-
– convertToGL:
converts a UIKit coordinate to an OpenGL coordinate Useful to convert (multi) touch coordinates to the current layout (portrait or landscape)
-
– convertToUI:
converts an OpenGL coordinate to a UIKit coordinate Useful to convert node points to window points for calls such as glScissor
-
– getZEye
XXX: missing description
-
– runWithScene:
Enters the Director’s main loop with the given Scene. Call it to run only your FIRST scene. Don’t call it if there is already a running scene.
-
– pushScene:
Suspends the execution of the running scene, pushing it on the stack of suspended scenes. The new scene will be executed. Try to avoid big stacks of pushed scenes to reduce memory allocation. ONLY call it if there is a running scene.
-
– 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. ONLY call it if there is a running scene.
-
– popToRootScene
Pops out all scenes from the queue until the root scene in 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. ONLY call it if there is a running scene.
-
– replaceScene:
Replaces the running scene with a new one. The running scene is terminated. ONLY call it if there is a running scene.
-
– end
Ends the execution, releases the running scene. It doesn’t remove the OpenGL view from its parent. You have to do it manually.
-
– pause
Pauses the running scene. The running scene will be drawed but all scheduled timers will be paused While paused, the draw rate will be 4 FPS to reduce CPU consumption
-
– resume
Resumes the paused scene The scheduled timers will be activated again. The “delta time” will be 0 (as if the game wasn’t paused)
-
– stopAnimation
Stops the animation. Nothing will be drawn. The main loop won’t be triggered anymore. If you want to pause your animation call [pause] instead.
-
– startAnimation
The main loop is triggered again. Call this function only if [stopAnimation] was called earlier
-
– drawScene
Draw the scene. This method is called every frame. Don’t call it manually.
-
– setView:
sets the openGL view
-
– view
returns the OpenGL view
-
– purgeCachedData
Removes all the cocos2d data that was cached automatically. It will purge the CCTextureCache, CCLabelBMFont cache. IMPORTANT: The CCSpriteFrameCache won’t be purged. If you want to purge it, you have to purge it manually.
-
– setGLDefaultValues
sets the OpenGL default values
-
– setAlphaBlending:
enables/disables OpenGL alpha blending
-
– setDepthTest:
enables/disables OpenGL depth test
-
– createStatsLabel
creates the Stats labels
Other Methods
-
displayFPS
Whether or not to display the FPS on the bottom-left corner
property -
openGLView
The EAGLView, where everything is rendered
property -
pixelFormat
Pixel format used to create the context
property -
deviceOrientation
The device orientattion
property -
contentScaleFactor
The size in pixels of the surface. It could be different than the screen size. High-res devices might have a higher surface size than the screen size. In non High-res device the contentScale will be emulated.
property -
+ setDirectorType:
There are 4 types of Director. – kCCDirectorTypeNSTimer (default) – kCCDirectorTypeMainLoop – kCCDirectorTypeThreadMainLoop – kCCDirectorTypeDisplayLink
-
– setPixelFormat:
Uses a new pixel format for the EAGLView. Call this class method before attaching it to a UIView Default pixel format: kRGB565. Supported pixel formats: kRGBA8 and kRGB565 (Deprecated: Set the pixel format when creating the EAGLView. This method will be removed in v1.0)
-
– setDepthBufferFormat:
Change depth buffer format of the render buffer. Call this class method before attaching it to a UIWindow/UIView Default depth buffer: 0 (none). Supported: kCCDepthBufferNone, kCCDepthBuffer16, and kCCDepthBuffer24 (Deprecated: Set the depth buffer format when creating the EAGLView. This method will be removed in v1.0)
-
– detach
detach the cocos2d view from the view/window
-
– attachInWindow:
attach in UIWindow using the full frame. It will create a EAGLView. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
-
– attachInView:
attach in UIView using the full frame. It will create a EAGLView. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
-
– attachInView:withFrame:
attach in UIView using the given frame. It will create a EAGLView and use it. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
-
– displaySize
returns the display size of the OpenGL view in pixels
-
– applyOrientation
KoboldExtensions Methods
-
screenCenter
Gives you the screen center position (in points), ie half of the screen size
property -
screenCenterInPixels
Gives you the screen center position (in pixels), ie half of the screen size
property -
screenRect
Gives you the screen size as a CGRect (in points) spanning from 0, 0 to screenWidth, screenHeight
property -
screenRectInPixels
Gives you the screen size as a CGRect (in pixels) spanning from 0, 0 to screenWidth, screenHeight
property -
screenSize
Gives you the screen size as a CGSize struct (in points)
property -
screenSizeInPixels
Gives you the screen size as a CGSize struct (in pixels)
property -
screenSizeAsPoint
Gives you the screen size as a CGPoint struct (in points)
property -
screenSizeAsPointInPixels
Gives you the screen size as a CGPoint struct (in pixels)
property -
isSceneStackEmpty
Checks if the scene stack is still empty, which means runWithScene hasn’t been called yet.
property -
currentPlatformIsIOS
Tells you if the app is currently running on iOS.
property -
currentPlatformIsMac
Tells you if the app is currently running on Mac.
property -
currentDeviceIsSimulator
Tells you if the app is currently running in the iPhone/iPad Simulator.
property -
currentDeviceIsIPad
Tells you if the app is currently running on the iPad rather than iPhone or iPod Touch.
property -
frameCount
Returns the number of frames drawn since the start of the app. You can use this to check if certain events occured in the same frame, to ensure that something happens exactly n frames from now regardless of framerate, or to implement a custom framerate display. The frameCount variable is never reset at runtime, only when the app is restarted. An overflow occurs if the app remains running for over 828 days of continuous operation, assuming a steady 60 frames per second.
property -
– isRetinaDisplayEnabled
Tells you whether Retina Display is currently enabled. It does not tell you if the device you’re running has a Retina display. it could be a Retina device but Retina display may simply not be enabled.
-
– contentScaleFactorInverse
Gives you the inverse of the scale factor: 1.0f / contentScaleFactor
-
– contentScaleFactorHalved
Gives you the scale factor halved: contentScaleFactor * 0.5f
iOSExtension Methods
-
touchDispatcher
sets the CCTouchDispatcher (iOS only)
property -
– setContentScaleFactor:
The size in pixels of the surface. It could be different than the screen size. High-res devices might have a higher surface size than the screen size. In non High-res device the contentScale will be emulated.
-
– enableRetinaDisplay:
Will enable Retina Display on devices that supports it. It will enable Retina Display on iPhone4 and iPod Touch 4. It will return YES, if it could enabled it, otherwise it will return NO.
-
– contentScaleFactor
returns the content scale factor
-
– convertTouchToGL:
converts a UITouch to a GL point
Deprecated Methods
MacExtension Methods
-
eventDispatcher
sets the CCEventDispatcher (Mac only)
property -
– convertEventToGL:
converts an NSEvent to GL coordinates (Mac only)
SwizzledMethods Methods
Properties
actionManager
CCActionManager associated with this director
@property (nonatomic, readwrite, retain) CCActionManager *actionManager
Availability
Declared In
CCDirector.h
animationInterval
The FPS value
@property (nonatomic, readwrite, assign) NSTimeInterval animationInterval
Declared In
CCDirector.h
contentScaleFactor
The size in pixels of the surface. It could be different than the screen size. High-res devices might have a higher surface size than the screen size. In non High-res device the contentScale will be emulated.
@property (nonatomic, readwrite) CGFloat contentScaleFactor
Availability
Discussion
Warning: Emulation of High-Res on iOS < 4 is an EXPERIMENTAL feature.
Declared In
CCDirector.h
currentDeviceIsIPad
Tells you if the app is currently running on the iPad rather than iPhone or iPod Touch.
@property (nonatomic, readonly) BOOL currentDeviceIsIPad
Declared In
CCDirectorExtensions.h
currentDeviceIsSimulator
Tells you if the app is currently running in the iPhone/iPad Simulator.
@property (nonatomic, readonly) BOOL currentDeviceIsSimulator
Declared In
CCDirectorExtensions.h
currentPlatformIsIOS
Tells you if the app is currently running on iOS.
@property (nonatomic, readonly) BOOL currentPlatformIsIOS
Declared In
CCDirectorExtensions.h
currentPlatformIsMac
Tells you if the app is currently running on Mac.
@property (nonatomic, readonly) BOOL currentPlatformIsMac
Declared In
CCDirectorExtensions.h
delegate
CCDirector delegate. It shall implement the CCDirectorDelegate protocol
@property (nonatomic, readwrite, assign) id<CCDirectorDelegate> delegate
Availability
Declared In
CCDirector.h
deviceOrientation
The device orientattion
@property (nonatomic, readwrite) ccDeviceOrientation deviceOrientation
Declared In
CCDirector.h
displayFPS
Whether or not to display the FPS on the bottom-left corner
@property (nonatomic, readwrite, assign) BOOL displayFPS
Declared In
CCDirector.h
displayStats
Whether or not to display director statistics
@property (nonatomic, readwrite, assign) BOOL displayStats
Declared In
CCDirector.h
eventDispatcher
sets the CCEventDispatcher (Mac only)
@property (nonatomic, readwrite, retain) CCEventDispatcher *eventDispatcher
Declared In
CCDirectorMac.h
frameCount
Returns the number of frames drawn since the start of the app. You can use this to check if certain events occured in the same frame, to ensure that something happens exactly n frames from now regardless of framerate, or to implement a custom framerate display. The frameCount variable is never reset at runtime, only when the app is restarted. An overflow occurs if the app remains running for over 828 days of continuous operation, assuming a steady 60 frames per second.
@property (nonatomic, readonly) NSUInteger frameCount
Declared In
CCDirectorExtensions.h
isAnimating
Whether or not the Director is active (animating)
@property (nonatomic, readonly) BOOL isAnimating
Declared In
CCDirector.h
isSceneStackEmpty
Checks if the scene stack is still empty, which means runWithScene hasn’t been called yet.
@property (nonatomic, readonly) BOOL isSceneStackEmpty
Declared In
CCDirectorExtensions.h
nextDeltaTimeZero
whether or not the next delta time will be zero
@property (nonatomic, readwrite, assign) BOOL nextDeltaTimeZero
Declared In
CCDirector.h
notificationNode
This object will be visited after the main scene is visited. This object MUST implement the “visit” selector. Useful to hook a notification object, like CCNotifications (http://github.com/manucorporat/CCNotifications)
@property (nonatomic, readwrite, retain) id notificationNode
Availability
Declared In
CCDirector.h
openGLView
The EAGLView, where everything is rendered
@property (nonatomic, readwrite, retain) EAGLView *openGLView
Declared In
CCDirector.h
paused
Whether or not the Director is paused
@property (nonatomic, readonly, getter=isPaused) BOOL paused
Declared In
CCDirector.h
pixelFormat
Pixel format used to create the context
@property (nonatomic, readonly) tPixelFormat pixelFormat
Declared In
CCDirector.h
projection
Sets an OpenGL projection
@property (nonatomic, readwrite) ccDirectorProjection projection
Declared In
CCDirector.h
runningScene
The current running Scene. Director can only run one Scene at the time
@property (nonatomic, readonly) CCScene *runningScene
Declared In
CCDirector.h
runningThread
returns the cocos2d thread. If you want to run any cocos2d task, run it in this thread. On iOS usually it is the main thread.
@property (readonly, nonatomic) NSThread *runningThread
Availability
Declared In
CCDirector.h
scheduler
CCScheduler associated with this director
@property (nonatomic, readwrite, retain) CCScheduler *scheduler
Availability
Declared In
CCDirector.h
screenCenter
Gives you the screen center position (in points), ie half of the screen size
@property (nonatomic, readonly) CGPoint screenCenter
Declared In
CCDirectorExtensions.h
screenCenterInPixels
Gives you the screen center position (in pixels), ie half of the screen size
@property (nonatomic, readonly) CGPoint screenCenterInPixels
Declared In
CCDirectorExtensions.h
screenRect
Gives you the screen size as a CGRect (in points) spanning from 0, 0 to screenWidth, screenHeight
@property (nonatomic, readonly) CGRect screenRect
Declared In
CCDirectorExtensions.h
screenRectInPixels
Gives you the screen size as a CGRect (in pixels) spanning from 0, 0 to screenWidth, screenHeight
@property (nonatomic, readonly) CGRect screenRectInPixels
Declared In
CCDirectorExtensions.h
screenSize
Gives you the screen size as a CGSize struct (in points)
@property (nonatomic, readonly) CGSize screenSize
Declared In
CCDirectorExtensions.h
screenSizeAsPoint
Gives you the screen size as a CGPoint struct (in points)
@property (nonatomic, readonly) CGPoint screenSizeAsPoint
Declared In
CCDirectorExtensions.h
screenSizeAsPointInPixels
Gives you the screen size as a CGPoint struct (in pixels)
@property (nonatomic, readonly) CGPoint screenSizeAsPointInPixels
Declared In
CCDirectorExtensions.h
screenSizeInPixels
Gives you the screen size as a CGSize struct (in pixels)
@property (nonatomic, readonly) CGSize screenSizeInPixels
Declared In
CCDirectorExtensions.h
secondsPerFrame
seconds per frame
@property (nonatomic, readonly) ccTime secondsPerFrame
Declared In
CCDirector.h
sendCleanupToScene
Whether or not the replaced scene will receive the cleanup message. If the new scene is pushed, then the old scene won’t receive the “cleanup” message. If the new scene replaces the old one, the it will receive the “cleanup” message.
@property (nonatomic, readonly) BOOL sendCleanupToScene
Availability
Declared In
CCDirector.h
Class Methods
setDirectorType:
There are 4 types of Director. – kCCDirectorTypeNSTimer (default) – kCCDirectorTypeMainLoop – kCCDirectorTypeThreadMainLoop – kCCDirectorTypeDisplayLink
+ (BOOL)setDirectorType:(ccDirectorType)directorType
Availability
Discussion
Each Director has it’s own benefits, limitations. If you are using SDK 3.1 or newer it is recommed to use the DisplayLink director
This method should be called before any other call to the director.
It will return NO if the director type is kCCDirectorTypeDisplayLink and the running SDK is < 3.1. Otherwise it will return YES.
Declared In
CCDirector.h
Instance Methods
attachInView:
attach in UIView using the full frame. It will create a EAGLView. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
- (BOOL)attachInView:(UIView *)view
Declared In
CCDirector.h
attachInView:withFrame:
attach in UIView using the given frame. It will create a EAGLView and use it. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
- (BOOL)attachInView:(UIView *)view withFrame:(CGRect)frame
Declared In
CCDirector.h
attachInWindow:
attach in UIWindow using the full frame. It will create a EAGLView. (Deprecated: set setOpenGLView instead. Will be removed in v1.0)
- (BOOL)attachInWindow:(UIWindow *)window
Declared In
CCDirector.h
contentScaleFactor
returns the content scale factor
- (CGFloat)contentScaleFactor
Declared In
CCDirectorIOS.h
contentScaleFactorHalved
Gives you the scale factor halved: contentScaleFactor * 0.5f
- (float)contentScaleFactorHalved
Declared In
CCDirectorExtensions.h
contentScaleFactorInverse
Gives you the inverse of the scale factor: 1.0f / contentScaleFactor
- (float)contentScaleFactorInverse
Declared In
CCDirectorExtensions.h
convertEventToGL:
converts an NSEvent to GL coordinates (Mac only)
- (CGPoint)convertEventToGL:(NSEvent *)event
Declared In
CCDirectorMac.h
convertToGL:
converts a UIKit coordinate to an OpenGL coordinate Useful to convert (multi) touch coordinates to the current layout (portrait or landscape)
- (CGPoint)convertToGL:(CGPoint)p
Declared In
CCDirector.h
convertToUI:
converts an OpenGL coordinate to a UIKit coordinate Useful to convert node points to window points for calls such as glScissor
- (CGPoint)convertToUI:(CGPoint)p
Declared In
CCDirector.h
convertTouchToGL:
converts a UITouch to a GL point
- (CGPoint)convertTouchToGL:(UITouch *)touch
Declared In
CCDirectorIOS.h
displaySize
returns the display size of the OpenGL view in pixels
- (CGSize)displaySize
Declared In
CCDirector.h
drawScene
Draw the scene. This method is called every frame. Don’t call it manually.
- (void)drawScene
Declared In
CCDirector.h
enableRetinaDisplay:
Will enable Retina Display on devices that supports it. It will enable Retina Display on iPhone4 and iPod Touch 4. It will return YES, if it could enabled it, otherwise it will return NO.
- (BOOL)enableRetinaDisplay:(BOOL)enableRetina
Availability
Discussion
This is the recommended way to enable Retina Display.
Declared In
CCDirectorIOS.h
end
Ends the execution, releases the running scene. It doesn’t remove the OpenGL view from its parent. You have to do it manually.
- (void)end
Declared In
CCDirector.h
isRetinaDisplayEnabled
Tells you whether Retina Display is currently enabled. It does not tell you if the device you’re running has a Retina display. it could be a Retina device but Retina display may simply not be enabled.
- (BOOL)isRetinaDisplayEnabled
Declared In
CCDirectorExtensions.h
pause
Pauses the running scene. The running scene will be drawed but all scheduled timers will be paused While paused, the draw rate will be 4 FPS to reduce CPU consumption
- (void)pause
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. ONLY call it if there is a running scene.
- (void)popScene
Declared In
CCDirector.h
popToRootScene
Pops out all scenes from the queue until the root scene in 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. ONLY call it if there is a running scene.
- (void)popToRootScene
Declared In
CCDirector.h
purgeCachedData
Removes all the cocos2d data that was cached automatically. It will purge the CCTextureCache, CCLabelBMFont cache. IMPORTANT: The CCSpriteFrameCache won’t be purged. If you want to purge it, you have to purge it manually.
- (void)purgeCachedData
Availability
Declared In
CCDirector.h
pushScene:
Suspends the execution of the running scene, pushing it on the stack of suspended scenes. The new scene will be executed. Try to avoid big stacks of pushed scenes to reduce memory allocation. ONLY call it if there is a running scene.
- (void)pushScene:(CCScene *)scene
Declared In
CCDirector.h
replaceScene:
Replaces the running scene with a new one. The running scene is terminated. ONLY call it if there is a running scene.
- (void)replaceScene:(CCScene *)scene
Declared In
CCDirector.h
reshapeProjection:
changes the projection size
- (void)reshapeProjection:(CGSize)newWindowSize
Declared In
CCDirector.h
resume
Resumes the paused scene The scheduled timers will be activated again. The “delta time” will be 0 (as if the game wasn’t paused)
- (void)resume
Declared In
CCDirector.h
runWithScene:
Enters the Director’s main loop with the given Scene. Call it to run only your FIRST scene. Don’t call it if there is already a running scene.
- (void)runWithScene:(CCScene *)scene
Discussion
It will call pushScene: and then it will call startAnimation
Declared In
CCDirector.h
setAlphaBlending:
enables/disables OpenGL alpha blending
- (void)setAlphaBlending:(BOOL)on
Declared In
CCDirector.h
setContentScaleFactor:
The size in pixels of the surface. It could be different than the screen size. High-res devices might have a higher surface size than the screen size. In non High-res device the contentScale will be emulated.
- (void)setContentScaleFactor:(CGFloat)scaleFactor
Availability
Discussion
The recommend way to enable Retina Display is by using the “enableRetinaDisplay:(BOOL)enabled” method.
Declared In
CCDirectorIOS.h
setDepthBufferFormat:
Change depth buffer format of the render buffer. Call this class method before attaching it to a UIWindow/UIView Default depth buffer: 0 (none). Supported: kCCDepthBufferNone, kCCDepthBuffer16, and kCCDepthBuffer24 (Deprecated: Set the depth buffer format when creating the EAGLView. This method will be removed in v1.0)
- (void)setDepthBufferFormat:(tDepthBufferFormat)db
Declared In
CCDirector.h
setDepthTest:
enables/disables OpenGL depth test
- (void)setDepthTest:(BOOL)on
Declared In
CCDirector.h
setGLDefaultValues
sets the OpenGL default values
- (void)setGLDefaultValues
Declared In
CCDirector.h
setPixelFormat:
Uses a new pixel format for the EAGLView. Call this class method before attaching it to a UIView Default pixel format: kRGB565. Supported pixel formats: kRGBA8 and kRGB565 (Deprecated: Set the pixel format when creating the EAGLView. This method will be removed in v1.0)
- (void)setPixelFormat:(tPixelFormat)p
Declared In
CCDirector.h
startAnimation
The main loop is triggered again. Call this function only if [stopAnimation] was called earlier
- (void)startAnimation
Discussion
Warning: Don’t call this function to start the main loop. To run the main loop call runWithScene
Declared In
CCDirector.h