CmApplication.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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 "CmRenderer.h"
  25. #include "CmDeferredRenderContext.h"
  26. #include "CmMaterial.h"
  27. #include "CmShader.h"
  28. #include "CmTechnique.h"
  29. #include "CmPass.h"
  30. #include "CmRendererManager.h"
  31. namespace CamelotEngine
  32. {
  33. Application::Application()
  34. :mPrimaryRenderWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
  35. { }
  36. void Application::startUp(const String& renderSystemName, const String& rendererName)
  37. {
  38. Time::startUp(new Time());
  39. Input::startUp(new Input());
  40. DynLibManager::startUp(new DynLibManager());
  41. CoreGpuObjectManager::startUp(new CoreGpuObjectManager());
  42. Resources::startUp(new Resources("D:\\CamelotResourceMetas"));
  43. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  44. RenderSystemManager::startUp(new RenderSystemManager());
  45. RenderSystemManager::instance().setActive(renderSystemName);
  46. RendererManager::startUp(new RendererManager());
  47. loadPlugin(rendererName);
  48. RendererManager::instance().setActive("ForwardRenderer");
  49. RenderSystem* renderSystem = RenderSystem::instancePtr();
  50. RENDER_WINDOW_DESC renderWindowDesc;
  51. renderWindowDesc.width = 1280;
  52. renderWindowDesc.height = 720;
  53. renderWindowDesc.title = "Camelot Renderer";
  54. renderWindowDesc.fullscreen = false;
  55. mPrimaryRenderWindow = RenderWindow::create(renderWindowDesc);
  56. mPrimaryRenderWindow->waitUntilInitialized(); // TODO: Created window is required for proper initialization of the render system so I need to wait
  57. // I plan on handling this differently. Either by somehow allowing the RS to be initialized without a window,
  58. // or forcing a window to be created with RenderSystemManager::startUp. Initializing OpenGL context without a window
  59. // is especially troublesome (apparently possible just poorly documented)
  60. mPrimaryRenderContext = renderSystem->createDeferredContext();
  61. SceneManager::startUp(new SceneManager());
  62. MeshManager::startUp(new MeshManager());
  63. Importer::startUp(new Importer());
  64. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  65. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  66. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  67. }
  68. void Application::runMainLoop()
  69. {
  70. mRunMainLoop = true;
  71. while(mRunMainLoop)
  72. {
  73. gSceneManager().update();
  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. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, this));
  92. mPrimaryRenderContext->submitToGpu();
  93. renderSystem->queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  94. }
  95. else
  96. mPrimaryRenderContext->cancelAll();
  97. gTime().update();
  98. gInput().update();
  99. }
  100. }
  101. void Application::stopMainLoop()
  102. {
  103. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  104. // a race condition we might run the loop one extra iteration which is acceptable
  105. }
  106. void Application::updateMessagePump()
  107. {
  108. WindowEventUtilities::messagePump();
  109. }
  110. void Application::updateResourcesCallback()
  111. {
  112. gResources().update();
  113. }
  114. void Application::frameRenderingFinishedCallback()
  115. {
  116. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  117. mIsFrameRenderingFinished = true;
  118. }
  119. void Application::shutDown()
  120. {
  121. Importer::shutDown();
  122. MeshManager::shutDown();
  123. SceneManager::shutDown();
  124. RendererManager::shutDown();
  125. RenderSystem::shutDown();
  126. HighLevelGpuProgramManager::shutDown();
  127. Resources::shutDown();
  128. CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
  129. DynLibManager::shutDown();
  130. Input::shutDown();
  131. Time::shutDown();
  132. }
  133. void Application::loadPlugin(const String& pluginName)
  134. {
  135. String name = pluginName;
  136. #if CM_PLATFORM == CM_PLATFORM_LINUX
  137. // dlopen() does not add .so to the filename, like windows does for .dll
  138. if (name.substr(name.length() - 3, 3) != ".so")
  139. name += ".so";
  140. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  141. // dlopen() does not add .dylib to the filename, like windows does for .dll
  142. if (name.substr(name.length() - 6, 6) != ".dylib")
  143. name += ".dylib";
  144. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  145. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  146. // if you include a relative path then it does not. So, add it to be sure.
  147. if (name.substr(name.length() - 4, 4) != ".dll")
  148. name += ".dll";
  149. #endif
  150. DynLib* library = gDynLibManager().load(name);
  151. if(library != nullptr)
  152. {
  153. typedef const void (*LoadPluginFunc)();
  154. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  155. loadPluginFunc();
  156. }
  157. }
  158. UINT64 Application::getAppWindowId()
  159. {
  160. if(!mPrimaryRenderWindow)
  161. {
  162. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  163. }
  164. UINT64 windowId = 0;
  165. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  166. return windowId;
  167. }
  168. Application& gApplication()
  169. {
  170. static Application application;
  171. return application;
  172. }
  173. }