BsCoreApplication.h 4.7 KB

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