SceneComponent.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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, 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_updateWeight = updateWeight;
  20. m_className = name;
  21. ANKI_ASSERT(size < getMaxNumericLimit<decltype(m_classSize)>());
  22. m_classSize = decltype(m_classSize)(size);
  23. ANKI_ASSERT(alignment < getMaxNumericLimit<decltype(m_classAlignment)>());
  24. m_classAlignment = decltype(m_classAlignment)(alignment);
  25. m_classId = kMaxU8;
  26. g_rttis[g_sceneComponentClassCount] = this;
  27. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) g_sceneComponentCallbacks.m_##name[g_sceneComponentClassCount] = vtable.m_##name;
  28. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  29. ++g_sceneComponentClassCount;
  30. // Sort everything because the IDs should be consistend between platforms and compilation builds
  31. {
  32. // Copy to a temp array
  33. class Temp
  34. {
  35. public:
  36. SceneComponentRtti* m_rrti;
  37. SceneComponentVtable m_vtable;
  38. };
  39. Temp temps[kMaxSceneComponentClasses];
  40. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  41. {
  42. temps[i].m_rrti = g_rttis[i];
  43. temps[i].m_vtable = {
  44. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) g_sceneComponentCallbacks.m_##name[i]
  45. #define ANKI_SCENE_COMPONENT_VIRTUAL_SEPERATOR ,
  46. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  47. };
  48. }
  49. std::sort(&temps[0], &temps[g_sceneComponentClassCount], [](const Temp& a, const Temp& b) {
  50. return std::strcmp(a.m_rrti->m_className, b.m_rrti->m_className) < 0;
  51. });
  52. // Re-calculate the glass IDs
  53. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  54. {
  55. temps[i].m_rrti->m_classId = U8(i);
  56. }
  57. // Copy back
  58. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  59. {
  60. g_rttis[i] = temps[i].m_rrti;
  61. #define ANKI_SCENE_COMPONENT_VIRTUAL(name, type) g_sceneComponentCallbacks.m_##name[i] = temps[i].m_vtable.m_##name;
  62. #include <AnKi/Scene/Components/SceneComponentVirtuals.defs.h>
  63. }
  64. }
  65. }
  66. SceneComponent::SceneComponent([[maybe_unused]] SceneNode* node, U8 classId)
  67. : m_classId(classId)
  68. {
  69. ANKI_ASSERT(classId < g_sceneComponentClassCount);
  70. }
  71. const SceneComponentRtti& SceneComponent::findClassRtti(CString className)
  72. {
  73. for(U32 i = 0; i < g_sceneComponentClassCount; ++i)
  74. {
  75. ANKI_ASSERT(g_rttis[i]);
  76. ANKI_ASSERT(g_rttis[i]->m_className);
  77. if(g_rttis[i]->m_className == className)
  78. {
  79. return *g_rttis[i];
  80. }
  81. }
  82. ANKI_ASSERT(0);
  83. return *g_rttis[0];
  84. }
  85. const SceneComponentRtti& SceneComponent::getClassRtti(U8 classId)
  86. {
  87. ANKI_ASSERT(classId < g_sceneComponentClassCount);
  88. ANKI_ASSERT(g_rttis[classId]);
  89. return *g_rttis[classId];
  90. }
  91. } // namespace anki