BsCoreApplication.h 4.7 KB

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