CmApplication.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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), mRunMainLoop(false)
  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. mRunMainLoop = true;
  61. while(mRunMainLoop)
  62. {
  63. gSceneManager().update();
  64. RenderSystem* renderSystem = RenderSystem::instancePtr();
  65. RendererManager::getActive()->renderAll();
  66. // Only queue new commands if render thread has finished rendering
  67. // TODO - There might be a more optimal way to sync simulation and render threads so we maximize
  68. // the amount of rendered frames
  69. bool readyForNextFrame = false;
  70. {
  71. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  72. readyForNextFrame = mIsFrameRenderingFinished;
  73. }
  74. if(readyForNextFrame)
  75. {
  76. {
  77. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  78. mIsFrameRenderingFinished = false;
  79. }
  80. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  81. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, this));
  82. mPrimaryRenderContext->submitToGpu();
  83. renderSystem->queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
  84. }
  85. else
  86. mPrimaryRenderContext->cancelAll();
  87. gTime().update();
  88. gInput().update();
  89. }
  90. }
  91. void Application::stopMainLoop()
  92. {
  93. mRunMainLoop = false; // No sync primitives needed, in that rare case of
  94. // a race condition we might run the loop one extra iteration which is acceptable
  95. }
  96. void Application::updateMessagePump()
  97. {
  98. WindowEventUtilities::messagePump();
  99. }
  100. void Application::updateResourcesCallback()
  101. {
  102. gResources().update();
  103. }
  104. void Application::frameRenderingFinishedCallback()
  105. {
  106. CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
  107. mIsFrameRenderingFinished = true;
  108. }
  109. void Application::shutDown()
  110. {
  111. SceneManager::shutDown();
  112. RenderSystem::shutDown();
  113. HighLevelGpuProgramManager::shutDown();
  114. //DynLibManager::shutDown(); // TODO - I never shut this down, because then I'd have to ensure I classes loaded from dynamic libraries
  115. // are destroyed before shutting it down, which is too hard to ensure at the moment
  116. Resources::shutDown();
  117. Input::shutDown();
  118. Time::shutDown();
  119. }
  120. void Application::loadPlugin(const String& pluginName)
  121. {
  122. String name = pluginName;
  123. #if CM_PLATFORM == CM_PLATFORM_LINUX
  124. // dlopen() does not add .so to the filename, like windows does for .dll
  125. if (name.substr(name.length() - 3, 3) != ".so")
  126. name += ".so";
  127. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  128. // dlopen() does not add .dylib to the filename, like windows does for .dll
  129. if (name.substr(name.length() - 6, 6) != ".dylib")
  130. name += ".dylib";
  131. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  132. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  133. // if you include a relative path then it does not. So, add it to be sure.
  134. if (name.substr(name.length() - 4, 4) != ".dll")
  135. name += ".dll";
  136. #endif
  137. DynLib* library = gDynLibManager().load(name);
  138. if(library != nullptr)
  139. {
  140. typedef const void (*LoadPluginFunc)();
  141. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  142. loadPluginFunc();
  143. }
  144. }
  145. UINT64 Application::getAppWindowId()
  146. {
  147. if(!mPrimaryRenderWindow)
  148. {
  149. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  150. }
  151. UINT64 windowId = 0;
  152. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  153. return windowId;
  154. }
  155. Application& gApplication()
  156. {
  157. static Application application;
  158. return application;
  159. }
  160. }