BsSceneManager.h 7.5 KB

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