CmApplication.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 = 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. renderSystem->queueCommand(boost::bind(&Application::updateMessagePump, this));
  65. renderSystem->queueCommand(boost::bind(&Application::updateResourcesCallback, this));
  66. RendererManager::getActive()->renderAll();
  67. mPrimaryRenderContext->submitToGpu();
  68. gTime().update();
  69. gInput().update();
  70. }
  71. }
  72. void Application::updateMessagePump()
  73. {
  74. WindowEventUtilities::messagePump();
  75. }
  76. void Application::updateResourcesCallback()
  77. {
  78. gResources().update();
  79. }
  80. void Application::shutDown()
  81. {
  82. SceneManager::shutDown();
  83. RenderSystem::shutDown();
  84. HighLevelGpuProgramManager::shutDown();
  85. DynLibManager::shutDown();
  86. Resources::shutDown();
  87. Input::shutDown();
  88. Time::shutDown();
  89. }
  90. void Application::loadPlugin(const String& pluginName)
  91. {
  92. String name = pluginName;
  93. #if CM_PLATFORM == CM_PLATFORM_LINUX
  94. // dlopen() does not add .so to the filename, like windows does for .dll
  95. if (name.substr(name.length() - 3, 3) != ".so")
  96. name += ".so";
  97. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  98. // dlopen() does not add .dylib to the filename, like windows does for .dll
  99. if (name.substr(name.length() - 6, 6) != ".dylib")
  100. name += ".dylib";
  101. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  102. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  103. // if you include a relative path then it does not. So, add it to be sure.
  104. if (name.substr(name.length() - 4, 4) != ".dll")
  105. name += ".dll";
  106. #endif
  107. DynLib* library = gDynLibManager().load(name);
  108. if(library != nullptr)
  109. {
  110. typedef const void (*LoadPluginFunc)();
  111. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  112. loadPluginFunc();
  113. }
  114. }
  115. UINT64 Application::getAppWindowId()
  116. {
  117. if(!mPrimaryRenderWindow)
  118. {
  119. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  120. }
  121. UINT64 windowId = 0;
  122. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  123. return windowId;
  124. }
  125. Application& gApplication()
  126. {
  127. static Application application;
  128. return application;
  129. }
  130. }