游戏引擎中的通用编程技术
2009-10-31 22:55
EXPOSE_INTERFACE( CPlayer , IPlayer , PALYER_VERSION_NAME );
如果在其他模块内你需要获得这个接口,可以这么做
CreateInterfaceFn factory = reinterpret_cast (GetProcAddress( hDLL , CREATEINTERFACE_PROCNAME ));
IPlayer player = factory( PLAYER_VERSION_NAME , 0 );
其中hDLL为模块的句柄。这里函数指针factory实际指向模块内部的CreateInterface函数,这个
函数通过比较传入的接口名从链表找到指定类指针。

解决了类厂问题,下面让我们看看如何建立模块对外的接口,在Game Programming Gems3的
《一个基于对象组合的游戏架构》一文提出了一种架构,Half Life2引擎中对这种架构进行了有效
的扩展,你可以让所有的对外暴露的接口都使用这个架构,前提是模块只有一个接口对外暴露。
class IAppSystem
{
public:
// Here's where the app systems get to learn about each other
virtual bool Connect( CreateInterfaceFn factory ) = 0;
virtual void Disconnect() = 0;

// Here's where systems can access other interfaces implemented by this object
// Returns NULL if it doesn't implement the requested interface
virtual void *QueryInterface( const char *pInterfaceName ) = 0;

// Init, shutdown

相关文章: