SceneComponent.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. #include <AnKi/Scene/Components/SceneComponent.h>
  6. namespace anki {
  7. constexpr U32 MAX_SCENE_COMPONENT_CLASSES = 64;
  8. static_assert(MAX_SCENE_COMPONENT_CLASSES < 128, "It can oly be 7 bits because of SceneComponent::m_classId");
  9. static SceneComponentRtti* g_rttis[MAX_SCENE_COMPONENT_CLASSES] = {};
  10. static U32 g_rttiCount = 0;
  11. SceneComponentRtti::SceneComponentRtti(const char* name, U32 size, U32 alignment, Constructor constructor)
  12. {
  13. if(g_rttiCount >= MAX_SCENE_COMPONENT_CLASSES)
  14. {
  15. // No special logging because this function is called before main
  16. printf("Reached maximum component count. Increase MAX_SCENE_COMPONENT_CLASSES\n");
  17. exit(-1);
  18. }
  19. m_constructorCallback = constructor;
  20. m_className = name;
  21. m_classSize = size;
  22. m_classAlignment = alignment;
  23. m_classId = MAX_U8;
  24. g_rttis[g_rttiCount++] = this;
  25. // Sort everything because the IDs should be consistend between platforms and compilation builds
  26. std::sort(&g_rttis[0], &g_rttis[g_rttiCount], [](const SceneComponentRtti* a, const SceneComponentRtti* b) {
  27. return std::strcmp(a->m_className, b->m_className) < 0;
  28. });
  29. // Re-calculate the glass IDs
  30. for(U32 i = 0; i < g_rttiCount; ++i)
  31. {
  32. g_rttis[i]->m_classId = U8(i);
  33. }
  34. }
  35. SceneComponent::SceneComponent(SceneNode* node, U8 classId, Bool isFeedbackComponent)
  36. : m_classId(classId & 0x7F)
  37. , m_feedbackComponent(isFeedbackComponent)
  38. {
  39. ANKI_ASSERT(classId < g_rttiCount);
  40. }
  41. const SceneComponentRtti& SceneComponent::findClassRtti(CString className)
  42. {
  43. for(U32 i = 0; i < g_rttiCount; ++i)
  44. {
  45. ANKI_ASSERT(g_rttis[i]);
  46. ANKI_ASSERT(g_rttis[i]->m_className);
  47. if(g_rttis[i]->m_className == className)
  48. {
  49. return *g_rttis[i];
  50. }
  51. }
  52. ANKI_ASSERT(0);
  53. return *g_rttis[0];
  54. }
  55. const SceneComponentRtti& SceneComponent::findClassRtti(U8 classId)
  56. {
  57. ANKI_ASSERT(classId < g_rttiCount);
  58. ANKI_ASSERT(g_rttis[classId]);
  59. return *g_rttis[classId];
  60. }
  61. } // namespace anki