CCScene.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /****************************************************************************
  2. Copyright (c) 2008-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #ifndef __CCSCENE_H__
  24. #define __CCSCENE_H__
  25. #include <string>
  26. #include "2d/CCNode.h"
  27. NS_CC_BEGIN
  28. class Camera;
  29. class BaseLight;
  30. class Renderer;
  31. class EventListenerCustom;
  32. class EventCustom;
  33. #if CC_USE_PHYSICS
  34. class PhysicsWorld;
  35. #endif
  36. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  37. class Physics3DWorld;
  38. #endif
  39. #if CC_USE_NAVMESH
  40. class NavMesh;
  41. #endif
  42. /**
  43. * @addtogroup _2d
  44. * @{
  45. */
  46. /** @class Scene
  47. * @brief Scene is a subclass of Node that is used only as an abstract concept.
  48. Scene and Node are almost identical with the difference that Scene has its
  49. anchor point (by default) at the center of the screen.
  50. For the moment Scene has no other logic than that, but in future releases it might have
  51. additional logic.
  52. It is a good practice to use a Scene as the parent of all your nodes.
  53. Scene will create a default camera for you.
  54. */
  55. class CC_DLL Scene : public Node
  56. {
  57. public:
  58. /** Creates a new Scene object.
  59. *
  60. * @return An autoreleased Scene object.
  61. */
  62. static Scene *create();
  63. /** Creates a new Scene object with a predefined Size.
  64. *
  65. * @param size The predefined size of scene.
  66. * @return An autoreleased Scene object.
  67. * @js NA
  68. */
  69. static Scene *createWithSize(const Size& size);
  70. using Node::addChild;
  71. virtual std::string getDescription() const override;
  72. /** Get all cameras.
  73. *
  74. * @return The vector of all cameras, ordered by camera depth.
  75. * @js NA
  76. */
  77. const std::vector<Camera*>& getCameras();
  78. /** Get the default camera.
  79. * @js NA
  80. * @return The default camera of scene.
  81. */
  82. Camera* getDefaultCamera() const { return _defaultCamera; }
  83. /** Get lights.
  84. * @return The vector of lights.
  85. * @js NA
  86. */
  87. const std::vector<BaseLight*>& getLights() const { return _lights; }
  88. /** Render the scene.
  89. * @param renderer The renderer use to render the scene.
  90. * @param eyeTransform The AdditionalTransform of camera.
  91. * @param eyeProjection The projection matrix of camera.
  92. * @js NA
  93. */
  94. virtual void render(Renderer* renderer, const Mat4& eyeTransform, const Mat4* eyeProjection = nullptr);
  95. /** Render the scene.
  96. * @param renderer The renderer use to render the scene.
  97. * @param eyeTransforms The AdditionalTransform List of camera of multiView.
  98. * @param eyeProjections The projection matrix List of camera of multiView.
  99. * @param multiViewCount The number of multiView.
  100. * @js NA
  101. */
  102. virtual void render(Renderer* renderer, const Mat4* eyeTransforms, const Mat4* eyeProjections, unsigned int multiViewCount);
  103. /** override function */
  104. virtual void removeAllChildren() override;
  105. CC_CONSTRUCTOR_ACCESS:
  106. Scene();
  107. virtual ~Scene();
  108. bool init() override;
  109. bool initWithSize(const Size& size);
  110. void setCameraOrderDirty() { _cameraOrderDirty = true; }
  111. void onProjectionChanged(EventCustom* event);
  112. protected:
  113. friend class Node;
  114. friend class ProtectedNode;
  115. friend class SpriteBatchNode;
  116. friend class Camera;
  117. friend class BaseLight;
  118. friend class Renderer;
  119. std::vector<Camera*> _cameras; //weak ref to Camera
  120. Camera* _defaultCamera; //weak ref, default camera created by scene, _cameras[0], Caution that the default camera can not be added to _cameras before onEnter is called
  121. bool _cameraOrderDirty; // order is dirty, need sort
  122. EventListenerCustom* _event;
  123. std::vector<BaseLight *> _lights;
  124. private:
  125. CC_DISALLOW_COPY_AND_ASSIGN(Scene);
  126. #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION))
  127. public:
  128. #if CC_USE_PHYSICS
  129. /** Get the physics world of the scene.
  130. * @return The physics world of the scene.
  131. * @js NA
  132. */
  133. PhysicsWorld* getPhysicsWorld() const { return _physicsWorld; }
  134. #endif
  135. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  136. /** Get the 3d physics world of the scene.
  137. * @return The 3d physics world of the scene.
  138. * @js NA
  139. */
  140. Physics3DWorld* getPhysics3DWorld() { return _physics3DWorld; }
  141. /**
  142. * Set Physics3D debug draw camera.
  143. */
  144. void setPhysics3DDebugCamera(Camera* camera);
  145. #endif
  146. /** Create a scene with physics.
  147. * @return An autoreleased Scene object with physics.
  148. * @js NA
  149. */
  150. static Scene *createWithPhysics();
  151. CC_CONSTRUCTOR_ACCESS:
  152. bool initWithPhysics();
  153. protected:
  154. void addChildToPhysicsWorld(Node* child);
  155. #if CC_USE_PHYSICS
  156. PhysicsWorld* _physicsWorld;
  157. #endif
  158. #if CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION
  159. Physics3DWorld* _physics3DWorld;
  160. Camera* _physics3dDebugCamera; //
  161. #endif
  162. #endif // (CC_USE_PHYSICS || CC_USE_3D_PHYSICS)
  163. #if CC_USE_NAVMESH
  164. public:
  165. /** set navigation mesh */
  166. void setNavMesh(NavMesh* navMesh);
  167. /** get navigation mesh */
  168. NavMesh* getNavMesh() const { return _navMesh; }
  169. /**
  170. * Set NavMesh debug draw camera.
  171. */
  172. void setNavMeshDebugCamera(Camera *camera);
  173. protected:
  174. NavMesh* _navMesh;
  175. Camera * _navMeshDebugCamera;
  176. #endif
  177. #if (CC_USE_PHYSICS || (CC_USE_3D_PHYSICS && CC_ENABLE_BULLET_INTEGRATION) || CC_USE_NAVMESH)
  178. public:
  179. void stepPhysicsAndNavigation(float deltaTime);
  180. #endif
  181. };
  182. // end of _2d group
  183. /// @}
  184. NS_CC_END
  185. #endif // __CCSCENE_H__