一个简单的C++延迟调用系统

本文介绍了一个简单的游戏对象事件调度系统实现。该系统使用Boost库,通过标准模板库(map)管理事件,支持事件调度、取消及检查事件是否活跃等功能。文章详细展示了如何在游戏开发中实现并维护对象级别的事件调度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// object.h

#include <boost/function.hpp>

#include <boost/bind.hpp>

#include <map>

#include <string>

 

class FObject

{

public:

typedef boost::function<void()>  EventFunction;

struct EventParam

{

std::string   strEvent;

EventFunction eventFunc;

float         fDeltaTime;

mutable float CurrentTime;

bool          bLoop;

};

 

typedef std::map<std::string, EventParam>   EventMap;

 

public:

virtual ~FObject();

virtual void                Tick( float fDeltaTime );

 

public:

void                        ScheduleEvent( const std::string& strEvent, float fDeltaTime, bool bLoop, EventFunction EventFunc );

bool                        IsEventActive( const std::string& strEvent ) const;

void                        KillEvent( const std::string& strEvent );

 

#define SCHEDULE_EVENT( DeltaTime, bLoop, nameFunc ) \

ScheduleEvent( #nameFunc, DeltaTime, bLoop, boost::bind( nameFunc, this ) )

 

#define SCHEDULE_KILL( nameFunc ) \

KillEvent( #nameFunc )

 

private:

EventMap                    m_Events;

};

 

// object.cpp

#include "Object.h"

 

FObject::~FObject()

{

}

 

void FObject::Tick( float fDeltaTime )

{

for( EventMap::iterator it = m_Events.begin();

it != m_Events.end();

)

{

it->second.CurrentTime -= fDeltaTime;

 

if( it->second.CurrentTime <= 0.0f )

{

if( it->second.eventFunc )

{

it->second.eventFunc();

}

 

if( !it->second.bLoop )

{

it = m_Events.erase(it);

}

else

{

it->second.CurrentTime = it->second.fDeltaTime;

++it;  // this is very important

}

}

else

{

++it; // this is very important

}

}

}

 

void FObject::ScheduleEvent( const std::string& strEvent, float fDeltaTime, bool bLoop, EventFunction EventFunc )

{

EventParam Param;

Param.strEvent = strEvent;

Param.fDeltaTime = fDeltaTime;

Param.CurrentTime = fDeltaTime;

Param.eventFunc = EventFunc;

Param.bLoop = bLoop;

 

EventMap::iterator itPos = m_Events.find( strEvent );

 

if( itPos != m_Events.end() )

{

itPos->second = Param;

}

else

{

m_Events.insert( std::make_pair( strEvent, Param ) );

}

}

 

bool FObject::IsEventActive( const std::string& strEvent ) const

{

return m_Events.find( strEvent ) != m_Events.end();

}

 

void FObject::KillEvent( const std::string& strEvent )

{

EventMap::iterator itPos = m_Events.find( strEvent );

 

if( itPos != m_Events.end() )

{

m_Events.erase(itPos);

}

}

 

      // 够简单吧,这个只是把函数对象保存下来的办法。通常只应该用在本Object身上,这样当本Object失效之后所有的延迟调用都会失效。缺陷就是每个Object都需要被tick才可以支持。可选的方案还可以创建一个用于统一管理的ScheduleManager,这样只需要tick manager就可以了,这个方案的缺陷可能会调用到已经不存在的Object的方法导致崩溃。

      这个姑且如此,能用了。先就这样,有问题再说。嘿嘿。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值