Advertisement
Guest User

CCDirector.h

a guest
May 30th, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.76 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.  
  27. #ifndef __CCDIRECTOR_H__
  28. #define __CCDIRECTOR_H__
  29.  
  30. #include "platform/CCPlatformMacros.h"
  31. #include "cocoa/CCObject.h"
  32. #include "ccTypes.h"
  33. #include "cocoa/CCGeometry.h"
  34. #include "cocoa/CCArray.h"
  35. #include "CCGL.h"
  36. #include "kazmath/mat4.h"
  37. #include "label_nodes/CCLabelTTF.h"
  38. #include "ccTypeInfo.h"
  39.  
  40.  
  41. NS_CC_BEGIN
  42.  
  43. /**
  44.  * @addtogroup base_nodes
  45.  * @{
  46.  */
  47.  
  48. /** @typedef ccDirectorProjection
  49.  Possible OpenGL projections used by director
  50.  */
  51. typedef enum {
  52.     /// sets a 2D projection (orthogonal projection)
  53.     kCCDirectorProjection2D,
  54.    
  55.     /// sets a 3D projection with a fovy=60, znear=0.5f and zfar=1500.
  56.     kCCDirectorProjection3D,
  57.    
  58.     /// it calls "updateProjection" on the projection delegate.
  59.     kCCDirectorProjectionCustom,
  60.    
  61.     /// Default projection is 3D projection
  62.     kCCDirectorProjectionDefault = kCCDirectorProjection3D,
  63. } ccDirectorProjection;
  64.  
  65. /* Forward declarations. */
  66. class CCLabelAtlas;
  67. class CCScene;
  68. class CCEGLView;
  69. class CCDirectorDelegate;
  70. class CCNode;
  71. class CCScheduler;
  72. class CCActionManager;
  73. class CCTouchDispatcher;
  74. class CCKeypadDispatcher;
  75. class CCAccelerometer;
  76.  
  77. /**
  78. @brief Class that creates and handle the main Window and manages how
  79. and when to execute the Scenes.
  80.  
  81.  The CCDirector is also responsible for:
  82.   - initializing the OpenGL context
  83.   - setting the OpenGL pixel format (default on is RGB565)
  84.   - setting the OpenGL buffer depth (default one is 0-bit)
  85.   - setting the projection (default one is 3D)
  86.   - setting the orientation (default one is Portrait)
  87.  
  88.  Since the CCDirector is a singleton, the standard way to use it is by calling:
  89.   _ CCDirector::sharedDirector()->methodName();
  90.  
  91.  The CCDirector also sets the default OpenGL context:
  92.   - GL_TEXTURE_2D is enabled
  93.   - GL_VERTEX_ARRAY is enabled
  94.   - GL_COLOR_ARRAY is enabled
  95.   - GL_TEXTURE_COORD_ARRAY is enabled
  96. */
  97. class CC_DLL CCDirector : public CCObject, public TypeInfo
  98. {
  99. public:
  100.    
  101.     // CORE HACK - TRY TO REMOVE
  102.     bool spaceBarDown, upArrowKeyDown, downArrowKeyDown, leftArrowKeyDown, rightArrowKeyDown, bKeyDown;
  103.     bool numberOneDown, numberTwoDown, numberThreeDown, numberFourDown, numberFiveDown, numberSixDown;
  104.     bool numberOnePressed, numberTwoPressed, numberThreePressed, numberFourPressed, numberFivePressed, numberSixPressed;
  105.     // CORE HACK - TRY TO REMOVE
  106.    
  107.    
  108.     CCDirector(void);
  109.     virtual ~CCDirector(void);
  110.     virtual bool init(void);
  111.     virtual long getClassTypeInfo() {
  112.         static const long id = cocos2d::getHashCodeByString(typeid(cocos2d::CCDirector).name());
  113.         return id;
  114.     }
  115.  
  116.     // attribute
  117.  
  118.     /** Get current running Scene. Director can only run one Scene at the time */
  119.     inline CCScene* getRunningScene(void) { return m_pRunningScene; }
  120.  
  121.     /** Get the FPS value */
  122.     inline double getAnimationInterval(void) { return m_dAnimationInterval; }
  123.     /** Set the FPS value. */
  124.     virtual void setAnimationInterval(double dValue) = 0;
  125.  
  126.     /** Whether or not to display the FPS on the bottom-left corner */
  127.     inline bool isDisplayStats(void) { return m_bDisplayStats; }
  128.     /** Display the FPS on the bottom-left corner */
  129.     inline void setDisplayStats(bool bDisplayStats) { m_bDisplayStats = bDisplayStats; }
  130.    
  131.     /** seconds per frame */
  132.     inline float getSecondsPerFrame() { return m_fSecondsPerFrame; }
  133.  
  134.     /** Get the CCEGLView, where everything is rendered */
  135.     inline CCEGLView* getOpenGLView(void) { return m_pobOpenGLView; }
  136.     void setOpenGLView(CCEGLView *pobOpenGLView);
  137.  
  138.     inline bool isNextDeltaTimeZero(void) { return m_bNextDeltaTimeZero; }
  139.     void setNextDeltaTimeZero(bool bNextDeltaTimeZero);
  140.  
  141.     /** Whether or not the Director is paused */
  142.     inline bool isPaused(void) { return m_bPaused; }
  143.  
  144.     /** How many frames were called since the director started */
  145.     inline unsigned int getTotalFrames(void) { return m_uTotalFrames; }
  146.    
  147.     /** Sets an OpenGL projection
  148.      @since v0.8.2
  149.      */
  150.     inline ccDirectorProjection getProjection(void) { return m_eProjection; }
  151.     void setProjection(ccDirectorProjection kProjection);
  152.    
  153.     /** Sets the glViewport*/
  154.     void setViewport();
  155.  
  156.     /** How many frames were called since the director started */
  157.    
  158.    
  159.     /** Whether or not the replaced scene will receive the cleanup message.
  160.      If the new scene is pushed, then the old scene won't receive the "cleanup" message.
  161.      If the new scene replaces the old one, the it will receive the "cleanup" message.
  162.      @since v0.99.0
  163.      */
  164.     inline bool isSendCleanupToScene(void) { return m_bSendCleanupToScene; }
  165.  
  166.     /** This object will be visited after the main scene is visited.
  167.      This object MUST implement the "visit" selector.
  168.      Useful to hook a notification object, like CCNotifications (http://github.com/manucorporat/CCNotifications)
  169.      @since v0.99.5
  170.      */
  171.     CCNode* getNotificationNode();
  172.     void setNotificationNode(CCNode *node);
  173.    
  174.     /** CCDirector delegate. It shall implemente the CCDirectorDelegate protocol
  175.      @since v0.99.5
  176.      */
  177.     CCDirectorDelegate* getDelegate() const;
  178.     void setDelegate(CCDirectorDelegate* pDelegate);
  179.  
  180.     // window size
  181.  
  182.     /** returns the size of the OpenGL view in points.
  183.     */
  184.     CCSize getWinSize(void);
  185.  
  186.     /** returns the size of the OpenGL view in pixels.
  187.     */
  188.     CCSize getWinSizeInPixels(void);
  189.    
  190.     /** returns visible size of the OpenGL view in points.
  191.      *  the value is equal to getWinSize if don't invoke
  192.      *  CCEGLView::setDesignResolutionSize()
  193.      */
  194.     CCSize getVisibleSize();
  195.    
  196.     /** returns visible origin of the OpenGL view in points.
  197.      */
  198.     CCPoint getVisibleOrigin();
  199.  
  200.     /** converts a UIKit coordinate to an OpenGL coordinate
  201.      Useful to convert (multi) touch coordinates to the current layout (portrait or landscape)
  202.      */
  203.     CCPoint convertToGL(const CCPoint& obPoint);
  204.  
  205.     /** converts an OpenGL coordinate to a UIKit coordinate
  206.      Useful to convert node points to window points for calls such as glScissor
  207.      */
  208.     CCPoint convertToUI(const CCPoint& obPoint);
  209.  
  210.     /// XXX: missing description
  211.     float getZEye(void);
  212.  
  213.     // Scene Management
  214.  
  215.     /**Enters the Director's main loop with the given Scene.
  216.      * Call it to run only your FIRST scene.
  217.      * Don't call it if there is already a running scene.
  218.      *
  219.      * It will call pushScene: and then it will call startAnimation
  220.      */
  221.     void runWithScene(CCScene *pScene);
  222.  
  223.     /**Suspends the execution of the running scene, pushing it on the stack of suspended scenes.
  224.      * The new scene will be executed.
  225.      * Try to avoid big stacks of pushed scenes to reduce memory allocation.
  226.      * ONLY call it if there is a running scene.
  227.      */
  228.     void pushScene(CCScene *pScene);
  229.  
  230.     /**Pops out a scene from the queue.
  231.      * This scene will replace the running one.
  232.      * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
  233.      * ONLY call it if there is a running scene.
  234.      */
  235.     void popScene(void);
  236.  
  237.     /**Pops out all scenes from the queue until the root scene in the queue.
  238.      * This scene will replace the running one.
  239.      * The running scene will be deleted. If there are no more scenes in the stack the execution is terminated.
  240.      * ONLY call it if there is a running scene.
  241.      */
  242.     void popToRootScene(void);
  243.  
  244.     /** Replaces the running scene with a new one. The running scene is terminated.
  245.      * ONLY call it if there is a running scene.
  246.      */
  247.     void replaceScene(CCScene *pScene);
  248.  
  249.     /** Ends the execution, releases the running scene.
  250.      It doesn't remove the OpenGL view from its parent. You have to do it manually.
  251.      */
  252.     void end(void);
  253.  
  254.     /** Pauses the running scene.
  255.      The running scene will be _drawed_ but all scheduled timers will be paused
  256.      While paused, the draw rate will be 4 FPS to reduce CPU consumption
  257.      */
  258.     void pause(void);
  259.  
  260.     /** Resumes the paused scene
  261.      The scheduled timers will be activated again.
  262.      The "delta time" will be 0 (as if the game wasn't paused)
  263.      */
  264.     void resume(void);
  265.  
  266.     /** Stops the animation. Nothing will be drawn. The main loop won't be triggered anymore.
  267.      If you don't want to pause your animation call [pause] instead.
  268.      */
  269.     virtual void stopAnimation(void) = 0;
  270.  
  271.     /** The main loop is triggered again.
  272.      Call this function only if [stopAnimation] was called earlier
  273.      @warning Don't call this function to start the main loop. To run the main loop call runWithScene
  274.      */
  275.     virtual void startAnimation(void) = 0;
  276.  
  277.     /** Draw the scene.
  278.     This method is called every frame. Don't call it manually.
  279.     */
  280.     void drawScene(void);
  281.  
  282.     // Memory Helper
  283.  
  284.     /** Removes cached all cocos2d cached data.
  285.      It will purge the CCTextureCache, CCSpriteFrameCache, CCLabelBMFont cache
  286.      @since v0.99.3
  287.      */
  288.     void purgeCachedData(void);
  289.  
  290.     // OpenGL Helper
  291.  
  292.     /** sets the OpenGL default values */
  293.     void setGLDefaultValues(void);
  294.  
  295.     /** enables/disables OpenGL alpha blending */
  296.     void setAlphaBlending(bool bOn);
  297.  
  298.     /** enables/disables OpenGL depth test */
  299.     void setDepthTest(bool bOn);
  300.  
  301.     virtual void mainLoop(void) = 0;
  302.  
  303.     /** The size in pixels of the surface. It could be different than the screen size.
  304.     High-res devices might have a higher surface size than the screen size.
  305.     Only available when compiled using SDK >= 4.0.
  306.     @since v0.99.4
  307.     */
  308.     void setContentScaleFactor(float scaleFactor);
  309.     float getContentScaleFactor(void);
  310.  
  311. public:
  312.     /** CCScheduler associated with this director
  313.      @since v2.0
  314.      */
  315.     CC_PROPERTY(CCScheduler*, m_pScheduler, Scheduler);
  316.  
  317.     /** CCActionManager associated with this director
  318.      @since v2.0
  319.      */
  320.     CC_PROPERTY(CCActionManager*, m_pActionManager, ActionManager);
  321.  
  322.     /** CCTouchDispatcher associated with this director
  323.      @since v2.0
  324.      */
  325.     CC_PROPERTY(CCTouchDispatcher*, m_pTouchDispatcher, TouchDispatcher);
  326.  
  327.     /** CCKeypadDispatcher associated with this director
  328.      @since v2.0
  329.      */
  330.     CC_PROPERTY(CCKeypadDispatcher*, m_pKeypadDispatcher, KeypadDispatcher);
  331.  
  332.     /** CCAccelerometer associated with this director
  333.      @since v2.0
  334.      */
  335.     CC_PROPERTY(CCAccelerometer*, m_pAccelerometer, Accelerometer);
  336.  
  337.     /* delta time since last tick to main loop */
  338.     CC_PROPERTY_READONLY(float, m_fDeltaTime, DeltaTime);
  339.    
  340.     /** returns a shared instance of the director */
  341.     static CCDirector* sharedDirector(void);
  342.  
  343. protected:
  344.  
  345.     void purgeDirector();
  346.     bool m_bPurgeDirecotorInNextLoop; // this flag will be set to true in end()
  347.    
  348.     void setNextScene(void);
  349.    
  350.     void showStats();
  351.     void createStatsLabel();
  352.     void calculateMPF();
  353.     void getFPSImageData(unsigned char** datapointer, unsigned int* length);
  354.    
  355.     /** calculates delta time since last time it was called */    
  356.     void calculateDeltaTime();
  357. protected:
  358.     /* The CCEGLView, where everything is rendered */
  359.     CCEGLView    *m_pobOpenGLView;
  360.  
  361.     double m_dAnimationInterval;
  362.     double m_dOldAnimationInterval;
  363.  
  364.     /* landscape mode ? */
  365.     bool m_bLandscape;
  366.    
  367.     bool m_bDisplayStats;
  368.     float m_fAccumDt;
  369.     float m_fFrameRate;
  370.    
  371.     CCLabelTTF *m_pFPSLabel;
  372.     CCLabelTTF *m_pSPFLabel;
  373.     CCLabelTTF *m_pDrawsLabel;
  374.    
  375.     /** Whether or not the Director is paused */
  376.     bool m_bPaused;
  377.  
  378.     /* How many frames were called since the director started */
  379.     unsigned int m_uTotalFrames;
  380.     unsigned int m_uFrames;
  381.     float m_fSecondsPerFrame;
  382.      
  383.     /* The running scene */
  384.     CCScene *m_pRunningScene;
  385.    
  386.     /* will be the next 'runningScene' in the next frame
  387.      nextScene is a weak reference. */
  388.     CCScene *m_pNextScene;
  389.    
  390.     /* If YES, then "old" scene will receive the cleanup message */
  391.     bool    m_bSendCleanupToScene;
  392.  
  393.     /* scheduled scenes */
  394.     CCArray* m_pobScenesStack;
  395.    
  396.     /* last time the main loop was updated */
  397.     struct cc_timeval *m_pLastUpdate;
  398.  
  399.     /* whether or not the next delta time will be zero */
  400.     bool m_bNextDeltaTimeZero;
  401.    
  402.     /* projection used */
  403.     ccDirectorProjection m_eProjection;
  404.  
  405.     /* window size in points */
  406.     CCSize    m_obWinSizeInPoints;
  407.    
  408.     /* content scale factor */
  409.     float    m_fContentScaleFactor;
  410.  
  411.     /* store the fps string */
  412.     char *m_pszFPS;
  413.  
  414.     /* This object will be visited after the scene. Useful to hook a notification node */
  415.     CCNode *m_pNotificationNode;
  416.  
  417.     /* Projection protocol delegate */
  418.     CCDirectorDelegate *m_pProjectionDelegate;
  419.    
  420.     // CCEGLViewProtocol will recreate stats labels to fit visible rect
  421.     friend class CCEGLViewProtocol;
  422. };
  423.  
  424. /**
  425.  @brief DisplayLinkDirector is a Director that synchronizes timers with the refresh rate of the display.
  426.  
  427.  Features and Limitations:
  428.   - Scheduled timers & drawing are synchronizes with the refresh rate of the display
  429.   - Only supports animation intervals of 1/60 1/30 & 1/15
  430.  
  431.  @since v0.8.2
  432.  */
  433. class CCDisplayLinkDirector : public CCDirector
  434. {
  435. public:
  436.     CCDisplayLinkDirector(void)
  437.         : m_bInvalid(false)
  438.     {}
  439.  
  440.     virtual void mainLoop(void);
  441.     virtual void setAnimationInterval(double dValue);
  442.     virtual void startAnimation(void);
  443.     virtual void stopAnimation();
  444.  
  445. protected:
  446.     bool m_bInvalid;
  447. };
  448.  
  449. // end of base_node group
  450. /// @}
  451.  
  452. NS_CC_END
  453.  
  454. #endif // __CCDIRECTOR_H__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement