SceneComponent.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (C) 2009-2021, 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. namespace anki {
  10. /// @addtogroup scene
  11. /// @{
  12. /// Scene component class info.
  13. class SceneComponentRtti
  14. {
  15. public:
  16. using Constructor = void (*)(SceneComponent*, SceneNode*);
  17. U8 m_classId;
  18. const char* m_className;
  19. U32 m_classSize;
  20. U32 m_classAlignment;
  21. Constructor m_constructorCallback;
  22. SceneComponentRtti(const char* name, U32 size, U32 alignment, Constructor constructor);
  23. };
  24. /// Define a scene component.
  25. #define ANKI_SCENE_COMPONENT(className) \
  26. static SceneComponentRtti _m_rtti; \
  27. static void _construct(SceneComponent* self, SceneNode* node) \
  28. { \
  29. ::new(self) className(node); \
  30. } \
  31. \
  32. public: \
  33. static U8 getStaticClassId() \
  34. { \
  35. return _m_rtti.m_classId; \
  36. } \
  37. \
  38. private:
  39. /// Define the statics of a scene component.
  40. #define ANKI_SCENE_COMPONENT_STATICS(className) \
  41. SceneComponentRtti className::_m_rtti(ANKI_STRINGIZE(className), sizeof(className), alignof(className), \
  42. className::_construct);
  43. /// Scene node component
  44. class SceneComponent
  45. {
  46. public:
  47. /// Construct the scene component.
  48. SceneComponent(SceneNode* node, U8 classId, Bool isFeedbackComponent = false);
  49. virtual ~SceneComponent()
  50. {
  51. }
  52. U8 getClassId() const
  53. {
  54. return m_classId;
  55. }
  56. Timestamp getTimestamp() const
  57. {
  58. return m_timestamp;
  59. }
  60. Bool isFeedbackComponent() const
  61. {
  62. return m_feedbackComponent;
  63. }
  64. /// Do some updating
  65. /// @param node The owner node of this component.
  66. /// @param prevTime Previous update time.
  67. /// @param crntTime Current update time.
  68. /// @param[out] updated true if an update happened.
  69. virtual ANKI_USE_RESULT Error update(SceneNode& node, Second prevTime, Second crntTime, Bool& updated)
  70. {
  71. updated = false;
  72. return Error::NONE;
  73. }
  74. /// Don't call it.
  75. void setTimestamp(Timestamp timestamp)
  76. {
  77. ANKI_ASSERT(timestamp > 0);
  78. ANKI_ASSERT(timestamp >= m_timestamp);
  79. m_timestamp = timestamp;
  80. }
  81. static const SceneComponentRtti& findClassRtti(CString className);
  82. static const SceneComponentRtti& findClassRtti(U8 classId);
  83. private:
  84. Timestamp m_timestamp = 1; ///< Indicates when an update happened
  85. U8 m_classId : 7; ///< Cache the type ID.
  86. U8 m_feedbackComponent : 1;
  87. };
  88. /// @}
  89. } // end namespace anki