在游戏管理器(GameManager)中维护一个消息队列std::queue(mSendMessageQueue),在GameManager::SendMessage中将消息放入队列中,如下
void GameManager::SendMessage(const Message& message){mGMImpl->mSendMessageQueue.push(dtCore::RefPtr<const Message>(&message));}
然后在每一帧的渲染过程中处理消息队列中的消息,调用所有组件的ProcessMessage函数,过程如下
void GameManager::DoSendMessages(){// PROCESS MESSAGES - Send all Process messages to components and interested actorswhile (!mGMImpl->mSendMessageQueue.empty()){mGMImpl->mGMStatistics.mStatsNumProcMessages += 1;// Forward to Components firstdtCore::RefPtr<const Message> messageRef = mGMImpl->mSendMessageQueue.front();mGMImpl->mSendMessageQueue.pop();if (!messageRef.valid()){mGMImpl->mLogger->LogMessage(dtUtil::Log::LOG_ERROR, __FUNCTION__, __LINE__,"Message in send queue is NULL. Something is majorly wrong with the GameManager.");continue;}const Message& message = *messageRef;DoSendMessage(message);}}void GameManager::DoSendMessage(const Message& message){DoSendMessageToComponents(message, false);}
void GameManager::DoSendMessageToComponents(const Message& message, bool toNetwork){GMImpl::GMComponentContainer::iterator compItr = mGMImpl->mComponentList.begin();while (compItr != mGMImpl->mComponentList.end()){ //RefPtr in case it get deleted during a Message. We need to hang onto it for a bit.dtCore::RefPtr<GMComponent>& component = *compItr; component->ProcessMessage(message); ++compItr;}}