1//************************************ bs::framework - Copyright 2018 Marko Pintera **************************************//
2//*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********//
3#include "BsApplication.h"
4#include "GUI/BsGUIManager.h"
5#include "2D/BsSpriteManager.h"
6#include "Resources/BsBuiltinResources.h"
7#include "Script/BsScriptManager.h"
8#include "Profiling/BsProfilingManager.h"
9#include "Input/BsVirtualInput.h"
10#include "Scene/BsSceneManager.h"
11#include "Scene/BsSceneObject.h"
12#include "Platform/BsCursor.h"
13#include "CoreThread/BsCoreThread.h"
14#include "FileSystem/BsFileSystem.h"
15#include "Resources/BsPlainTextImporter.h"
16#include "Importer/BsImporter.h"
17#include "GUI/BsShortcutManager.h"
18#include "CoreThread/BsCoreObjectManager.h"
19#include "Renderer/BsRendererManager.h"
20#include "Renderer/BsRendererMaterialManager.h"
21#include "Debug/BsDebugDraw.h"
22#include "Platform/BsPlatform.h"
23#include "Resources/BsEngineShaderIncludeHandler.h"
24#include "Resources/BsResources.h"
25#include "BsEngineConfig.h"
26#include "GUI/BsProfilerOverlay.h"
27
28namespace bs
29{
30 Application::Application(const START_UP_DESC& desc)
31 : CoreApplication(desc)
32 { }
33
34 Application::~Application()
35 {
36 // Cleanup any new objects queued for destruction by unloaded scripts
37 CoreObjectManager::instance().syncToCore();
38 gCoreThread().update();
39 gCoreThread().submitAll(true);
40
41 Cursor::shutDown();
42
43 GUIManager::shutDown();
44 SpriteManager::shutDown();
45 BuiltinResources::shutDown();
46 RendererMaterialManager::shutDown();
47 VirtualInput::shutDown();
48 }
49
50 void Application::onStartUp()
51 {
52 CoreApplication::onStartUp();
53
54 PlainTextImporter* importer = bs_new<PlainTextImporter>();
55 Importer::instance()._registerAssetImporter(importer);
56
57 VirtualInput::startUp();
58 BuiltinResources::startUp();
59 RendererMaterialManager::startUp();
60 RendererManager::instance().initialize();
61 SpriteManager::startUp();
62 GUIManager::startUp();
63 ShortcutManager::startUp();
64
65 Cursor::startUp();
66 Cursor::instance().setCursor(CursorType::Arrow);
67 Platform::setIcon(BuiltinResources::instance().getFrameworkIcon());
68
69 SceneManager::instance().setMainRenderTarget(getPrimaryWindow());
70 DebugDraw::startUp();
71
72 startUpScriptManager();
73 }
74
75 void Application::onShutDown()
76 {
77 // Need to clear all objects before I unload any plugins, as they
78 // could have allocated parts or all of those objects.
79 SceneManager::instance().clearScene(true);
80
81 // Resources too (Prefabs especially, since they hold the same data as a scene)
82 Resources::instance().unloadAll();
83
84 // Shut down before script manager as scripts could have registered shortcut callbacks
85 ShortcutManager::shutDown();
86
87 ScriptManager::shutDown();
88 DebugDraw::shutDown();
89
90 CoreApplication::onShutDown();
91 }
92
93 void Application::preUpdate()
94 {
95 CoreApplication::preUpdate();
96
97 VirtualInput::instance()._update();
98
99 if(mProfilerOverlay)
100 mProfilerOverlay->update();
101 }
102
103 void Application::postUpdate()
104 {
105 CoreApplication::postUpdate();
106 updateScriptManager();
107
108 PROFILE_CALL(GUIManager::instance().update(), "GUI");
109 DebugDraw::instance()._update();
110 }
111
112 void Application::showProfilerOverlay(ProfilerOverlayType type, const SPtr<Camera>& camera)
113 {
114 const SPtr<Camera>& overlayCamera = camera ? camera : gSceneManager().getMainCamera();
115 if(!overlayCamera)
116 return;
117
118 if(!mProfilerOverlay)
119 mProfilerOverlay = bs_shared_ptr_new<ProfilerOverlay>(overlayCamera);
120 else
121 mProfilerOverlay->setTarget(overlayCamera);
122
123 mProfilerOverlay->show(type);
124 }
125
126 void Application::hideProfilerOverlay()
127 {
128 if(mProfilerOverlay)
129 mProfilerOverlay->hide();
130
131 mProfilerOverlay = nullptr;
132 }
133
134 void Application::startUpRenderer()
135 {
136 // Do nothing, we activate the renderer at a later stage
137 }
138
139 void Application::startUpScriptManager()
140 {
141 ScriptManager::startUp();
142 }
143
144 void Application::updateScriptManager()
145 {
146 ScriptManager::instance().update();
147 }
148
149 START_UP_DESC Application::buildStartUpDesc(VideoMode videoMode, const String& title, bool fullscreen)
150 {
151 START_UP_DESC desc;
152
153 // Set up default plugins
154 desc.renderAPI = BS_RENDER_API_MODULE;
155 desc.renderer = BS_RENDERER_MODULE;
156 desc.audio = BS_AUDIO_MODULE;
157 desc.physics = BS_PHYSICS_MODULE;
158
159 desc.importers.push_back("bsfFreeImgImporter");
160 desc.importers.push_back("bsfFBXImporter");
161 desc.importers.push_back("bsfFontImporter");
162 desc.importers.push_back("bsfSL");
163
164 desc.primaryWindowDesc.videoMode = videoMode;
165 desc.primaryWindowDesc.fullscreen = fullscreen;
166 desc.primaryWindowDesc.title = title;
167
168 return desc;
169 }
170
171 SPtr<IShaderIncludeHandler> Application::getShaderIncludeHandler() const
172 {
173 return bs_shared_ptr_new<EngineShaderIncludeHandler>();
174 }
175
176 Application& gApplication()
177 {
178 return static_cast<Application&>(Application::instance());
179 }
180}
181