SceneNode.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright (C) 2009-2016, Panagiotis Christopoulos Charitos.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <anki/scene/Common.h>
  7. #include <anki/util/Hierarchy.h>
  8. #include <anki/util/Rtti.h>
  9. #include <anki/util/Bitset.h>
  10. #include <anki/util/List.h>
  11. #include <anki/util/Enum.h>
  12. #include <anki/scene/SceneComponent.h>
  13. namespace anki
  14. {
  15. class SceneGraph; // Don't include
  16. class ResourceManager;
  17. /// @addtogroup scene
  18. /// @{
  19. /// Interface class backbone of scene
  20. class SceneNode : public Hierarchy<SceneNode>,
  21. public IntrusiveListEnabled<SceneNode>
  22. {
  23. public:
  24. using Base = Hierarchy<SceneNode>;
  25. /// The one and only constructor
  26. SceneNode(SceneGraph* scene);
  27. /// Unregister node
  28. virtual ~SceneNode();
  29. /// @param name The unique name of the node. If it's nullptr the the node
  30. /// is not searchable.
  31. ANKI_USE_RESULT Error create(const CString& name);
  32. SceneGraph& getSceneGraph()
  33. {
  34. return *m_scene;
  35. }
  36. /// Return the name. It may be empty for nodes that we don't want to track
  37. CString getName() const
  38. {
  39. return (!m_name.isEmpty()) ? m_name.toCString() : CString();
  40. }
  41. U32 getComponentsCount() const
  42. {
  43. return m_componentsCount;
  44. }
  45. Bool getMarkedForDeletion() const
  46. {
  47. return m_flags.bitsEnabled(Flag::MARKED_FOR_DELETION);
  48. }
  49. void setMarkedForDeletion();
  50. Timestamp getGlobalTimestamp() const;
  51. SceneAllocator<U8> getSceneAllocator() const;
  52. SceneFrameAllocator<U8> getFrameAllocator() const;
  53. void addChild(SceneNode* obj)
  54. {
  55. Base::addChild(getSceneAllocator(), obj);
  56. }
  57. /// This is called by the scene every frame after logic and before
  58. /// rendering. By default it does nothing
  59. /// @param prevUpdateTime Timestamp of the previous update
  60. /// @param crntTime Timestamp of this update
  61. virtual ANKI_USE_RESULT Error frameUpdate(F32 prevUpdateTime, F32 crntTime)
  62. {
  63. (void)prevUpdateTime;
  64. (void)crntTime;
  65. return ErrorCode::NONE;
  66. }
  67. /// Return the last frame the node was updated. It checks all components
  68. U32 getLastUpdateFrame() const;
  69. /// Inform if a sector has visited this node.
  70. void setSectorVisited(Bool visited)
  71. {
  72. m_flags.enableBits(Flag::SECTOR_VISITED, visited);
  73. }
  74. /// Check if a sector has visited this node.
  75. Bool getSectorVisited() const
  76. {
  77. return m_flags.bitsEnabled(Flag::SECTOR_VISITED);
  78. }
  79. /// Iterate all components
  80. template<typename Func>
  81. ANKI_USE_RESULT Error iterateComponents(Func func) const
  82. {
  83. Error err = ErrorCode::NONE;
  84. auto it = m_components.getBegin();
  85. auto end = it + m_componentsCount;
  86. for(; !err && it != end; ++it)
  87. {
  88. err = func(*(*it));
  89. }
  90. return err;
  91. }
  92. /// Iterate all components of a specific type
  93. template<typename Component, typename Func>
  94. ANKI_USE_RESULT Error iterateComponentsOfType(Func func)
  95. {
  96. Error err = ErrorCode::NONE;
  97. auto it = m_components.getBegin();
  98. auto end = it + m_componentsCount;
  99. for(; !err && it != end; ++it)
  100. {
  101. SceneComponent* comp = *it;
  102. if(isa<Component>(comp))
  103. {
  104. err = func(*dcast<Component*>(comp));
  105. }
  106. }
  107. return err;
  108. }
  109. /// Try geting a pointer to the first component of the requested type
  110. template<typename Component>
  111. Component* tryGetComponent()
  112. {
  113. U count = m_componentsCount;
  114. while(count-- != 0)
  115. {
  116. if(isa<Component>(m_components[count]))
  117. {
  118. return dcast<Component*>(m_components[count]);
  119. }
  120. }
  121. return nullptr;
  122. }
  123. /// Try geting a pointer to the first component of the requested type
  124. template<typename Component>
  125. const Component* tryGetComponent() const
  126. {
  127. U count = m_componentsCount;
  128. while(count-- != 0)
  129. {
  130. if(isa<Component>(m_components[count]))
  131. {
  132. return dcast<const Component*>(m_components[count]);
  133. }
  134. }
  135. return nullptr;
  136. }
  137. /// Get a pointer to the first component of the requested type
  138. template<typename Component>
  139. Component& getComponent()
  140. {
  141. Component* out = tryGetComponent<Component>();
  142. ANKI_ASSERT(out != nullptr);
  143. return *out;
  144. }
  145. /// Get a pointer to the first component of the requested type
  146. template<typename Component>
  147. const Component& getComponent() const
  148. {
  149. const Component* out = tryGetComponent<Component>();
  150. ANKI_ASSERT(out != nullptr);
  151. return *out;
  152. }
  153. protected:
  154. /// Append a component to the components container. The SceneNode will not
  155. /// take ownership
  156. void addComponent(SceneComponent* comp, Bool transferOwnership = false);
  157. /// Remove a component from the container
  158. void removeComponent(SceneComponent* comp);
  159. ResourceManager& getResourceManager();
  160. private:
  161. enum class Flag
  162. {
  163. MARKED_FOR_DELETION = 1 << 0,
  164. SECTOR_VISITED = 1 << 1
  165. };
  166. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(Flag, friend)
  167. SceneGraph* m_scene = nullptr;
  168. DArray<SceneComponent*> m_components;
  169. U8 m_componentsCount = 0;
  170. String m_name; ///< A unique name
  171. Bitset<Flag> m_flags;
  172. void cacheImportantComponents();
  173. };
  174. /// @}
  175. } // end namespace anki