CmApplication.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "CmViewport.h"
  8. #include "CmVector2.h"
  9. #include "CmHighLevelGpuProgram.h"
  10. #include "CmHighLevelGpuProgramManager.h"
  11. #include "CmCoreObjectManager.h"
  12. #include "CmDynLib.h"
  13. #include "CmDynLibManager.h"
  14. #include "CmSceneManager.h"
  15. #include "CmImporter.h"
  16. #include "CmResources.h"
  17. #include "CmMesh.h"
  18. #include "CmSceneObject.h"
  19. #include "CmTime.h"
  20. #include "CmInput.h"
  21. #include "CmRendererManager.h"
  22. #include "CmMeshManager.h"
  23. #include "CmMaterialManager.h"
  24. #include "CmFontManager.h"
  25. #include "CmRenderWindowManager.h"
  26. #include "CmRenderer.h"
  27. #include "CmMaterial.h"
  28. #include "CmShader.h"
  29. #include "CmTechnique.h"
  30. #include "CmPass.h"
  31. #include "CmCursor.h"
  32. #include "CmRendererManager.h"
  33. namespace CamelotFramework
  34. {
  35. Application::Application()
  36. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  37. {
  38. }
  39. void Application::startUp(START_UP_DESC& desc)
  40. {
  41. MemStack::setupHeap(HID_Main);
  42. Time::startUp(cm_new<Time>());
  43. DynLibManager::startUp(cm_new<DynLibManager>());
  44. CoreGpuObjectManager::startUp(cm_new<CoreGpuObjectManager>());
  45. Resources::startUp(cm_new<Resources>(desc.resourceCacheDirectory));
  46. HighLevelGpuProgramManager::startUp(cm_new<HighLevelGpuProgramManager>());
  47. RenderSystemManager::startUp(cm_new<RenderSystemManager>());
  48. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  49. Input::startUp(cm_new<Input>());
  50. RendererManager::startUp(cm_new<RendererManager>());
  51. loadPlugin(desc.renderer);
  52. RendererManager::instance().setActive(desc.renderer);
  53. RenderSystem* renderSystem = RenderSystem::instancePtr();
  54. mPrimaryRenderContext = renderSystem->createDeferredContext();
  55. mPrimarySyncedRenderContext = &renderSystem->getSyncedDeferredContext();
  56. SceneManager::startUp((SceneManager*)loadPlugin(desc.sceneManager));
  57. MeshManager::startUp(cm_new<MeshManager>());
  58. MaterialManager::startUp(cm_new<MaterialManager>());
  59. FontManager::startUp(cm_new<FontManager>());
  60. Importer::startUp(cm_new<Importer>());
  61. for(auto& importerName : desc.importers)
  62. loadPlugin(importerName);
  63. loadPlugin(desc.input);
  64. Cursor::setCursor(CursorType::Arrow);
  65. }
  66. void Application::runMainLoop()
  67. {
  68. mRunMainLoop = true;
  69. while(mRunMainLoop)
  70. {
  71. RenderWindowManager::instance()._update();
  72. gInput().update();
  73. gSceneManager().update();
  74. if(!mainLoopCallback.empty())
  75. mainLoopCallback();
  76. RenderSystem* renderSystem = RenderSystem::instancePtr();
  77. RendererManager::instance().getActive()->renderAll();
  78. // Only queue new commands if render thread has finished rendering
  79. // TODO - There might be a more optimal way to sync simulation and render threads so we maximize
  80. // the amount of rendered frames
  81. bool readyForNextFrame = false;
  82. {
  83. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  84. readyForNextFrame = mIsFrameRenderingFinished;
  85. }
  86. if(readyForNextFrame)
  87. {
  88. {
  89. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  90. mIsFrameRenderingFinished = false;
  91. }
  92. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  93. mPrimaryRenderContext->submitToGpu();
  94. renderSystem->queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  95. }
  96. else
  97. mPrimaryRenderContext->cancelAll();
  98. gTime().update();
  99. }
  100. }
  101. void Application::stopMainLoop()
  102. {
  103. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  104. // a race condition we might run the loop one extra iteration which is acceptable
  105. }
  106. void Application::updateMessagePump()
  107. {
  108. WindowEventUtilities::messagePump();
  109. }
  110. void Application::frameRenderingFinishedCallback()
  111. {
  112. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  113. mIsFrameRenderingFinished = true;
  114. }
  115. void Application::shutDown()
  116. {
  117. mPrimaryWindow->destroy();
  118. mPrimaryWindow = nullptr;
  119. Importer::shutDown();
  120. FontManager::shutDown();
  121. MaterialManager::shutDown();
  122. MeshManager::shutDown();
  123. SceneManager::shutDown();
  124. RendererManager::shutDown();
  125. RenderSystem::shutDown();
  126. Input::shutDown();
  127. HighLevelGpuProgramManager::shutDown();
  128. Resources::shutDown();
  129. CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  130. DynLibManager::shutDown();
  131. Time::shutDown();
  132. }
  133. void* Application::loadPlugin(const String& pluginName)
  134. {
  135. String name = pluginName;
  136. #if CM_PLATFORM == CM_PLATFORM_LINUX
  137. // dlopen() does not add .so to the filename, like windows does for .dll
  138. if (name.substr(name.length() - 3, 3) != ".so")
  139. name += ".so";
  140. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  141. // dlopen() does not add .dylib to the filename, like windows does for .dll
  142. if (name.substr(name.length() - 6, 6) != ".dylib")
  143. name += ".dylib";
  144. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  145. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  146. // if you include a relative path then it does not. So, add it to be sure.
  147. if (name.substr(name.length() - 4, 4) != ".dll")
  148. name += ".dll";
  149. #endif
  150. DynLib* library = gDynLibManager().load(name);
  151. if(library != nullptr)
  152. {
  153. typedef void* (*LoadPluginFunc)();
  154. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  155. return loadPluginFunc();
  156. }
  157. return nullptr;
  158. }
  159. UINT64 Application::getAppWindowId()
  160. {
  161. if(!mPrimaryWindow)
  162. {
  163. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window exists!");
  164. }
  165. UINT64 windowId = 0;
  166. mPrimaryWindow->getCustomAttribute("WINDOW", &windowId);
  167. return windowId;
  168. }
  169. Application& gApplication()
  170. {
  171. static Application application;
  172. return application;
  173. }
  174. RenderContext& gMainRC()
  175. {
  176. return *gApplication().mPrimaryRenderContext.get();
  177. }
  178. SyncedRenderContext& gMainSyncedRC()
  179. {
  180. return *gApplication().mPrimarySyncedRenderContext;
  181. }
  182. }