CmApplication.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "CmDynLib.h"
  13. #include "CmDynLibManager.h"
  14. #include "CmSceneManager.h"
  15. #include "CmImporter.h"
  16. #include "CmResources.h"
  17. #include "CmMesh.h"
  18. #include "CmGameObject.h"
  19. #include "CmTime.h"
  20. #include "CmInput.h"
  21. #include "CmRendererManager.h"
  22. #include "CmRenderer.h"
  23. #include "CmDeferredRenderContext.h"
  24. #include "CmMaterial.h"
  25. #include "CmShader.h"
  26. #include "CmTechnique.h"
  27. #include "CmPass.h"
  28. #include "CmRendererManager.h"
  29. namespace CamelotEngine
  30. {
  31. Application::Application()
  32. :mPrimaryRenderWindow(nullptr), mIsFrameRenderingFinished(true)
  33. { }
  34. void Application::startUp(const String& renderSystemName, const String& rendererName)
  35. {
  36. Time::startUp(new Time());
  37. Input::startUp(new Input());
  38. DynLibManager::startUp(new DynLibManager());
  39. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  40. RenderSystemManager::startUp(renderSystemName);
  41. loadPlugin(rendererName);
  42. RendererManager::setActive("ForwardRenderer");
  43. RenderSystem* renderSystem = RenderSystem::instancePtr();
  44. RENDER_WINDOW_DESC renderWindowDesc;
  45. renderWindowDesc.width = 1280;
  46. renderWindowDesc.height = 720;
  47. renderWindowDesc.title = "Camelot Renderer";
  48. renderWindowDesc.fullscreen = false;
  49. mPrimaryRenderWindow = RenderWindow::create(renderWindowDesc);
  50. mPrimaryRenderContext = renderSystem->createDeferredContext();
  51. SceneManager::startUp(new SceneManager());
  52. Resources::startUp(new Resources("D:\\CamelotResourceMetas"));
  53. Importer::startUp(new Importer());
  54. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  55. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  56. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  57. }
  58. void Application::runMainLoop()
  59. {
  60. while(true)
  61. {
  62. gSceneManager().update();
  63. RenderSystem* renderSystem = RenderSystem::instancePtr();
  64. RendererManager::getActive()->renderAll();
  65. // Only queue new commands if render thread has finished rendering
  66. // TODO - There might be a more optimal way to sync simulation and render threads so we maximize
  67. // the amount of rendered frames
  68. bool readyForNextFrame = false;
  69. {
  70. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  71. readyForNextFrame = mIsFrameRenderingFinished;
  72. }
  73. if(readyForNextFrame)
  74. {
  75. {
  76. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  77. mIsFrameRenderingFinished = false;
  78. }
  79. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  80. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, this));
  81. mPrimaryRenderContext->submitToGpu();
  82. renderSystem->queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  83. }
  84. else
  85. mPrimaryRenderContext->cancelAll();
  86. gTime().update();
  87. gInput().update();
  88. }
  89. }
  90. void Application::updateMessagePump()
  91. {
  92. WindowEventUtilities::messagePump();
  93. }
  94. void Application::updateResourcesCallback()
  95. {
  96. gResources().update();
  97. }
  98. void Application::frameRenderingFinishedCallback()
  99. {
  100. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  101. mIsFrameRenderingFinished = true;
  102. }
  103. void Application::shutDown()
  104. {
  105. SceneManager::shutDown();
  106. RenderSystem::shutDown();
  107. HighLevelGpuProgramManager::shutDown();
  108. DynLibManager::shutDown();
  109. Resources::shutDown();
  110. Input::shutDown();
  111. Time::shutDown();
  112. }
  113. void Application::loadPlugin(const String& pluginName)
  114. {
  115. String name = pluginName;
  116. #if CM_PLATFORM == CM_PLATFORM_LINUX
  117. // dlopen() does not add .so to the filename, like windows does for .dll
  118. if (name.substr(name.length() - 3, 3) != ".so")
  119. name += ".so";
  120. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  121. // dlopen() does not add .dylib to the filename, like windows does for .dll
  122. if (name.substr(name.length() - 6, 6) != ".dylib")
  123. name += ".dylib";
  124. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  125. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  126. // if you include a relative path then it does not. So, add it to be sure.
  127. if (name.substr(name.length() - 4, 4) != ".dll")
  128. name += ".dll";
  129. #endif
  130. DynLib* library = gDynLibManager().load(name);
  131. if(library != nullptr)
  132. {
  133. typedef const void (*LoadPluginFunc)();
  134. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  135. loadPluginFunc();
  136. }
  137. }
  138. UINT64 Application::getAppWindowId()
  139. {
  140. if(!mPrimaryRenderWindow)
  141. {
  142. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  143. }
  144. UINT64 windowId = 0;
  145. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  146. return windowId;
  147. }
  148. Application& gApplication()
  149. {
  150. static Application application;
  151. return application;
  152. }
  153. }