CmApplication.cpp 6.3 KB

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