CmApplication.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #include "CmApplication.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmWindowEventUtilities.h"
  5. #include "CmHardwareBufferManager.h"
  6. #include "CmRenderWindow.h"
  7. #include "CmCamera.h"
  8. #include "CmViewport.h"
  9. #include "CmVector2.h"
  10. #include "CmHighLevelGpuProgram.h"
  11. #include "CmHighLevelGpuProgramManager.h"
  12. #include "CmCoreObjectManager.h"
  13. #include "CmDynLib.h"
  14. #include "CmDynLibManager.h"
  15. #include "CmSceneManager.h"
  16. #include "CmImporter.h"
  17. #include "CmResources.h"
  18. #include "CmMesh.h"
  19. #include "CmSceneObject.h"
  20. #include "CmTime.h"
  21. #include "CmInput.h"
  22. #include "CmRendererManager.h"
  23. #include "CmMeshManager.h"
  24. #include "CmMaterialManager.h"
  25. #include "CmFontManager.h"
  26. #include "CmRenderer.h"
  27. #include "CmDeferredRenderContext.h"
  28. #include "CmOverlayManager.h"
  29. #include "CmGUIMaterialManager.h"
  30. #include "CmGUIManager.h"
  31. #include "CmMaterial.h"
  32. #include "CmShader.h"
  33. #include "CmTechnique.h"
  34. #include "CmPass.h"
  35. #include "CmRendererManager.h"
  36. namespace CamelotEngine
  37. {
  38. Application::Application()
  39. :mPrimaryRenderWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  40. { }
  41. void Application::startUp(const String& renderSystemName, const String& rendererName)
  42. {
  43. Time::startUp(CM_NEW(Time, GenAlloc) Time());
  44. Input::startUp(CM_NEW(Input, GenAlloc) Input());
  45. DynLibManager::startUp(CM_NEW(DynLibManager, GenAlloc) DynLibManager());
  46. CoreGpuObjectManager::startUp(CM_NEW(CoreGpuObjectManager, GenAlloc) CoreGpuObjectManager());
  47. Resources::startUp(CM_NEW(Resources, GenAlloc) Resources("D:\\CamelotResourceMetas"));
  48. HighLevelGpuProgramManager::startUp(CM_NEW(HighLevelGpuProgramManager, GenAlloc) HighLevelGpuProgramManager());
  49. RenderSystemManager::startUp(CM_NEW(RenderSystemManager, GenAlloc) RenderSystemManager());
  50. RenderSystemManager::instance().setActive(renderSystemName);
  51. RendererManager::startUp(CM_NEW(RendererManager, GenAlloc) RendererManager());
  52. loadPlugin(rendererName);
  53. RendererManager::instance().setActive("ForwardRenderer");
  54. RenderSystem* renderSystem = RenderSystem::instancePtr();
  55. RENDER_WINDOW_DESC renderWindowDesc;
  56. renderWindowDesc.width = 1280;
  57. renderWindowDesc.height = 720;
  58. renderWindowDesc.title = "Camelot Renderer";
  59. renderWindowDesc.fullscreen = false;
  60. mPrimaryRenderWindow = RenderWindow::create(renderWindowDesc);
  61. mPrimaryRenderWindow->waitUntilInitialized(); // TODO: Created window is required for proper initialization of the render system so I need to wait.
  62. // I plan on handling this differently. Either by somehow allowing the RS to be initialized without a window,
  63. // or forcing a window to be created with RenderSystemManager::startUp. Initializing OpenGL context without a window
  64. // is especially troublesome (apparently possible just poorly documented)
  65. mPrimaryRenderContext = renderSystem->createDeferredContext();
  66. SceneManager::startUp(CM_NEW(SceneManager, GenAlloc) SceneManager());
  67. MeshManager::startUp(CM_NEW(MeshManager, GenAlloc) MeshManager());
  68. MaterialManager::startUp(CM_NEW(MaterialManager, GenAlloc) MaterialManager());
  69. FontManager::startUp(CM_NEW(FontManager, GenAlloc) FontManager());
  70. OverlayManager::startUp(CM_NEW(OverlayManager, GenAlloc) OverlayManager());
  71. GUIMaterialManager::startUp(CM_NEW(GUIMaterialManager, GenAlloc) GUIMaterialManager());
  72. GUIManager::startUp(CM_NEW(GUIManager, GenAlloc) GUIManager());
  73. Importer::startUp(CM_NEW(Importer, GenAlloc) Importer());
  74. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  75. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  76. loadPlugin("CamelotFontImporter"); // TODO - Load this automatically somehow
  77. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  78. }
  79. void Application::runMainLoop()
  80. {
  81. mRunMainLoop = true;
  82. while(mRunMainLoop)
  83. {
  84. gSceneManager().update();
  85. RenderSystem* renderSystem = RenderSystem::instancePtr();
  86. RendererManager::instance().getActive()->renderAll();
  87. // Only queue new commands if render thread has finished rendering
  88. // TODO - There might be a more optimal way to sync simulation and render threads so we maximize
  89. // the amount of rendered frames
  90. bool readyForNextFrame = false;
  91. {
  92. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  93. readyForNextFrame = mIsFrameRenderingFinished;
  94. }
  95. if(readyForNextFrame)
  96. {
  97. {
  98. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  99. mIsFrameRenderingFinished = false;
  100. }
  101. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  102. mPrimaryRenderContext->submitToGpu();
  103. renderSystem->queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  104. }
  105. else
  106. mPrimaryRenderContext->cancelAll();
  107. gTime().update();
  108. gInput().update();
  109. }
  110. }
  111. void Application::stopMainLoop()
  112. {
  113. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  114. // a race condition we might run the loop one extra iteration which is acceptable
  115. }
  116. void Application::updateMessagePump()
  117. {
  118. WindowEventUtilities::messagePump();
  119. }
  120. void Application::frameRenderingFinishedCallback()
  121. {
  122. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  123. mIsFrameRenderingFinished = true;
  124. }
  125. void Application::shutDown()
  126. {
  127. mPrimaryRenderWindow = nullptr;
  128. Importer::shutDown();
  129. FontManager::shutDown();
  130. MaterialManager::shutDown();
  131. MeshManager::shutDown();
  132. SceneManager::shutDown();
  133. GUIManager::shutDown();
  134. GUIMaterialManager::shutDown();
  135. OverlayManager::shutDown();
  136. RendererManager::shutDown();
  137. RenderSystem::shutDown();
  138. HighLevelGpuProgramManager::shutDown();
  139. Resources::shutDown();
  140. CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  141. Input::shutDown();
  142. DynLibManager::shutDown();
  143. Time::shutDown();
  144. }
  145. void Application::loadPlugin(const String& pluginName)
  146. {
  147. String name = pluginName;
  148. #if CM_PLATFORM == CM_PLATFORM_LINUX
  149. // dlopen() does not add .so to the filename, like windows does for .dll
  150. if (name.substr(name.length() - 3, 3) != ".so")
  151. name += ".so";
  152. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  153. // dlopen() does not add .dylib to the filename, like windows does for .dll
  154. if (name.substr(name.length() - 6, 6) != ".dylib")
  155. name += ".dylib";
  156. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  157. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  158. // if you include a relative path then it does not. So, add it to be sure.
  159. if (name.substr(name.length() - 4, 4) != ".dll")
  160. name += ".dll";
  161. #endif
  162. DynLib* library = gDynLibManager().load(name);
  163. if(library != nullptr)
  164. {
  165. typedef const void (*LoadPluginFunc)();
  166. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  167. loadPluginFunc();
  168. }
  169. }
  170. UINT64 Application::getAppWindowId()
  171. {
  172. if(!mPrimaryRenderWindow)
  173. {
  174. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  175. }
  176. UINT64 windowId = 0;
  177. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  178. return windowId;
  179. }
  180. Application& gApplication()
  181. {
  182. static Application application;
  183. return application;
  184. }
  185. }