Advertisement
Guest User

CCDirector.cpp

a guest
May 30th, 2013
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 25.90 KB | None | 0 0
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011      Zynga Inc.
  5.  
  6. http://www.cocos2d-x.org
  7.  
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights
  11. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. copies of the Software, and to permit persons to whom the Software is
  13. furnished to do so, subject to the following conditions:
  14.  
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. ****************************************************************************/
  26. #include "CCDirector.h"
  27. #include "ccFPSImages.h"
  28. #include "draw_nodes/CCDrawingPrimitives.h"
  29. #include "CCConfiguration.h"
  30. #include "cocoa/CCNS.h"
  31. #include "layers_scenes_transitions_nodes/CCScene.h"
  32. #include "cocoa/CCArray.h"
  33. #include "CCScheduler.h"
  34. #include "ccMacros.h"
  35. #include "touch_dispatcher/CCTouchDispatcher.h"
  36. #include "support/CCPointExtension.h"
  37. #include "support/CCNotificationCenter.h"
  38. #include "layers_scenes_transitions_nodes/CCTransition.h"
  39. #include "textures/CCTextureCache.h"
  40. #include "sprite_nodes/CCSpriteFrameCache.h"
  41. #include "cocoa/CCAutoreleasePool.h"
  42. #include "platform/platform.h"
  43. #include "platform/CCFileUtils.h"
  44. #include "CCApplication.h"
  45. #include "label_nodes/CCLabelBMFont.h"
  46. #include "label_nodes/CCLabelAtlas.h"
  47. #include "actions/CCActionManager.h"
  48. #include "CCConfiguration.h"
  49. #include "keypad_dispatcher/CCKeypadDispatcher.h"
  50. #include "CCAccelerometer.h"
  51. #include "sprite_nodes/CCAnimationCache.h"
  52. #include "touch_dispatcher/CCTouch.h"
  53. #include "support/user_default/CCUserDefault.h"
  54. #include "shaders/ccGLStateCache.h"
  55. #include "shaders/CCShaderCache.h"
  56. #include "kazmath/kazmath.h"
  57. #include "kazmath/GL/matrix.h"
  58. #include "support/CCProfiling.h"
  59. #include "CCEGLView.h"
  60. #include <string>
  61.  
  62. /**
  63.  Position of the FPS
  64.  
  65.  Default: 0,0 (bottom-left corner)
  66.  */
  67. #ifndef CC_DIRECTOR_STATS_POSITION
  68. #define CC_DIRECTOR_STATS_POSITION CCDirector::sharedDirector()->getVisibleOrigin()
  69. #endif
  70.  
  71. using namespace std;
  72.  
  73. unsigned int g_uNumberOfDraws = 0;
  74.  
  75. NS_CC_BEGIN
  76. // XXX it should be a Director ivar. Move it there once support for multiple directors is added
  77.  
  78. // singleton stuff
  79. static CCDisplayLinkDirector *s_SharedDirector = NULL;
  80.  
  81. #define kDefaultFPS        60  // 60 frames per second
  82. extern const char* cocos2dVersion(void);
  83.  
  84. CCDirector* CCDirector::sharedDirector(void)
  85. {
  86.     if (!s_SharedDirector)
  87.     {
  88.         s_SharedDirector = new CCDisplayLinkDirector();
  89.         s_SharedDirector->init();
  90.     }
  91.  
  92.     return s_SharedDirector;
  93. }
  94.  
  95. CCDirector::CCDirector(void)
  96. {
  97.  
  98. }
  99.  
  100. bool CCDirector::init(void)
  101. {
  102.     CCLOG("cocos2d: %s", cocos2dVersion());
  103.    
  104.    
  105.     // CORE HACK - TRY TO REMOVE
  106.     spaceBarDown = upArrowKeyDown = downArrowKeyDown = leftArrowKeyDown = rightArrowKeyDown = bKeyDown = false;
  107.     numberOneDown = numberTwoDown = numberThreeDown = numberFourDown = numberFiveDown = numberSixDown = false;
  108.     numberOnePressed = numberTwoPressed = numberThreePressed = numberFourPressed = numberFivePressed = numberSixPressed = false;
  109.     // CORE HACK - TRY TO REMOVE
  110.    
  111.    
  112.    
  113.     // scenes
  114.     m_pRunningScene = NULL;
  115.     m_pNextScene = NULL;
  116.  
  117.     m_pNotificationNode = NULL;
  118.  
  119.     m_dOldAnimationInterval = m_dAnimationInterval = 1.0 / kDefaultFPS;    
  120.     m_pobScenesStack = new CCArray();
  121.     m_pobScenesStack->init();
  122.  
  123.     // Set default projection (3D)
  124.     m_eProjection = kCCDirectorProjectionDefault;
  125.  
  126.     // projection delegate if "Custom" projection is used
  127.     m_pProjectionDelegate = NULL;
  128.  
  129.     // FPS
  130.     m_fAccumDt = 0.0f;
  131.     m_fFrameRate = 0.0f;
  132.     m_pFPSLabel = NULL;
  133.     m_pSPFLabel = NULL;
  134.     m_pDrawsLabel = NULL;
  135.     m_bDisplayStats = false;
  136.     m_uTotalFrames = m_uFrames = 0;
  137.     m_pszFPS = new char[10];
  138.     m_pLastUpdate = new struct cc_timeval();
  139.  
  140.     // paused ?
  141.     m_bPaused = false;
  142.    
  143.     // purge ?
  144.     m_bPurgeDirecotorInNextLoop = false;
  145.  
  146.     m_obWinSizeInPoints = CCSizeZero;    
  147.  
  148.     m_pobOpenGLView = NULL;
  149.  
  150.     m_fContentScaleFactor = 1.0f;
  151.  
  152.     // scheduler
  153.     m_pScheduler = new CCScheduler();
  154.     // action manager
  155.     m_pActionManager = new CCActionManager();
  156.     m_pScheduler->scheduleUpdateForTarget(m_pActionManager, kCCPrioritySystem, false);
  157.     // touchDispatcher
  158.     m_pTouchDispatcher = new CCTouchDispatcher();
  159.     m_pTouchDispatcher->init();
  160.  
  161.     // KeypadDispatcher
  162.     m_pKeypadDispatcher = new CCKeypadDispatcher();
  163.  
  164.     // Accelerometer
  165.     m_pAccelerometer = new CCAccelerometer();
  166.  
  167.     // create autorelease pool
  168.     CCPoolManager::sharedPoolManager()->push();
  169.  
  170.     return true;
  171. }
  172.    
  173. CCDirector::~CCDirector(void)
  174. {
  175.     CCLOG("cocos2d: deallocing CCDirector %p", this);
  176.  
  177.     CC_SAFE_RELEASE(m_pFPSLabel);
  178.     CC_SAFE_RELEASE(m_pSPFLabel);
  179.     CC_SAFE_RELEASE(m_pDrawsLabel);
  180.    
  181.     CC_SAFE_RELEASE(m_pRunningScene);
  182.     CC_SAFE_RELEASE(m_pNotificationNode);
  183.     CC_SAFE_RELEASE(m_pobScenesStack);
  184.     CC_SAFE_RELEASE(m_pScheduler);
  185.     CC_SAFE_RELEASE(m_pActionManager);
  186.     CC_SAFE_RELEASE(m_pTouchDispatcher);
  187.     CC_SAFE_RELEASE(m_pKeypadDispatcher);
  188.     CC_SAFE_DELETE(m_pAccelerometer);
  189.  
  190.     // pop the autorelease pool
  191.     CCPoolManager::sharedPoolManager()->pop();
  192.     CCPoolManager::purgePoolManager();
  193.  
  194.     // delete m_pLastUpdate
  195.     CC_SAFE_DELETE(m_pLastUpdate);
  196.     // delete fps string
  197.     delete []m_pszFPS;
  198.  
  199.     s_SharedDirector = NULL;
  200. }
  201.  
  202. void CCDirector::setGLDefaultValues(void)
  203. {
  204.     // This method SHOULD be called only after openGLView_ was initialized
  205.     CCAssert(m_pobOpenGLView, "opengl view should not be null");
  206.  
  207.     setAlphaBlending(true);
  208.     // XXX: Fix me, should enable/disable depth test according the depth format as cocos2d-iphone did
  209.     // [self setDepthTest: view_.depthFormat];
  210.     setDepthTest(false);
  211.     setProjection(m_eProjection);
  212.  
  213.     // set other opengl default values
  214.     glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  215. }
  216.  
  217. // Draw the Scene
  218. void CCDirector::drawScene(void)
  219. {
  220.     // calculate "global" dt
  221.     calculateDeltaTime();
  222.  
  223.     //tick before glClear: issue #533
  224.     if (! m_bPaused)
  225.     {
  226.         m_pScheduler->update(m_fDeltaTime);
  227.     }
  228.  
  229.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  230.  
  231.     /* to avoid flickr, nextScene MUST be here: after tick and before draw.
  232.      XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */
  233.     if (m_pNextScene)
  234.     {
  235.         setNextScene();
  236.     }
  237.  
  238.     kmGLPushMatrix();
  239.  
  240.     // draw the scene
  241.     if (m_pRunningScene)
  242.     {
  243.         m_pRunningScene->visit();
  244.     }
  245.  
  246.     // draw the notifications node
  247.     if (m_pNotificationNode)
  248.     {
  249.         m_pNotificationNode->visit();
  250.     }
  251.    
  252.     if (m_bDisplayStats)
  253.     {
  254.         showStats();
  255.     }
  256.  
  257.     kmGLPopMatrix();
  258.  
  259.     m_uTotalFrames++;
  260.  
  261.     // swap buffers
  262.     if (m_pobOpenGLView)
  263.     {
  264.         m_pobOpenGLView->swapBuffers();
  265.     }
  266.    
  267.     if (m_bDisplayStats)
  268.     {
  269.         calculateMPF();
  270.     }
  271. }
  272.  
  273. void CCDirector::calculateDeltaTime(void)
  274. {
  275.     struct cc_timeval now;
  276.  
  277.     if (CCTime::gettimeofdayCocos2d(&now, NULL) != 0)
  278.     {
  279.         CCLOG("error in gettimeofday");
  280.         m_fDeltaTime = 0;
  281.         return;
  282.     }
  283.  
  284.     // new delta time. Re-fixed issue #1277
  285.     if (m_bNextDeltaTimeZero)
  286.     {
  287.         m_fDeltaTime = 0;
  288.         m_bNextDeltaTimeZero = false;
  289.     }
  290.     else
  291.     {
  292.         m_fDeltaTime = (now.tv_sec - m_pLastUpdate->tv_sec) + (now.tv_usec - m_pLastUpdate->tv_usec) / 1000000.0f;
  293.         m_fDeltaTime = MAX(0, m_fDeltaTime);
  294.     }
  295.  
  296. #ifdef DEBUG
  297.     // If we are debugging our code, prevent big delta time
  298.     if(m_fDeltaTime > 0.2f)
  299.     {
  300.         m_fDeltaTime = 1 / 60.0f;
  301.     }
  302. #endif
  303.  
  304.     *m_pLastUpdate = now;
  305. }
  306. float CCDirector::getDeltaTime()
  307. {
  308.     return m_fDeltaTime;
  309. }
  310. void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView)
  311. {
  312.     CCAssert(pobOpenGLView, "opengl view should not be null");
  313.  
  314.     if (m_pobOpenGLView != pobOpenGLView)
  315.     {
  316.         // EAGLView is not a CCObject
  317.         delete m_pobOpenGLView; // [openGLView_ release]
  318.         m_pobOpenGLView = pobOpenGLView;
  319.  
  320.         // set size
  321.         m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize();
  322.        
  323.         createStatsLabel();
  324.        
  325.         if (m_pobOpenGLView)
  326.         {
  327.             setGLDefaultValues();
  328.         }  
  329.        
  330.         CHECK_GL_ERROR_DEBUG();
  331.  
  332.         m_pobOpenGLView->setTouchDelegate(m_pTouchDispatcher);
  333.         m_pTouchDispatcher->setDispatchEvents(true);
  334.     }
  335. }
  336.  
  337. void CCDirector::setViewport()
  338. {
  339.     if (m_pobOpenGLView)
  340.     {
  341.         m_pobOpenGLView->setViewPortInPoints(0, 0, m_obWinSizeInPoints.width, m_obWinSizeInPoints.height);
  342.     }
  343. }
  344.  
  345. void CCDirector::setNextDeltaTimeZero(bool bNextDeltaTimeZero)
  346. {
  347.     m_bNextDeltaTimeZero = bNextDeltaTimeZero;
  348. }
  349.  
  350. void CCDirector::setProjection(ccDirectorProjection kProjection)
  351. {
  352.     CCSize size = m_obWinSizeInPoints;
  353.  
  354.     setViewport();
  355.  
  356.     switch (kProjection)
  357.     {
  358.     case kCCDirectorProjection2D:
  359.         {
  360.             kmGLMatrixMode(KM_GL_PROJECTION);
  361.             kmGLLoadIdentity();
  362.             kmMat4 orthoMatrix;
  363.             kmMat4OrthographicProjection(&orthoMatrix, 0, size.width, 0, size.height, -1024, 1024 );
  364.             kmGLMultMatrix(&orthoMatrix);
  365.             kmGLMatrixMode(KM_GL_MODELVIEW);
  366.             kmGLLoadIdentity();
  367.         }
  368.         break;
  369.  
  370.     case kCCDirectorProjection3D:
  371.         {
  372.             float zeye = this->getZEye();
  373.  
  374.             kmMat4 matrixPerspective, matrixLookup;
  375.  
  376.             kmGLMatrixMode(KM_GL_PROJECTION);
  377.             kmGLLoadIdentity();
  378.  
  379.             // issue #1334
  380.             kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, zeye*2);
  381.             // kmMat4PerspectiveProjection( &matrixPerspective, 60, (GLfloat)size.width/size.height, 0.1f, 1500);
  382.  
  383.             kmGLMultMatrix(&matrixPerspective);
  384.  
  385.             kmGLMatrixMode(KM_GL_MODELVIEW);
  386.             kmGLLoadIdentity();
  387.             kmVec3 eye, center, up;
  388.             kmVec3Fill( &eye, size.width/2, size.height/2, zeye );
  389.             kmVec3Fill( &center, size.width/2, size.height/2, 0.0f );
  390.             kmVec3Fill( &up, 0.0f, 1.0f, 0.0f);
  391.             kmMat4LookAt(&matrixLookup, &eye, &center, &up);
  392.             kmGLMultMatrix(&matrixLookup);
  393.         }
  394.         break;
  395.            
  396.     case kCCDirectorProjectionCustom:
  397.         if (m_pProjectionDelegate)
  398.         {
  399.             m_pProjectionDelegate->updateProjection();
  400.         }
  401.         break;
  402.            
  403.     default:
  404.         CCLOG("cocos2d: Director: unrecognized projection");
  405.         break;
  406.     }
  407.  
  408.     m_eProjection = kProjection;
  409.     ccSetProjectionMatrixDirty();
  410. }
  411.  
  412. void CCDirector::purgeCachedData(void)
  413. {
  414.     CCLabelBMFont::purgeCachedData();
  415.     if (s_SharedDirector->getOpenGLView())
  416.     {
  417.         CCTextureCache::sharedTextureCache()->removeUnusedTextures();
  418.     }
  419.     CCFileUtils::sharedFileUtils()->purgeCachedEntries();
  420. }
  421.  
  422. float CCDirector::getZEye(void)
  423. {
  424.     return (m_obWinSizeInPoints.height / 1.1566f);
  425. }
  426.  
  427. void CCDirector::setAlphaBlending(bool bOn)
  428. {
  429.     if (bOn)
  430.     {
  431.         ccGLBlendFunc(CC_BLEND_SRC, CC_BLEND_DST);
  432.     }
  433.     else
  434.     {
  435.         ccGLBlendFunc(GL_ONE, GL_ZERO);
  436.     }
  437.  
  438.     CHECK_GL_ERROR_DEBUG();
  439. }
  440.  
  441. void CCDirector::setDepthTest(bool bOn)
  442. {
  443.     if (bOn)
  444.     {
  445.         glClearDepth(1.0f);
  446.         glEnable(GL_DEPTH_TEST);
  447.         glDepthFunc(GL_LEQUAL);
  448. //        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  449.     }
  450.     else
  451.     {
  452.         glDisable(GL_DEPTH_TEST);
  453.     }
  454.     CHECK_GL_ERROR_DEBUG();
  455. }
  456.  
  457. static void
  458. GLToClipTransform(kmMat4 *transformOut)
  459. {
  460.     kmMat4 projection;
  461.     kmGLGetMatrix(KM_GL_PROJECTION, &projection);
  462.    
  463.     kmMat4 modelview;
  464.     kmGLGetMatrix(KM_GL_MODELVIEW, &modelview);
  465.    
  466.     kmMat4Multiply(transformOut, &projection, &modelview);
  467. }
  468.  
  469. CCPoint CCDirector::convertToGL(const CCPoint& uiPoint)
  470. {
  471.     kmMat4 transform;
  472.     GLToClipTransform(&transform);
  473.    
  474.     kmMat4 transformInv;
  475.     kmMat4Inverse(&transformInv, &transform);
  476.    
  477.     // Calculate z=0 using -> transform*[0, 0, 0, 1]/w
  478.     kmScalar zClip = transform.mat[14]/transform.mat[15];
  479.    
  480.     CCSize glSize = m_pobOpenGLView->getDesignResolutionSize();
  481.     kmVec3 clipCoord = {2.0f*uiPoint.x/glSize.width - 1.0f, 1.0f - 2.0f*uiPoint.y/glSize.height, zClip};
  482.    
  483.     kmVec3 glCoord;
  484.     kmVec3TransformCoord(&glCoord, &clipCoord, &transformInv);
  485.    
  486.     return ccp(glCoord.x, glCoord.y);
  487. }
  488.  
  489. CCPoint CCDirector::convertToUI(const CCPoint& glPoint)
  490. {
  491.     kmMat4 transform;
  492.     GLToClipTransform(&transform);
  493.    
  494.     kmVec3 clipCoord;
  495.     // Need to calculate the zero depth from the transform.
  496.     kmVec3 glCoord = {glPoint.x, glPoint.y, 0.0};
  497.     kmVec3TransformCoord(&clipCoord, &glCoord, &transform);
  498.    
  499.     CCSize glSize = m_pobOpenGLView->getDesignResolutionSize();
  500.     return ccp(glSize.width*(clipCoord.x*0.5 + 0.5), glSize.height*(-clipCoord.y*0.5 + 0.5));
  501. }
  502.  
  503. CCSize CCDirector::getWinSize(void)
  504. {
  505.     return m_obWinSizeInPoints;
  506. }
  507.  
  508. CCSize CCDirector::getWinSizeInPixels()
  509. {
  510.     return CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor);
  511. }
  512.  
  513. CCSize CCDirector::getVisibleSize()
  514. {
  515.     if (m_pobOpenGLView)
  516.     {
  517.         return m_pobOpenGLView->getVisibleSize();
  518.     }
  519.     else
  520.     {
  521.         return CCSizeZero;
  522.     }
  523. }
  524.  
  525. CCPoint CCDirector::getVisibleOrigin()
  526. {
  527.     if (m_pobOpenGLView)
  528.     {
  529.         return m_pobOpenGLView->getVisibleOrigin();
  530.     }
  531.     else
  532.     {
  533.         return CCPointZero;
  534.     }
  535. }
  536.  
  537. // scene management
  538.  
  539. void CCDirector::runWithScene(CCScene *pScene)
  540. {
  541.     CCAssert(pScene != NULL, "This command can only be used to start the CCDirector. There is already a scene present.");
  542.     CCAssert(m_pRunningScene == NULL, "m_pRunningScene should be null");
  543.  
  544.     pushScene(pScene);
  545.     startAnimation();
  546. }
  547.  
  548. void CCDirector::replaceScene(CCScene *pScene)
  549. {
  550.     CCAssert(m_pRunningScene, "Use runWithScene: instead to start the director");
  551.     CCAssert(pScene != NULL, "the scene should not be null");
  552.  
  553.     unsigned int index = m_pobScenesStack->count();
  554.  
  555.     m_bSendCleanupToScene = true;
  556.     m_pobScenesStack->replaceObjectAtIndex(index - 1, pScene);
  557.  
  558.     m_pNextScene = pScene;
  559. }
  560.  
  561. void CCDirector::pushScene(CCScene *pScene)
  562. {
  563.     CCAssert(pScene, "the scene should not null");
  564.  
  565.     m_bSendCleanupToScene = false;
  566.  
  567.     m_pobScenesStack->addObject(pScene);
  568.     m_pNextScene = pScene;
  569. }
  570.  
  571. void CCDirector::popScene(void)
  572. {
  573.     CCAssert(m_pRunningScene != NULL, "running scene should not null");
  574.  
  575.     m_pobScenesStack->removeLastObject();
  576.     unsigned int c = m_pobScenesStack->count();
  577.  
  578.     if (c == 0)
  579.     {
  580.         end();
  581.     }
  582.     else
  583.     {
  584.         m_bSendCleanupToScene = true;
  585.         m_pNextScene = (CCScene*)m_pobScenesStack->objectAtIndex(c - 1);
  586.     }
  587. }
  588.  
  589. void CCDirector::popToRootScene(void)
  590. {
  591.     CCAssert(m_pRunningScene != NULL, "A running Scene is needed");
  592.     unsigned int c = m_pobScenesStack->count();
  593.  
  594.     if (c == 1)
  595.     {
  596.         m_pobScenesStack->removeLastObject();
  597.         this->end();
  598.     }
  599.     else
  600.     {
  601.         while (c > 1)
  602.         {
  603.             CCScene *current = (CCScene*)m_pobScenesStack->lastObject();
  604.             if( current->isRunning() )
  605.             {
  606.                 current->onExitTransitionDidStart();
  607.                 current->onExit();
  608.             }
  609.             current->cleanup();
  610.  
  611.             m_pobScenesStack->removeLastObject();
  612.             c--;
  613.         }
  614.         m_pNextScene = (CCScene*)m_pobScenesStack->lastObject();
  615.         m_bSendCleanupToScene = false;
  616.     }
  617. }
  618.  
  619. void CCDirector::end()
  620. {
  621.     m_bPurgeDirecotorInNextLoop = true;
  622. }
  623.  
  624. void CCDirector::purgeDirector()
  625. {
  626.     // cleanup scheduler
  627.     getScheduler()->unscheduleAll();
  628.    
  629.     // don't release the event handlers
  630.     // They are needed in case the director is run again
  631.     m_pTouchDispatcher->removeAllDelegates();
  632.  
  633.     if (m_pRunningScene)
  634.     {
  635.         m_pRunningScene->onExitTransitionDidStart();
  636.         m_pRunningScene->onExit();
  637.         m_pRunningScene->cleanup();
  638.         m_pRunningScene->release();
  639.     }
  640.    
  641.     m_pRunningScene = NULL;
  642.     m_pNextScene = NULL;
  643.  
  644.     // remove all objects, but don't release it.
  645.     // runWithScene might be executed after 'end'.
  646.     m_pobScenesStack->removeAllObjects();
  647.  
  648.     stopAnimation();
  649.  
  650.     CC_SAFE_RELEASE_NULL(m_pFPSLabel);
  651.     CC_SAFE_RELEASE_NULL(m_pSPFLabel);
  652.     CC_SAFE_RELEASE_NULL(m_pDrawsLabel);
  653.  
  654.     // purge bitmap cache
  655.     CCLabelBMFont::purgeCachedData();
  656.  
  657.     // purge all managed caches
  658.     ccDrawFree();
  659.     CCAnimationCache::purgeSharedAnimationCache();
  660.     CCSpriteFrameCache::purgeSharedSpriteFrameCache();
  661.     CCTextureCache::purgeSharedTextureCache();
  662.     CCShaderCache::purgeSharedShaderCache();
  663.     CCFileUtils::purgeFileUtils();
  664.     CCConfiguration::purgeConfiguration();
  665.  
  666.     // cocos2d-x specific data structures
  667.     CCUserDefault::purgeSharedUserDefault();
  668.     CCNotificationCenter::purgeNotificationCenter();
  669.  
  670.     ccGLInvalidateStateCache();
  671.    
  672.     CHECK_GL_ERROR_DEBUG();
  673.    
  674.     // OpenGL view
  675.     m_pobOpenGLView->end();
  676.     m_pobOpenGLView = NULL;
  677.  
  678.     // delete CCDirector
  679.     release();
  680. }
  681.  
  682. void CCDirector::setNextScene(void)
  683. {
  684.     bool runningIsTransition = dynamic_cast<CCTransitionScene*>(m_pRunningScene) != NULL;
  685.     bool newIsTransition = dynamic_cast<CCTransitionScene*>(m_pNextScene) != NULL;
  686.  
  687.     // If it is not a transition, call onExit/cleanup
  688.      if (! newIsTransition)
  689.      {
  690.          if (m_pRunningScene)
  691.          {
  692.              m_pRunningScene->onExitTransitionDidStart();
  693.              m_pRunningScene->onExit();
  694.          }
  695.  
  696.          // issue #709. the root node (scene) should receive the cleanup message too
  697.          // otherwise it might be leaked.
  698.          if (m_bSendCleanupToScene && m_pRunningScene)
  699.          {
  700.              m_pRunningScene->cleanup();
  701.          }
  702.      }
  703.  
  704.     if (m_pRunningScene)
  705.     {
  706.         m_pRunningScene->release();
  707.     }
  708.     m_pRunningScene = m_pNextScene;
  709.     m_pNextScene->retain();
  710.     m_pNextScene = NULL;
  711.  
  712.     if ((! runningIsTransition) && m_pRunningScene)
  713.     {
  714.         m_pRunningScene->onEnter();
  715.         m_pRunningScene->onEnterTransitionDidFinish();
  716.     }
  717. }
  718.  
  719. void CCDirector::pause(void)
  720. {
  721.     if (m_bPaused)
  722.     {
  723.         return;
  724.     }
  725.  
  726.     m_dOldAnimationInterval = m_dAnimationInterval;
  727.  
  728.     // when paused, don't consume CPU
  729.     setAnimationInterval(1 / 4.0);
  730.     m_bPaused = true;
  731. }
  732.  
  733. void CCDirector::resume(void)
  734. {
  735.     if (! m_bPaused)
  736.     {
  737.         return;
  738.     }
  739.  
  740.     setAnimationInterval(m_dOldAnimationInterval);
  741.  
  742.     if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
  743.     {
  744.         CCLOG("cocos2d: Director: Error in gettimeofday");
  745.     }
  746.  
  747.     m_bPaused = false;
  748.     m_fDeltaTime = 0;
  749. }
  750.  
  751. // display the FPS using a LabelAtlas
  752. // updates the FPS every frame
  753. void CCDirector::showStats(void)
  754. {
  755.     m_uFrames++;
  756.     m_fAccumDt += m_fDeltaTime;
  757.    
  758.     if (m_bDisplayStats)
  759.     {
  760.         if (m_pFPSLabel && m_pSPFLabel && m_pDrawsLabel)
  761.         {
  762.             if (m_fAccumDt > CC_DIRECTOR_STATS_INTERVAL)
  763.             {
  764.                 sprintf(m_pszFPS, "%.3f", m_fSecondsPerFrame);
  765.                 m_pSPFLabel->setString(m_pszFPS);
  766.                
  767.                 m_fFrameRate = m_uFrames / m_fAccumDt;
  768.                 m_uFrames = 0;
  769.                 m_fAccumDt = 0;
  770.                
  771.                 sprintf(m_pszFPS, "%.1f", m_fFrameRate);
  772.                 m_pFPSLabel->setString(m_pszFPS);
  773.                
  774.                 sprintf(m_pszFPS, "%4lu", (unsigned long)g_uNumberOfDraws);
  775.                 m_pDrawsLabel->setString(m_pszFPS);
  776.             }
  777.            
  778.             m_pDrawsLabel->visit();
  779.             m_pFPSLabel->visit();
  780.             m_pSPFLabel->visit();
  781.         }
  782.     }    
  783.    
  784.     g_uNumberOfDraws = 0;
  785. }
  786.  
  787. void CCDirector::calculateMPF()
  788. {
  789.     struct cc_timeval now;
  790.     CCTime::gettimeofdayCocos2d(&now, NULL);
  791.    
  792.     m_fSecondsPerFrame = (now.tv_sec - m_pLastUpdate->tv_sec) + (now.tv_usec - m_pLastUpdate->tv_usec) / 1000000.0f;
  793. }
  794.  
  795. // returns the FPS image data pointer and len
  796. void CCDirector::getFPSImageData(unsigned char** datapointer, unsigned int* length)
  797. {
  798.     // XXX fixed me if it should be used
  799. //    *datapointer = cc_fps_images_png;
  800. //  *length = cc_fps_images_len();
  801. }
  802.  
  803. void CCDirector::createStatsLabel()
  804. {    
  805.     if( m_pFPSLabel && m_pSPFLabel )
  806.     {
  807.         CC_SAFE_RELEASE_NULL(m_pFPSLabel);
  808.         CC_SAFE_RELEASE_NULL(m_pSPFLabel);
  809.         CC_SAFE_RELEASE_NULL(m_pDrawsLabel);
  810.         CCFileUtils::sharedFileUtils()->purgeCachedEntries();
  811.     }
  812.  
  813.     int fontSize = 0;
  814.     if (m_obWinSizeInPoints.width > m_obWinSizeInPoints.height)
  815.     {
  816.         fontSize = (int)(m_obWinSizeInPoints.height / 320.0f * 24);
  817.     }
  818.     else
  819.     {
  820.         fontSize = (int)(m_obWinSizeInPoints.width / 320.0f * 24);
  821.     }
  822.    
  823.     m_pFPSLabel = CCLabelTTF::create("00.0", "Arial", fontSize);
  824.     m_pFPSLabel->retain();
  825.     m_pSPFLabel = CCLabelTTF::create("0.000", "Arial", fontSize);
  826.     m_pSPFLabel->retain();
  827.     m_pDrawsLabel = CCLabelTTF::create("000", "Arial", fontSize);
  828.     m_pDrawsLabel->retain();
  829.  
  830.     CCSize contentSize = m_pDrawsLabel->getContentSize();
  831.     m_pDrawsLabel->setPosition(ccpAdd(ccp(contentSize.width/2, contentSize.height*5/2), CC_DIRECTOR_STATS_POSITION));
  832.     contentSize = m_pSPFLabel->getContentSize();
  833.     m_pSPFLabel->setPosition(ccpAdd(ccp(contentSize.width/2, contentSize.height*3/2), CC_DIRECTOR_STATS_POSITION));
  834.     contentSize = m_pFPSLabel->getContentSize();
  835.     m_pFPSLabel->setPosition(ccpAdd(ccp(contentSize.width/2, contentSize.height/2), CC_DIRECTOR_STATS_POSITION));
  836. }
  837.  
  838. float CCDirector::getContentScaleFactor(void)
  839. {
  840.     return m_fContentScaleFactor;
  841. }
  842.  
  843. void CCDirector::setContentScaleFactor(float scaleFactor)
  844. {
  845.     if (scaleFactor != m_fContentScaleFactor)
  846.     {
  847.         m_fContentScaleFactor = scaleFactor;
  848.         createStatsLabel();
  849.     }
  850. }
  851.  
  852. CCNode* CCDirector::getNotificationNode()
  853. {
  854.     return m_pNotificationNode;
  855. }
  856.  
  857. void CCDirector::setNotificationNode(CCNode *node)
  858. {
  859.     CC_SAFE_RELEASE(m_pNotificationNode);
  860.     m_pNotificationNode = node;
  861.     CC_SAFE_RETAIN(m_pNotificationNode);
  862. }
  863.  
  864. CCDirectorDelegate* CCDirector::getDelegate() const
  865. {
  866.     return m_pProjectionDelegate;
  867. }
  868.  
  869. void CCDirector::setDelegate(CCDirectorDelegate* pDelegate)
  870. {
  871.     m_pProjectionDelegate = pDelegate;
  872. }
  873.  
  874. void CCDirector::setScheduler(CCScheduler* pScheduler)
  875. {
  876.     if (m_pScheduler != pScheduler)
  877.     {
  878.         CC_SAFE_RETAIN(pScheduler);
  879.         CC_SAFE_RELEASE(m_pScheduler);
  880.         m_pScheduler = pScheduler;
  881.     }
  882. }
  883.  
  884. CCScheduler* CCDirector::getScheduler()
  885. {
  886.     return m_pScheduler;
  887. }
  888.  
  889. void CCDirector::setActionManager(CCActionManager* pActionManager)
  890. {
  891.     if (m_pActionManager != pActionManager)
  892.     {
  893.         CC_SAFE_RETAIN(pActionManager);
  894.         CC_SAFE_RELEASE(m_pActionManager);
  895.         m_pActionManager = pActionManager;
  896.     }    
  897. }
  898.  
  899. CCActionManager* CCDirector::getActionManager()
  900. {
  901.     return m_pActionManager;
  902. }
  903.  
  904. void CCDirector::setTouchDispatcher(CCTouchDispatcher* pTouchDispatcher)
  905. {
  906.     if (m_pTouchDispatcher != pTouchDispatcher)
  907.     {
  908.         CC_SAFE_RETAIN(pTouchDispatcher);
  909.         CC_SAFE_RELEASE(m_pTouchDispatcher);
  910.         m_pTouchDispatcher = pTouchDispatcher;
  911.     }    
  912. }
  913.  
  914. CCTouchDispatcher* CCDirector::getTouchDispatcher()
  915. {
  916.     return m_pTouchDispatcher;
  917. }
  918.  
  919. void CCDirector::setKeypadDispatcher(CCKeypadDispatcher* pKeypadDispatcher)
  920. {
  921.     CC_SAFE_RETAIN(pKeypadDispatcher);
  922.     CC_SAFE_RELEASE(m_pKeypadDispatcher);
  923.     m_pKeypadDispatcher = pKeypadDispatcher;
  924. }
  925.  
  926. CCKeypadDispatcher* CCDirector::getKeypadDispatcher()
  927. {
  928.     return m_pKeypadDispatcher;
  929. }
  930.  
  931. void CCDirector::setAccelerometer(CCAccelerometer* pAccelerometer)
  932. {
  933.     if (m_pAccelerometer != pAccelerometer)
  934.     {
  935.         CC_SAFE_DELETE(m_pAccelerometer);
  936.         m_pAccelerometer = pAccelerometer;
  937.     }
  938. }
  939.  
  940. CCAccelerometer* CCDirector::getAccelerometer()
  941. {
  942.     return m_pAccelerometer;
  943. }
  944.  
  945. /***************************************************
  946. * implementation of DisplayLinkDirector
  947. **************************************************/
  948.  
  949. // should we implement 4 types of director ??
  950. // I think DisplayLinkDirector is enough
  951. // so we now only support DisplayLinkDirector
  952. void CCDisplayLinkDirector::startAnimation(void)
  953. {
  954.     if (CCTime::gettimeofdayCocos2d(m_pLastUpdate, NULL) != 0)
  955.     {
  956.         CCLOG("cocos2d: DisplayLinkDirector: Error on gettimeofday");
  957.     }
  958.  
  959.     m_bInvalid = false;
  960.     CCApplication::sharedApplication()->setAnimationInterval(m_dAnimationInterval);
  961. }
  962.  
  963. void CCDisplayLinkDirector::mainLoop(void)
  964. {
  965.     if (m_bPurgeDirecotorInNextLoop)
  966.     {
  967.         m_bPurgeDirecotorInNextLoop = false;
  968.         purgeDirector();
  969.     }
  970.     else if (! m_bInvalid)
  971.      {
  972.          drawScene();
  973.      
  974.          // release the objects
  975.          CCPoolManager::sharedPoolManager()->pop();        
  976.      }
  977. }
  978.  
  979. void CCDisplayLinkDirector::stopAnimation(void)
  980. {
  981.     m_bInvalid = true;
  982. }
  983.  
  984. void CCDisplayLinkDirector::setAnimationInterval(double dValue)
  985. {
  986.     m_dAnimationInterval = dValue;
  987.     if (! m_bInvalid)
  988.     {
  989.         stopAnimation();
  990.         startAnimation();
  991.     }    
  992. }
  993.  
  994. NS_CC_END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement