CmApplication.cpp 6.9 KB

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