CmApplication.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "CmCoreGpuObjectManager.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 "CmGameObject.h"
  20. #include "CmTime.h"
  21. #include "CmInput.h"
  22. #include "CmRendererManager.h"
  23. #include "CmMeshManager.h"
  24. #include "CmMaterialManager.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 CamelotEngine
  33. {
  34. Application::Application()
  35. :mPrimaryRenderWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  36. { }
  37. void Application::startUp(const String& renderSystemName, const String& rendererName)
  38. {
  39. Time::startUp(new Time());
  40. Input::startUp(new Input());
  41. DynLibManager::startUp(new DynLibManager());
  42. CoreGpuObjectManager::startUp(new CoreGpuObjectManager());
  43. Resources::startUp(new Resources("D:\\CamelotResourceMetas"));
  44. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  45. RenderSystemManager::startUp(new RenderSystemManager());
  46. RenderSystemManager::instance().setActive(renderSystemName);
  47. RendererManager::startUp(new RendererManager());
  48. loadPlugin(rendererName);
  49. RendererManager::instance().setActive("ForwardRenderer");
  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(new SceneManager());
  63. MeshManager::startUp(new MeshManager());
  64. MaterialManager::startUp(new MaterialManager());
  65. Importer::startUp(new Importer());
  66. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  67. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  68. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  69. }
  70. void Application::runMainLoop()
  71. {
  72. mRunMainLoop = true;
  73. while(mRunMainLoop)
  74. {
  75. gSceneManager().update();
  76. RenderSystem* renderSystem = RenderSystem::instancePtr();
  77. RendererManager::instance().getActive()->renderAll();
  78. // Only queue new commands if render thread has finished rendering
  79. // TODO - There might be a more optimal way to sync simulation and render threads so we maximize
  80. // the amount of rendered frames
  81. bool readyForNextFrame = false;
  82. {
  83. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  84. readyForNextFrame = mIsFrameRenderingFinished;
  85. }
  86. if(readyForNextFrame)
  87. {
  88. {
  89. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  90. mIsFrameRenderingFinished = false;
  91. }
  92. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  93. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, 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::updateResourcesCallback()
  113. {
  114. gResources().update();
  115. }
  116. void Application::frameRenderingFinishedCallback()
  117. {
  118. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  119. mIsFrameRenderingFinished = true;
  120. }
  121. void Application::shutDown()
  122. {
  123. mPrimaryRenderWindow = nullptr;
  124. Importer::shutDown();
  125. MaterialManager::shutDown();
  126. MeshManager::shutDown();
  127. SceneManager::shutDown();
  128. RendererManager::shutDown();
  129. RenderSystem::shutDown();
  130. HighLevelGpuProgramManager::shutDown();
  131. Resources::shutDown();
  132. CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  133. DynLibManager::shutDown();
  134. Input::shutDown();
  135. Time::shutDown();
  136. }
  137. void Application::loadPlugin(const String& pluginName)
  138. {
  139. String name = pluginName;
  140. #if CM_PLATFORM == CM_PLATFORM_LINUX
  141. // dlopen() does not add .so to the filename, like windows does for .dll
  142. if (name.substr(name.length() - 3, 3) != ".so")
  143. name += ".so";
  144. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  145. // dlopen() does not add .dylib to the filename, like windows does for .dll
  146. if (name.substr(name.length() - 6, 6) != ".dylib")
  147. name += ".dylib";
  148. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  149. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  150. // if you include a relative path then it does not. So, add it to be sure.
  151. if (name.substr(name.length() - 4, 4) != ".dll")
  152. name += ".dll";
  153. #endif
  154. DynLib* library = gDynLibManager().load(name);
  155. if(library != nullptr)
  156. {
  157. typedef const void (*LoadPluginFunc)();
  158. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  159. loadPluginFunc();
  160. }
  161. }
  162. UINT64 Application::getAppWindowId()
  163. {
  164. if(!mPrimaryRenderWindow)
  165. {
  166. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  167. }
  168. UINT64 windowId = 0;
  169. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  170. return windowId;
  171. }
  172. Application& gApplication()
  173. {
  174. static Application application;
  175. return application;
  176. }
  177. }