BsCoreApplication.h 4.6 KB

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