SceneComponent.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/Scene/Components/SceneComponent.h>
  6. #include <AnKi/Scene/SceneNode.h>
  7. namespace anki {
  8. static SceneComponentRtti* g_rttis[kMaxSceneComponentClasses] = {};
  9. SceneComponentCallbacks g_sceneComponentCallbacks;
  10. static U32 g_sceneComponentClassCount = 0;
  11. SceneComponentRtti::SceneComponentRtti(const char* name, U32 size, U32 alignment, SceneComponentVtable vtable)
  12. {
  13. if(g_sceneComponentClassCount >= kMaxSceneComponentClasses)
  14. {
  15. // No special logging because this function is called before main
  16. printf("Reached maximum component count. Increase kMaxSceneComponentClasses\n");
  17. exit(-1);
  18. }
  19. m_className = name;
  20. ANKI_ASSERT(size < getMaxNumericLimit<decltype(m_classSize)>());
  21. m_classSize = decltype(m_classSize)(size);
  22. ANKI_ASSERT(alignment < getMaxNumericLimit<decltype(m_classAlignment)>());
  23. m_classAlignment = decltype(m_classAlignment)(alignment);
  24. m_classId = kMaxU8;
  25. g_rttis[g_sceneComponentClassCount] = this;
  26. g_sceneComponentCallbacks.m_constructors[g_sceneComponentClassCount] = vtable.m_constructor;
  27. g_sceneComponentCallbacks.m_destructors[g_sceneComponentClassCount] = vtable.m_destructor;
  28. g_sceneComponentCallbacks.m_onDestroys[g_sceneComponentClassCount] = vtable.m_onDestroy;
  29. g_sceneComponentCallbacks.m_updates[g_sceneComponentClassCount] = vtable.m_update;
  30. ++g_sceneComponentClassCount;
  31. // Sort everything because the IDs should be consistend between platforms and compilation builds
  32. {
  33. // Copy to a temp array
  34. class Temp
  35. {
  36. public:
  37. SceneComponentRtti* m_rrti;
  38. SceneComponentVtable m_vtable;
  39. };
  40. Temp temps[kMaxSceneComponentClasses];
  41. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  42. {
  43. temps[i].m_rrti = g_rttis[i];
  44. temps[i].m_vtable = {g_sceneComponentCallbacks.m_constructors[i],
  45. g_sceneComponentCallbacks.m_destructors[i], g_sceneComponentCallbacks.m_onDestroys[i],
  46. g_sceneComponentCallbacks.m_updates[i]};
  47. }
  48. std::sort(&temps[0], &temps[g_sceneComponentClassCount], [](const Temp& a, const Temp& b) {
  49. return std::strcmp(a.m_rrti->m_className, b.m_rrti->m_className) < 0;
  50. });
  51. // Re-calculate the glass IDs
  52. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  53. {
  54. temps[i].m_rrti->m_classId = U8(i);
  55. }
  56. // Copy back
  57. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  58. {
  59. g_rttis[i] = temps[i].m_rrti;
  60. g_sceneComponentCallbacks.m_constructors[i] = temps[i].m_vtable.m_constructor;
  61. g_sceneComponentCallbacks.m_destructors[i] = temps[i].m_vtable.m_destructor;
  62. g_sceneComponentCallbacks.m_onDestroys[i] = temps[i].m_vtable.m_onDestroy;
  63. g_sceneComponentCallbacks.m_updates[i] = temps[i].m_vtable.m_update;
  64. }
  65. }
  66. }
  67. SceneComponent::SceneComponent([[maybe_unused]] SceneNode* node, U8 classId, Bool isFeedbackComponent)
  68. : m_classId(classId & 0x7F)
  69. , m_feedbackComponent(isFeedbackComponent)
  70. {
  71. ANKI_ASSERT(classId < g_sceneComponentClassCount);
  72. }
  73. const SceneComponentRtti& SceneComponent::findClassRtti(CString className)
  74. {
  75. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  76. {
  77. ANKI_ASSERT(g_rttis[i]);
  78. ANKI_ASSERT(g_rttis[i]->m_className);
  79. if(g_rttis[i]->m_className == className)
  80. {
  81. return *g_rttis[i];
  82. }
  83. }
  84. ANKI_ASSERT(0);
  85. return *g_rttis[0];
  86. }
  87. const SceneComponentRtti& SceneComponent::getClassRtti(U8 classId)
  88. {
  89. ANKI_ASSERT(classId < g_sceneComponentClassCount);
  90. ANKI_ASSERT(g_rttis[classId]);
  91. return *g_rttis[classId];
  92. }
  93. SceneGraphExternalSubsystems& SceneComponent::getExternalSubsystems(const SceneNode& node)
  94. {
  95. return node.getExternalSubsystems();
  96. }
  97. } // namespace anki