BsCoreApplication.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsModule.h"
  4. #include "BsCoreThreadAccessor.h"
  5. #include "BsRenderWindow.h"
  6. #include "BsEvent.h"
  7. namespace BansheeEngine
  8. {
  9. /**
  10. * @brief Structure containing parameters for starting the application.
  11. */
  12. struct START_UP_DESC
  13. {
  14. String renderAPI; /**< Name of the render system plugin to use. */
  15. String renderer; /**< Name of the renderer plugin to use. */
  16. String input; /**< Name of the input plugin to use. */
  17. RENDER_WINDOW_DESC primaryWindowDesc; /**< Describes the window to create during start-up. */
  18. Vector<String> importers; /**< A list of importer plugins to load. */
  19. };
  20. /**
  21. * @brief Represents the primary entry point for the core systems. Handles
  22. * start-up, shutdown, primary loop and allows you to load and unload
  23. * plugins.
  24. *
  25. * @note Sim thread only.
  26. */
  27. class BS_CORE_EXPORT CoreApplication : public Module<CoreApplication>
  28. {
  29. public:
  30. CoreApplication(START_UP_DESC& desc);
  31. virtual ~CoreApplication();
  32. /**
  33. * @brief Executes the main loop. This will update your components and modules, queue objects
  34. * for rendering and run the simulation. Usually called immediately after startUp().
  35. *
  36. * This will run infinitely until stopMainLoop is called (usually from another thread or internally).
  37. */
  38. void runMainLoop();
  39. /**
  40. * @brief Stops a (infinite) main loop from running. The loop will complete its current cycle before stopping.
  41. */
  42. void stopMainLoop();
  43. /**
  44. * @brief Issues a request for the application to close. Application may choose to ignore the request
  45. * depending on the circumstances and the implementation.
  46. */
  47. virtual void quitRequested();
  48. /**
  49. * @brief Returns the main window that was created on application start-up.
  50. */
  51. RenderWindowPtr getPrimaryWindow() const { return mPrimaryWindow; }
  52. /**
  53. * @brief Returns the id of the simulation thread.
  54. *
  55. * @note Thread safe.
  56. */
  57. BS_THREAD_ID_TYPE getSimThreadId() { return mSimThreadId; }
  58. /**
  59. * @brief Loads a plugin.
  60. *
  61. * @param pluginName Name of the plugin to load, without extension.
  62. * @param [out] library Specify as not null to receive a reference to
  63. * the loaded library.
  64. * @param passThrough Optional parameter that will be passed to the loadPlugin function.
  65. *
  66. * @returns Value returned from the plugin start-up method.
  67. */
  68. void* loadPlugin(const String& pluginName, DynLib** library = nullptr, void* passThrough = nullptr);
  69. /**
  70. * @brief Unloads a previously loaded plugin.
  71. */
  72. void unloadPlugin(DynLib* library);
  73. protected:
  74. /**
  75. * @copydoc Module::onStartUp
  76. */
  77. virtual void onStartUp() override;
  78. /**
  79. * @brief Called for each iteration of the main loop. Called before any game objects or plugins are updated.
  80. */
  81. virtual void preUpdate();
  82. /**
  83. * @brief Called for each iteration of the main loop. Called after all game objects and plugins are updated.
  84. */
  85. virtual void postUpdate();
  86. /**
  87. * @brief Initializes the renderer specified during construction. Called during initialization.
  88. */
  89. virtual void startUpRenderer();
  90. /**
  91. * @brief Returns a handler that is used for resolving shader include file paths.
  92. */
  93. virtual ShaderIncludeHandlerPtr getShaderIncludeHandler() const;
  94. private:
  95. /**
  96. * @brief Called when the frame finishes rendering.
  97. */
  98. void frameRenderingFinishedCallback();
  99. /**
  100. * @brief Called by the core thread to begin profiling.
  101. */
  102. void beginCoreProfiling();
  103. /**
  104. * @brief Called by the core thread to end profiling.
  105. */
  106. void endCoreProfiling();
  107. private:
  108. typedef void(*UpdatePluginFunc)();
  109. RenderWindowPtr mPrimaryWindow;
  110. START_UP_DESC mStartUpDesc;
  111. DynLib* mRendererPlugin;
  112. Map<DynLib*, UpdatePluginFunc> mPluginUpdateFunctions;
  113. bool mIsFrameRenderingFinished;
  114. BS_MUTEX(mFrameRenderingFinishedMutex);
  115. BS_THREAD_SYNCHRONISER(mFrameRenderingFinishedCondition);
  116. BS_THREAD_ID_TYPE mSimThreadId;
  117. volatile bool mRunMainLoop;
  118. };
  119. /**
  120. * @brief Provides easy access to primary entry point for the engine.
  121. */
  122. BS_CORE_EXPORT CoreApplication& gCoreApplication();
  123. }