BsEditorApplication.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "BsEditorApplication.h"
  2. #include "BsEditorWindowManager.h"
  3. #include "BsEditorWidgetManager.h"
  4. #include "BsMainEditorWindow.h"
  5. #include "BsApplication.h"
  6. #include "CmApplication.h"
  7. #include "CmRenderWindow.h"
  8. #include "BsEditorGUI.h"
  9. #include "BsUndoRedo.h"
  10. // DEBUG ONLY
  11. #include "DbgEditorWidget1.h"
  12. #include "DbgEditorWidget2.h"
  13. #include "CmResources.h"
  14. #include "CmSceneObject.h"
  15. #include "CmImporter.h"
  16. #include "CmGpuProgram.h"
  17. #include "CmGpuProgramImportOptions.h"
  18. #include "CmShader.h"
  19. #include "CmTexture.h"
  20. #include "CmMaterial.h"
  21. #include "CmTechnique.h"
  22. #include "CmPass.h"
  23. #include "BsRenderable.h"
  24. #include "BsDbgTestGameObjectRef.h"
  25. #include "BsVirtualInput.h"
  26. #include "CmWin32FolderMonitor.h"
  27. #include "BsProjectLibrary.h"
  28. using namespace CamelotFramework;
  29. using namespace BansheeEngine;
  30. namespace BansheeEditor
  31. {
  32. EditorApplication::EditorApplication(RenderSystemPlugin renderSystemPlugin)
  33. :mActiveRSPlugin(renderSystemPlugin)
  34. {
  35. RENDER_WINDOW_DESC renderWindowDesc;
  36. renderWindowDesc.width = 1280;
  37. renderWindowDesc.height = 720;
  38. renderWindowDesc.title = "BansheeEditor";
  39. renderWindowDesc.fullscreen = false;
  40. renderWindowDesc.border = WindowBorder::None;
  41. const String& renderSystemLibraryName = getLibraryNameForRenderSystem(renderSystemPlugin);
  42. gBansheeApp().startUp(renderWindowDesc, renderSystemLibraryName, "BansheeForwardRenderer"); // TODO - Make renderer and resource cache dir customizable
  43. EditorGUI::startUp(cm_new<EditorGUI>());
  44. {
  45. auto inputConfig = VirtualInput::instance().getConfiguration();
  46. inputConfig->registerButton("Rename", BC_F2);
  47. inputConfig->registerButton("Undo", BC_Z, VButtonModifier::Ctrl);
  48. inputConfig->registerButton("Redo", BC_Y, VButtonModifier::Ctrl);
  49. inputConfig->registerButton("Copy", BC_C, VButtonModifier::Ctrl);
  50. inputConfig->registerButton("Cut", BC_X, VButtonModifier::Ctrl);
  51. inputConfig->registerButton("Paste", BC_V, VButtonModifier::Ctrl);
  52. inputConfig->registerButton("Delete", BC_DELETE);
  53. }
  54. ProjectLibrary::startUp(cm_new<ProjectLibrary>(getActiveProjectPath()));
  55. //gApplication().loadPlugin("SBansheeEditor"); // Managed part of the editor
  56. /************************************************************************/
  57. /* DEBUG CODE */
  58. /************************************************************************/
  59. RenderSystem* renderSystem = RenderSystem::instancePtr();
  60. RenderWindowPtr renderWindow = gApplication().getPrimaryWindow();
  61. HSceneObject testModelGO = SceneObject::create("TestMesh");
  62. HRenderable testRenderable = testModelGO->addComponent<Renderable>();
  63. WString psLoc;
  64. WString vsLoc;
  65. GpuProgramProfile psProfile;
  66. GpuProgramProfile vsProfile;
  67. String psEntry;
  68. String vsEntry;
  69. String language;
  70. switch (renderSystemPlugin)
  71. {
  72. case RenderSystemPlugin::DX11:
  73. {
  74. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_ps.gpuprog";
  75. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl11_vs.gpuprog";
  76. language = "hlsl";
  77. psProfile = GPP_PS_4_0;
  78. vsProfile = GPP_VS_4_0;
  79. psEntry = "ps_main";
  80. vsEntry = "vs_main";
  81. break;
  82. }
  83. case RenderSystemPlugin::DX9:
  84. {
  85. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_ps.gpuprog";
  86. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\hlsl9_vs.gpuprog";
  87. language = "hlsl";
  88. psProfile = GPP_PS_2_0;
  89. vsProfile = GPP_VS_2_0;
  90. psEntry = "ps_main";
  91. vsEntry = "vs_main";
  92. break;
  93. }
  94. case RenderSystemPlugin::OpenGL:
  95. {
  96. psLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_ps.gpuprog";
  97. vsLoc = L"C:\\Projects\\BansheeEngine\\Data\\glsl_vs.gpuprog";
  98. language = "glsl";
  99. psProfile = GPP_PS_2_0;
  100. vsProfile = GPP_VS_2_0;
  101. psEntry = "main";
  102. vsEntry = "main";
  103. break;
  104. }
  105. }
  106. ImportOptionsPtr gpuProgImportOptions = Importer::instance().createImportOptions(psLoc);
  107. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  108. {
  109. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  110. importOptions->setEntryPoint(psEntry);
  111. importOptions->setLanguage(language);
  112. importOptions->setProfile(psProfile);
  113. importOptions->setType(GPT_FRAGMENT_PROGRAM);
  114. }
  115. HHighLevelGpuProgram fragProgRef = Importer::instance().import(psLoc, gpuProgImportOptions);
  116. gpuProgImportOptions = Importer::instance().createImportOptions(vsLoc);
  117. if(rtti_is_of_type<GpuProgramImportOptions>(gpuProgImportOptions))
  118. {
  119. GpuProgramImportOptions* importOptions = static_cast<GpuProgramImportOptions*>(gpuProgImportOptions.get());
  120. importOptions->setEntryPoint(vsEntry);
  121. importOptions->setLanguage(language);
  122. importOptions->setProfile(vsProfile);
  123. importOptions->setType(GPT_VERTEX_PROGRAM);
  124. }
  125. HHighLevelGpuProgram vertProgRef = Importer::instance().import(vsLoc, gpuProgImportOptions);
  126. gResources().save(vertProgRef, L"C:\\vertProgCg.vprog", true);
  127. gResources().unload(vertProgRef);
  128. vertProgRef = gResources().load(L"C:\\vertProgCg.vprog");
  129. gResources().save(fragProgRef, L"C:\\fragProgCg.vprog", true);
  130. gResources().unload(fragProgRef);
  131. fragProgRef = gResources().load(L"C:\\fragProgCg.vprog");
  132. ShaderPtr testShader = Shader::create("TestShader");
  133. testShader->addParameter("matViewProjection", "matViewProjection", GPDT_MATRIX_4X4);
  134. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  135. testShader->addParameter("input", "input", GPDT_STRUCT, 2, 8);
  136. testShader->addParameter("samp", "samp", GPOT_SAMPLER2D);
  137. testShader->addParameter("tex", "tex", GPOT_TEXTURE2D);
  138. TechniquePtr newTechniqueGL = testShader->addTechnique("GLRenderSystem", "ForwardRenderer");
  139. PassPtr newPassGL = newTechniqueGL->addPass();
  140. newPassGL->setVertexProgram(vertProgRef);
  141. newPassGL->setFragmentProgram(fragProgRef);
  142. // TODO - I need to create different techniques for different render systems (and renderers, if there were any),
  143. // which is redundant as some techniques can be reused. I should add a functionality that supports multiple
  144. // render systems/renderers per technique
  145. TechniquePtr newTechniqueDX = testShader->addTechnique("D3D9RenderSystem", "ForwardRenderer");
  146. PassPtr newPassDX = newTechniqueDX->addPass();
  147. newPassDX->setVertexProgram(vertProgRef);
  148. newPassDX->setFragmentProgram(fragProgRef);
  149. TechniquePtr newTechniqueDX11 = testShader->addTechnique("D3D11RenderSystem", "ForwardRenderer");
  150. PassPtr newPassDX11 = newTechniqueDX11->addPass();
  151. newPassDX11->setVertexProgram(vertProgRef);
  152. newPassDX11->setFragmentProgram(fragProgRef);
  153. HMaterial testMaterial = Material::create();
  154. testMaterial->setShader(testShader);
  155. testMaterial->setMat4("matViewProjection", Matrix4::IDENTITY);
  156. if(renderSystemPlugin == RenderSystemPlugin::DX11)
  157. {
  158. float dbgMultipliers1[2];
  159. dbgMultipliers1[0] = 0.0f;
  160. dbgMultipliers1[1] = 0.0f;
  161. float dbgMultipliers2[2];
  162. dbgMultipliers2[0] = 1.0f;
  163. dbgMultipliers2[1] = 1.0f;
  164. testMaterial->setStructData("input", dbgMultipliers1, sizeof(dbgMultipliers1), 0);
  165. testMaterial->setStructData("input", dbgMultipliers2, sizeof(dbgMultipliers2), 1);
  166. }
  167. HTexture testTexRef = static_resource_cast<Texture>(Importer::instance().import(L"C:\\ArenaTowerDFS.psd"));
  168. HMesh dbgMeshRef = static_resource_cast<Mesh>(Importer::instance().import(L"C:\\X_Arena_Tower.FBX"));
  169. gResources().save(testTexRef, L"C:\\ExportTest.tex", true);
  170. gResources().save(dbgMeshRef, L"C:\\ExportMesh.mesh", true);
  171. gResources().unload(testTexRef);
  172. gResources().unload(dbgMeshRef);
  173. testTexRef = static_resource_cast<Texture>(gResources().loadAsync(L"C:\\ExportTest.tex"));
  174. dbgMeshRef = static_resource_cast<Mesh>(gResources().loadAsync(L"C:\\ExportMesh.mesh"));
  175. dbgMeshRef.synchronize();
  176. testTexRef.synchronize();
  177. testMaterial->setTexture("tex", testTexRef);
  178. gResources().save(testMaterial, L"C:\\ExportMaterial.mat", true);
  179. gResources().unload(testMaterial);
  180. testMaterial = gResources().load(L"C:\\ExportMaterial.mat");
  181. testRenderable->setMesh(dbgMeshRef);
  182. testRenderable->setMaterial(0, testMaterial);
  183. GameObjectHandle<DbgTestGameObjectRef> dbgTestGameObjectRef = testModelGO->addComponent<DbgTestGameObjectRef>();
  184. dbgTestGameObjectRef->mRenderable = testRenderable;
  185. HSceneObject clone = testModelGO->clone();
  186. GameObjectHandle<DbgTestGameObjectRef> clonedDbgTestGameObjectRef = clone->getComponent<DbgTestGameObjectRef>();
  187. testModelGO->destroy();
  188. //Win32FolderMonitor* folderMonitor = cm_new<Win32FolderMonitor>();
  189. //FolderChange folderChanges = (FolderChange)((UINT32)FolderChange::FileName | (UINT32)FolderChange::DirName |
  190. // (UINT32)FolderChange::Creation | (UINT32)FolderChange::LastWrite);
  191. //folderMonitor->startMonitor(L"D:\\TestDetect", true, folderChanges);
  192. HTexture dbgCursor = static_resource_cast<Texture>(Importer::instance().import(L"C:\\CursorDbg.psd"));
  193. PixelDataPtr cursorPixelData = dbgCursor->allocateSubresourceBuffer(0);
  194. gMainSyncedCA().readSubresource(dbgCursor.getInternalPtr(), 0, cursorPixelData);
  195. gMainSyncedCA().submitToCoreThread(true);
  196. /************************************************************************/
  197. /* END DEBUG CODE */
  198. /************************************************************************/
  199. UndoRedo::startUp(cm_new<UndoRedo>());
  200. EditorWindowManager::startUp(cm_new<EditorWindowManager>());
  201. EditorWidgetManager::startUp(cm_new<EditorWidgetManager>());
  202. MainEditorWindow* mainWindow = MainEditorWindow::create(gApplication().getPrimaryWindow());
  203. gApplication().mainLoopCallback.connect(boost::bind(&EditorApplication::update, this));
  204. DbgEditorWidget1::open(); // DEBUG ONLY
  205. DbgEditorWidget2::open(); // DEBUG ONLY
  206. gBansheeApp().runMainLoop();
  207. EditorWidgetManager::shutDown();
  208. EditorWindowManager::shutDown();
  209. UndoRedo::shutDown();
  210. /************************************************************************/
  211. /* DEBUG CODE */
  212. /************************************************************************/
  213. gResources().unload(testTexRef);
  214. gResources().unload(dbgMeshRef);
  215. gResources().unload(fragProgRef);
  216. gResources().unload(vertProgRef);
  217. gResources().unload(testMaterial);
  218. testMaterial = nullptr;
  219. testTexRef = nullptr;
  220. dbgMeshRef = nullptr;
  221. fragProgRef = nullptr;
  222. vertProgRef = nullptr;
  223. newPassGL = nullptr;
  224. newTechniqueGL = nullptr;
  225. newPassDX = nullptr;
  226. newTechniqueDX = nullptr;
  227. newPassDX11 = nullptr;
  228. newTechniqueDX11 = nullptr;
  229. testShader = nullptr;
  230. renderWindow = nullptr;
  231. /************************************************************************/
  232. /* END DEBUG CODE */
  233. /************************************************************************/
  234. ProjectLibrary::shutDown();
  235. EditorGUI::shutDown();
  236. gBansheeApp().shutDown();
  237. }
  238. EditorApplication::~EditorApplication()
  239. {
  240. // TODO - Move shutdown code from constructor to here. Right now I don't care because cleanup
  241. // isn't working as intended and if I move stuff I will probably break it even more
  242. }
  243. void EditorApplication::runMainLoop()
  244. {
  245. // TODO - Move "runMainLoop" code from constructor to here. Right now I don't care because cleanup
  246. // isn't working as intended and if I move stuff I will probably break it even more
  247. }
  248. void EditorApplication::update()
  249. {
  250. ProjectLibrary::instance().update();
  251. EditorWindowManager::instance().update();
  252. }
  253. bool EditorApplication::isProjectLoaded() const
  254. {
  255. return true; // TODO - DEBUG ONLY
  256. }
  257. const WString& EditorApplication::getActiveProjectPath() const
  258. {
  259. static WString dummyProjectPath = L"D:\\DummyBansheeProject";
  260. return dummyProjectPath;
  261. }
  262. const String& EditorApplication::getLibraryNameForRenderSystem(RenderSystemPlugin plugin)
  263. {
  264. static String DX11Name = "CamelotD3D11RenderSystem";
  265. static String DX9Name = "CamelotD3D9RenderSystem";
  266. static String OpenGLName = "CamelotGLRenderSystem";
  267. switch(plugin)
  268. {
  269. case RenderSystemPlugin::DX11:
  270. return DX11Name;
  271. case RenderSystemPlugin::DX9:
  272. return DX9Name;
  273. case RenderSystemPlugin::OpenGL:
  274. return OpenGLName;
  275. }
  276. return StringUtil::BLANK;
  277. }
  278. }