class SceneBackgroundLoading : public Ogre::WorkQueue::RequestHandler, public Ogre::WorkQueue::ResponseHandler, public Ogre::FrameListener { public: SceneBackgroundLoading(void){} ~SceneBackgroundLoading(void){} Ogre::uint16 mWorkQueueChannel; Ogre::DefaultWorkQueue* mpSceneWorkQueue; enum { _EntityType, _DefaltType }; class EntityData { public: EntityData(){} ~EntityData(){} public: Ogre::uint16 mType; Ogre::String mName; Ogre::String mMeshName; Ogre::String mResGroup; Ogre::Entity* mEntity; }; public: void SceneBackgroundLoading::Initialize() { //Ogre::DefaultWorkQueueBase *pWQ = static_cast<Ogre::DefaultWorkQueueBase*>(Ogre::Root::getSingleton().getWorkQueue()); mpSceneWorkQueue = OGRE_NEW Ogre::DefaultWorkQueue("SceneBackgroundLoading"); mpSceneWorkQueue->setResponseProcessingTimeLimit(14); mpSceneWorkQueue->setWorkerThreadCount(3); mpSceneWorkQueue->setWorkersCanAccessRenderSystem(true); mpSceneWorkQueue->startup(true); mWorkQueueChannel = mpSceneWorkQueue->getChannel("SceneBackgroundLoading"); mpSceneWorkQueue->addResponseHandler(mWorkQueueChannel,this); mpSceneWorkQueue->addRequestHandler(mWorkQueueChannel,this); Ogre::Root::getSingleton().addFrameListener(this); } bool canHandleRequest(const Ogre::WorkQueue::Request* req, const Ogre::WorkQueue* srcQ){return true;} bool canHandleResponse(const Ogre::WorkQueue::Response* res, const Ogre::WorkQueue* srcQ){return true;} Ogre::uint16 AddRequest(EntityData* pData) { Ogre::WorkQueue::RequestID requestID = mpSceneWorkQueue->addRequest(mWorkQueueChannel, pData->mType, Ogre::Any(pData), 0, false); return requestID; } #define _Back_CreateEnt Ogre::WorkQueue::Response * handleRequest(const Ogre::WorkQueue::Request* req, const Ogre::WorkQueue* srcQ) { if (req->getAborted()) return OGRE_NEW Ogre::WorkQueue::Response(req, false, Ogre::Any(req->getData())); if (req->getType() == _EntityType) { EntityData* pData = Ogre::any_cast<EntityData*>(req->getData()); pData->mEntity = pData->mSceneManeger->createEntity(pData->mName, pData->mMeshName); } else if (req->getType() == _DefaltType) { } return OGRE_NEW Ogre::WorkQueue::Response(req,true,Ogre::Any(req->getData())); } void handleResponse(const Ogre::WorkQueue::Response* res, const Ogre::WorkQueue* srcQ) { if(res->getRequest()->getAborted()) return; if (res->succeeded()) { if (res->getRequest()->getType() == _EntityType) { EntityData* pData = Ogre::any_cast<EntityData*>(res->getData()); } else if (res->getRequest()->getType() == _DefaltType) { } } } public: virtual bool frameStarted(const Ogre::FrameEvent& evt) { //_update(); return true; } virtual bool frameEnded(const Ogre::FrameEvent& evt) { if (mpSceneWorkQueue) mpSceneWorkQueue->processResponses(); return true; } };
在主线程里这样初始化
g_pSceneBKLoading = new SceneBackgroundLoading;
g_pSceneBKLoading->Initialize();
.......
主线程里使用
EntityData data;
data.mType = EntityType;
data.mName = "backres1";
data.mMeshName = "ogrehead.mesh";
data.mResGroup = "General";
g_pSceneBKLoading->AddRequest(da);//增加一个线程请求,会在后台创建
后台创建完成后可以在 handleResponse里处理完成后事件,handleResponse是在主线程里执行的。