日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Delta3d角色注册机制

發布時間:2024/8/23 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Delta3d角色注册机制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

角色注冊主要通過繼承自類dtDAL::ActorPluginRegistry類來實現,重寫其中的RegisterActorTypes()即可;在對象工廠ObjectFactory中保存了“角色類型到負責創建角色對象的全局函數”的Map;

關鍵函數有:

dtCore::RefPtr<BaseActorObject> ActorPluginRegistry::CreateActor(const ActorType& type){dtCore::RefPtr<BaseActorObject> proxy = mActorFactory->CreateObject(dtCore::RefPtr<const ActorType>(&type));proxy->Init(type);proxy->InitDefaults();return proxy;}void XXGameActorsRegistry::RegisterActorTypes() { XXActorType = new dtDAL::ActorType("XX","TutorialActors", "Simple tank that moves and acts like a basic hover craft."); mActorFactory->RegisterType<XXActorProxy>(XXActorType. get()); }

在動態鏈接庫Dll中導出全局函數:

extern "C" TUTORIAL_HOVER_EXPORT dtDAL::ActorPluginRegistry* CreatePluginRegistry() { return new TutorialGameActorsRegistry(); } / // extern "C" TUTORIAL_HOVER_EXPORT void DestroyPluginRegistry(dtDAL::ActorPluginRegistry *registry) { delete registry; }


核心了對象工廠實現代碼如下:

template<typename BaseType, typename DerivedType>BaseType *construct(){return new DerivedType();}/*** This class is a template object factory. It allows one to* create any type of object as long as there is a common base* class. The common base class is defined on a per-factory* basis using the templated parameter <code>BaseType</code>.* @note* The ObjectFactory implementation only supports objects with* a default constructor. It will not work with objects that* only have named constructors.*/template<typename UniqueIdTypeClass,typename BaseTypeClass,typename ltCmpClass=std::less<UniqueIdTypeClass> >class ObjectFactory : public osg::Referenced{public:typedef UniqueIdTypeClass UniqueIdType;typedef BaseTypeClass BaseType;typedef ltCmpClass ltCmp;typedef BaseType *(*createObjectFunc)(); /// Function pointer type for functions creating objects.typedef std::map<UniqueIdType,createObjectFunc,ltCmp> ObjectMap;typedef typename ObjectMap::iterator ObjTypeItor;typedef typename ObjectMap::const_iterator ObjTypeItorConst;ObjectFactory() {} // constructorprotected:virtual ~ObjectFactory() {}public:/*** Registers a new type of object with the factory.* @return false if the type is a duplicate.*/template<typename DerivedType>bool RegisterType(UniqueIdType id){if (this->objectTypeMap.find(id) != this->objectTypeMap.end()){return false;}this->objectTypeMap[id] = &construct<BaseType,DerivedType>;return true;}/*** Removes an existing object type from the factory's known list* of object types.*/void RemoveType(UniqueIdType id) {this->objectTypeMap.erase(id);}/*** Checks to see if the factory can create objects of the given type.* @param id The type of object to check for.* @return True if the type is supported, false otherwise.*/bool IsTypeSupported(UniqueIdType id) const{ObjTypeItorConst itor(this->objectTypeMap.find(id));if (itor != this->objectTypeMap.end()){return true;}else{return false;}}/*** Gets a list of types that this factory knows how to create.*/void GetSupportedTypes(std::vector<UniqueIdType> &types) const{types.clear();for (ObjTypeItorConst itor=this->objectTypeMap.begin();itor != this->objectTypeMap.end(); ++itor){types.push_back(itor->first);}}/*** Creates a new object.* @param id - Type of object to create.* @return Returns a pointer to the newly created object or NULL if the given id has not been registered.* @throw Exception is thrown if the factory does not know how to create* the requested type.*/BaseType* CreateObject(const UniqueIdType id) const{ObjTypeItorConst itor(this->objectTypeMap.find(id));// We cannot create a new object if we do not know what type it is// so throw an exception.if (itor == this->objectTypeMap.end()){return NULL;}return (itor->second)();}const ObjectMap& GetMap() const { return objectTypeMap; }private:///Maps a unique id to a function pointer that when called creates an///object of the appropriate type.ObjectMap objectTypeMap;};

總結

以上是生活随笔為你收集整理的Delta3d角色注册机制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。