CmApplication.cpp 6.3 KB

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