CmApplication.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "CmMaterial.h"
  24. #include "CmShader.h"
  25. #include "CmTechnique.h"
  26. #include "CmPass.h"
  27. #include "CmRendererManager.h"
  28. namespace CamelotEngine
  29. {
  30. Application::Application()
  31. :mPrimaryRenderWindow(nullptr)
  32. { }
  33. void Application::startUp(const String& renderSystemDll, const String& rendererDll)
  34. {
  35. Time::startUp(new Time());
  36. Input::startUp(new Input());
  37. DynLibManager::startUp(new DynLibManager());
  38. HighLevelGpuProgramManager::startUp(new HighLevelGpuProgramManager());
  39. RenderSystemManager::startUp(renderSystemDll);
  40. loadPlugin(rendererDll);
  41. RendererManager::setActive("ForwardRenderer");
  42. RenderSystem* renderSystem = RenderSystemManager::getActive();
  43. renderSystem->startUp(true, false, "Camelot Renderer");
  44. renderSystem->addPreRenderThreadUpdateCallback(boost::bind(&Application::updateResourcesCallback, this));
  45. mPrimaryDeferredRenderSystem = renderSystem->createDeferredRenderSystem();
  46. mPrimaryRenderWindow = renderSystem->createRenderWindow("Camelot Renderer", 1280, 720, false);
  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. WindowEventUtilities::messagePump();
  59. gSceneManager().update();
  60. RendererManager::getActive()->renderAll();
  61. RenderSystem* renderSystem = RenderSystemManager::getActive();
  62. renderSystem->update();
  63. gTime().update();
  64. gInput().update();
  65. }
  66. }
  67. void Application::updateResourcesCallback()
  68. {
  69. gResources().update();
  70. }
  71. void Application::shutDown()
  72. {
  73. SceneManager::shutDown();
  74. if(RenderSystemManager::getActive() != nullptr)
  75. RenderSystemManager::getActive()->shutdown();
  76. HighLevelGpuProgramManager::shutDown();
  77. DynLibManager::shutDown();
  78. Resources::shutDown();
  79. Input::shutDown();
  80. Time::shutDown();
  81. }
  82. void Application::loadPlugin(const String& pluginName)
  83. {
  84. String name = pluginName;
  85. #if CM_PLATFORM == CM_PLATFORM_LINUX
  86. // dlopen() does not add .so to the filename, like windows does for .dll
  87. if (name.substr(name.length() - 3, 3) != ".so")
  88. name += ".so";
  89. #elif CM_PLATFORM == CM_PLATFORM_APPLE
  90. // dlopen() does not add .dylib to the filename, like windows does for .dll
  91. if (name.substr(name.length() - 6, 6) != ".dylib")
  92. name += ".dylib";
  93. #elif CM_PLATFORM == CM_PLATFORM_WIN32
  94. // Although LoadLibraryEx will add .dll itself when you only specify the library name,
  95. // if you include a relative path then it does not. So, add it to be sure.
  96. if (name.substr(name.length() - 4, 4) != ".dll")
  97. name += ".dll";
  98. #endif
  99. DynLib* library = gDynLibManager().load(name);
  100. if(library != nullptr)
  101. {
  102. typedef const void (*LoadPluginFunc)();
  103. LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
  104. loadPluginFunc();
  105. }
  106. }
  107. UINT32 Application::getAppWindowId()
  108. {
  109. if(!mPrimaryRenderWindow)
  110. {
  111. CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window is set!");
  112. }
  113. UINT32 windowId;
  114. mPrimaryRenderWindow->getCustomAttribute("WINDOW", &windowId);
  115. return windowId;
  116. }
  117. Application& gApplication()
  118. {
  119. static Application application;
  120. return application;
  121. }
  122. }