BsApplication.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsApplication.h"
  4. #include "BsGUIManager.h"
  5. #include "BsBuiltinResources.h"
  6. #include "BsScriptManager.h"
  7. #include "BsProfilingManager.h"
  8. #include "BsVirtualInput.h"
  9. #include "BsSceneManager.h"
  10. #include "BsSceneObject.h"
  11. #include "BsCursor.h"
  12. #include "BsCoreThread.h"
  13. #include "BsFileSystem.h"
  14. #include "BsPlainTextImporter.h"
  15. #include "BsImporter.h"
  16. #include "BsShortcutManager.h"
  17. #include "BsCoreObjectManager.h"
  18. #include "BsRendererManager.h"
  19. #include "BsRendererMaterialManager.h"
  20. #include "BsPlatform.h"
  21. #include "BsEngineShaderIncludeHandler.h"
  22. namespace BansheeEngine
  23. {
  24. START_UP_DESC createStartUpDesc(RENDER_WINDOW_DESC& primaryWindowDesc, const String& renderAPI, const String& renderer,
  25. const Vector<String>& importers)
  26. {
  27. START_UP_DESC desc;
  28. desc.renderAPI = renderAPI;
  29. desc.renderer = renderer;
  30. desc.physics = "BansheePhysX";
  31. desc.input = "BansheeOISInput";
  32. desc.primaryWindowDesc = primaryWindowDesc;
  33. desc.importers = importers;
  34. return desc;
  35. }
  36. Application::Application(RENDER_WINDOW_DESC primaryWindowDesc, RenderAPIPlugin renderAPI, RendererPlugin renderer,
  37. const Vector<String>& importers)
  38. :CoreApplication(createStartUpDesc(primaryWindowDesc, getLibNameForRenderAPI(renderAPI),
  39. getLibNameForRenderer(renderer), importers)), mMonoPlugin(nullptr), mSBansheeEnginePlugin(nullptr)
  40. {
  41. }
  42. Application::~Application()
  43. {
  44. // Cleanup any new objects queued for destruction by unloaded scripts
  45. CoreObjectManager::instance().clearDirty();
  46. gCoreThread().update();
  47. gCoreThread().submitAccessors(true);
  48. Cursor::shutDown();
  49. ShortcutManager::shutDown();
  50. GUIManager::shutDown();
  51. BuiltinResources::shutDown();
  52. RendererMaterialManager::shutDown();
  53. VirtualInput::shutDown();
  54. }
  55. void Application::onStartUp()
  56. {
  57. CoreApplication::onStartUp();
  58. PlainTextImporter* importer = bs_new<PlainTextImporter>();
  59. Importer::instance()._registerAssetImporter(importer);
  60. VirtualInput::startUp();
  61. BuiltinResources::startUp();
  62. RendererMaterialManager::startUp();
  63. RendererManager::instance().initialize();
  64. GUIManager::startUp();
  65. ShortcutManager::startUp();
  66. Cursor::startUp();
  67. Cursor::instance().setCursor(CursorType::Arrow);
  68. Platform::setIcon(BuiltinResources::instance().getBansheeIcon());
  69. SceneManager::instance().setMainRenderTarget(getPrimaryWindow());
  70. ScriptManager::startUp();
  71. loadScriptSystem();
  72. }
  73. void Application::onShutDown()
  74. {
  75. // Need to clear all objects before I unload any plugins, as they
  76. // could have allocated parts or all of those objects.
  77. SceneManager::instance().clearScene(true);
  78. ScriptManager::shutDown();
  79. unloadScriptSystem();
  80. CoreApplication::onShutDown();
  81. }
  82. void Application::startUp(RENDER_WINDOW_DESC& primaryWindowDesc, RenderAPIPlugin renderAPI,
  83. RendererPlugin renderer, const Vector<String>& importers)
  84. {
  85. CoreApplication::startUp<Application>(primaryWindowDesc, renderAPI, renderer, importers);
  86. }
  87. void Application::preUpdate()
  88. {
  89. CoreApplication::preUpdate();
  90. VirtualInput::instance()._update();
  91. }
  92. void Application::postUpdate()
  93. {
  94. CoreApplication::postUpdate();
  95. PROFILE_CALL(GUIManager::instance().update(), "GUI");
  96. }
  97. void Application::loadScriptSystem()
  98. {
  99. loadPlugin("BansheeMono", &mMonoPlugin);
  100. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  101. ScriptManager::instance().initialize();
  102. }
  103. void Application::unloadScriptSystem()
  104. {
  105. // These plugins must be unloaded before any other script plugins, because
  106. // they will cause finalizers to trigger and various modules those finalizers
  107. // might reference must still be active
  108. unloadPlugin(mSBansheeEnginePlugin);
  109. unloadPlugin(mMonoPlugin);
  110. }
  111. void Application::startUpRenderer()
  112. {
  113. // Do nothing, we activate the renderer at a later stage
  114. }
  115. Path Application::getEngineAssemblyPath() const
  116. {
  117. Path assemblyPath = getBuiltinAssemblyFolder();
  118. assemblyPath.append(toWString(String(ENGINE_ASSEMBLY)) + L".dll");
  119. return assemblyPath;
  120. }
  121. Path Application::getGameAssemblyPath() const
  122. {
  123. Path assemblyPath = getScriptAssemblyFolder();
  124. assemblyPath.append(toWString(String(SCRIPT_GAME_ASSEMBLY)) + L".dll");
  125. return assemblyPath;
  126. }
  127. Path Application::getBuiltinAssemblyFolder() const
  128. {
  129. Path releaseAssemblyFolder = FileSystem::getWorkingDirectoryPath();
  130. releaseAssemblyFolder.append(Paths::getReleaseAssemblyPath());
  131. Path debugAssemblyFolder = FileSystem::getWorkingDirectoryPath();
  132. debugAssemblyFolder.append(Paths::getDebugAssemblyPath());
  133. #if BS_DEBUG_MODE == 0
  134. if (FileSystem::exists(releaseAssemblyFolder))
  135. return releaseAssemblyFolder;
  136. return debugAssemblyFolder;
  137. #else
  138. if (FileSystem::exists(debugAssemblyFolder))
  139. return debugAssemblyFolder;
  140. return releaseAssemblyFolder;
  141. #endif
  142. }
  143. Path Application::getScriptAssemblyFolder() const
  144. {
  145. return getBuiltinAssemblyFolder();
  146. }
  147. ShaderIncludeHandlerPtr Application::getShaderIncludeHandler() const
  148. {
  149. return bs_shared_ptr_new<EngineShaderIncludeHandler>();
  150. }
  151. String Application::getLibNameForRenderAPI(RenderAPIPlugin plugin)
  152. {
  153. static String DX11Name = "BansheeD3D11RenderAPI";
  154. static String DX9Name = "BansheeD3D9RenderAPI";
  155. static String OpenGLName = "BansheeGLRenderAPI";
  156. switch (plugin)
  157. {
  158. case RenderAPIPlugin::DX11:
  159. return DX11Name;
  160. case RenderAPIPlugin::DX9:
  161. return DX9Name;
  162. case RenderAPIPlugin::OpenGL:
  163. return OpenGLName;
  164. }
  165. return StringUtil::BLANK;
  166. }
  167. String Application::getLibNameForRenderer(RendererPlugin plugin)
  168. {
  169. static String DefaultName = "RenderBeast";
  170. switch (plugin)
  171. {
  172. case RendererPlugin::Default:
  173. return DefaultName;
  174. }
  175. return StringUtil::BLANK;
  176. }
  177. Application& gApplication()
  178. {
  179. return static_cast<Application&>(Application::instance());
  180. }
  181. }