游戏的天空

坚持不懈每一天

C++博客 联系 聚合 管理
  7 Posts :: 0 Stories :: 4 Comments :: 0 Trackbacks
      在游戏里,我把资源分成了 texture(纹理),sprite(精灵),Animation(动画), sound(声音).  我们可以把这些资源看成一个个的类 ,那么这些资源具体指的是什么东东呢? 我来说明下:
1.texture: 可以理解成游戏中的图片对象.
2.sprite:   来处理游戏中图片的绘制的对象.
3.Animation:来处理游戏中动画播放的对象,它继承与sprite类.
4.sound:来处理游戏中声音播放的对象.

       这四种资源类都继承一个基类CresObject 这个基类 把这四种资源类的相同的数据全部抽象出来,比如,id号,资源的类型. 写到这里大家可能会问为什么这四种资源类都需要ID号和类型?因为 接下来我需要把这些资源管理起来,创建一个管理类,并且把这些资源用一种数据结构保存起来,在这里我使用了STL里的vector,为什么要使用VECTOR呢?由于我做的是一款飞机小游戏,那么游戏中的资源一般不存在中间删除某个资源,要不就是全部删除,所以我就排除了使用list的想法,那么为什么不用map呢?这个我想VECTOR已经足够,使用MAP的话应该会更方便,更直观,因为MAP里的每个元素都是一一对应的,如果使用MAP 我们可以通过自己定义的资源名字,对应资源对象,查找的时候通过资源名字快速的找到资源的对象,这样就避免了vector便利去查找,而且还需要通过id和type的标示来判断,进行查找,这样一来MAP显然比VECTOR好,不过由于时间的原因我也不想做太多的修改,以后把自己写的再换成MAP来做.
      这个管理类负责的工作是什么呢?
      1.载入资源. 
      2.删除资源 
      3.获取资源.
      那我们在载入资源和 获取资源的时候  就需要通过id号和类型 从保存资源的数据结构中查找我们想要的资源.这时候资源的基类里的id和类型变量就派上用场了.

下面我将给出具体实现的代码:

文件名: 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号
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     //设置帧数
28     void SetFrame(WORD _frame)
29     {
30         m_curframe = _frame;
31     }
32     
33     //设置状态
34     void SetAtion(WORD _ation)
35     {
36         m_curation = _ation;
37     }
38 
39     //设置是否开始播放
40     void SetPlay(bool _isbegin)
41     {
42         m_isplay = _isbegin;
43     }
44 
45     //获取当前桢
46     WORD GetFrame() const
47     {
48         return m_curframe;
49     } 
50 
51     //获取当前状态
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     //每一状态的帧数的总量
66     WORD m_framemax;
67 
68     //当前帧数
69     WORD m_curframe;
70 
71     //状态的总量
72     WORD m_ationmax;
73 
74     //当前状态
75     WORD m_curation;
76 
77     //计数器
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     //精灵的坐标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         //旋转,平移矩阵
 61         D3DXMATRIX matWorld,matRotationZ,matWorld1;  
 62 
 63         CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matRotationZ);
 64 
 65         //精灵的坐标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             //旋转,平移矩阵
105         D3DXMATRIX matScall, matMove, matMove1,matMove2,matResult;  
106 
107         //精灵的坐标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         //为了让精灵在反转的时候坐标不改变做了平移处理
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         //计算结果
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         //转换
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     //绘制-缩放+翻转 x=-1为x轴翻转 y=-1为y轴翻转
22     virtual void Render(DFPOINT _pos, FLOAT _x, FLOAT _y);   
23 
24     //设置精灵宽
25     void SetWidth(UINT _width)
26     {
27         m_width = _width;
28     }  
29 
30     //设置精灵高
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     //翻转
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     //精灵的坐标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     //旋转,平移矩阵
 68     D3DXMATRIX matWorld,matRotationZ,matWorld1;  
 69 
 70     CDx9App::GetDx9App().GetD3dSprite()->SetTransform(&matRotationZ);
 71 
 72     //精灵的坐标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     //旋转,平移矩阵
102     D3DXMATRIX matScall, matMove, matMove1,matMove2,matResult;  
103 
104     //精灵的坐标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     //为了让精灵在反转的时候坐标不改变做了平移处理
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     //计算结果
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     //转换
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 设备指针 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种资源类的写法,由于游戏项目还未完成声音部分资源来没写.目前只能提供纹理,精灵,动画,资源基类 的类的写法.
在写这些资源类的时候,我曾经想了一些很纠结的问题,比如DXsprite和Animation类为什么没有合并成一个类来写,合并在一起形成的类就会有动态的效果和静态的效果这样一来 不是也很方便吗?经过了一些思考后我个人觉得还是分开写出来比较好.因为,如果在游戏中所有的元素都没有动态的图片效果或者很少有效果,那么我们在创建精灵对象的时候不就无形的浪费了一些内存吗?一些和动画播放的变量完全不会用到.所以分开还是比较好.

下面我将给出管理这些资源的类;

文件名: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     //获取资源管理对象指针
20     static CResManager*  GetResManager(); 
21      
22     //添加资源 参数1 资源的类  参数2 项目了文件路径
23     void AddResource(TYPE_RES _type,LPCTSTR _pSrcFile); 
24 
25     //删除所有资源
26     void ReMoveAll(); 
27 
28     //获取纹理对象
29     CDxTexture& GetTex(UINT _id); 
30 
31     //获取动画对象
32     CAnimation& GetAnimation(UINT _id);     
33 protected:
34 
35 private:
36     CResManager(void);
37 
38     //存放资源
39     vector<CResObject*> m_ResVector;   
40 
41     //管理类对象
42     static CResManager* s_presmanager;
43 
44     //-----------------------XML部分--------------------//
45     //父节点
46     TiXmlNode* m_pnode;
47         
48     //子节点
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 //每一种资源的添加方式不一样,根据不同的资源的
42 //添加方式来创建资源
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 }

以上给出的管理类,大家可以看出来资源是用外部读取的方法,在这里我简单的说下,资源数据读取到底是读取的什么呢?
就是我们在创建以上资源对象的时候 所需要的数据,把这些数据放在xml文件里面,这样一来,我们程序员就不需要每次加载资源的时候自己去手写数据,手写去创建资源对象,我们需要添加资源就直接在XML文件里面去添加,有多少资源就添加多少,这样一来 就不用去修改代码了. 我这种写法只是一种简单的写法,体现了数据驱动程序的思想,何谓数据驱动程序? 我们想去添加资源,只需要在外部的XML文件里面添加需要的数据就行,添加后程序就直接会自动多载入添加的新资源,这样一来不就形成了数据驱动程序. 这种载入的方法可以再细化,在不同的场景载入不同XML文件的数据等等.不过这种方法也有弊端,如果数据过去庞大添加起来也是很痛苦的,所以在以后的游戏开发中,我们就制造出了很多工具,比如地图编辑器,动画编辑器等等.

今天就讲解这多了,由于本人初学C++ 在类的细节设计上会出现一些问题,希望大家能指点指点.希望学习游戏编成的兄弟们多多交流,大家都拥有一个open的心,把自己的对游戏制作的想法和一些创新的实现拿出来分享分享, 互相学习 互相努力,为中国游戏产业做出贡献  本人QQ:350544011  程序员讨论群QQ 45811732 希望大家能加入!

posted on 2012-04-03 23:22 GLpro 阅读(2140) 评论(0)  编辑 收藏 引用 所属分类: 自制小游戏学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理