BsCoreApplication.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /** @addtogroup Application-Core
  10. * @{
  11. */
  12. /** Structure containing parameters for starting the application. */
  13. struct START_UP_DESC
  14. {
  15. String renderAPI; /**< Name of the render system plugin to use. */
  16. String renderer; /**< Name of the renderer plugin to use. */
  17. String input; /**< Name of the input plugin to use. */
  18. RENDER_WINDOW_DESC primaryWindowDesc; /**< Describes the window to create during start-up. */
  19. Vector<String> importers; /**< A list of importer plugins to load. */
  20. };
  21. /**
  22. * Represents the primary entry point for the core systems. Handles start-up, shutdown, primary loop and allows you to
  23. * load and unload 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. * Executes the main loop. This will update your components and modules, queue objects for rendering and run
  34. * 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. /** Stops a (infinite) main loop from running. The loop will complete its current cycle before stopping. */
  40. void stopMainLoop();
  41. /**
  42. * Issues a request for the application to close. Application may choose to ignore the request depending on the
  43. * circumstances and the implementation.
  44. */
  45. virtual void quitRequested();
  46. /** Returns the main window that was created on application start-up. */
  47. RenderWindowPtr getPrimaryWindow() const { return mPrimaryWindow; }
  48. /**
  49. * Returns the id of the simulation thread.
  50. *
  51. * @note Thread safe.
  52. */
  53. BS_THREAD_ID_TYPE getSimThreadId() { return mSimThreadId; }
  54. /**
  55. * Loads a plugin.
  56. *
  57. * @param[in] pluginName Name of the plugin to load, without extension.
  58. * @param[out] library Specify as not null to receive a reference to the loaded library.
  59. * @param[in] passThrough Optional parameter that will be passed to the loadPlugin function.
  60. * @return Value returned from the plugin start-up method.
  61. */
  62. void* loadPlugin(const String& pluginName, DynLib** library = nullptr, void* passThrough = nullptr);
  63. /** Unloads a previously loaded plugin. */
  64. void unloadPlugin(DynLib* library);
  65. protected:
  66. /** @copydoc Module::onStartUp */
  67. virtual void onStartUp() override;
  68. /** Called for each iteration of the main loop. Called before any game objects or plugins are updated. */
  69. virtual void preUpdate();
  70. /** Called for each iteration of the main loop. Called after all game objects and plugins are updated. */
  71. virtual void postUpdate();
  72. /** Initializes the renderer specified during construction. Called during initialization. */
  73. virtual void startUpRenderer();
  74. /** Returns a handler that is used for resolving shader include file paths. */
  75. virtual ShaderIncludeHandlerPtr getShaderIncludeHandler() const;
  76. private:
  77. /** Called when the frame finishes rendering. */
  78. void frameRenderingFinishedCallback();
  79. /** Called by the core thread to begin profiling. */
  80. void beginCoreProfiling();
  81. /** Called by the core thread to end profiling. */
  82. void endCoreProfiling();
  83. private:
  84. typedef void(*UpdatePluginFunc)();
  85. RenderWindowPtr mPrimaryWindow;
  86. START_UP_DESC mStartUpDesc;
  87. DynLib* mRendererPlugin;
  88. Map<DynLib*, UpdatePluginFunc> mPluginUpdateFunctions;
  89. bool mIsFrameRenderingFinished;
  90. BS_MUTEX(mFrameRenderingFinishedMutex);
  91. BS_THREAD_SYNCHRONISER(mFrameRenderingFinishedCondition);
  92. BS_THREAD_ID_TYPE mSimThreadId;
  93. volatile bool mRunMainLoop;
  94. };
  95. /** Provides easy access to CoreApplication. */
  96. BS_CORE_EXPORT CoreApplication& gCoreApplication();
  97. /** @} */
  98. }