SceneComponent.cpp 2.0 KB

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