ComponentsManager.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "ComponentsManager.h"
  2. #include "../FileSystem/ConfigFile.h"
  3. #include "../framework.h"
  4. #include "Input/InputEmulator.h"
  5. #include "Input/TouchSliderEmulator.h"
  6. #include "Input/TouchPanelEmulator.h"
  7. #include "PlayerDataManager.h"
  8. #include "FrameRateManager.h"
  9. #include "FastLoader.h"
  10. #include "CameraController.h"
  11. #include "DebugComponent.h"
  12. #include "ScaleComponent.h"
  13. #include "ScoreSaver.h"
  14. #include "Pause.h"
  15. #include "GameTargets/TargetInspector.h"
  16. using ConfigFile = TLAC::FileSystem::ConfigFile;
  17. namespace TLAC::Components
  18. {
  19. typedef void EngineUpdateInput(void*);
  20. ComponentsManager::ComponentsManager()
  21. {
  22. }
  23. ComponentsManager::~ComponentsManager()
  24. {
  25. }
  26. void ComponentsManager::ParseAddComponents()
  27. {
  28. EmulatorComponent* allComponents[]
  29. {
  30. new TargetInspector(),
  31. new InputEmulator(),
  32. new TouchSliderEmulator(),
  33. new TouchPanelEmulator(),
  34. new Pause(), // ensure pause is always immediately after input emulators so it can swallow inputs
  35. new PlayerDataManager(),
  36. new FrameRateManager(),
  37. new FastLoader(),
  38. new ScaleComponent(),
  39. new ScoreSaver(),
  40. new CameraController(),
  41. new DebugComponent(),
  42. };
  43. ConfigFile componentsConfig(framework::GetModuleDirectory(), COMPONENTS_CONFIG_FILE_NAME);
  44. bool success = componentsConfig.OpenRead();
  45. if (!success)
  46. {
  47. printf("ComponentsManager::ParseAddComponents(): Unable to parse %s\n", COMPONENTS_CONFIG_FILE_NAME.c_str());
  48. return;
  49. }
  50. size_t componentCount = sizeof(allComponents) / sizeof(EmulatorComponent*);
  51. components.reserve(componentCount);
  52. std::string trueString = "true", falseString = "false";
  53. for (int i = 0; i < componentCount; i++)
  54. {
  55. std::string* value;
  56. auto name = allComponents[i]->GetDisplayName();
  57. //printf("ComponentsManager::ParseAddComponents(): searching name: %s\n", name);
  58. if (componentsConfig.TryGetValue(name, &value))
  59. {
  60. //printf("ComponentsManager::ParseAddComponents(): %s found\n", name);
  61. if (*value == trueString)
  62. {
  63. //printf("ComponentsManager::ParseAddComponents(): enabling %s...\n", name);
  64. components.push_back(allComponents[i]);
  65. }
  66. else if (*value == falseString)
  67. {
  68. //printf("ComponentsManager::ParseAddComponents(): disabling %s...\n", name);
  69. }
  70. else
  71. {
  72. //printf("ComponentsManager::ParseAddComponents(): invalid value %s for component %s\n", value, name);
  73. }
  74. delete value;
  75. }
  76. else
  77. {
  78. //printf("ParseAddComponents(): component %s not found\n", name);
  79. delete allComponents[i];
  80. }
  81. }
  82. }
  83. void ComponentsManager::Initialize()
  84. {
  85. dwGuiDisplay = (DwGuiDisplay*) * (uint64_t*)DW_GUI_DISPLAY_INSTANCE_PTR_ADDRESS;
  86. ParseAddComponents();
  87. updateStopwatch.Start();
  88. for (auto& component : components)
  89. component->Initialize(this);
  90. }
  91. void ComponentsManager::Update()
  92. {
  93. elpasedTime = updateStopwatch.Restart();
  94. for (auto& component : components)
  95. {
  96. component->SetElapsedTime(elpasedTime);
  97. component->Update();
  98. }
  99. }
  100. void ComponentsManager::UpdateInput()
  101. {
  102. if (!GetIsInputEmulatorUsed())
  103. {
  104. uint64_t* inputStatePtr = (uint64_t*)INPUT_STATE_PTR_ADDRESS;
  105. // poll input using the original PollInput function we overwrote with the update hook instead
  106. if (inputStatePtr != nullptr)
  107. ((EngineUpdateInput*)ENGINE_UPDATE_INPUT_ADDRESS)((void*)* inputStatePtr);
  108. }
  109. for (auto& component : components)
  110. component->UpdateInput();
  111. }
  112. void ComponentsManager::UpdatePostInput()
  113. {
  114. for (auto& component : components)
  115. {
  116. component->UpdatePostInput();
  117. }
  118. }
  119. void ComponentsManager::UpdateDraw2D()
  120. {
  121. for (auto& component : components)
  122. {
  123. component->UpdateDraw2D();
  124. }
  125. }
  126. void ComponentsManager::OnFocusGain()
  127. {
  128. for (auto& component : components)
  129. component->OnFocusGain();
  130. }
  131. void ComponentsManager::OnFocusLost()
  132. {
  133. for (auto& component : components)
  134. component->OnFocusLost();
  135. }
  136. void ComponentsManager::Dispose()
  137. {
  138. for (auto& component : components)
  139. delete component;
  140. }
  141. }