| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #include "CmApplication.h"
- #include "CmRenderSystem.h"
- #include "CmRenderSystemManager.h"
- #include "CmWindowEventUtilities.h"
- #include "CmHardwareBufferManager.h"
- #include "CmRenderWindow.h"
- #include "CmViewport.h"
- #include "CmVector2.h"
- #include "CmHighLevelGpuProgram.h"
- #include "CmHighLevelGpuProgramManager.h"
- #include "CmCoreObjectManager.h"
- #include "CmDynLib.h"
- #include "CmDynLibManager.h"
- #include "CmSceneManager.h"
- #include "CmImporter.h"
- #include "CmResources.h"
- #include "CmMesh.h"
- #include "CmSceneObject.h"
- #include "CmTime.h"
- #include "CmInput.h"
- #include "CmRendererManager.h"
- #include "CmMeshManager.h"
- #include "CmMaterialManager.h"
- #include "CmFontManager.h"
- #include "CmRenderWindowManager.h"
- #include "CmRenderer.h"
- #include "CmCoreThread.h"
- #include "CmMaterial.h"
- #include "CmShader.h"
- #include "CmTechnique.h"
- #include "CmPass.h"
- #include "CmCursor.h"
- #include "CmRendererManager.h"
- namespace CamelotFramework
- {
- Application::Application()
- :mPrimaryWindow(nullptr), mIsFrameRenderingFinished(true), mRunMainLoop(false)
- {
-
- }
- void Application::startUp(START_UP_DESC& desc)
- {
- MemStack::setupHeap(HID_Main);
- Time::startUp(cm_new<Time>());
- DynLibManager::startUp(cm_new<DynLibManager>());
- CoreGpuObjectManager::startUp(cm_new<CoreGpuObjectManager>());
- Resources::startUp(cm_new<Resources>(desc.resourceCacheDirectory));
- HighLevelGpuProgramManager::startUp(cm_new<HighLevelGpuProgramManager>());
- CoreThread::startUp(cm_new<CoreThread>());
- RenderSystemManager::startUp(cm_new<RenderSystemManager>());
- mPrimaryWindow = RenderSystemManager::instance().initialize(desc.renderSystem, desc.primaryWindowDesc);
- Input::startUp(cm_new<Input>());
- RendererManager::startUp(cm_new<RendererManager>());
- loadPlugin(desc.renderer);
- RendererManager::instance().setActive(desc.renderer);
- mPrimaryCoreAccessor = gCoreThread().createAccessor();
- mPrimarySyncedCoreAccessor = &gCoreThread().getSyncedAccessor();
- SceneManager::startUp((SceneManager*)loadPlugin(desc.sceneManager));
- MeshManager::startUp(cm_new<MeshManager>());
- MaterialManager::startUp(cm_new<MaterialManager>());
- FontManager::startUp(cm_new<FontManager>());
- Importer::startUp(cm_new<Importer>());
- for(auto& importerName : desc.importers)
- loadPlugin(importerName);
- loadPlugin(desc.input);
- Cursor::setCursor(CursorType::Arrow);
- }
- void Application::runMainLoop()
- {
- mRunMainLoop = true;
- while(mRunMainLoop)
- {
- RenderWindowManager::instance()._update();
- gInput().update();
- gSceneManager().update();
- if(!mainLoopCallback.empty())
- mainLoopCallback();
- RendererManager::instance().getActive()->renderAll();
- // Only queue new commands if core thread has finished rendering
- // TODO - There might be a more optimal way to sync simulation and core threads so we maximize
- // the amount of rendered frames
- bool readyForNextFrame = false;
- {
- CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
- readyForNextFrame = mIsFrameRenderingFinished;
- }
- if(readyForNextFrame)
- {
- {
- CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
- mIsFrameRenderingFinished = false;
- }
-
- gCoreThread().queueCommand(boost::bind(&Application::updateMessagePump, this));
- mPrimaryCoreAccessor->submitToCoreThread();
- gCoreThread().queueCommand(boost::bind(&Application::frameRenderingFinishedCallback, this));
- }
- else
- mPrimaryCoreAccessor->cancelAll();
- gTime().update();
- }
- }
- void Application::stopMainLoop()
- {
- mRunMainLoop = false; // No sync primitives needed, in that rare case of
- // a race condition we might run the loop one extra iteration which is acceptable
- }
- void Application::updateMessagePump()
- {
- WindowEventUtilities::messagePump();
- }
- void Application::frameRenderingFinishedCallback()
- {
- CM_LOCK_MUTEX(mFrameRenderingFinishedMutex);
- mIsFrameRenderingFinished = true;
- }
- void Application::shutDown()
- {
- mPrimaryWindow->destroy();
- mPrimaryWindow = nullptr;
- Importer::shutDown();
- FontManager::shutDown();
- MaterialManager::shutDown();
- MeshManager::shutDown();
- SceneManager::shutDown();
- RendererManager::shutDown();
- RenderSystem::shutDown();
- CoreThread::shutDown();
- Input::shutDown();
- HighLevelGpuProgramManager::shutDown();
- Resources::shutDown();
- CoreGpuObjectManager::shutDown(); // Must shut down before DynLibManager to ensure all objects are destroyed before unloading their libraries
- DynLibManager::shutDown();
- Time::shutDown();
- }
- void* Application::loadPlugin(const String& pluginName)
- {
- String name = pluginName;
- #if CM_PLATFORM == CM_PLATFORM_LINUX
- // dlopen() does not add .so to the filename, like windows does for .dll
- if (name.substr(name.length() - 3, 3) != ".so")
- name += ".so";
- #elif CM_PLATFORM == CM_PLATFORM_APPLE
- // dlopen() does not add .dylib to the filename, like windows does for .dll
- if (name.substr(name.length() - 6, 6) != ".dylib")
- name += ".dylib";
- #elif CM_PLATFORM == CM_PLATFORM_WIN32
- // Although LoadLibraryEx will add .dll itself when you only specify the library name,
- // if you include a relative path then it does not. So, add it to be sure.
- if (name.substr(name.length() - 4, 4) != ".dll")
- name += ".dll";
- #endif
- DynLib* library = gDynLibManager().load(name);
- if(library != nullptr)
- {
- typedef void* (*LoadPluginFunc)();
- LoadPluginFunc loadPluginFunc = (LoadPluginFunc)library->getSymbol("loadPlugin");
- return loadPluginFunc();
- }
- return nullptr;
- }
- UINT64 Application::getAppWindowId()
- {
- if(!mPrimaryWindow)
- {
- CM_EXCEPT(InternalErrorException, "Unable to get window handle. No active window exists!");
- }
- UINT64 windowId = 0;
- mPrimaryWindow->getCustomAttribute("WINDOW", &windowId);
- return windowId;
- }
- Application& gApplication()
- {
- static Application application;
- return application;
- }
- CoreAccessor& gMainCA()
- {
- return *gApplication().mPrimaryCoreAccessor.get();
- }
- SyncedCoreAccessor& gMainSyncedCA()
- {
- return *gApplication().mPrimarySyncedCoreAccessor;
- }
- }
|