• <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>

            游戲的天空

            堅(jiān)持不懈每一天

            C++博客 聯(lián)系 聚合 管理
              7 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks
                  在游戲里,我把資源分成了 texture(紋理),sprite(精靈),Animation(動(dòng)畫), sound(聲音).  我們可以把這些資源看成一個(gè)個(gè)的類 ,那么這些資源具體指的是什么東東呢? 我來說明下:
            1.texture: 可以理解成游戲中的圖片對(duì)象.
            2.sprite:   來處理游戲中圖片的繪制的對(duì)象.
            3.Animation:來處理游戲中動(dòng)畫播放的對(duì)象,它繼承與sprite類.
            4.sound:來處理游戲中聲音播放的對(duì)象.

                   這四種資源類都繼承一個(gè)基類CresObject 這個(gè)基類 把這四種資源類的相同的數(shù)據(jù)全部抽象出來,比如,id號(hào),資源的類型. 寫到這里大家可能會(huì)問為什么這四種資源類都需要ID號(hào)和類型?因?yàn)?接下來我需要把這些資源管理起來,創(chuàng)建一個(gè)管理類,并且把這些資源用一種數(shù)據(jù)結(jié)構(gòu)保存起來,在這里我使用了STL里的vector,為什么要使用VECTOR呢?由于我做的是一款飛機(jī)小游戲,那么游戲中的資源一般不存在中間刪除某個(gè)資源,要不就是全部刪除,所以我就排除了使用list的想法,那么為什么不用map呢?這個(gè)我想VECTOR已經(jīng)足夠,使用MAP的話應(yīng)該會(huì)更方便,更直觀,因?yàn)镸AP里的每個(gè)元素都是一一對(duì)應(yīng)的,如果使用MAP 我們可以通過自己定義的資源名字,對(duì)應(yīng)資源對(duì)象,查找的時(shí)候通過資源名字快速的找到資源的對(duì)象,這樣就避免了vector便利去查找,而且還需要通過id和type的標(biāo)示來判斷,進(jìn)行查找,這樣一來MAP顯然比VECTOR好,不過由于時(shí)間的原因我也不想做太多的修改,以后把自己寫的再換成MAP來做.
                  這個(gè)管理類負(fù)責(zé)的工作是什么呢?
                  1.載入資源. 
                  2.刪除資源 
                  3.獲取資源.
                  那我們?cè)谳d入資源和 獲取資源的時(shí)候  就需要通過id號(hào)和類型 從保存資源的數(shù)據(jù)結(jié)構(gòu)中查找我們想要的資源.這時(shí)候資源的基類里的id和類型變量就派上用場(chǎng)了.

            下面我將給出具體實(shí)現(xiàn)的代碼:

            文件名: ResObject.h
             1 #ifndef RESOBJECT_H
             2 #define RESOBJECT_H
             3 #include "GlobalData.h"
             4 #include "Dx9App.h"
             5 
             6 class CResObject
             7 {
             8 public:
             9     CResObject(void);
            10     
            11     ~CResObject(void); 
            12 
            13     UINT GetId()
            14     {
            15        return m_id;
            16     }
            17 
            18     UINT GetType()
            19     {
            20        return m_type;
            21     } 
            22 
            23 protected:
            24     //資源id號(hào)
            25     UINT m_id;
            26 
            27     //資源類型
            28     UINT m_type;
            29 };
            30 
            31 #endif 
            32 

            文件名: ResObject.cpp
             1 #include "ResObject.h"
             2 
             3 CResObject::CResObject(void)
             4 {
             5 }
             6 
             7 CResObject::~CResObject(void)
             8 {
             9 }
            10 
             
            文件名:Animation.h
             1 #ifndef ANIMATION_H
             2 #define ANIMATION_H
             3 #include "DxSprite.h"
             4 #include "Dx9App.h"
             5 
             6 class CAnimation:public CDxSprite
             7 {
             8 public:
             9      CAnimation(UINT _id, CDxTexture *_tex,UINT _type, UINT _tx, UINT _ty, UINT _tw, UINT _th,
            10                 WORD _framemax, WORD _ationmax, WORD _playerspeed); 
            11       
            12     virtual ~CAnimation(void);
            13 
            14     //邏輯更新
            15     void UpData(float _time);
            16 
            17     //繪制
            18     virtual void Render(DFPOINT _pos); 
            19 
            20     virtual void  Render(DFPOINT _pos,DOUBLE _Angle); 
            21 
            22     virtual void Render(DFPOINT _pos,FLOAT _x,FLOAT _y);
            23 
            24     //播放下一幀 
            25     void NextFrame(); 
            26 
            27     //設(shè)置幀數(shù)
            28     void SetFrame(WORD _frame)
            29     {
            30         m_curframe = _frame;
            31     }
            32     
            33     //設(shè)置狀態(tài)
            34     void SetAtion(WORD _ation)
            35     {
            36         m_curation = _ation;
            37     }
            38 
            39     //設(shè)置是否開始播放
            40     void SetPlay(bool _isbegin)
            41     {
            42         m_isplay = _isbegin;
            43     }
            44 
            45     //獲取當(dāng)前楨
            46     WORD GetFrame() const
            47     {
            48         return m_curframe;
            49     } 
            50 
            51     //獲取當(dāng)前狀態(tài)
            52     WORD GetAtion() const
            53     {
            54         return m_curation;
            55     }
            56 
            57     //獲取是否播放
            58     bool GetIsPlay() const 
            59     {
            60         return m_isplay;
            61     } 
            62 protected:
            63     CAnimation(void); 
            64 private:
            65     //每一狀態(tài)的幀數(shù)的總量
            66     WORD m_framemax;
            67 
            68     //當(dāng)前幀數(shù)
            69     WORD m_curframe;
            70 
            71     //狀態(tài)的總量
            72     WORD m_ationmax;
            73 
            74     //當(dāng)前狀態(tài)
            75     WORD m_curation;
            76 
            77     //計(jì)數(shù)器
            78     UINT m_timecount;
            79 
            80     //播放的速度
            81     WORD m_playerSpeed; 
            82 
            83     //是否播放
            84     bool m_isplay;  
            85 };
            86 
            87 #endif 
             
            文件名:Animation.cpp
              1 #include "Animation.h"
              2 
              3 CAnimation::CAnimation(void)
              4 {
              5 }
              6 
              7 CAnimation::~CAnimation(void)
              8 {
              9 }
             10 
             11 CAnimation::CAnimation(UINT _id, CDxTexture *_tex,UINT _type, UINT _tx, UINT _ty, UINT _tw, UINT _th,
             12                 WORD _framemax, WORD _ationmax, WORD _playerspeed)
             13                 :CDxSprite(_id,_tex,_type,_tx,_ty,_tw,_th)
             14  {
             15          m_framemax = _framemax;
             16 
             17          m_ationmax = _ationmax;
             18 
             19          m_curframe = 0;
             20 
             21          m_curation = 0;
             22 
             23          m_playerSpeed = _playerspeed; 
             24  }
             25 
             26 void CAnimation::UpData(float _time)
             27 {
             28     NextFrame();
             29 }
             30 
             31 void CAnimation::Render(DFPOINT _pos)
             32 {
             33     //精靈的坐標(biāo)x
             34     m_position.x = _pos.x;
             35 
             36     //精靈的左邊y
             37     m_position.y = _pos.y; 
             38 
             39     //frame的變化
             40     RECT temprect;
             41 
             42     temprect.left = m_rect.left + m_curframe*m_width;
             43     
             44     temprect.right = m_rect.right + m_curframe*m_width;
             45 
             46     //ation的變化
             47     temprect.top = m_rect.top + m_curation*m_hight; 
             48 
             49     temprect.bottom = m_rect.bottom + m_curation*m_hight;
             50 
             51     if(CDx9App::GetDx9App().GetD3dSprite())
             52     {  
             53         CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&temprect,
             54                     &m_vcenter,&m_position,-1); 
             55     }
             56 }
             57 
             58 void CAnimation::Render(DFPOINT _pos,DOUBLE _Angle)
             59 {
             60         //旋轉(zhuǎn),平移矩陣
             61         D3DXMATRIX matWorld,matRotationZ,matWorld1;  
             62 
             63         CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matRotationZ);
             64 
             65         //精靈的坐標(biāo)x
             66         m_position.x = _pos.x;
             67 
             68         //精靈的左邊y
             69         m_position.y = _pos.y; 
             70 
             71         //frame的變化
             72         RECT temprect;
             73 
             74         temprect.left = m_rect.left + m_curframe*m_width;
             75          
             76         temprect.right = m_rect.right + m_curframe*m_width;
             77 
             78         //ation的變化
             79         temprect.top = m_rect.top + m_curation*m_hight; 
             80         temprect.bottom = m_rect.bottom + m_curation*m_hight;
             81 
             82         FLOAT x = (GetWidth()/2);
             83 
             84         FLOAT y = (GetHight()/2);
             85 
             86         D3DXMatrixTranslation(&matWorld,  -x,  -y,0);
             87 
             88          D3DXMatrixRotationZ(&matRotationZ,(2*PAI) - _Angle); 
             89 
             90         D3DXMatrixTranslation(&matWorld1,m_position.x,m_position.y,0);
             91          
             92         matWorld = matWorld*matRotationZ*matWorld1 ;
             93      
             94         CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matWorld); 
             95       
             96         if(CDx9App::GetDx9App().GetD3dSprite())
             97         {  
             98             CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&temprect,&m_vcenter,&D3DXVECTOR3(0,0,0),-1); 
             99         }
            100 }
            101 
            102 void CAnimation::Render(DFPOINT _pos,FLOAT _x,FLOAT _y)
            103 {
            104             //旋轉(zhuǎn),平移矩陣
            105         D3DXMATRIX matScall, matMove, matMove1,matMove2,matResult;  
            106 
            107         //精靈的坐標(biāo)x
            108         m_position.x = _pos.x;
            109 
            110         //精靈的左邊y
            111         m_position.y = _pos.y; 
            112 
            113         //frame的變化
            114         RECT temprect;
            115 
            116         temprect.left = m_rect.left + m_curframe*m_width;
            117         
            118         temprect.right = m_rect.right + m_curframe*m_width;
            119 
            120         //ation的變化
            121         temprect.top = m_rect.top + m_curation*m_hight; 
            122 
            123         temprect.bottom = m_rect.bottom + m_curation*m_hight;
            124 
            125         FLOAT x = (GetWidth()/2);
            126 
            127         FLOAT y = (GetHight()/2); 
            128         
            129         //縮放 
            130         D3DXMatrixScaling(&matScall, _x ,_y, 0); 
            131 
            132         //為了讓精靈在反轉(zhuǎn)的時(shí)候坐標(biāo)不改變做了平移處理
            133         if(_x == -1)
            134             D3DXMatrixTranslation(&matMove,GetWidth(),00); 
            135 
            136         if(_y == -1)
            137             D3DXMatrixTranslation(&matMove2,0,GetHight(), 0); 
            138 
            139         if(_x!=-1&&_y!=-1)
            140             D3DXMatrixTranslation(&matMove,0,00);
            141          
            142         //平移
            143         D3DXMatrixTranslation(&matMove1,m_position.x ,m_position.y, 0);
            144          
            145         //計(jì)算結(jié)果
            146         if(_x == -1)
            147             matResult = matScall*matMove*matMove1 ;
            148         
            149         if(_y == -1)
            150             matResult = matScall*matMove2*matMove1 ;
            151         
            152         if(_x!=-1&&_y!=-1)
            153             matResult = matScall*matMove ; 
            154         
            155         if(_x ==-1&&_y == -1)
            156             matResult = matScall*matMove*matMove2*matMove1 ;
            157 
            158         if(_x == 1&&_y == 1)
            159             matResult = matScall*matMove*matMove1;
            160         
            161         if(_x>1)
            162             matResult = matScall*matMove*matMove1;
            163         if(_y>1)
            164             matResult = matScall*matMove*matMove1;  
            165         
            166         //轉(zhuǎn)換
            167         CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matResult); 
            168          
            169         if(CDx9App::GetDx9App().GetD3dSprite())
            170         {
            171             CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&temprect,&m_vcenter,&D3DXVECTOR3(0.0,0,0),-1);
            172         }
            173 }
            174 
            175 void CAnimation::NextFrame()
            176 {
            177     if(m_isplay)
            178     {
            179         m_timecount++;
            180 
            181         if(m_timecount>1000)
            182             m_timecount = 0;
            183 
            184         if(m_timecount%m_playerSpeed == 0)
            185             m_curframe++;
            186 
            187         if(m_curframe>m_framemax-1)
            188         {
            189             m_curation++;
            190 
            191             if(m_curation>m_ationmax-1)
            192             {
            193                 m_curation = 0;
            194 
            195                 m_curframe = 0;
            196 
            197                 m_isplay = false;
            198             }
            199 
            200             m_curframe = 0;  
            201         }
            202     }
            203 }
            204 
             
            文件名:DxSprite.h
             1 #ifndef DXSPRITE_H
             2 #define DXSPRITE_H 
             3 #include "DxTexture.h"
             4 #include "Dx9App.h" 
             5 
             6 class CDxSprite: public CResObject
             7 {
             8 public
             9      
            10     //0 id 1 紋理指針 2 紋理上的x 3紋理上的y 4 需要截取的寬度 5需要截取的高度 
            11     CDxSprite(short _id, CDxTexture *_tex, UINT _type, UINT _tx, UINT _ty, UINT _tw, UINT _th); 
            12  
            13     virtual ~CDxSprite(void); 
            14 
            15     //繪制
            16     virtual void Render(DFPOINT _pos);
            17 
            18     //繪制-角度
            19     virtual void Render(DFPOINT _pos, DOUBLE _Angle);
            20 
            21     //繪制-縮放+翻轉(zhuǎn) x=-1為x軸翻轉(zhuǎn) y=-1為y軸翻轉(zhuǎn)
            22     virtual void Render(DFPOINT _pos, FLOAT _x, FLOAT _y);   
            23 
            24     //設(shè)置精靈寬
            25     void SetWidth(UINT _width)
            26     {
            27         m_width = _width;
            28     }  
            29 
            30     //設(shè)置精靈高
            31     void SetHeight(UINT _height)
            32     {
            33         m_hight = _height;
            34     }  
            35 
            36     //獲取精靈的寬
            37     UINT GetWidth() const
            38     {
            39         return m_width;
            40     }
            41 
            42     //獲取精靈的高
            43     UINT GetHight() const
            44     {
            45         return m_hight;
            46     } 
            47 
            48 protected:
            49     CDxSprite(void);  
            50      
            51     //紋理指針
            52     CDxTexture *m_tex;
            53 
            54     //矩形
            55     RECT m_rect; 
            56 
            57     D3DXVECTOR3 m_vcenter;
            58 
            59     D3DXVECTOR3 m_position; 
            60     
            61     //精靈寬 
            62     UINT m_width;
            63 
            64     //精靈高
            65     UINT m_hight;
            66 
            67     //翻轉(zhuǎn)
            68     OVERTURN m_overturn;  
            69 };
            70 
            71 #endif
            72 
             
            文件名:DXSprite.cpp
              1 #include "DxSprite.h"
              2 #include "DxTexture.h"
              3 
              4 CDxSprite::CDxSprite(void)
              5 {
              6 
              7 
              8 
              9 CDxSprite::~CDxSprite(void)
             10 
             11      m_tex = NULL;
             12 
             13 
             14 CDxSprite::CDxSprite(short _id, CDxTexture *_tex, UINT _type, UINT _tx, UINT _ty, UINT _tw, UINT _th)
             15 {  
             16         m_id = _id; 
             17          
             18         m_tex = _tex;
             19 
             20         m_type = _type;
             21 
             22         m_rect.left = _tx;
             23 
             24         m_rect.top = _ty;
             25 
             26         m_rect.right = _tx + _tw;
             27 
             28         m_rect.bottom = _ty + _th; 
             29 
             30         m_width = _tw;
             31 
             32         m_hight = _th;   
             33 
             34         m_vcenter.x = 0;
             35 
             36         m_vcenter.y = 0;
             37 
             38         m_vcenter.z = 0;
             39 
             40         m_position.x = 0;
             41 
             42         m_position.y = 0;
             43 
             44         m_position.z = 0
             45 }
             46  
             47  
             48 
             49 void CDxSprite::Render(DFPOINT _pos)
             50 
             51     //精靈的坐標(biāo)x
             52     m_position.x = _pos.x;
             53 
             54     //精靈的左邊y
             55     m_position.y = _pos.y;   
             56 
             57     if(CDx9App::GetDx9App().GetD3dSprite())
             58     {  
             59         CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&m_rect,
             60             &m_vcenter,&m_position,-1); 
             61     }
             62 }
             63 
             64 
             65 void CDxSprite:: Render(DFPOINT _pos,DOUBLE _Angle)
             66  {
             67     //旋轉(zhuǎn),平移矩陣
             68     D3DXMATRIX matWorld,matRotationZ,matWorld1;  
             69 
             70     CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matRotationZ);
             71 
             72     //精靈的坐標(biāo)x
             73     m_position.x = _pos.x;
             74 
             75     //精靈的左邊y
             76     m_position.y = _pos.y;  
             77 
             78     FLOAT x = (GetWidth()/2);
             79 
             80     FLOAT y = (GetHight()/2);
             81 
             82     D3DXMatrixTranslation(&matWorld,  -x,  -y,0);
             83 
             84      D3DXMatrixRotationZ(&matRotationZ,(2*PAI) - _Angle); 
             85 
             86     D3DXMatrixTranslation(&matWorld1,m_position.x,m_position.y,0);
             87         
             88     matWorld = matWorld*matRotationZ*matWorld1 ;
             89     
             90     CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matWorld); 
             91      
             92     if(CDx9App::GetDx9App().GetD3dSprite())
             93     {  
             94         CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&m_rect,&m_vcenter,&D3DXVECTOR3(0,0,0),-1); 
             95     }
             96  }
             97 
             98 
             99 void CDxSprite::Render(DFPOINT _pos,FLOAT _x,FLOAT _y)
            100 {
            101     //旋轉(zhuǎn),平移矩陣
            102     D3DXMATRIX matScall, matMove, matMove1,matMove2,matResult;  
            103 
            104     //精靈的坐標(biāo)x
            105     m_position.x = _pos.x;
            106 
            107     //精靈的左邊y
            108     m_position.y = _pos.y;   
            109 
            110     FLOAT x = (GetWidth()/2);
            111 
            112     FLOAT y = (GetHight()/2); 
            113     
            114     //縮放 
            115     D3DXMatrixScaling(&matScall, _x ,_y, 0); 
            116 
            117     //為了讓精靈在反轉(zhuǎn)的時(shí)候坐標(biāo)不改變做了平移處理
            118     if(_x == -1)
            119         D3DXMatrixTranslation(&matMove,GetWidth(),00); 
            120 
            121     if(_y == -1)
            122         D3DXMatrixTranslation(&matMove2,0,GetHight(), 0); 
            123 
            124     if(_x!=-1&&_y!=-1)
            125         D3DXMatrixTranslation(&matMove,0,00);
            126      
            127     //平移
            128     D3DXMatrixTranslation(&matMove1,m_position.x ,m_position.y, 0);
            129      
            130     //計(jì)算結(jié)果
            131     if(_x == -1)
            132         matResult = matScall*matMove*matMove1 ;
            133     
            134     if(_y == -1)
            135         matResult = matScall*matMove2*matMove1 ;
            136     
            137     if(_x!=-1&&_y!=-1)
            138         matResult = matScall*matMove ; 
            139     
            140     if(_x ==-1&&_y == -1)
            141         matResult = matScall*matMove*matMove2*matMove1 ;
            142 
            143     if(_x == 1&&_y == 1)
            144         matResult = matScall*matMove*matMove1;
            145     
            146     if(_x>1)
            147         matResult = matScall*matMove*matMove1;
            148     if(_y>1)
            149         matResult = matScall*matMove*matMove1;  
            150     
            151     //轉(zhuǎn)換
            152     CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matResult); 
            153      
            154     if(CDx9App::GetDx9App().GetD3dSprite())
            155     {
            156         CDx9App::GetDx9App().GetD3dSprite()->Draw(m_tex->GetTex(),&m_rect,&m_vcenter,&D3DXVECTOR3(0.0,0,0),-1);
            157     }        
            158           
            159      
            160 }
             

            文件名:DxTexture.h
             1 //紋理類
             2 #ifndef DXTEXTURE_H 
             3 #define DXTEXTURE_H 
             4  
             5 #include "ResObject.h"
             6 
             7 class CDxTexture: public CResObject
             8 {
             9 public:  
            10     //1 id 2 設(shè)備指針 3 文件路徑 4 圖片寬 5 圖片高
            11     CDxTexture(SHORT _id, UINT _type, LPDIRECT3DDEVICE9 _pDevice, LPCTSTR _pSrcFile, UINT _w, UINT _h);
            12 
            13     //返回指向紋理的指針
            14     LPDIRECT3DTEXTURE9 GetTex()
            15     {
            16         return m_ptexture;
            17     }
            18 
            19     //返回圖片寬
            20     UINT GetWidth()
            21     {
            22         return m_info.Width;
            23     }
            24 
            25     //返回圖片高
            26     UINT GetHight()
            27     {
            28         return m_info.Height;
            29     } 
            30 
            31     ~CDxTexture(void);
            32 protected:
            33 
            34 private:
            35     CDxTexture(void);
            36 
            37     //路徑
            38     LPCTSTR m_path;   
            39 
            40     //紋理指針
            41     LPDIRECT3DTEXTURE9 m_ptexture;
            42 
            43     //紋理信息
            44     D3DXIMAGE_INFO m_info;   
            45 };
            46 
            47 #endif
            48 
            49     
             
            文件名:DxTexture.cpp
             1 #include "DxTexture.h"
             2 
             3 
             4 CDxTexture::CDxTexture(void)
             5 {
             6 }
             7 
             8 CDxTexture::CDxTexture(SHORT _id, UINT _type, LPDIRECT3DDEVICE9 _pDevice,
             9                        LPCTSTR _pSrcFile, UINT _w, UINT _h )
            10 {
            11     m_path = _pSrcFile;
            12 
            13     m_id = _id; 
            14 
            15     m_type = _type;
            16 
            17     //從文件中載入紋理
            18     D3DXCreateTextureFromFileExA(_pDevice, m_path, _w, _h,1,
            19                                 D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, 
            20                                 D3DPOOL_DEFAULT, D3DX_DEFAULT, 
            21                                 D3DX_DEFAULT,  0xffff00ff,
            22                                 &m_info,NULL,&m_ptexture);  
            23     
            24     if(!m_ptexture)
            25     {
            26         MessageBox( NULL, " LPDIRECT3DTEXTURE is NULL,please check this funtion! ""Error!", MB_OK );
            27           
            28     }
            29 
            30  
            31 
            32 
            33 CDxTexture::~CDxTexture(void)
            34 {
            35     //釋放指針
            36     if(m_ptexture)
            37     {
            38          m_ptexture->Release();
            39     }
            40 
            41     if(m_path)
            42     {
            43          
            44         m_path = NULL;
            45     }
            46 }
            47 
             
            以上的3種資源類的寫法,由于游戲項(xiàng)目還未完成聲音部分資源來沒寫.目前只能提供紋理,精靈,動(dòng)畫,資源基類 的類的寫法.
            在寫這些資源類的時(shí)候,我曾經(jīng)想了一些很糾結(jié)的問題,比如DXsprite和Animation類為什么沒有合并成一個(gè)類來寫,合并在一起形成的類就會(huì)有動(dòng)態(tài)的效果和靜態(tài)的效果這樣一來 不是也很方便嗎?經(jīng)過了一些思考后我個(gè)人覺得還是分開寫出來比較好.因?yàn)?如果在游戲中所有的元素都沒有動(dòng)態(tài)的圖片效果或者很少有效果,那么我們?cè)趧?chuàng)建精靈對(duì)象的時(shí)候不就無形的浪費(fèi)了一些內(nèi)存嗎?一些和動(dòng)畫播放的變量完全不會(huì)用到.所以分開還是比較好.

            下面我將給出管理這些資源的類;

            文件名:ResManager.h

             1 #ifndef RESMANAGER_H
             2 #define RESMANAGER_H 
             3 #include "GlobalData.h"
             4 #include "DxTexture.h"
             5 #include "DxSprite.h"
             6 #include "Animation.h"
             7 #include "ResObject.h"
             8 #include "Dx9App.h"
             9 //XML
            10 #include "tinyxml.h"
            11 #include "tinystr.h"
            12 #pragma comment(lib, "XMLLIB.lib"
            13 
            14 class CResManager
            15 {
            16 public
            17     ~CResManager(void);
            18 
            19     //獲取資源管理對(duì)象指針
            20     static CResManager*  GetResManager(); 
            21      
            22     //添加資源 參數(shù)1 資源的類  參數(shù)2 項(xiàng)目了文件路徑
            23     void AddResource(TYPE_RES _type,LPCTSTR _pSrcFile); 
            24 
            25     //刪除所有資源
            26     void ReMoveAll(); 
            27 
            28     //獲取紋理對(duì)象
            29     CDxTexture& GetTex(UINT _id); 
            30 
            31     //獲取動(dòng)畫對(duì)象
            32     CAnimation& GetAnimation(UINT _id);     
            33 protected:
            34 
            35 private:
            36     CResManager(void);
            37 
            38     //存放資源
            39     vector<CResObject*> m_ResVector;   
            40 
            41     //管理類對(duì)象
            42     static CResManager* s_presmanager;
            43 
            44     //-----------------------XML部分--------------------//
            45     //父節(jié)點(diǎn)
            46     TiXmlNode* m_pnode;
            47         
            48     //子節(jié)點(diǎn)
            49     TiXmlNode* m_psubnode; 
            50 
            51     //元素
            52     TiXmlElement* m_pelement; 
            53 };
            54 
            55 #endif
            56 
            57 
             
            文件名:ResManager.cpp


             
            1 #include "ResManager.h"
            2
            3 CResManager* CResManager::s_presmanager = 0;
            4
            5 CResManager::CResManager(void)
            6 {
            7
            8 }
            9
            10
            11 CResManager::~CResManager(void)
            12 {
            13
            14 }
            15
            16 CResManager* CResManager:: GetResManager()
            17 {
            18 if(!s_presmanager)
            19 {
            20 s_presmanager = new CResManager();
            21
            22 return s_presmanager;
            23 }
            24
            25 return s_presmanager;
            26 }
            27
            28 void CResManager::AddResource(TYPE_RES _type,LPCTSTR _pSrcFile)
            29 {
            30 TiXmlDocument doc(_pSrcFile);
            31
            32 bool loadOkay = doc.LoadFile();
            33
            34 if ( !loadOkay )
            35 {
            36 MessageBox( NULL, "Can not Create TiXmlDocument!,please chect FilePath!!!", "Error!", MB_OK );
            37
            38 return ;
            39 }
            40
            41 //每一種資源的添加方式不一樣,根據(jù)不同的資源的
            42 //添加方式來創(chuàng)建資源
            43 switch(_type)
            44 {
            45 case TEX:
            46 for(m_pnode = doc.RootElement();m_pnode!=0;m_pnode = m_pnode->NextSibling())
            47 {
            48 m_pelement = m_pnode->ToElement();
            49
            50 int tmp_id = 0;
            51 int tmp_type = 0;
            52 LPCTSTR tmp_path = "";
            53 int tmp_w = 0;
            54 int tmp_h = 0;
            55
            56 m_pelement->Attribute("ID",&tmp_id);
            57 m_pelement->Attribute("Type",&tmp_type);
            58 tmp_path = m_pelement->Attribute("Pathname");
            59
            60 m_psubnode = m_pelement->FirstChild();
            61 m_pelement = m_psubnode->ToElement();
            62 m_pelement->Attribute("width",&tmp_w);
            63 m_pelement->Attribute("height",&tmp_h);
            64
            65 CDxTexture *temp = new CDxTexture(tmp_id,tmp_type,
            66 &CDx9App::GetDx9App().GetD3ddevice(),
            67 tmp_path,tmp_w,tmp_h);
            68
            69 m_ResVector.push_back(temp);
            70
            71 temp = NULL;
            72
            73 }
            74 break;
            75 case ANI:
            76 for(m_pnode = doc.RootElement();m_pnode!=0;m_pnode = m_pnode->NextSibling())
            77 {
            78 m_pelement = m_pnode->ToElement();
            79
            80 int tmp_id1 = 0;
            81 int tmp_textid1 = 0;
            82 int tmp_typ1 = 0;
            83 int tmp_tx1 = 0;
            84 int tmp_ty1 = 0;
            85 int tmp_tw = 0;
            86 int tmp_th = 0;
            87 int tmp_framemax = 0;
            88 int tmp_ationmax = 0;
            89 int tmp_PlaySpeed = 0;
            90
            91 m_pelement->Attribute("ID",&tmp_id1);
            92 m_pelement->Attribute("TexId",&tmp_textid1);
            93 m_pelement->Attribute("Type",&tmp_typ1);
            94
            95 m_psubnode = m_pelement->FirstChild();
            96 m_pelement = m_psubnode->ToElement();
            97 m_pelement->Attribute("Tx",&tmp_tx1);
            98 m_pelement->Attribute("Ty",&tmp_ty1);
            99 m_pelement->Attribute("Tw",&tmp_tw);
            100 m_pelement->Attribute("Th",&tmp_th);
            101 m_pelement->Attribute("FrameMax",&tmp_framemax);
            102 m_pelement->Attribute("AtionMax",&tmp_ationmax);
            103 m_pelement->Attribute("PlaySpeed",&tmp_PlaySpeed);
            104
            105 CAnimation *tempAnimation = new CAnimation(tmp_id1,&GetTex(tmp_textid1),tmp_typ1,tmp_tx1,tmp_ty1,
            106 tmp_tw,tmp_th,tmp_framemax,tmp_ationmax,tmp_PlaySpeed);
            107
            108 m_ResVector.push_back(tempAnimation);
            109
            110 int a = m_ResVector.size();
            111
            112 tempAnimation = NULL;
            113 }
            114
            115 break;
            116 }
            117
            118 }
            119
            120 CDxTexture& CResManager::GetTex(UINT _id)
            121 {
            122 for(int i=0;i<m_ResVector.size();i++)
            123 {
            124 if(TEX == m_ResVector[i]->GetType() && _id == m_ResVector[i]->GetId())
            125 {
            126 return *(CDxTexture*)m_ResVector[i];
            127 }
            128 }
            129
            130 MessageBox( NULL, "Can not find CDxTexture please create CDxTexture object", "Error!", MB_OK );
            131
            132 }
            133
            134 CAnimation& CResManager::GetAnimation(UINT _id)
            135 {
            136 for(int i=0;i<m_ResVector.size();i++)
            137 {
            138 if(ANI == m_ResVector[i]->GetType() && _id == m_ResVector[i]->GetId())
            139 {
            140 return *(CAnimation*)m_ResVector[i];
            141 }
            142 }
            143
            144 MessageBox( NULL, "Can not find CAnimation please create CAnimation object", "Error!", MB_OK );
            145 }
            146
            147 void CResManager::ReMoveAll()
            148 {
            149 for(int i=0;i<m_ResVector.size();i++)
            150 {
            151 delete m_ResVector[i];
            152
            153 m_ResVector[i] = NULL;
            154 }
            155
            156 m_ResVector.clear();
            157 }

            以上給出的管理類,大家可以看出來資源是用外部讀取的方法,在這里我簡(jiǎn)單的說下,資源數(shù)據(jù)讀取到底是讀取的什么呢?
            就是我們?cè)趧?chuàng)建以上資源對(duì)象的時(shí)候 所需要的數(shù)據(jù),把這些數(shù)據(jù)放在xml文件里面,這樣一來,我們程序員就不需要每次加載資源的時(shí)候自己去手寫數(shù)據(jù),手寫去創(chuàng)建資源對(duì)象,我們需要添加資源就直接在XML文件里面去添加,有多少資源就添加多少,這樣一來 就不用去修改代碼了. 我這種寫法只是一種簡(jiǎn)單的寫法,體現(xiàn)了數(shù)據(jù)驅(qū)動(dòng)程序的思想,何謂數(shù)據(jù)驅(qū)動(dòng)程序? 我們想去添加資源,只需要在外部的XML文件里面添加需要的數(shù)據(jù)就行,添加后程序就直接會(huì)自動(dòng)多載入添加的新資源,這樣一來不就形成了數(shù)據(jù)驅(qū)動(dòng)程序. 這種載入的方法可以再細(xì)化,在不同的場(chǎng)景載入不同XML文件的數(shù)據(jù)等等.不過這種方法也有弊端,如果數(shù)據(jù)過去龐大添加起來也是很痛苦的,所以在以后的游戲開發(fā)中,我們就制造出了很多工具,比如地圖編輯器,動(dòng)畫編輯器等等.

            今天就講解這多了,由于本人初學(xué)C++ 在類的細(xì)節(jié)設(shè)計(jì)上會(huì)出現(xiàn)一些問題,希望大家能指點(diǎn)指點(diǎn).希望學(xué)習(xí)游戲編成的兄弟們多多交流,大家都擁有一個(gè)open的心,把自己的對(duì)游戲制作的想法和一些創(chuàng)新的實(shí)現(xiàn)拿出來分享分享, 互相學(xué)習(xí) 互相努力,為中國(guó)游戲產(chǎn)業(yè)做出貢獻(xiàn)  本人QQ:350544011  程序員討論群QQ 45811732 希望大家能加入!

            posted on 2012-04-03 23:22 GLpro 閱讀(2174) 評(píng)論(0)  編輯 收藏 引用 所屬分類: 自制小游戲?qū)W習(xí)筆記

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            91精品国产9l久久久久| 久久久久免费看成人影片| 亚洲国产成人精品无码久久久久久综合 | 久久精品国产亚洲AV无码麻豆| 国内高清久久久久久| 狠狠色丁香久久综合婷婷| 亚洲精品乱码久久久久久蜜桃| 亚洲精品久久久www| 午夜久久久久久禁播电影| 亚洲午夜福利精品久久| 狠狠色婷婷久久一区二区| 久久综合香蕉国产蜜臀AV| 性做久久久久久久久老女人| 波多野结衣久久精品| 久久久精品久久久久久| 久久精品国产一区二区三区日韩| 色综合久久综合网观看| 99久久99久久久精品齐齐| 国产精品美女久久久免费| 久久国产精品-国产精品| 久久久久免费视频| 亚洲精品乱码久久久久久自慰| 国产V亚洲V天堂无码久久久| 久久久久久精品免费免费自慰| 婷婷国产天堂久久综合五月| 久久天天日天天操综合伊人av| 狠狠色综合久久久久尤物| 99久久精品国产毛片| 日本精品久久久久中文字幕| 亚洲国产一成久久精品国产成人综合 | 蜜臀av性久久久久蜜臀aⅴ | 久久中文字幕视频、最近更新| 亚洲午夜无码久久久久| 国产激情久久久久影院老熟女| 久久婷婷五月综合成人D啪 | 久久精品亚洲乱码伦伦中文| 精品久久人人爽天天玩人人妻| 青青青国产成人久久111网站| 欧美精品乱码99久久蜜桃| 99久久免费只有精品国产| 亚洲va国产va天堂va久久|