/libs/cocos2d/CCDirector.h

http://github.com/kstenerud/ObjectAL-for-iPhone · C Header · 539 lines · 150 code · 86 blank · 303 comment · 0 complexity · 9ee22cbcb7d31992138d242ec51366ab MD5 · raw file

  1. /*
  2. * cocos2d for iPhone: http://www.cocos2d-iphone.org
  3. *
  4. * Copyright (c) 2008-2010 Ricardo Quesada
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. //
  25. #import "ccConfig.h"
  26. #import "ccTypes.h"
  27. // OpenGL related
  28. #import "Support/EAGLView.h"
  29. /** @typedef tPixelFormat
  30. Possible Pixel Formats for the EAGLView.
  31. @deprecated Will be removed in v1.0
  32. */
  33. typedef enum {
  34. /** RGB565 pixel format. No alpha. 16-bit. (Default) */
  35. kCCPixelFormatRGB565,
  36. /** RGBA format. 32-bit. Needed for some 3D effects. It is not as fast as the RGB565 format. */
  37. kCCPixelFormatRGBA8888,
  38. /** default pixel format */
  39. kCCPixelFormatDefault = kCCPixelFormatRGB565,
  40. // backward compatibility stuff
  41. kPixelFormatRGB565 = kCCPixelFormatRGB565,
  42. kRGB565 = kCCPixelFormatRGB565,
  43. kPixelFormatRGBA8888 = kCCPixelFormatRGBA8888,
  44. kRGBA8 = kCCPixelFormatRGBA8888,
  45. } tPixelFormat;
  46. /** @typedef tDepthBufferFormat
  47. Possible DepthBuffer Formats for the EAGLView.
  48. Use 16 or 24 bit depth buffers if you are going to use real 3D objects.
  49. @deprecated Will be removed in v1.0
  50. */
  51. typedef enum {
  52. /// A Depth Buffer of 0 bits will be used (default)
  53. kCCDepthBufferNone,
  54. /// A depth buffer of 16 bits will be used
  55. kCCDepthBuffer16,
  56. /// A depth buffer of 24 bits will be used
  57. kCCDepthBuffer24,
  58. // backward compatibility stuff
  59. kDepthBuffer16 = kCCDepthBuffer16,
  60. kDepthBuffer24 = kCCDepthBuffer24,
  61. } tDepthBufferFormat;
  62. /** @typedef ccDirectorProjection
  63. Possible OpenGL projections used by director
  64. */
  65. typedef enum {
  66. /// sets a 2D projection (orthogonal projection)
  67. kCCDirectorProjection2D,
  68. /// sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.
  69. kCCDirectorProjection3D,
  70. /// it does nothing. But if you are using a custom projection set it this value.
  71. kCCDirectorProjectionCustom,
  72. /// Detault projection is 3D projection
  73. kCCDirectorProjectionDefault = kCCDirectorProjection3D,
  74. // backward compatibility stuff
  75. CCDirectorProjection2D = kCCDirectorProjection2D,
  76. CCDirectorProjection3D = kCCDirectorProjection3D,
  77. CCDirectorProjectionCustom = kCCDirectorProjectionCustom,
  78. } ccDirectorProjection;
  79. /** @typedef ccDirectorType
  80. Possible Director Types.
  81. @since v0.8.2
  82. */
  83. typedef enum {
  84. /** Will use a Director that triggers the main loop from an NSTimer object
  85. *
  86. * Features and Limitations:
  87. * - Integrates OK with UIKit objects
  88. * - It the slowest director
  89. * - The invertal update is customizable from 1 to 60
  90. */
  91. kCCDirectorTypeNSTimer,
  92. /** will use a Director that triggers the main loop from a custom main loop.
  93. *
  94. * Features and Limitations:
  95. * - Faster than NSTimer Director
  96. * - It doesn't integrate well with UIKit objecgts
  97. * - The interval update can't be customizable
  98. */
  99. kCCDirectorTypeMainLoop,
  100. /** Will use a Director that triggers the main loop from a thread, but the main loop will be executed on the main thread.
  101. *
  102. * Features and Limitations:
  103. * - Faster than NSTimer Director
  104. * - It doesn't integrate well with UIKit objecgts
  105. * - The interval update can't be customizable
  106. */
  107. kCCDirectorTypeThreadMainLoop,
  108. /** Will use a Director that synchronizes timers with the refresh rate of the display.
  109. *
  110. * Features and Limitations:
  111. * - Faster than NSTimer Director
  112. * - Only available on 3.1+
  113. * - Scheduled timers & drawing are synchronizes with the refresh rate of the display
  114. * - Integrates OK with UIKit objects
  115. * - The interval update can be 1/60, 1/30, 1/15
  116. */
  117. kCCDirectorTypeDisplayLink,
  118. /** Default director is the NSTimer directory */
  119. kCCDirectorTypeDefault = kCCDirectorTypeNSTimer,
  120. // backward compatibility stuff
  121. CCDirectorTypeNSTimer = kCCDirectorTypeNSTimer,
  122. CCDirectorTypeMainLoop = kCCDirectorTypeMainLoop,
  123. CCDirectorTypeThreadMainLoop = kCCDirectorTypeThreadMainLoop,
  124. CCDirectorTypeDisplayLink = kCCDirectorTypeDisplayLink,
  125. CCDirectorTypeDefault = kCCDirectorTypeDefault,
  126. } ccDirectorType;
  127. /** @typedef ccDeviceOrientation
  128. Possible device orientations
  129. */
  130. typedef enum {
  131. /// Device oriented vertically, home button on the bottom
  132. kCCDeviceOrientationPortrait = UIDeviceOrientationPortrait,
  133. /// Device oriented vertically, home button on the top
  134. kCCDeviceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
  135. /// Device oriented horizontally, home button on the right
  136. kCCDeviceOrientationLandscapeLeft = UIDeviceOrientationLandscapeLeft,
  137. /// Device oriented horizontally, home button on the left
  138. kCCDeviceOrientationLandscapeRight = UIDeviceOrientationLandscapeRight,
  139. // Backward compatibility stuff
  140. CCDeviceOrientationPortrait = kCCDeviceOrientationPortrait,
  141. CCDeviceOrientationPortraitUpsideDown = kCCDeviceOrientationPortraitUpsideDown,
  142. CCDeviceOrientationLandscapeLeft = kCCDeviceOrientationLandscapeLeft,
  143. CCDeviceOrientationLandscapeRight = kCCDeviceOrientationLandscapeRight,
  144. } ccDeviceOrientation;
  145. @class CCLabelAtlas;
  146. @class CCScene;
  147. /**Class that creates and handle the main Window and manages how
  148. and when to execute the Scenes.
  149. The CCDirector is also resposible for:
  150. - initializing the OpenGL ES context
  151. - setting the OpenGL ES pixel format (default on is RGB565)
  152. - setting the OpenGL ES buffer depth (default one is 0-bit)
  153. - setting the projection (default one is 2D)
  154. - setting the orientation (default one is Protrait)
  155. Since the CCDirector is a singleton, the standard way to use it is by calling:
  156. - [[CCDirector sharedDirector] xxxx];
  157. The CCDirector also sets the default OpenGL ES context:
  158. - GL_TEXTURE_2D is enabled
  159. - GL_VERTEX_ARRAY is enabled
  160. - GL_COLOR_ARRAY is enabled
  161. - GL_TEXTURE_COORD_ARRAY is enabled
  162. */
  163. @interface CCDirector : NSObject
  164. {
  165. EAGLView *openGLView_;
  166. // internal timer
  167. NSTimeInterval animationInterval_;
  168. NSTimeInterval oldAnimationInterval_;
  169. tPixelFormat pixelFormat_;
  170. tDepthBufferFormat depthBufferFormat_;
  171. /* orientation */
  172. ccDeviceOrientation deviceOrientation_;
  173. /* display FPS ? */
  174. BOOL displayFPS_;
  175. int frames_;
  176. ccTime accumDt_;
  177. ccTime frameRate_;
  178. #if CC_DIRECTOR_FAST_FPS
  179. CCLabelAtlas *FPSLabel_;
  180. #endif
  181. /* is the running scene paused */
  182. BOOL isPaused_;
  183. /* The running scene */
  184. CCScene *runningScene_;
  185. /* will be the next 'runningScene' in the next frame
  186. nextScene is a weak reference. */
  187. CCScene *nextScene_;
  188. /* If YES, then "old" scene will receive the cleanup message */
  189. BOOL sendCleanupToScene_;
  190. /* scheduled scenes */
  191. NSMutableArray *scenesStack_;
  192. /* last time the main loop was updated */
  193. struct timeval lastUpdate_;
  194. /* delta time since last tick to main loop */
  195. ccTime dt;
  196. /* whether or not the next delta time will be zero */
  197. BOOL nextDeltaTimeZero_;
  198. /* projection used */
  199. ccDirectorProjection projection_;
  200. /* screen, different than surface size */
  201. CGSize screenSize_;
  202. /* screen, different than surface size */
  203. CGSize surfaceSize_;
  204. /* content scale factor */
  205. CGFloat contentScaleFactor_;
  206. /* contentScaleFactor could be simulated */
  207. BOOL isContentScaleSupported_;
  208. #if CC_ENABLE_PROFILERS
  209. ccTime accumDtForProfiler_;
  210. #endif
  211. }
  212. /** The current running Scene. Director can only run one Scene at the time */
  213. @property (nonatomic,readonly) CCScene* runningScene;
  214. /** The FPS value */
  215. @property (nonatomic,readwrite, assign) NSTimeInterval animationInterval;
  216. /** Whether or not to display the FPS on the bottom-left corner */
  217. @property (nonatomic,readwrite, assign) BOOL displayFPS;
  218. /** The EAGLView, where everything is rendered */
  219. @property (nonatomic,readwrite,retain) EAGLView *openGLView;
  220. /** Pixel format used to create the context */
  221. @property (nonatomic,readonly) tPixelFormat pixelFormat DEPRECATED_ATTRIBUTE;
  222. /** whether or not the next delta time will be zero */
  223. @property (nonatomic,readwrite,assign) BOOL nextDeltaTimeZero;
  224. /** The device orientation.
  225. If the EAGLView is going to be controlled by an UIViewController, then set the CCDirector orientation to portrait.
  226. */
  227. @property (nonatomic,readwrite) ccDeviceOrientation deviceOrientation;
  228. /** Whether or not the Director is paused */
  229. @property (nonatomic,readonly) BOOL isPaused;
  230. /** Sets an OpenGL projection
  231. @since v0.8.2
  232. */
  233. @property (nonatomic,readwrite) ccDirectorProjection projection;
  234. /** Whether or not the replaced scene will receive the cleanup message.
  235. If the new scene is pushed, then the old scene won't receive the "cleanup" message.
  236. If the new scene replaces the old one, the it will receive the "cleanup" message.
  237. @since v0.99.0
  238. */
  239. @property (nonatomic, readonly) BOOL sendCleanupToScene;
  240. /** The size in pixels of the surface. It could be different than the screen size.
  241. High-res devices might have a higher surface size than the screen size.
  242. In non High-res device the contentScale will be emulated.
  243. Warning: Emulation of High-Res on iOS < 4 is an EXPERIMENTAL feature.
  244. @since v0.99.4
  245. */
  246. @property (nonatomic, readwrite) CGFloat contentScaleFactor;
  247. /** returns a shared instance of the director */
  248. +(CCDirector *)sharedDirector;
  249. + (void) purgeSharedDirector;
  250. /** There are 4 types of Director.
  251. - kCCDirectorTypeNSTimer (default)
  252. - kCCDirectorTypeMainLoop
  253. - kCCDirectorTypeThreadMainLoop
  254. - kCCDirectorTypeDisplayLink
  255. Each Director has it's own benefits, limitations.
  256. If you are using SDK 3.1 or newer it is recommed to use the DisplayLink director
  257. This method should be called before any other call to the director.
  258. It will return NO if the director type is kCCDirectorTypeDisplayLink and the running SDK is < 3.1. Otherwise it will return YES.
  259. @since v0.8.2
  260. */
  261. +(BOOL) setDirectorType:(ccDirectorType) directorType;
  262. // iPhone Specific
  263. /** Uses a new pixel format for the EAGLView.
  264. Call this class method before attaching it to a UIView
  265. Default pixel format: kRGB565. Supported pixel formats: kRGBA8 and kRGB565
  266. @deprecated Set the pixel format when creating the EAGLView. This method will be removed in v1.0
  267. */
  268. -(void) setPixelFormat: (tPixelFormat)p DEPRECATED_ATTRIBUTE;
  269. /** Change depth buffer format of the render buffer.
  270. Call this class method before attaching it to a UIWindow/UIView
  271. Default depth buffer: 0 (none). Supported: kCCDepthBufferNone, kCCDepthBuffer16, and kCCDepthBuffer24
  272. @deprecated Set the depth buffer format when creating the EAGLView. This method will be removed in v1.0
  273. */
  274. -(void) setDepthBufferFormat: (tDepthBufferFormat)db DEPRECATED_ATTRIBUTE;
  275. // Integration with UIKit
  276. /** detach the cocos2d view from the view/window */
  277. -(BOOL)detach DEPRECATED_ATTRIBUTE;
  278. /** attach in UIWindow using the full frame.
  279. It will create a EAGLView.
  280. @deprecated set setOpenGLView instead. Will be removed in v1.0
  281. */
  282. -(BOOL)attachInWindow:(UIWindow *)window DEPRECATED_ATTRIBUTE;
  283. /** attach in UIView using the full frame.
  284. It will create a EAGLView.
  285. @deprecated set setOpenGLView instead. Will be removed in v1.0
  286. */
  287. -(BOOL)attachInView:(UIView *)view DEPRECATED_ATTRIBUTE;
  288. /** attach in UIView using the given frame.
  289. It will create a EAGLView and use it.
  290. @deprecated set setOpenGLView instead. Will be removed in v1.0
  291. */
  292. -(BOOL)attachInView:(UIView *)view withFrame:(CGRect)frame DEPRECATED_ATTRIBUTE;
  293. // Landscape
  294. /** returns the size of the OpenGL view in pixels, according to the landspace */
  295. - (CGSize) winSize;
  296. /** returns the display size of the OpenGL view in pixels */
  297. -(CGSize) displaySize;
  298. /** converts a UIKit coordinate to an OpenGL coordinate
  299. Useful to convert (multi) touchs coordinates to the current layout (portrait or landscape)
  300. */
  301. -(CGPoint) convertToGL: (CGPoint) p;
  302. /** converts an OpenGL coordinate to a UIKit coordinate
  303. Useful to convert node points to window points for calls such as glScissor
  304. */
  305. -(CGPoint) convertToUI:(CGPoint)p;
  306. // rotates the screen if an orientation differnent than Portrait is used
  307. -(void) applyOrientation;
  308. /// XXX: missing description
  309. -(float) getZEye;
  310. // Scene Management
  311. /**Enters the Director's main loop with the given Scene.
  312. * Call it to run only your FIRST scene.
  313. * Don't call it if there is already a running scene.
  314. */
  315. - (void) runWithScene:(CCScene*) scene;
  316. /**Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
  317. * The new scene will be executed.
  318. * Try to avoid big stacks of pushed scenes to reduce memory allocation.
  319. * ONLY call it if there is a running scene.
  320. */
  321. - (void) pushScene:(CCScene*) scene;
  322. /**Pops out a scene from the queue.
  323. * This scene will replace the running one.
  324. * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
  325. * ONLY call it if there is a running scene.
  326. */
  327. - (void) popScene;
  328. /** Replaces the running scene with a new one. The running scene is terminated.
  329. * ONLY call it if there is a running scene.
  330. */
  331. -(void) replaceScene: (CCScene*) scene;
  332. /** Ends the execution, releases the running scene.
  333. It doesn't remove the OpenGL view from its parent. You have to do it manually.
  334. */
  335. -(void) end;
  336. /** Pauses the running scene.
  337. The running scene will be _drawed_ but all scheduled timers will be paused
  338. While paused, the draw rate will be 4 FPS to reduce CPU consuption
  339. */
  340. -(void) pause;
  341. /** Resumes the paused scene
  342. The scheduled timers will be activated again.
  343. The "delta time" will be 0 (as if the game wasn't paused)
  344. */
  345. -(void) resume;
  346. /** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
  347. If you wan't to pause your animation call [pause] instead.
  348. */
  349. -(void) stopAnimation;
  350. /** The main loop is triggered again.
  351. Call this function only if [stopAnimation] was called earlier
  352. @warning Dont' call this function to start the main loop. To run the main loop call runWithScene
  353. */
  354. -(void) startAnimation;
  355. /** Draw the scene.
  356. This method is called every frame. Don't call it manually.
  357. */
  358. -(void) drawScene;
  359. // Memory Helper
  360. /** Removes all the cocos2d data that was cached automatically.
  361. It will purge the CCTextureCache, CCBitmapFont cache.
  362. IMPORTANT: The CCSpriteFrameCache won't be purged. If you want to purge it, you have to purge it manually.
  363. @since v0.99.3
  364. */
  365. -(void) purgeCachedData;
  366. // OpenGL Helper
  367. /** sets the OpenGL default values */
  368. -(void) setGLDefaultValues;
  369. /** enables/disables OpenGL alpha blending */
  370. - (void) setAlphaBlending: (BOOL) on;
  371. /** enables/disables OpenGL depth test */
  372. - (void) setDepthTest: (BOOL) on;
  373. /** recalculate the projection view and projection size based on the EAGLVIEW
  374. @since v0.99.4
  375. */
  376. - (void) recalculateProjectionAndEAGLViewSize;
  377. @end
  378. /** FastDirector is a Director that triggers the main loop as fast as possible.
  379. *
  380. * Features and Limitations:
  381. * - Faster than "normal" director
  382. * - Consumes more battery than the "normal" director
  383. * - It has some issues while using UIKit objects
  384. */
  385. @interface CCFastDirector : CCDirector
  386. {
  387. BOOL isRunning;
  388. NSAutoreleasePool *autoreleasePool;
  389. }
  390. -(void) preMainLoop;
  391. @end
  392. /** ThreadedFastDirector is a Director that triggers the main loop from a thread.
  393. *
  394. * Features and Limitations:
  395. * - Faster than "normal" director
  396. * - Consumes more battery than the "normal" director
  397. * - It can be used with UIKit objects
  398. *
  399. * @since v0.8.2
  400. */
  401. @interface CCThreadedFastDirector : CCDirector
  402. {
  403. BOOL isRunning;
  404. }
  405. -(void) preMainLoop;
  406. @end
  407. /** DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display.
  408. *
  409. * Features and Limitations:
  410. * - Only available on 3.1+
  411. * - Scheduled timers & drawing are synchronizes with the refresh rate of the display
  412. * - Only supports animation intervals of 1/60 1/30 & 1/15
  413. *
  414. * It is the recommended Director if the SDK is 3.1 or newer
  415. *
  416. * @since v0.8.2
  417. */
  418. @interface CCDisplayLinkDirector : CCDirector
  419. {
  420. id displayLink;
  421. }
  422. -(void) preMainLoop:(id)sender;
  423. @end
  424. /** TimerDirector is a Director that calls the main loop from an NSTimer object
  425. *
  426. * Features and Limitations:
  427. * - Integrates OK with UIKit objects
  428. * - It the slowest director
  429. * - The invertal update is customizable from 1 to 60
  430. *
  431. * It is the default Director.
  432. */
  433. @interface CCTimerDirector : CCDirector
  434. {
  435. NSTimer *animationTimer;
  436. }
  437. @end