BsCoreSceneManager.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 BansheeEngine
  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. /**
  23. * Manages all objects in the scene and provides various query methods for finding objects. This is just the base class
  24. * with basic query functionality. You should override it with your own version.
  25. */
  26. class BS_CORE_EXPORT CoreSceneManager : public Module<CoreSceneManager>
  27. {
  28. public:
  29. CoreSceneManager();
  30. virtual ~CoreSceneManager();
  31. /** Returns the root scene object. */
  32. HSceneObject getRootNode() const { return mRootNode; }
  33. /**
  34. * Destroys all scene objects in the scene.
  35. *
  36. * @param[in] forceAll If true, then even the persistent objects will be unloaded.
  37. */
  38. void clearScene(bool forceAll = false);
  39. /** Returns all cameras in the scene. */
  40. const Map<Camera*, SceneCameraData>& getAllCameras() const { return mCameras; }
  41. /**
  42. * Returns the camera in the scene marked as main. Main camera controls the final render surface that is displayed
  43. * to the user. If there are multiple main cameras, the first one found returned.
  44. */
  45. SceneCameraData getMainCamera() const;
  46. /**
  47. * Sets the render target that the main camera in the scene (if any) will render its view to. This generally means
  48. * the main game window when running standalone, or the Game viewport when running in editor.
  49. */
  50. void setMainRenderTarget(const SPtr<RenderTarget>& rt);
  51. /** Notifies the scene manager that a new camera was created. */
  52. void _registerCamera(const SPtr<Camera>& camera, const HSceneObject& so);
  53. /** Notifies the scene manager that a camera was removed. */
  54. void _unregisterCamera(const SPtr<Camera>& camera);
  55. /** Notifies the scene manager that a camera either became the main camera, or has stopped being main camera. */
  56. void _notifyMainCameraStateChanged(const SPtr<Camera>& camera);
  57. /** Changes the root scene object. Any persistent objects will remain in the scene, now parented to the new root. */
  58. void _setRootNode(const HSceneObject& root);
  59. /** Called every frame. Calls update methods on all scene objects and their components. */
  60. virtual void _update();
  61. /** Updates dirty transforms on any core objects that may be tied with scene objects. */
  62. virtual void _updateCoreObjectTransforms() { }
  63. protected:
  64. friend class SceneObject;
  65. /**
  66. * Register a new node in the scene manager, on the top-most level of the hierarchy.
  67. *
  68. * @param[in] node Node you wish to add. It's your responsibility not to add duplicate or null nodes. This
  69. * method won't check.
  70. *
  71. * @note
  72. * After you add a node in the scene manager, it takes ownership of its memory and is responsible for releasing it.
  73. * Do NOT add nodes that have already been added (if you just want to change their parent). Normally this
  74. * method will only be called by SceneObject.
  75. */
  76. void registerNewSO(const HSceneObject& node);
  77. /** Callback that is triggered when the main render target size is changed. */
  78. void onMainRenderTargetResized();
  79. protected:
  80. HSceneObject mRootNode;
  81. Map<Camera*, SceneCameraData> mCameras;
  82. Vector<SceneCameraData> mMainCameras;
  83. SPtr<RenderTarget> mMainRT;
  84. HEvent mMainRTResizedConn;
  85. };
  86. /**
  87. * Handles creation of a scene manager.
  88. *
  89. * @note
  90. * Since scene manager implementations may vary it is expected that a concrete implementation of a scene manager will
  91. * register its creation method using setFactoryMethod() which will then later be used for creating the scene manager
  92. * during application start up.
  93. */
  94. class BS_CORE_EXPORT SceneManagerFactory
  95. {
  96. public:
  97. /** Creates a concrete scene manager, depending on the currently set factory method. */
  98. static void create();
  99. /** Sets method that will be used for creating the scene manager when create() gets called. */
  100. static void setFactoryMethod(const std::function<void()>& method)
  101. {
  102. mFactoryMethod = method;
  103. }
  104. private:
  105. static std::function<void()> mFactoryMethod;
  106. };
  107. /** Provides easy access to the CoreSceneManager. */
  108. BS_CORE_EXPORT CoreSceneManager& gCoreSceneManager();
  109. /** @} */
  110. }