BsSceneManager.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsCorePrerequisites.h"
  5. #include "BsModule.h"
  6. #include "BsGameObject.h"
  7. namespace bs
  8. {
  9. /** @addtogroup Scene-Internal
  10. * @{
  11. */
  12. /** Contains information about a camera managed by the scene manager. */
  13. struct SceneCameraData
  14. {
  15. SceneCameraData() { }
  16. SceneCameraData(const SPtr<Camera>& camera, const HSceneObject& sceneObject)
  17. :camera(camera), sceneObject(sceneObject)
  18. { }
  19. SPtr<Camera> camera;
  20. HSceneObject sceneObject;
  21. };
  22. /** Contains information about a renderable managed by the scene manager. */
  23. struct SceneRenderableData
  24. {
  25. SceneRenderableData() { }
  26. SceneRenderableData(const SPtr<Renderable>& renderable, const HSceneObject& sceneObject)
  27. :renderable(renderable), sceneObject(sceneObject)
  28. { }
  29. SPtr<Renderable> renderable;
  30. HSceneObject sceneObject;
  31. };
  32. /** Contains information about a light managed by the scene manager. */
  33. struct SceneLightData
  34. {
  35. SceneLightData() { }
  36. SceneLightData(const SPtr<Light>& light, const HSceneObject& sceneObject)
  37. :light(light), sceneObject(sceneObject)
  38. { }
  39. SPtr<Light> light;
  40. HSceneObject sceneObject;
  41. };
  42. /** Contains information about a reflection probe managed by the scene manager. */
  43. struct SceneReflectionProbeData
  44. {
  45. SceneReflectionProbeData() { }
  46. SceneReflectionProbeData(const SPtr<ReflectionProbe>& probe, const HSceneObject& sceneObject)
  47. :probe(probe), sceneObject(sceneObject)
  48. { }
  49. SPtr<ReflectionProbe> probe;
  50. HSceneObject sceneObject;
  51. };
  52. /** Possible states components can be in. Controls which component callbacks are triggered. */
  53. enum class ComponentState
  54. {
  55. Running, /**< All components callbacks are being triggered normally. */
  56. Paused, /**< All component callbacks except update are being triggered normally. */
  57. Stopped /**< No component callbacks are being triggered. */
  58. };
  59. /** Manages active SceneObjects and provides ways for querying and updating them or their components. */
  60. class BS_CORE_EXPORT SceneManager : public Module<SceneManager>
  61. {
  62. public:
  63. SceneManager();
  64. ~SceneManager();
  65. /** Returns the root scene object. */
  66. HSceneObject getRootNode() const { return mRootNode; }
  67. /**
  68. * Destroys all scene objects in the scene.
  69. *
  70. * @param[in] forceAll If true, then even the persistent objects will be unloaded.
  71. */
  72. void clearScene(bool forceAll = false);
  73. /**
  74. * Changes the component state that globally determines which component callbacks are activated. Only affects
  75. * components that don't have the ComponentFlag::AlwaysRun flag set.
  76. */
  77. void setComponentState(ComponentState state);
  78. /** Checks are the components currently in the Running state. */
  79. bool isRunning() const { return mComponentState == ComponentState::Running; }
  80. /** Returns all cameras in the scene. */
  81. const Map<Camera*, SceneCameraData>& getAllCameras() const { return mCameras; }
  82. /**
  83. * Returns the camera in the scene marked as main. Main camera controls the final render surface that is displayed
  84. * to the user. If there are multiple main cameras, the first one found returned.
  85. */
  86. SceneCameraData getMainCamera() const;
  87. /**
  88. * Sets the render target that the main camera in the scene (if any) will render its view to. This generally means
  89. * the main game window when running standalone, or the Game viewport when running in editor.
  90. */
  91. void setMainRenderTarget(const SPtr<RenderTarget>& rt);
  92. /** Returns all renderables in the scene. */
  93. const Map<Renderable*, SceneRenderableData>& getAllRenderables() const { return mRenderables; }
  94. /** Notifies the scene manager that a new renderable was created. */
  95. void _registerRenderable(const SPtr<Renderable>& renderable, const HSceneObject& so);
  96. /** Notifies the scene manager that a renderable was removed. */
  97. void _unregisterRenderable(const SPtr<Renderable>& renderable);
  98. /** Notifies the scene manager that a new light was created. */
  99. void _registerLight(const SPtr<Light>& light, const HSceneObject& so);
  100. /** Notifies the scene manager that a light was removed. */
  101. void _unregisterLight(const SPtr<Light>& light);
  102. /** Notifies the scene manager that a new camera was created. */
  103. void _registerCamera(const SPtr<Camera>& camera, const HSceneObject& so);
  104. /** Notifies the scene manager that a camera was removed. */
  105. void _unregisterCamera(const SPtr<Camera>& camera);
  106. /** Notifies the scene manager that a new reflection probe was created. */
  107. void _registerReflectionProbe(const SPtr<ReflectionProbe>& probe, const HSceneObject& so);
  108. /** Notifies the scene manager that a reflection probe was removed. */
  109. void _unregisterReflectionProbe(const SPtr<ReflectionProbe>& probe);
  110. /** Notifies the scene manager that a camera either became the main camera, or has stopped being main camera. */
  111. void _notifyMainCameraStateChanged(const SPtr<Camera>& camera);
  112. /** Changes the root scene object. Any persistent objects will remain in the scene, now parented to the new root. */
  113. void _setRootNode(const HSceneObject& root);
  114. /** Called every frame. Calls update methods on all scene objects and their components. */
  115. void _update();
  116. /** Updates dirty transforms on any core objects that may be tied with scene objects. */
  117. void _updateCoreObjectTransforms();
  118. /** Notifies the manager that a new component has just been created. The manager triggers necessary callbacks. */
  119. void _notifyComponentCreated(const HComponent& component, bool parentActive);
  120. /**
  121. * Notifies the manager that a scene object the component belongs to was activated. The manager triggers necessary
  122. * callbacks.
  123. */
  124. void _notifyComponentActivated(const HComponent& component, bool triggerEvent);
  125. /**
  126. * Notifies the manager that a scene object the component belongs to was deactivated. The manager triggers necessary
  127. * callbacks.
  128. */
  129. void _notifyComponentDeactivated(const HComponent& component, bool triggerEvent);
  130. /** Notifies the manager that a component is about to be destroyed. The manager triggers necessary callbacks. */
  131. void _notifyComponentDestroyed(const HComponent& component);
  132. protected:
  133. friend class SceneObject;
  134. /**
  135. * Register a new node in the scene manager, on the top-most level of the hierarchy.
  136. *
  137. * @param[in] node Node you wish to add. It's your responsibility not to add duplicate or null nodes. This
  138. * method won't check.
  139. *
  140. * @note
  141. * After you add a node in the scene manager, it takes ownership of its memory and is responsible for releasing it.
  142. * Do NOT add nodes that have already been added (if you just want to change their parent). Normally this
  143. * method will only be called by SceneObject.
  144. */
  145. void registerNewSO(const HSceneObject& node);
  146. /** Callback that is triggered when the main render target size is changed. */
  147. void onMainRenderTargetResized();
  148. /** Removes a component from the active component list. */
  149. void removeFromActiveList(const HComponent& component);
  150. /** Removes a component from the inactive component list. */
  151. void removeFromInactiveList(const HComponent& component);
  152. /** Removes a component from the uninitialized component list. */
  153. void removeFromUninitializedList(const HComponent& component);
  154. /**
  155. * Encodes an index and a type into a single 32-bit integer. Top 2 bits represent the type, while the rest represent
  156. * the index.
  157. */
  158. UINT32 encodeComponentId(UINT32 idx, UINT32 type);
  159. /** Decodes an id encoded with encodeComponentId(). */
  160. void decodeComponentId(UINT32 id, UINT32& idx, UINT32& type);
  161. protected:
  162. HSceneObject mRootNode;
  163. Map<Camera*, SceneCameraData> mCameras;
  164. Vector<SceneCameraData> mMainCameras;
  165. Map<Renderable*, SceneRenderableData> mRenderables;
  166. Map<Light*, SceneLightData> mLights;
  167. Map<ReflectionProbe*, SceneReflectionProbeData> mReflectionProbes;
  168. Vector<HComponent> mActiveComponents;
  169. Vector<HComponent> mInactiveComponents;
  170. Vector<HComponent> mUnintializedComponents;
  171. SPtr<RenderTarget> mMainRT;
  172. HEvent mMainRTResizedConn;
  173. ComponentState mComponentState = ComponentState::Running;
  174. };
  175. /** Provides easy access to the SceneManager. */
  176. BS_CORE_EXPORT SceneManager& gSceneManager();
  177. /** @} */
  178. }