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