CCScene.h 6.7 KB

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