游戏中的资源管理部分的设计(C++语言描述) (七)

2014-11-24 12:10:55 · 作者: · 浏览: 5
ger* 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 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 137 {
138 if(ANI == m_ResVector[i]->GetType() && _id == m