BsApplication.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include "BsApplication.h"
  2. #include "BsGUIMaterialManager.h"
  3. #include "BsGUIManager.h"
  4. #include "BsOverlayManager.h"
  5. #include "BsShapeMeshes2D.h"
  6. #include "BsShapeMeshes3D.h"
  7. #include "BsBuiltinResources.h"
  8. #include "BsScriptManager.h"
  9. #include "BsProfilingManager.h"
  10. #include "BsVirtualInput.h"
  11. #include "BsSceneManager.h"
  12. #include "BsSceneObject.h"
  13. #include "BsCursor.h"
  14. #include "BsCoreThread.h"
  15. #include "BsFileSystem.h"
  16. #include "BsPlainTextImporter.h"
  17. #include "BsImporter.h"
  18. #include "BsShortcutManager.h"
  19. namespace BansheeEngine
  20. {
  21. START_UP_DESC createStartUpDesc(RENDER_WINDOW_DESC& primaryWindowDesc, const String& renderAPI, const String& renderer)
  22. {
  23. START_UP_DESC desc;
  24. desc.renderAPI = renderAPI;
  25. desc.renderer = renderer;
  26. desc.primaryWindowDesc = primaryWindowDesc;
  27. desc.input = "BansheeOISInput";
  28. desc.importers.push_back("BansheeFreeImgImporter");
  29. desc.importers.push_back("BansheeFBXImporter");
  30. desc.importers.push_back("BansheeFontImporter");
  31. desc.importers.push_back("BansheeSL");
  32. return desc;
  33. }
  34. Application::Application(RENDER_WINDOW_DESC& primaryWindowDesc, RenderAPIPlugin renderAPI, RendererPlugin renderer)
  35. :CoreApplication(createStartUpDesc(primaryWindowDesc, getLibNameForRenderAPI(renderAPI), getLibNameForRenderer(renderer))),
  36. mMonoPlugin(nullptr), mSBansheeEnginePlugin(nullptr)
  37. {
  38. }
  39. Application::~Application()
  40. {
  41. // Cleanup any new objects queued for destruction by unloaded scripts
  42. gCoreThread().update();
  43. gCoreThread().submitAccessors(true);
  44. Cursor::shutDown();
  45. GUIMaterialManager::instance().clearMaterials();
  46. ShortcutManager::shutDown();
  47. OverlayManager::shutDown();
  48. GUIManager::shutDown();
  49. GUIMaterialManager::shutDown();
  50. BuiltinResources::shutDown();
  51. VirtualInput::shutDown();
  52. }
  53. void Application::onStartUp()
  54. {
  55. CoreApplication::onStartUp();
  56. PlainTextImporter* importer = bs_new<PlainTextImporter>();
  57. Importer::instance()._registerAssetImporter(importer);
  58. VirtualInput::startUp();
  59. BuiltinResources::startUp();
  60. GUIManager::startUp();
  61. GUIMaterialManager::startUp();
  62. OverlayManager::startUp();
  63. ShortcutManager::startUp();
  64. Cursor::startUp();
  65. Cursor::instance().setCursor(CursorType::Arrow);
  66. ScriptManager::startUp();
  67. loadScriptSystem();
  68. }
  69. void Application::onShutDown()
  70. {
  71. // Need to clear all objects before I unload any plugins, as they
  72. // could have allocated parts or all of those objects.
  73. SceneManager::instance().clearScene(true);
  74. ScriptManager::shutDown();
  75. unloadScriptSystem();
  76. CoreApplication::onShutDown();
  77. }
  78. void Application::startUp(RENDER_WINDOW_DESC& primaryWindowDesc, RenderAPIPlugin renderAPI, RendererPlugin renderer)
  79. {
  80. CoreApplication::startUp<Application>(primaryWindowDesc, renderAPI, renderer);
  81. }
  82. void Application::preUpdate()
  83. {
  84. CoreApplication::preUpdate();
  85. VirtualInput::instance()._update();
  86. }
  87. void Application::postUpdate()
  88. {
  89. CoreApplication::postUpdate();
  90. PROFILE_CALL(GUIManager::instance().update(), "GUI");
  91. }
  92. void Application::loadScriptSystem()
  93. {
  94. loadPlugin("BansheeMono", &mMonoPlugin);
  95. loadPlugin("SBansheeEngine", &mSBansheeEnginePlugin);
  96. ScriptManager::instance().initialize();
  97. }
  98. void Application::unloadScriptSystem()
  99. {
  100. // These plugins must be unloaded before any other script plugins, because
  101. // they will cause finalizers to trigger and various modules those finalizers
  102. // might reference must still be active
  103. unloadPlugin(mSBansheeEnginePlugin);
  104. unloadPlugin(mMonoPlugin);
  105. }
  106. ViewportPtr Application::getPrimaryViewport() const
  107. {
  108. // TODO - Need a way to determine primary viewport!
  109. return nullptr;
  110. }
  111. Path Application::getEngineAssemblyPath() const
  112. {
  113. Path assemblyPath = getBuiltinAssemblyFolder();
  114. assemblyPath.append(toWString(String(ENGINE_ASSEMBLY)) + L".dll");
  115. return assemblyPath;
  116. }
  117. Path Application::getGameAssemblyPath() const
  118. {
  119. Path assemblyPath = getScriptAssemblyFolder();
  120. assemblyPath.append(toWString(String(SCRIPT_GAME_ASSEMBLY)) + L".dll");
  121. return assemblyPath;
  122. }
  123. Path Application::getBuiltinAssemblyFolder() const
  124. {
  125. Path assemblyFolder = FileSystem::getWorkingDirectoryPath();
  126. assemblyFolder.append(ASSEMBLY_PATH);
  127. return assemblyFolder;
  128. }
  129. Path Application::getScriptAssemblyFolder() const
  130. {
  131. Path assemblyFolder = FileSystem::getWorkingDirectoryPath();
  132. assemblyFolder.append(ASSEMBLY_PATH);
  133. return assemblyFolder;
  134. }
  135. const String& Application::getLibNameForRenderAPI(RenderAPIPlugin plugin)
  136. {
  137. static String DX11Name = "BansheeD3D11RenderAPI";
  138. static String DX9Name = "BansheeD3D9RenderAPI";
  139. static String OpenGLName = "BansheeGLRenderAPI";
  140. switch (plugin)
  141. {
  142. case RenderAPIPlugin::DX11:
  143. return DX11Name;
  144. case RenderAPIPlugin::DX9:
  145. return DX9Name;
  146. case RenderAPIPlugin::OpenGL:
  147. return OpenGLName;
  148. }
  149. return StringUtil::BLANK;
  150. }
  151. const String& Application::getLibNameForRenderer(RendererPlugin plugin)
  152. {
  153. static String DefaultName = "RenderBeast";
  154. switch (plugin)
  155. {
  156. case RendererPlugin::Default:
  157. return DefaultName;
  158. }
  159. return StringUtil::BLANK;
  160. }
  161. Application& gApplication()
  162. {
  163. return static_cast<Application&>(Application::instance());
  164. }
  165. }