App.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. #pragma once
  6. #include <AnKi/Core/Common.h>
  7. #include <AnKi/Util/Allocator.h>
  8. #include <AnKi/Util/String.h>
  9. #include <AnKi/Util/Ptr.h>
  10. #include <AnKi/Ui/UiImmediateModeBuilder.h>
  11. namespace anki {
  12. // Forward
  13. class CoreTracer;
  14. class ConfigSet;
  15. class ThreadHive;
  16. class NativeWindow;
  17. class Input;
  18. class GrManager;
  19. class MainRenderer;
  20. class PhysicsWorld;
  21. class SceneGraph;
  22. class ScriptManager;
  23. class ResourceManager;
  24. class ResourceFilesystem;
  25. class StagingGpuMemoryManager;
  26. class UiManager;
  27. class UiQueueElement;
  28. class RenderQueue;
  29. /// The core class of the engine.
  30. class App
  31. {
  32. public:
  33. App();
  34. virtual ~App();
  35. ANKI_USE_RESULT Error init(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  36. Second getTimerTick() const
  37. {
  38. return m_timerTick;
  39. }
  40. void setTimerTick(const F32 x)
  41. {
  42. m_timerTick = x;
  43. }
  44. const String& getSettingsDirectory() const
  45. {
  46. return m_settingsDir;
  47. }
  48. const String& getCacheDirectory() const
  49. {
  50. return m_cacheDir;
  51. }
  52. AllocAlignedCallback getAllocationCallback() const
  53. {
  54. return m_allocCb;
  55. }
  56. void* getAllocationCallbackData() const
  57. {
  58. return m_allocCbData;
  59. }
  60. ThreadHive& getThreadHive()
  61. {
  62. return *m_threadHive;
  63. }
  64. HeapAllocator<U8>& getAllocator()
  65. {
  66. return m_heapAlloc;
  67. }
  68. Timestamp getGlobalTimestamp() const
  69. {
  70. return m_globalTimestamp;
  71. }
  72. /// Run the main loop.
  73. ANKI_USE_RESULT Error mainLoop();
  74. /// The user code to run along with the other main loop code.
  75. virtual ANKI_USE_RESULT Error userMainLoop(Bool& quit, Second elapsedTime)
  76. {
  77. // Do nothing
  78. return Error::NONE;
  79. }
  80. Input& getInput()
  81. {
  82. return *m_input;
  83. }
  84. SceneGraph& getSceneGraph()
  85. {
  86. return *m_scene;
  87. }
  88. MainRenderer& getMainRenderer()
  89. {
  90. return *m_renderer;
  91. }
  92. ResourceManager& getResourceManager()
  93. {
  94. return *m_resources;
  95. }
  96. ScriptManager& getScriptManager()
  97. {
  98. return *m_script;
  99. }
  100. PhysicsWorld& getPhysicsWorld()
  101. {
  102. return *m_physics;
  103. }
  104. NativeWindow& getWindow()
  105. {
  106. return *m_window;
  107. }
  108. HeapAllocator<U8> getAllocator() const
  109. {
  110. return m_heapAlloc;
  111. }
  112. void setDisplayStats(Bool enable)
  113. {
  114. m_displayStats = enable;
  115. }
  116. Bool getDisplayStats() const
  117. {
  118. return m_displayStats;
  119. }
  120. void setDisplayDeveloperConsole(Bool display)
  121. {
  122. m_consoleEnabled = display;
  123. }
  124. Bool getDisplayDeveloperConsole() const
  125. {
  126. return m_consoleEnabled;
  127. }
  128. private:
  129. class StatsUi;
  130. // Allocation
  131. AllocAlignedCallback m_allocCb;
  132. void* m_allocCbData;
  133. HeapAllocator<U8> m_heapAlloc;
  134. // Sybsystems
  135. #if ANKI_ENABLE_TRACE
  136. CoreTracer* m_coreTracer = nullptr;
  137. #endif
  138. NativeWindow* m_window = nullptr;
  139. Input* m_input = nullptr;
  140. GrManager* m_gr = nullptr;
  141. StagingGpuMemoryManager* m_stagingMem = nullptr;
  142. PhysicsWorld* m_physics = nullptr;
  143. ResourceFilesystem* m_resourceFs = nullptr;
  144. ResourceManager* m_resources = nullptr;
  145. UiManager* m_ui = nullptr;
  146. MainRenderer* m_renderer = nullptr;
  147. SceneGraph* m_scene = nullptr;
  148. ScriptManager* m_script = nullptr;
  149. // Misc
  150. StatsUi* m_statsUi = nullptr;
  151. Bool m_displayStats = false;
  152. UiImmediateModeBuilderPtr m_console;
  153. Bool m_consoleEnabled = false;
  154. Timestamp m_globalTimestamp = 1;
  155. ThreadHive* m_threadHive = nullptr;
  156. String m_settingsDir; ///< The path that holds the configuration
  157. String m_cacheDir; ///< This is used as a cache
  158. Second m_timerTick;
  159. U64 m_resourceCompletedAsyncTaskCount = 0;
  160. class MemStats
  161. {
  162. public:
  163. Atomic<PtrSize> m_allocatedMem = {0};
  164. Atomic<U64> m_allocCount = {0};
  165. Atomic<U64> m_freeCount = {0};
  166. void* m_originalUserData = nullptr;
  167. AllocAlignedCallback m_originalAllocCallback = nullptr;
  168. static void* allocCallback(void* userData, void* ptr, PtrSize size, PtrSize alignment);
  169. } m_memStats;
  170. void initMemoryCallbacks(AllocAlignedCallback allocCb, void* allocCbUserData);
  171. ANKI_USE_RESULT Error initInternal(const ConfigSet& config, AllocAlignedCallback allocCb, void* allocCbUserData);
  172. ANKI_USE_RESULT Error initDirs(const ConfigSet& cfg);
  173. void cleanup();
  174. /// Inject a new UI element in the render queue for displaying various stuff.
  175. void injectUiElements(DynamicArrayAuto<UiQueueElement>& elements, RenderQueue& rqueue);
  176. void setSignalHandlers();
  177. };
  178. } // end namespace anki