CmApplication.cpp 6.6 KB

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