BsCoreApplication.h 4.6 KB

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