SceneComponent.h 2.4 KB

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