CmApplication.cpp 4.1 KB

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