CmApplication.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #include "CmApplication.h"
  2. #include "CmRenderSystem.h"
  3. #include "CmRenderSystemManager.h"
  4. #include "CmPlatform.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 "CmDeferredCallManager.h"
  28. #include "CmCoreThread.h"
  29. #include "CmStringTable.h"
  30. #include "CmMaterial.h"
  31. #include "CmShader.h"
  32. #include "CmTechnique.h"
  33. #include "CmPass.h"
  34. #include "CmRendererManager.h"
  35. namespace CamelotFramework
  36. {
  37. Application::Application()
  38. :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  39. {
  40. }
  41. void Application::startUp(START_UP_DESC& desc)
  42. {
  43. MemStack::setupHeap(HID_Main);
  44. StringTable::startUp(cm_new<StringTable>());
  45. DeferredCallManager::startUp(cm_new<DeferredCallManager>());
  46. Time::startUp(cm_new<Time>());
  47. DynLibManager::startUp(cm_new<DynLibManager>());
  48. CoreGpuObjectManager::startUp(cm_new<CoreGpuObjectManager>());
  49. Resources::startUp(cm_new<Resources>(desc.resourceCacheDirectory));
  50. HighLevelGpuProgramManager::startUp(cm_new<HighLevelGpuProgramManager>());
  51. CoreThread::startUp(cm_new<CoreThread>());
  52. RenderSystemManager::startUp(cm_new<RenderSystemManager>());
  53. mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
  54. Input::startUp(cm_new<Input>());
  55. RendererManager::startUp(cm_new<RendererManager>());
  56. loadPlugin(desc.renderer);
  57. RendererManager::instance().setActive(desc.renderer);
  58. mPrimaryCoreAccessor = gCoreThread().createAccessor();
  59. mPrimarySyncedCoreAccessor = &gCoreThread().getSyncedAccessor();
  60. SceneManager::startUp((SceneManager*)loadPlugin(desc.sceneManager));
  61. MeshManager::startUp(cm_new<MeshManager>());
  62. MaterialManager::startUp(cm_new<MaterialManager>());
  63. FontManager::startUp(cm_new<FontManager>());
  64. Importer::startUp(cm_new<Importer>());
  65. for(auto& importerName : desc.importers)
  66. loadPlugin(importerName);
  67. loadPlugin(desc.input);
  68. Platform::setCursor(CursorType::Arrow);
  69. }
  70. void Application::runMainLoop()
  71. {
  72. mRunMainLoop = true;
  73. while(mRunMainLoop)
  74. {
  75. Platform::update();
  76. DeferredCallManager::instance().update();
  77. RenderWindowManager::instance().update();
  78. gInput().update();
  79. gSceneManager().update();
  80. if(!mainLoopCallback.empty())
  81. mainLoopCallback();
  82. RendererManager::instance().getActive()->renderAll();
  83. // Core and sim thread run in lockstep. This will result in a larger input latency than if I was
  84. // running just a single thread. Latency becomes worse if the core thread takes longer than sim
  85. // thread, in which case sim thread needs to wait. Optimal solution would be to get an average
  86. // difference between sim/core thread and start the sim thread a bit later so they finish at nearly the same time.
  87. {
  88. CM_LOCK_MUTEX_NAMED(mFrameRenderingFinishedMutex, lock);
  89. while(!mIsFrameRenderingFinished)
  90. CM_THREAD_WAIT(mFrameRenderingFinishedCondition, mFrameRenderingFinishedMutex, lock);
  91. mIsFrameRenderingFinished = false;
  92. }
  93. gCoreThread().queueCommand(boost::bind(&Application::updateMessagePump, this));
  94. mPrimaryCoreAccessor->submitToCoreThread();
  95. gCoreThread().queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  96. gTime().update();
  97. }
  98. }
  99. void Application::stopMainLoop()
  100. {
  101. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  102. // a race condition we might run the loop one extra iteration which is acceptable
  103. }
  104. void Application::updateMessagePump()
  105. {
  106. Platform::messagePump();
  107. }
  108. void Application::frameRenderingFinishedCallback()
  109. {
  110. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  111. mIsFrameRenderingFinished = true;
  112. CM_THREAD_NOTIFY_ONE(mFrameRenderingFinishedCondition);
  113. }
  114. void Application::shutDown()
  115. {
  116. mPrimaryWindow->destroy();
  117. mPrimaryWindow = nullptr;
  118. Importer::shutDown();
  119. FontManager::shutDown();
  120. MaterialManager::shutDown();
  121. MeshManager::shutDown();
  122. SceneManager::shutDown();
  123. RendererManager::shutDown();
  124. RenderSystem::shutDown();
  125. CoreThread::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. DeferredCallManager::shutDown();
  133. StringTable::shutDown();
  134. }
  135. void* Application::loadPlugin(const String& pluginName)
  136. {
  137. String name = pluginName;
  138. #if CM_PLATFORM == CM_PLATFORM_LINUX
  139. // dlopen() does not add .so to the filename, like windows does for .dll
  140. if (name.substr(name.length() - 3, 3) != ".so")
  141. name += ".so";
  142. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  143. // dlopen() does not add .dylib to the filename, like windows does for .dll
  144. if (name.substr(name.length() - 6, 6) != ".dylib")
  145. name += ".dylib";
  146. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  147. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  148. // if you include a relative path then it does not. So, add it to be sure.
  149. if (name.substr(name.length() - 4, 4) != ".dll")
  150. name += ".dll";
  151. #endif
  152. DynLib* library = gDynLibManager().load(name);
  153. if(library != nullptr)
  154. {
  155. typedef void* (*LoadPluginFunc)();
  156. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  157. return loadPluginFunc();
  158. }
  159. return nullptr;
  160. }
  161. UINT64 Application::getAppWindowId()
  162. {
  163. if(!mPrimaryWindow)
  164. {
  165. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window exists!");
  166. }
  167. UINT64 windowId = 0;
  168. mPrimaryWindow->getCustomAttribute("WINDOW", &windowId);
  169. return windowId;
  170. }
  171. Application& gApplication()
  172. {
  173. static Application application;
  174. return application;
  175. }
  176. CoreAccessor& gMainCA()
  177. {
  178. return *gApplication().mPrimaryCoreAccessor.get();
  179. }
  180. SyncedCoreAccessor& gMainSyncedCA()
  181. {
  182. return *gApplication().mPrimarySyncedCoreAccessor;
  183. }
  184. }