CmApplication.cpp 6.1 KB

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