BsApplication.cpp 5.4 KB

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