SceneComponent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright (C) 2009-2023, 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, F32 updateWeight, U32 size, U32 alignment,
  12. SceneComponentVtable vtable)
  13. {
  14. if(g_sceneComponentClassCount >= kMaxSceneComponentClasses)
  15. {
  16. // No special logging because this function is called before main
  17. printf("Reached maximum component count. Increase kMaxSceneComponentClasses\n");
  18. exit(-1);
  19. }
  20. m_updateWeight = updateWeight;
  21. m_className = name;
  22. ANKI_ASSERT(size < getMaxNumericLimit<decltype(m_classSize)>());
  23. m_classSize = decltype(m_classSize)(size);
  24. ANKI_ASSERT(alignment < getMaxNumericLimit<decltype(m_classAlignment)>());
  25. m_classAlignment = decltype(m_classAlignment)(alignment);
  26. m_classId = kMaxU8;
  27. g_rttis[g_sceneComponentClassCount] = this;
  28. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) \
  29. g_sceneComponentCallbacks.m_##name[g_sceneComponentClassCount] = vtable.m_##name;
  30. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  31. ++g_sceneComponentClassCount;
  32. // Sort everything because the IDs should be consistend between platforms and compilation builds
  33. {
  34. // Copy to a temp array
  35. class Temp
  36. {
  37. public:
  38. SceneComponentRtti* m_rrti;
  39. SceneComponentVtable m_vtable;
  40. };
  41. Temp temps[kMaxSceneComponentClasses];
  42. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  43. {
  44. temps[i].m_rrti = g_rttis[i];
  45. temps[i].m_vtable = {
  46. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) g_sceneComponentCallbacks.m_##name[i]
  47. #define ANKI_SCENE_COMPONENT_VIRTUAL_SEPERATOR ,
  48. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  49. };
  50. }
  51. std::sort(&temps[0], &temps[g_sceneComponentClassCount], [](const Temp& a, const Temp& b) {
  52. return std::strcmp(a.m_rrti->m_className, b.m_rrti->m_className) < 0;
  53. });
  54. // Re-calculate the glass IDs
  55. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  56. {
  57. temps[i].m_rrti->m_classId = U8(i);
  58. }
  59. // Copy back
  60. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  61. {
  62. g_rttis[i] = temps[i].m_rrti;
  63. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) g_sceneComponentCallbacks.m_##name[i] = temps[i].m_vtable.m_##name;
  64. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  65. }
  66. }
  67. }
  68. SceneComponent::SceneComponent([[maybe_unused]] SceneNode* node, U8 classId)
  69. : m_classId(classId)
  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. } // namespace anki