CCDirector.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2011-2012 cocos2d-x.org
  4. Copyright (c) 2013-2014 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  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. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. cc.g_NumberOfDraws = 0;
  23. cc.GLToClipTransform = function (transformOut) {
  24. var projection = new cc.kmMat4();
  25. cc.kmGLGetMatrix(cc.KM_GL_PROJECTION, projection);
  26. var modelview = new cc.kmMat4();
  27. cc.kmGLGetMatrix(cc.KM_GL_MODELVIEW, modelview);
  28. cc.kmMat4Multiply(transformOut, projection, modelview);
  29. };
  30. //----------------------------------------------------------------------------------------------------------------------
  31. /**
  32. * <p>
  33. * ATTENTION: USE cc.director INSTEAD OF cc.Director.<br/>
  34. * cc.director is a singleton object which manage your game's logic flow.<br/>
  35. * Since the cc.director is a singleton, you don't need to call any constructor or create functions,<br/>
  36. * the standard way to use it is by calling:<br/>
  37. * - cc.director.methodName(); <br/>
  38. *
  39. * It creates and handle the main Window and manages how and when to execute the Scenes.<br/>
  40. * <br/>
  41. * The cc.director is also responsible for:<br/>
  42. * - initializing the OpenGL context<br/>
  43. * - setting the OpenGL pixel format (default on is RGB565)<br/>
  44. * - setting the OpenGL pixel format (default on is RGB565)<br/>
  45. * - setting the OpenGL buffer depth (default one is 0-bit)<br/>
  46. * - setting the projection (default one is 3D)<br/>
  47. * - setting the orientation (default one is Portrait)<br/>
  48. * <br/>
  49. * <br/>
  50. * The cc.director also sets the default OpenGL context:<br/>
  51. * - GL_TEXTURE_2D is enabled<br/>
  52. * - GL_VERTEX_ARRAY is enabled<br/>
  53. * - GL_COLOR_ARRAY is enabled<br/>
  54. * - GL_TEXTURE_COORD_ARRAY is enabled<br/>
  55. * </p>
  56. * <p>
  57. * cc.director also synchronizes timers with the refresh rate of the display.<br/>
  58. * Features and Limitations:<br/>
  59. * - Scheduled timers & drawing are synchronizes with the refresh rate of the display<br/>
  60. * - Only supports animation intervals of 1/60 1/30 & 1/15<br/>
  61. * </p>
  62. * @class
  63. * @name cc.Director
  64. */
  65. cc.Director = cc.Class.extend(/** @lends cc.Director# */{
  66. //Variables
  67. _landscape: false,
  68. _nextDeltaTimeZero: false,
  69. _paused: false,
  70. _purgeDirectorInNextLoop: false,
  71. _sendCleanupToScene: false,
  72. _animationInterval: 0.0,
  73. _oldAnimationInterval: 0.0,
  74. _projection: 0,
  75. _accumDt: 0.0,
  76. _contentScaleFactor: 1.0,
  77. _displayStats: false,
  78. _deltaTime: 0.0,
  79. _frameRate: 0.0,
  80. _FPSLabel: null,
  81. _SPFLabel: null,
  82. _drawsLabel: null,
  83. _winSizeInPoints: null,
  84. _lastUpdate: null,
  85. _nextScene: null,
  86. _notificationNode: null,
  87. _openGLView: null,
  88. _scenesStack: null,
  89. _projectionDelegate: null,
  90. _runningScene: null,
  91. _frames: 0,
  92. _totalFrames: 0,
  93. _secondsPerFrame: 0,
  94. _dirtyRegion: null,
  95. _scheduler: null,
  96. _actionManager: null,
  97. _eventProjectionChanged: null,
  98. _eventAfterDraw: null,
  99. _eventAfterVisit: null,
  100. _eventAfterUpdate: null,
  101. ctor: function () {
  102. var self = this;
  103. self._lastUpdate = Date.now();
  104. cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function () {
  105. self._lastUpdate = Date.now();
  106. });
  107. },
  108. init: function () {
  109. // scenes
  110. this._oldAnimationInterval = this._animationInterval = 1.0 / cc.defaultFPS;
  111. this._scenesStack = [];
  112. // Set default projection (3D)
  113. this._projection = cc.Director.PROJECTION_DEFAULT;
  114. // projection delegate if "Custom" projection is used
  115. this._projectionDelegate = null;
  116. //FPS
  117. this._accumDt = 0;
  118. this._frameRate = 0;
  119. this._displayStats = false;//can remove
  120. this._totalFrames = this._frames = 0;
  121. this._lastUpdate = Date.now();
  122. //Paused?
  123. this._paused = false;
  124. //purge?
  125. this._purgeDirectorInNextLoop = false;
  126. this._winSizeInPoints = cc.size(0, 0);
  127. this._openGLView = null;
  128. this._contentScaleFactor = 1.0;
  129. //scheduler
  130. this._scheduler = new cc.Scheduler();
  131. //action manager
  132. this._actionManager = cc.ActionManager ? new cc.ActionManager() : null;
  133. this._scheduler.scheduleUpdateForTarget(this._actionManager, cc.Scheduler.PRIORITY_SYSTEM, false);
  134. this._eventAfterDraw = new cc.EventCustom(cc.Director.EVENT_AFTER_DRAW);
  135. this._eventAfterDraw.setUserData(this);
  136. this._eventAfterVisit = new cc.EventCustom(cc.Director.EVENT_AFTER_VISIT);
  137. this._eventAfterVisit.setUserData(this);
  138. this._eventAfterUpdate = new cc.EventCustom(cc.Director.EVENT_AFTER_UPDATE);
  139. this._eventAfterUpdate.setUserData(this);
  140. this._eventProjectionChanged = new cc.EventCustom(cc.Director.EVENT_PROJECTION_CHANGED);
  141. this._eventProjectionChanged.setUserData(this);
  142. return true;
  143. },
  144. /**
  145. * calculates delta time since last time it was called
  146. */
  147. calculateDeltaTime: function () {
  148. var now = Date.now();
  149. // new delta time.
  150. if (this._nextDeltaTimeZero) {
  151. this._deltaTime = 0;
  152. this._nextDeltaTimeZero = false;
  153. } else {
  154. this._deltaTime = (now - this._lastUpdate) / 1000;
  155. }
  156. if ((cc.game.config[cc.game.CONFIG_KEY.debugMode] > 0) && (this._deltaTime > 0.2))
  157. this._deltaTime = 1 / 60.0;
  158. this._lastUpdate = now;
  159. },
  160. /**
  161. * Converts a view coordinate to an WebGL coordinate<br/>
  162. * Useful to convert (multi) touches coordinates to the current layout (portrait or landscape)<br/>
  163. * Implementation can be found in CCDirectorWebGL
  164. * @function
  165. * @param {cc.Point} uiPoint
  166. * @return {cc.Point}
  167. */
  168. convertToGL: null,
  169. /**
  170. * Converts an WebGL coordinate to a view coordinate<br/>
  171. * Useful to convert node points to window points for calls such as glScissor<br/>
  172. * Implementation can be found in CCDirectorWebGL
  173. * @function
  174. * @param {cc.Point} glPoint
  175. * @return {cc.Point}
  176. */
  177. convertToUI: null,
  178. /**
  179. * Draw the scene. This method is called every frame. Don't call it manually.
  180. */
  181. drawScene: function () {
  182. // calculate "global" dt
  183. this.calculateDeltaTime();
  184. //tick before glClear: issue #533
  185. if (!this._paused) {
  186. this._scheduler.update(this._deltaTime);
  187. cc.eventManager.dispatchEvent(this._eventAfterUpdate);
  188. }
  189. this._clear();
  190. /* to avoid flickr, nextScene MUST be here: after tick and before draw.
  191. XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
  192. if (this._nextScene) {
  193. this.setNextScene();
  194. }
  195. if (this._beforeVisitScene) this._beforeVisitScene();
  196. // draw the scene
  197. if (this._runningScene) {
  198. this._runningScene.visit();
  199. cc.eventManager.dispatchEvent(this._eventAfterVisit);
  200. }
  201. // draw the notifications node
  202. if (this._notificationNode)
  203. this._notificationNode.visit();
  204. if (this._displayStats)
  205. this._showStats();
  206. if (this._afterVisitScene) this._afterVisitScene();
  207. //TODO
  208. cc.eventManager.dispatchEvent(this._eventAfterDraw);
  209. this._totalFrames++;
  210. if (this._displayStats)
  211. this._calculateMPF();
  212. },
  213. _beforeVisitScene: null,
  214. _afterVisitScene: null,
  215. /**
  216. * End the life of director in the next frame
  217. */
  218. end: function () {
  219. this._purgeDirectorInNextLoop = true;
  220. },
  221. /**
  222. * Returns the size in pixels of the surface. It could be different than the screen size.<br/>
  223. * High-res devices might have a higher surface size than the screen size.
  224. * @return {Number}
  225. */
  226. getContentScaleFactor: function () {
  227. return this._contentScaleFactor;
  228. },
  229. /**
  230. * This object will be visited after the main scene is visited.<br/>
  231. * This object MUST implement the "visit" selector.<br/>
  232. * Useful to hook a notification object
  233. * @return {cc.Node}
  234. */
  235. getNotificationNode: function () {
  236. return this._notificationNode;
  237. },
  238. /**
  239. * Returns the size of the WebGL view in points.<br/>
  240. * It takes into account any possible rotation (device orientation) of the window
  241. * @return {cc.Size}
  242. */
  243. getWinSize: function () {
  244. return cc.size(this._winSizeInPoints);
  245. },
  246. /**
  247. * Returns the size of the OpenGL view in pixels.<br/>
  248. * It takes into account any possible rotation (device orientation) of the window.<br/>
  249. * On Mac winSize and winSizeInPixels return the same value.
  250. * @return {cc.Size}
  251. */
  252. getWinSizeInPixels: function () {
  253. return cc.size(this._winSizeInPoints.width * this._contentScaleFactor, this._winSizeInPoints.height * this._contentScaleFactor);
  254. },
  255. /**
  256. * getVisibleSize/getVisibleOrigin move to CCDirectorWebGL/CCDirectorCanvas
  257. * getZEye move to CCDirectorWebGL
  258. */
  259. /**
  260. * Returns the visible size of the running scene
  261. * @function
  262. * @return {cc.Size}
  263. */
  264. getVisibleSize: null,
  265. /**
  266. * Returns the visible origin of the running scene
  267. * @function
  268. * @return {cc.Point}
  269. */
  270. getVisibleOrigin: null,
  271. /**
  272. * Returns the z eye, only available in WebGL mode
  273. * @function
  274. * @return {Number}
  275. */
  276. getZEye: null,
  277. /**
  278. * Pause the director's ticker
  279. */
  280. pause: function () {
  281. if (this._paused)
  282. return;
  283. this._oldAnimationInterval = this._animationInterval;
  284. // when paused, don't consume CPU
  285. this.setAnimationInterval(1 / 4.0);
  286. this._paused = true;
  287. },
  288. /**
  289. * Pops out a scene from the queue.<br/>
  290. * This scene will replace the running one.<br/>
  291. * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.<br/>
  292. * ONLY call it if there is a running scene.
  293. */
  294. popScene: function () {
  295. cc.assert(this._runningScene, cc._LogInfos.Director_popScene);
  296. this._scenesStack.pop();
  297. var c = this._scenesStack.length;
  298. if (c == 0)
  299. this.end();
  300. else {
  301. this._sendCleanupToScene = true;
  302. this._nextScene = this._scenesStack[c - 1];
  303. }
  304. },
  305. /**
  306. * Removes cached all cocos2d cached data. It will purge the cc.textureCache, cc.spriteFrameCache, cc.animationCache
  307. */
  308. purgeCachedData: function () {
  309. cc.animationCache._clear();
  310. cc.spriteFrameCache._clear();
  311. cc.textureCache._clear();
  312. },
  313. /**
  314. * Purge the cc.director itself, including unschedule all schedule, remove all event listeners, clean up and exit the running scene, stops all animations, clear cached data.
  315. */
  316. purgeDirector: function () {
  317. //cleanup scheduler
  318. this.getScheduler().unscheduleAllCallbacks();
  319. // Disable event dispatching
  320. if (cc.eventManager)
  321. cc.eventManager.setEnabled(false);
  322. // don't release the event handlers
  323. // They are needed in case the director is run again
  324. if (this._runningScene) {
  325. this._runningScene.onExitTransitionDidStart();
  326. this._runningScene.onExit();
  327. this._runningScene.cleanup();
  328. }
  329. this._runningScene = null;
  330. this._nextScene = null;
  331. // remove all objects, but don't release it.
  332. // runScene might be executed after 'end'.
  333. this._scenesStack.length = 0;
  334. this.stopAnimation();
  335. // Clear all caches
  336. this.purgeCachedData();
  337. cc.checkGLErrorDebug();
  338. },
  339. /**
  340. * Suspends the execution of the running scene, pushing it on the stack of suspended scenes.<br/>
  341. * The new scene will be executed.<br/>
  342. * Try to avoid big stacks of pushed scenes to reduce memory allocation.<br/>
  343. * ONLY call it if there is a running scene.
  344. * @param {cc.Scene} scene
  345. */
  346. pushScene: function (scene) {
  347. cc.assert(scene, cc._LogInfos.Director_pushScene);
  348. this._sendCleanupToScene = false;
  349. this._scenesStack.push(scene);
  350. this._nextScene = scene;
  351. },
  352. /**
  353. * Run a scene. Replaces the running scene with a new one or enter the first scene.
  354. * @param {cc.Scene} scene
  355. */
  356. runScene: function (scene) {
  357. cc.assert(scene, cc._LogInfos.Director_pushScene);
  358. if (!this._runningScene) {
  359. //start scene
  360. this.pushScene(scene);
  361. this.startAnimation();
  362. } else {
  363. //replace scene
  364. var i = this._scenesStack.length;
  365. if (i === 0) {
  366. this._sendCleanupToScene = true;
  367. this._scenesStack[i] = scene;
  368. this._nextScene = scene;
  369. } else {
  370. this._sendCleanupToScene = true;
  371. this._scenesStack[i - 1] = scene;
  372. this._nextScene = scene;
  373. }
  374. }
  375. },
  376. /**
  377. * Resume director after pause, if the current scene is not paused, nothing will happen.
  378. */
  379. resume: function () {
  380. if (!this._paused) {
  381. return;
  382. }
  383. this.setAnimationInterval(this._oldAnimationInterval);
  384. this._lastUpdate = Date.now();
  385. if (!this._lastUpdate) {
  386. cc.log(cc._LogInfos.Director_resume);
  387. }
  388. this._paused = false;
  389. this._deltaTime = 0;
  390. },
  391. /**
  392. * The size in pixels of the surface. It could be different than the screen size.<br/>
  393. * High-res devices might have a higher surface size than the screen size.
  394. * @param {Number} scaleFactor
  395. */
  396. setContentScaleFactor: function (scaleFactor) {
  397. if (scaleFactor != this._contentScaleFactor) {
  398. this._contentScaleFactor = scaleFactor;
  399. this._createStatsLabel();
  400. }
  401. },
  402. /**
  403. * Enables or disables WebGL depth test.<br/>
  404. * Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js
  405. * @function
  406. * @param {Boolean} on
  407. */
  408. setDepthTest: null,
  409. /**
  410. * Sets the default values based on the CCConfiguration info
  411. */
  412. setDefaultValues: function () {
  413. },
  414. /**
  415. * Sets whether next delta time equals to zero
  416. * @param {Boolean} nextDeltaTimeZero
  417. */
  418. setNextDeltaTimeZero: function (nextDeltaTimeZero) {
  419. this._nextDeltaTimeZero = nextDeltaTimeZero;
  420. },
  421. /**
  422. * Starts the registered next scene
  423. */
  424. setNextScene: function () {
  425. var runningIsTransition = false, newIsTransition = false;
  426. if (cc.TransitionScene) {
  427. runningIsTransition = this._runningScene ? this._runningScene instanceof cc.TransitionScene : false;
  428. newIsTransition = this._nextScene ? this._nextScene instanceof cc.TransitionScene : false;
  429. }
  430. // If it is not a transition, call onExit/cleanup
  431. if (!newIsTransition) {
  432. var locRunningScene = this._runningScene;
  433. if (locRunningScene) {
  434. locRunningScene.onExitTransitionDidStart();
  435. locRunningScene.onExit();
  436. }
  437. // issue #709. the root node (scene) should receive the cleanup message too
  438. // otherwise it might be leaked.
  439. if (this._sendCleanupToScene && locRunningScene)
  440. locRunningScene.cleanup();
  441. }
  442. this._runningScene = this._nextScene;
  443. this._nextScene = null;
  444. if ((!runningIsTransition) && (this._runningScene != null)) {
  445. this._runningScene.onEnter();
  446. this._runningScene.onEnterTransitionDidFinish();
  447. }
  448. },
  449. /**
  450. * Sets Notification Node
  451. * @param {cc.Node} node
  452. */
  453. setNotificationNode: function (node) {
  454. this._notificationNode = node;
  455. },
  456. /**
  457. * Returns the cc.director delegate.
  458. * @return {cc.DirectorDelegate}
  459. */
  460. getDelegate: function () {
  461. return this._projectionDelegate;
  462. },
  463. /**
  464. * Sets the cc.director delegate. It shall implement the CCDirectorDelegate protocol
  465. * @return {cc.DirectorDelegate}
  466. */
  467. setDelegate: function (delegate) {
  468. this._projectionDelegate = delegate;
  469. },
  470. /**
  471. * Sets the view, where everything is rendered, do not call this function.<br/>
  472. * Implementation can be found in CCDirectorCanvas.js/CCDirectorWebGL.js.
  473. * @function
  474. * @param {cc.view} openGLView
  475. */
  476. setOpenGLView: null,
  477. /**
  478. * Sets an OpenGL projection.<br/>
  479. * Implementation can be found in CCDiretorCanvas.js/CCDiretorWebGL.js.
  480. * @function
  481. * @param {Number} projection
  482. */
  483. setProjection: null,
  484. /**
  485. * Update the view port.<br/>
  486. * Implementation can be found in CCDiretorCanvas.js/CCDiretorWebGL.js.
  487. * @function
  488. */
  489. setViewport: null,
  490. /**
  491. * Get the CCEGLView, where everything is rendered.<br/>
  492. * Implementation can be found in CCDiretorCanvas.js/CCDiretorWebGL.js.
  493. * @function
  494. * @return {cc.view}
  495. */
  496. getOpenGLView: null,
  497. /**
  498. * Sets an OpenGL projection.<br/>
  499. * Implementation can be found in CCDiretorCanvas.js/CCDiretorWebGL.js.
  500. * @function
  501. * @return {Number}
  502. */
  503. getProjection: null,
  504. /**
  505. * Enables/disables OpenGL alpha blending.<br/>
  506. * Implementation can be found in CCDiretorCanvas.js/CCDiretorWebGL.js.
  507. * @function
  508. * @param {Boolean} on
  509. */
  510. setAlphaBlending: null,
  511. _showStats: function () {
  512. this._frames++;
  513. this._accumDt += this._deltaTime;
  514. if (this._FPSLabel && this._SPFLabel && this._drawsLabel) {
  515. if (this._accumDt > cc.DIRECTOR_FPS_INTERVAL) {
  516. this._SPFLabel.string = this._secondsPerFrame.toFixed(3);
  517. this._frameRate = this._frames / this._accumDt;
  518. this._frames = 0;
  519. this._accumDt = 0;
  520. this._FPSLabel.string = this._frameRate.toFixed(1);
  521. this._drawsLabel.string = (0 | cc.g_NumberOfDraws).toString();
  522. }
  523. this._FPSLabel.visit();
  524. this._SPFLabel.visit();
  525. this._drawsLabel.visit();
  526. } else
  527. this._createStatsLabel();
  528. cc.g_NumberOfDraws = 0;
  529. },
  530. /**
  531. * Returns whether or not the replaced scene will receive the cleanup message.<br>
  532. * If the new scene is pushed, then the old scene won't receive the "cleanup" message.<br/>
  533. * If the new scene replaces the old one, the it will receive the "cleanup" message.
  534. * @return {Boolean}
  535. */
  536. isSendCleanupToScene: function () {
  537. return this._sendCleanupToScene;
  538. },
  539. /**
  540. * Returns current running Scene. Director can only run one Scene at the time
  541. * @return {cc.Scene}
  542. */
  543. getRunningScene: function () {
  544. return this._runningScene;
  545. },
  546. /**
  547. * Returns the FPS value
  548. * @return {Number}
  549. */
  550. getAnimationInterval: function () {
  551. return this._animationInterval;
  552. },
  553. /**
  554. * Returns whether or not to display the FPS informations
  555. * @return {Boolean}
  556. */
  557. isDisplayStats: function () {
  558. return this._displayStats;
  559. },
  560. /**
  561. * Sets whether display the FPS on the bottom-left corner
  562. * @param {Boolean} displayStats
  563. */
  564. setDisplayStats: function (displayStats) {
  565. this._displayStats = displayStats;
  566. },
  567. /**
  568. * Returns seconds per frame
  569. * @return {Number}
  570. */
  571. getSecondsPerFrame: function () {
  572. return this._secondsPerFrame;
  573. },
  574. /**
  575. * Returns whether next delta time equals to zero
  576. * @return {Boolean}
  577. */
  578. isNextDeltaTimeZero: function () {
  579. return this._nextDeltaTimeZero;
  580. },
  581. /**
  582. * Returns whether or not the Director is paused
  583. * @return {Boolean}
  584. */
  585. isPaused: function () {
  586. return this._paused;
  587. },
  588. /**
  589. * Returns how many frames were called since the director started
  590. * @return {Number}
  591. */
  592. getTotalFrames: function () {
  593. return this._totalFrames;
  594. },
  595. /**
  596. * Pops out all scenes from the queue until the root scene in the queue. <br/>
  597. * This scene will replace the running one. <br/>
  598. * Internally it will call "popToSceneStackLevel(1)"
  599. */
  600. popToRootScene: function () {
  601. this.popToSceneStackLevel(1);
  602. },
  603. /**
  604. * Pops out all scenes from the queue until it reaches "level". <br/>
  605. * If level is 0, it will end the director. <br/>
  606. * If level is 1, it will pop all scenes until it reaches to root scene. <br/>
  607. * If level is <= than the current stack level, it won't do anything.
  608. * @param {Number} level
  609. */
  610. popToSceneStackLevel: function (level) {
  611. cc.assert(this._runningScene, cc._LogInfos.Director_popToSceneStackLevel_2);
  612. var locScenesStack = this._scenesStack;
  613. var c = locScenesStack.length;
  614. if (c == 0) {
  615. this.end();
  616. return;
  617. }
  618. // current level or lower -> nothing
  619. if (level > c)
  620. return;
  621. // pop stack until reaching desired level
  622. while (c > level) {
  623. var current = locScenesStack.pop();
  624. if (current.running) {
  625. current.onExitTransitionDidStart();
  626. current.onExit();
  627. }
  628. current.cleanup();
  629. c--;
  630. }
  631. this._nextScene = locScenesStack[locScenesStack.length - 1];
  632. this._sendCleanupToScene = false;
  633. },
  634. /**
  635. * Returns the cc.Scheduler associated with this director
  636. * @return {cc.Scheduler}
  637. */
  638. getScheduler: function () {
  639. return this._scheduler;
  640. },
  641. /**
  642. * Sets the cc.Scheduler associated with this director
  643. * @param {cc.Scheduler} scheduler
  644. */
  645. setScheduler: function (scheduler) {
  646. if (this._scheduler != scheduler) {
  647. this._scheduler = scheduler;
  648. }
  649. },
  650. /**
  651. * Returns the cc.ActionManager associated with this director
  652. * @return {cc.ActionManager}
  653. */
  654. getActionManager: function () {
  655. return this._actionManager;
  656. },
  657. /**
  658. * Sets the cc.ActionManager associated with this director
  659. * @param {cc.ActionManager} actionManager
  660. */
  661. setActionManager: function (actionManager) {
  662. if (this._actionManager != actionManager) {
  663. this._actionManager = actionManager;
  664. }
  665. },
  666. /**
  667. * Returns the delta time since last frame
  668. * @return {Number}
  669. */
  670. getDeltaTime: function () {
  671. return this._deltaTime;
  672. },
  673. _createStatsLabel: null,
  674. _calculateMPF: function () {
  675. var now = Date.now();
  676. this._secondsPerFrame = (now - this._lastUpdate) / 1000;
  677. }
  678. });
  679. /**
  680. * The event projection changed of cc.Director
  681. * @constant
  682. * @type {string}
  683. * @example
  684. * cc.eventManager.addCustomListener(cc.Director.EVENT_PROJECTION_CHANGED, function(event) {
  685. * cc.log("Projection changed.");
  686. * });
  687. */
  688. cc.Director.EVENT_PROJECTION_CHANGED = "director_projection_changed";
  689. /**
  690. * The event after draw of cc.Director
  691. * @constant
  692. * @type {string}
  693. * @example
  694. * cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_DRAW, function(event) {
  695. * cc.log("after draw event.");
  696. * });
  697. */
  698. cc.Director.EVENT_AFTER_DRAW = "director_after_draw";
  699. /**
  700. * The event after visit of cc.Director
  701. * @constant
  702. * @type {string}
  703. * @example
  704. * cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_VISIT, function(event) {
  705. * cc.log("after visit event.");
  706. * });
  707. */
  708. cc.Director.EVENT_AFTER_VISIT = "director_after_visit";
  709. /**
  710. * The event after update of cc.Director
  711. * @constant
  712. * @type {string}
  713. * @example
  714. * cc.eventManager.addCustomListener(cc.Director.EVENT_AFTER_UPDATE, function(event) {
  715. * cc.log("after update event.");
  716. * });
  717. */
  718. cc.Director.EVENT_AFTER_UPDATE = "director_after_update";
  719. /***************************************************
  720. * implementation of DisplayLinkDirector
  721. **************************************************/
  722. cc.DisplayLinkDirector = cc.Director.extend(/** @lends cc.Director# */{
  723. invalid: false,
  724. /**
  725. * Starts Animation
  726. */
  727. startAnimation: function () {
  728. this._nextDeltaTimeZero = true;
  729. this.invalid = false;
  730. },
  731. /**
  732. * Run main loop of director
  733. */
  734. mainLoop: function () {
  735. if (this._purgeDirectorInNextLoop) {
  736. this._purgeDirectorInNextLoop = false;
  737. this.purgeDirector();
  738. }
  739. else if (!this.invalid) {
  740. this.drawScene();
  741. }
  742. },
  743. /**
  744. * Stops animation
  745. */
  746. stopAnimation: function () {
  747. this.invalid = true;
  748. },
  749. /**
  750. * Sets animation interval
  751. * @param {Number} value the animation interval desired
  752. */
  753. setAnimationInterval: function (value) {
  754. this._animationInterval = value;
  755. if (!this.invalid) {
  756. this.stopAnimation();
  757. this.startAnimation();
  758. }
  759. }
  760. });
  761. cc.Director.sharedDirector = null;
  762. cc.Director.firstUseDirector = true;
  763. cc.Director._getInstance = function () {
  764. if (cc.Director.firstUseDirector) {
  765. cc.Director.firstUseDirector = false;
  766. cc.Director.sharedDirector = new cc.DisplayLinkDirector();
  767. cc.Director.sharedDirector.init();
  768. }
  769. return cc.Director.sharedDirector;
  770. };
  771. /**
  772. * Default fps is 60
  773. * @type {Number}
  774. */
  775. cc.defaultFPS = 60;
  776. //Possible OpenGL projections used by director
  777. /**
  778. * Constant for 2D projection (orthogonal projection)
  779. * @constant
  780. * @type {Number}
  781. */
  782. cc.Director.PROJECTION_2D = 0;
  783. /**
  784. * Constant for 3D projection with a fovy=60, znear=0.5f and zfar=1500.
  785. * @constant
  786. * @type {Number}
  787. */
  788. cc.Director.PROJECTION_3D = 1;
  789. /**
  790. * Constant for custom projection, if cc.Director's projection set to it, it calls "updateProjection" on the projection delegate.
  791. * @constant
  792. * @type {Number}
  793. */
  794. cc.Director.PROJECTION_CUSTOM = 3;
  795. /**
  796. * Constant for default projection of cc.Director, default projection is 3D projection
  797. * @constant
  798. * @type {Number}
  799. */
  800. cc.Director.PROJECTION_DEFAULT = cc.Director.PROJECTION_3D;
  801. if (cc._renderType === cc._RENDER_TYPE_CANVAS) {
  802. var _p = cc.Director.prototype;
  803. _p.setProjection = function (projection) {
  804. this._projection = projection;
  805. cc.eventManager.dispatchEvent(this._eventProjectionChanged);
  806. };
  807. _p.setDepthTest = function () {
  808. };
  809. _p.setOpenGLView = function (openGLView) {
  810. // set size
  811. this._winSizeInPoints.width = cc._canvas.width; //this._openGLView.getDesignResolutionSize();
  812. this._winSizeInPoints.height = cc._canvas.height;
  813. this._openGLView = openGLView || cc.view;
  814. if (cc.eventManager)
  815. cc.eventManager.setEnabled(true);
  816. };
  817. _p._clear = function () {
  818. var viewport = this._openGLView.getViewPortRect();
  819. cc._renderContext.clearRect(-viewport.x, viewport.y, viewport.width, -viewport.height);
  820. };
  821. _p._createStatsLabel = function () {
  822. var _t = this;
  823. var fontSize = 0;
  824. if (_t._winSizeInPoints.width > _t._winSizeInPoints.height)
  825. fontSize = 0 | (_t._winSizeInPoints.height / 320 * 24);
  826. else
  827. fontSize = 0 | (_t._winSizeInPoints.width / 320 * 24);
  828. _t._FPSLabel = cc.LabelTTF.create("000.0", "Arial", fontSize);
  829. _t._SPFLabel = cc.LabelTTF.create("0.000", "Arial", fontSize);
  830. _t._drawsLabel = cc.LabelTTF.create("0000", "Arial", fontSize);
  831. var locStatsPosition = cc.DIRECTOR_STATS_POSITION;
  832. _t._drawsLabel.setPosition(_t._drawsLabel.width / 2 + locStatsPosition.x, _t._drawsLabel.height * 5 / 2 + locStatsPosition.y);
  833. _t._SPFLabel.setPosition(_t._SPFLabel.width / 2 + locStatsPosition.x, _t._SPFLabel.height * 3 / 2 + locStatsPosition.y);
  834. _t._FPSLabel.setPosition(_t._FPSLabel.width / 2 + locStatsPosition.x, _t._FPSLabel.height / 2 + locStatsPosition.y);
  835. };
  836. _p.getVisibleSize = function () {
  837. //if (this._openGLView) {
  838. //return this._openGLView.getVisibleSize();
  839. //} else {
  840. return this.getWinSize();
  841. //}
  842. };
  843. _p.getVisibleOrigin = function () {
  844. //if (this._openGLView) {
  845. //return this._openGLView.getVisibleOrigin();
  846. //} else {
  847. return cc.p(0, 0);
  848. //}
  849. };
  850. } else {
  851. cc.Director._fpsImage = new Image();
  852. cc._addEventListener(cc.Director._fpsImage, "load", function () {
  853. cc.Director._fpsImageLoaded = true;
  854. });
  855. if (cc._fpsImage) {
  856. cc.Director._fpsImage.src = cc._fpsImage;
  857. }
  858. cc.assert(typeof cc._tmp.DirectorWebGL === "function", cc._LogInfos.MissingFile, "CCDirectorWebGL.js");
  859. cc._tmp.DirectorWebGL();
  860. delete cc._tmp.DirectorWebGL;
  861. }