CmApplication.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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)
  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 = RenderSystemManager::getActive();
  44. renderSystem->startUp();
  45. mPrimaryRenderWindow = renderSystem->createRenderWindow("Camelot Renderer", 1280, 720, false);
  46. mPrimaryRenderContext = renderSystem->createDeferredContext();
  47. SceneManager::startUp(new SceneManager());
  48. Resources::startUp(new Resources("D:\\CamelotResourceMetas"));
  49. Importer::startUp(new Importer());
  50. loadPlugin("CamelotFreeImgImporter"); // TODO - Load this automatically somehow
  51. loadPlugin("CamelotFBXImporter"); // TODO - Load this automatically somehow
  52. loadPlugin("CamelotOISInput"); // TODO - Load this automatically somehow
  53. }
  54. void Application::runMainLoop()
  55. {
  56. while(true)
  57. {
  58. gSceneManager().update();
  59. RenderSystem* renderSystem = RenderSystemManager::getActive();
  60. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  61. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, this));
  62. RendererManager::getActive()->renderAll();
  63. mPrimaryRenderContext->submitToGpu();
  64. gTime().update();
  65. gInput().update();
  66. }
  67. }
  68. void Application::updateMessagePump()
  69. {
  70. WindowEventUtilities::messagePump();
  71. }
  72. void Application::updateResourcesCallback()
  73. {
  74. gResources().update();
  75. }
  76. void Application::shutDown()
  77. {
  78. SceneManager::shutDown();
  79. if(RenderSystemManager::getActive() != nullptr)
  80. RenderSystemManager::getActive()->shutdown();
  81. HighLevelGpuProgramManager::shutDown();
  82. DynLibManager::shutDown();
  83. Resources::shutDown();
  84. Input::shutDown();
  85. Time::shutDown();
  86. }
  87. void Application::loadPlugin(const String& pluginName)
  88. {
  89. String name = pluginName;
  90. #if CM_PLATFORM == CM_PLATFORM_LINUX
  91. // dlopen() does not add .so to the filename, like windows does for .dll
  92. if (name.substr(name.length() - 3, 3) != ".so")
  93. name += ".so";
  94. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  95. // dlopen() does not add .dylib to the filename, like windows does for .dll
  96. if (name.substr(name.length() - 6, 6) != ".dylib")
  97. name += ".dylib";
  98. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  99. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  100. // if you include a relative path then it does not. So, add it to be sure.
  101. if (name.substr(name.length() - 4, 4) != ".dll")
  102. name += ".dll";
  103. #endif
  104. DynLib* library = gDynLibManager().load(name);
  105. if(library != nullptr)
  106. {
  107. typedef const void (*LoadPluginFunc)();
  108. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  109. loadPluginFunc();
  110. }
  111. }
  112. UINT64 Application::getAppWindowId()
  113. {
  114. if(!mPrimaryRenderWindow)
  115. {
  116. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  117. }
  118. UINT64 windowId = 0;
  119. mPrimaryRenderWindow->getCustomAttribute_internal("WINDOW", &windowId);
  120. return windowId;
  121. }
  122. Application& gApplication()
  123. {
  124. static Application application;
  125. return application;
  126. }
  127. }