CmApplication.cpp 6.8 KB

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