SceneComponent.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  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/Functions.h>
  8. #include <AnKi/Util/BitMask.h>
  9. #include <AnKi/Util/Enum.h>
  10. namespace anki {
  11. /// @addtogroup scene
  12. /// @{
  13. /// @memberof SceneComponent
  14. enum class SceneComponentType : U8
  15. {
  16. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) k##name,
  17. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  18. kCount,
  19. kFirst = 0
  20. };
  21. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(SceneComponentType)
  22. /// @memberof SceneComponent
  23. enum class SceneComponentTypeMask : U32
  24. {
  25. kNone = 0,
  26. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) k##name = 1 << U32(SceneComponentType::k##name),
  27. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  28. };
  29. ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(SceneComponentTypeMask)
  30. class SceneComponentType2
  31. {
  32. public:
  33. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) static constexpr SceneComponentType k##name##Component = SceneComponentType::k##name;
  34. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  35. };
  36. #define ANKI_SCENE_COMPONENT(className) \
  37. public: \
  38. static constexpr SceneComponentType kClassType = SceneComponentType2::k##className; \
  39. \
  40. private:
  41. /// Passed to SceneComponent::update.
  42. /// @memberof SceneComponent
  43. class SceneComponentUpdateInfo
  44. {
  45. public:
  46. SceneNode* m_node = nullptr;
  47. const Second m_previousTime;
  48. const Second m_currentTime;
  49. const Second m_dt;
  50. StackMemoryPool* m_framePool = nullptr;
  51. SceneComponentUpdateInfo(Second prevTime, Second crntTime)
  52. : m_previousTime(prevTime)
  53. , m_currentTime(crntTime)
  54. , m_dt(crntTime - prevTime)
  55. {
  56. }
  57. };
  58. /// Scene node component.
  59. class SceneComponent
  60. {
  61. public:
  62. /// Construct the scene component.
  63. SceneComponent(SceneNode* node, SceneComponentType type);
  64. virtual ~SceneComponent() = default;
  65. SceneComponentType getType() const
  66. {
  67. return SceneComponentType(m_type);
  68. }
  69. Timestamp getTimestamp() const
  70. {
  71. return m_timestamp;
  72. }
  73. U32 getUuid() const
  74. {
  75. return m_uuid;
  76. }
  77. ANKI_INTERNAL U32 getArrayIndex() const
  78. {
  79. ANKI_ASSERT(m_arrayIdx != kMaxU32);
  80. return m_arrayIdx;
  81. }
  82. ANKI_INTERNAL void setArrayIndex(U32 idx)
  83. {
  84. m_arrayIdx = idx & (kMaxU32 >> 24u);
  85. }
  86. ANKI_INTERNAL virtual void onDestroy([[maybe_unused]] SceneNode& node)
  87. {
  88. }
  89. ANKI_INTERNAL virtual void update(SceneComponentUpdateInfo& info, Bool& updated) = 0;
  90. ANKI_INTERNAL virtual void onOtherComponentRemovedOrAdded([[maybe_unused]] SceneComponent* other, [[maybe_unused]] Bool added)
  91. {
  92. }
  93. /// Don't call it directly.
  94. ANKI_INTERNAL void setTimestamp(Timestamp timestamp)
  95. {
  96. ANKI_ASSERT(timestamp > 0);
  97. ANKI_ASSERT(timestamp >= m_timestamp);
  98. m_timestamp = timestamp;
  99. }
  100. static constexpr F32 getUpdateOrderWeight(SceneComponentType type)
  101. {
  102. return m_updateOrderWeights[type];
  103. }
  104. protected:
  105. U32 regenerateUuid();
  106. private:
  107. Timestamp m_timestamp = 1; ///< Indicates when an update happened
  108. U32 m_uuid = 0;
  109. U32 m_arrayIdx : 24 = kMaxU32 >> 24u;
  110. U32 m_type : 8 = 0; ///< Cache the type ID.
  111. static constexpr Array<F32, U32(SceneComponentType::kCount)> m_updateOrderWeights = {
  112. #define ANKI_DEFINE_SCENE_COMPONENT(name, weight) weight
  113. #define ANKI_SCENE_COMPONENT_SEPARATOR ,
  114. #include <AnKi/Scene/Components/SceneComponentClasses.def.h>
  115. };
  116. };
  117. /// @}
  118. } // end namespace anki