• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            Cpper
            C/C++高級工程師 Android高級軟件工程師 IT集成工程師 音頻工程師 熟悉c,c++,java,c#,py,js,asp等多種語言 程序猿

            對于像游戲引擎的程序設計而言
            引擎的體系結構的重要性質是不言而喻的
            其中對象基類的設計也是很重要的
            主要的功能應該包含以下方面:
            1.對象類型識別
            2.對象的繼承關系識別
            3.獲取對象名字
            4.獲取對象編號
            5.附帶有智能指針
            6.當然必要的序列話作用也是必要的
            最基本的是對象引用計數類:

             1 ////////////////////////////////////////////////////////////
             2 /// 定義引擎引用計數類
             3 ////////////////////////////////////////////////////////////     
             4 class RefCount
             5 {
             6 public:
             7     ////////////////////////////////////////////////////////////
             8     /// 引用計數構造函數
             9     ////////////////////////////////////////////////////////////
            10     RefCount(): refcnt(1),auto_del(true){}
            11 
            12     ////////////////////////////////////////////////////////////
            13     /// 引用計數析構函數
            14     ////////////////////////////////////////////////////////////
            15     virtual ~RefCount(){}
            16     
            17     ////////////////////////////////////////////////////////////
            18     /// 使用自刪除機制
            19     ////////////////////////////////////////////////////////////    
            20     void SetAutoDel(bool able = true){auto_del = able;}
            21     
            22     ////////////////////////////////////////////////////////////
            23     /// 檢測是否為自刪除機制
            24     ////////////////////////////////////////////////////////////
            25     const bool GetAutoDel()const{return auto_del;}
            26     
            27     ////////////////////////////////////////////////////////////
            28     /// 增加對象引用計數
            29     ////////////////////////////////////////////////////////////
            30     void Grab()const{++refcnt;}; 
            31  
            32     ////////////////////////////////////////////////////////////
            33     /// 減少對象引用計數
            34     ////////////////////////////////////////////////////////////
            35     bool Drop() const
            36     {
            37         ASSERT(refcnt>0 && "bad refcnt number!");
            38 
            39         --refcnt;
            40         if (refcnt == 0 && auto_del == true)
            41         {
            42             delete this;
            43             return true;
            44         }
            45 
            46         return false;
            47     }
            48 
            49     ////////////////////////////////////////////////////////////
            50     /// 獲取引用計數
            51     ////////////////////////////////////////////////////////////
            52     int GetRefCnt()const{return refcnt;}; 
            53  
            54 private:
            55     mutable int  refcnt;
            56     bool    auto_del; 
            57 };

            該類的作用是對象計數的增加和減少以及獲取對象計數,是否調用對象自動刪除功能

            下面這個是rtti類
             1 ////////////////////////////////////////////////////////////
             2 /// 定義引擎實時對象識別類RTTI
             3 ////////////////////////////////////////////////////////////
             4 class G_DLL_API Rtti
             5 {
             6 public:
             7     ////////////////////////////////////////////////////////
             8     /// 必須確保引擎中對象的名字都是唯一的:)
             9     ////////////////////////////////////////////////////////
            10     Rtti(const std::string &name, const Rtti* parent);
            11     
            12     ////////////////////////////////////////////////////////
            13     /// 析構函數
            14     ////////////////////////////////////////////////////////    
            15     ~Rtti();
            16 
            17     ////////////////////////////////////////////////////////
            18     /// 獲取Rtti指定對象名字
            19     ////////////////////////////////////////////////////////    
            20     std::string GetName() const;
            21 
            22     ////////////////////////////////////////////////////////
            23     /// 獲取Rtti指定對象使用的磁盤大小 
            24     ////////////////////////////////////////////////////////    
            25     int GetDiskUsed() const;
            26 
            27     ////////////////////////////////////////////////////////
            28     /// 檢測2個rtti是不是表示同意對象
            29     ////////////////////////////////////////////////////////    
            30     bool IsExactly(const Rtti& type) const;    
            31     
            32     ////////////////////////////////////////////////////////
            33     /// 查詢Rtti指定對象的關系
            34     ////////////////////////////////////////////////////////    
            35     bool IsDerived(const Rtti& type) const;
            36 
            37 private:
            38     const std::string  name;
            39     const Rtti*  parent;
            40 };
            該類并不直接調用
            他的作用是輔助獲取對象名字以及檢測對象是不是同一類的,對象的繼承關系
             1 ////////////////////////////////////////////////////////////
             2 /// 引擎大部分對象的基類(提供對象id,名稱,rtti信息)
             3 ////////////////////////////////////////////////////////////
             4 class G_DLL_API Object: public RefCount
             5 /*#ifdef SLOT_EXT_SIGCPLUSPLUS
             6 ,public sigc::trackable
             7 #elif defined(SLOT_EXT_SIGSLOT)
             8 ,public SlotHolder
             9 #endif*/  
            10 {
            11 public:
            12     ////////////////////////////////////////////////////////
            13     /// 對象構造函數
            14     ////////////////////////////////////////////////////////    
            15     Object(); 
            16     
            17 protected:
            18     ////////////////////////////////////////////////////////
            19     /// 基類對象的純析構函數
            20     ////////////////////////////////////////////////////////
            21     virtual ~Object() = 0;    
            22     
            23 public:    
            24 
            25     ////////////////////////////////////////////////////////
            26     ///實時對象宏
            27     ////////////////////////////////////////////////////////    
            28     DECLARE_OBJECT(Object);
            29     
            30     ////////////////////////////////////////////////////////
            31     /// 檢測是否為同一對象類型
            32     ////////////////////////////////////////////////////////    
            33     bool  IsExactly(const Rtti& type) const;
            34 
            35     ////////////////////////////////////////////////////////
            36     /// 檢測是否有繼承關系
            37     ////////////////////////////////////////////////////////    
            38     bool  IsDerived(const Rtti& baseconst;
            39     
            40     ////////////////////////////////////////////////////////
            41     /// 檢測是否為指定對象實例
            42     ////////////////////////////////////////////////////////    
            43     bool  IsExactlyTypeOf(const Object* obj) const;
            44     
            45     ////////////////////////////////////////////////////////
            46     /// 檢測是否為指定對象的子類實例
            47     ////////////////////////////////////////////////////////    
            48     bool  IsDerivedTypeOf(const Object* obj) const;    
            49     
            50 public:    
            51 
            52     ////////////////////////////////////////////////////////
            53     /// 獲取對象實例名字
            54     ////////////////////////////////////////////////////////    
            55     std::string GetName() const;
            56 
            57     ////////////////////////////////////////////////////////
            58     /// 設置對象實例名字
            59     ////////////////////////////////////////////////////////    
            60     void SetName(const std::string &name);
            61     
            62     ////////////////////////////////////////////////////////
            63     /// 設置對象實例名字
            64     ////////////////////////////////////////////////////////    
            65     const uint GetObjectID()const{return id;}    
            66     
            67     ////////////////////////////////////////////////////////
            68     /// 獲取下一個ID索引
            69     ////////////////////////////////////////////////////////    
            70     static uint GetNextObjectID(){return nextid;}  
            71 
            72 public:  
            73     ////////////////////////////////////////////////////////
            74     /// 類型轉化
            75     ////////////////////////////////////////////////////////
            76     template<class T>
            77     T* CastTo() 
            78     {   
            79         return dynamic_cast<T*>(this); 
            80     }
            81     
            82     template<class T>
            83     const T* CastTo() const 
            84     {   
            85         return dynamic_cast<const T*>(this); 
            86     }
            87 
            88 protected:
            89     ////////////////////////////////////////////////////////
            90     /// 對象的對象實例名字
            91     ////////////////////////////////////////////////////////     
            92     std::string name;
            93     uint   id;
            94     static uint nextid;     
            95 };
            對象Object是引擎對象的一般基類
            該類提供了一般該有的功能同時提供了2個函數模板提供對象指針向上和向下轉換的功能

            同時配套的智能指針也是必不可少的
            如下所示:
             1 ////////////////////////////////////////////////////////
             2 /// 模板RefPtr用于基于計數的對象管理
             3 /// 所有對象都必須要從RefCount繼承之
             4 ////////////////////////////////////////////////////////
             5 template<class T>
             6 class RefPtr
             7 {
             8 public:
             9     RefPtr(T* object = NULL)
            10     {
            11         this->object = object;
            12         if(this->object)
            13            object->Grab();
            14     }
            15     
            16     RefPtr(const RefPtr<T>& other)
            17     {
            18         object = NULL;
            19         *this = other;
            20     }
            21     
            22     ~RefPtr() 
            23     {
            24         if(object)
            25            object->Drop();
            26         object = NULL;
            27     }
            28  
            29     RefPtr<T>& operator=(const RefPtr<T>& other)
            30     {
            31         if (other)
            32            other->Grab();
            33         if (object)
            34            object->Drop();
            35         object = other.get();
            36         return *this;
            37     }
            38     
            39     RefPtr& operator=(T* other)
            40     {
            41         if (other)
            42             other->Grab(); 
            43          if (object)
            44             object->Drop();
            45         object = other;
            46         return *this;
            47     }
            48     
            49     void swap(RefPtr<T>& other)
            50     {
            51         T* tmp = other.get();
            52         other = object
            53         object = tmp;
            54     }
            55     
            56     void reset(T* other){*this = other;}
            57     
            58     T* get() const {return object;}
            59     
            60     T* operator->() const 
            61     {   
            62         ASSERT(object && "bad object ptr");
            63         return object
            64     }
            65     
            66     T& operator*() const 
            67     {   
            68         ASSERT(object && "bad object ptr"); 
            69         return *object
            70     }
            71     
            72     bool operator<(const RefPtr<T>& other) const 
            73     {   
            74         return object < other.get(); 
            75     }
            76   
            77     ////////////////////////////////////////////////////////
            78     /// 檢測指針是否為空
            79     ////////////////////////////////////////////////////////
            80     operator bool() const {return object != NULL;}
            81 
            82 protected:
            83     T*   object;
            84 };
            這里的編碼并沒有按照引擎函數的命名習慣并是采用一般的智能指針函數的寫法

            同時為了簡化編碼采取了類似mfc的宏寫法
             1 //////////////////////////////////////////////////////// 
             2 /// 把對象中的實時類對象轉化為指針的宏定義
             3 ////////////////////////////////////////////////////////
             4 #define RUNTIME_RTTI(object)\
             5     (core::Rtti*)(&object::rtti##object)    
             6     
             7 //////////////////////////////////////////////////////// 
             8 /// 聲明對象的宏定義在每個引擎對象中加入
             9 //////////////////////////////////////////////////////// 
            10 #define DECLARE_OBJECT(object)\
            11 public: \
            12     static  const core::Rtti rtti##object;;\
            13     virtual const core::Rtti* GetRtti()const;\
            14     virtual std::string GetObjectName()const
            15     
            16 //////////////////////////////////////////////////////// 
            17 /// 實現對象的宏定義 
            18 //////////////////////////////////////////////////////// 
            19 #define IMPLEMENT_BASE_OBJECT(object)\
            20     const core::Rtti  object::rtti##object(#object,NULL);\
            21     inline const core::Rtti*  object::GetRtti()const\
            22     {\
            23         return  RUNTIME_RTTI(object);\
            24     }\
            25     inline std::string object::GetObjectName()const\
            26     {\
            27         return GetRtti()->GetName();\
            28     }    
            29     
            30 //////////////////////////////////////////////////////// 
            31 /// 實現對象的宏定義在每個引擎對象實現中加入
            32 //////////////////////////////////////////////////////// 
            33 #define IMPLEMENT_OBJECT(object,parent)\
            34     const core::Rtti   object::rtti##object(#object,RUNTIME_RTTI(parent));\
            35     const core::Rtti*  object::GetRtti()const\
            36     {\
            37         return  RUNTIME_RTTI(object);\
            38     }\
            39     std::string  object::GetObjectName()const\
            40     {\
            41         return  GetRtti()->GetName();\
            42     }     

            當然當前這里并沒有加入對象系列化的功能.

            當前蓋莫引擎對象基類設計主要參考了mfc,irr object,vgl以及g3d engine.
            如果設計有什么問題請提示我ccsdu2009@126.com
            posted on 2010-03-21 16:46 ccsdu2009 閱讀(1490) 評論(7)  編輯 收藏 引用 所屬分類: Game引擎
            Comments
            • # re: 蓋莫游戲引擎的對象基類設計
              陳梓瀚(vczh)
              Posted @ 2010-03-21 17:11
              我認為C++標準的rtti實現非常好,你用不找自己搞一個,反正gcc和VC++都支持。  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計
              ccsdu2009
              Posted @ 2010-03-21 17:15
              @陳梓瀚(vczh)
              那對象的動態生成呢?  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計
              陳梓瀚(vczh)
              Posted @ 2010-03-21 17:18
              @ccsdu2009
              游戲引擎應該避免這種事情  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計[未登錄]
              ccsdu2009
              Posted @ 2010-03-21 17:22
              @陳梓瀚(vczh)
              hehe  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計
              Davy.xu
              Posted @ 2010-03-21 23:56
              @陳梓瀚(vczh)
              虛幻,Gamebryo都是使用對象動態生成。而且這類反射系統對于代碼結構優化有很多好處  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計
              陳梓瀚(vczh)
              Posted @ 2010-03-24 12:54
              @Davy.xu
              既然每一個rtti對象都是你在代碼里面指定的,那跟直接寫一個類名還是factory.CreateXXX在語義上都是沒有區別的。就算你可能寫creater=factory.GetXXXCreater();creater.CreateObject()等,這也不需要創建真實的rtti對象。所以這其實只是風格問題,但是這種風格讓你的類被侵入了一些靜態對象,這經常會令VC++內置的內存泄漏檢查輸出一些噪音,非常不利于調試。  回復  更多評論   
            • # re: 蓋莫游戲引擎的對象基類設計
              Davy.xu
              Posted @ 2010-03-24 14:18
              @陳梓瀚(vczh)
              呵呵,我的RTTI跟虛幻的一樣,虛幻在一定程度上用靜態當singleton用,其實也沒什么。至于內存泄漏檢查這塊,核心模塊自己檢查下,避免出問題,核心外代碼還是用內存檢查,沒什么不妥。
              虛幻里的創建對象類似于 UObject::CreateInstance 沒有creator之類的東西,可以在引擎任何地方創建出對象  回復  更多評論   
             
            国产精品久久新婚兰兰| 午夜视频久久久久一区| 国产69精品久久久久9999APGF | 日韩亚洲欧美久久久www综合网| 久久精品中文无码资源站| 久久福利青草精品资源站| 久久精品无码一区二区三区免费| 超级97碰碰碰碰久久久久最新| 99精品久久久久久久婷婷| 久久久青草青青亚洲国产免观| 人妻少妇精品久久| 久久亚洲精品视频| 久久亚洲精品无码播放| 精品久久久久久成人AV| 亚洲精品乱码久久久久久不卡| 亚洲女久久久噜噜噜熟女| 亚洲国产精品无码久久九九| 久久精品成人国产午夜| 伊人久久无码中文字幕| 久久精品无码一区二区三区免费| 成人国内精品久久久久一区| 日韩亚洲国产综合久久久| 中文字幕亚洲综合久久| 久久丫精品国产亚洲av| 国产精品久久久久免费a∨| 久久夜色撩人精品国产| 日本三级久久网| 久久夜色精品国产噜噜噜亚洲AV | 波多野结衣中文字幕久久| 久久久久亚洲AV无码专区首JN| 国产精品久久久久乳精品爆| 国产午夜福利精品久久2021| 人妻无码中文久久久久专区| 久久人人爽人人爽人人爽| 亚洲午夜久久久| 中文字幕无码久久久| 久久亚洲精品成人无码网站| 尹人香蕉久久99天天拍| 久久亚洲AV成人无码| 久久久久亚洲精品日久生情| 久久久精品国产免大香伊|