BsEditorApplication.cpp 12 KB

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