M2的各数据结构改为使用模版方式定义后,简化了很多,真不知道,我早干吗去了~下面是两种定义方式的比较。
原定义方式
1 #ifndef __M2STRUCTURE_H__
2 #define __M2STRUCTURE_H__
3
4 #include <vector>
5
6 #include "DataTypes.h"
7 #include "FileBuffer.h"
8
9 namespace M2
10 {
11 enum StructType { ST_BASE, ST_HEADER, ST_NAME, ST_SEQUENCE, ST_ANIMATION, ST_ANIMLOOKUP,
12 ST_BONE, ST_BONELOOKUP, ST_VERTEX, ST_COLOR, ST_TEXTURE, ST_TRANSPARENCY,
13 ST_TEXANIM, ST_TEXREPLACE, ST_RENDERFLAG, ST_BONETABLE, ST_TEXLOOKUP, ST_TEXTABLE,
14 ST_TRANSLOOKUP, ST_BOUNDTRIANGLE, ST_BOUNDVERTEX, ST_BOUNDNORMAL, ST_ATTACHMENT,
15 ST_ATTACHLOOKUP, ST_ATTACHMENT2, ST_LIGHT, ST_CAMERA, ST_CAMERALOOKUP, ST_RIBBON, ST_PARTICLE};
16
17 class CBase
18 {
19 public:
20 CBase(StructType type)
21 : m_eType(type)
22 {
23 }
24 virtual ~CBase() {}
25 public:
26 StructType m_eType;
27 };
28
29 class CHeader : public CBase
30 {
31 public:
32 CHeader();
33 virtual ~CHeader();
34
35 int Read(CFileBuffer& fb);
36 public:
37 M2::Header_t m_stHeader;
38 };
39
40 class CName : public CBase
41 {
42 public:
43 CName() : CBase(ST_NAME) {}
44 virtual ~CName() {}
45
46 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
47 public:
48 std::string m_strName;
49 };
50
51 class CSequence : public CBase
52 {
53 public:
54 typedef std::vector<unsigned int> TTimestampVector;
55 public:
56 CSequence() : CBase(ST_SEQUENCE) {}
57 virtual ~CSequence() {}
58
59 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
60 public:
61 TTimestampVector m_vctTimestamp;
62 };
63
64 class CAnimation : public CBase
65 {
66 public:
67 typedef std::vector<Animation_t> TAnimationVector;
68 public:
69 CAnimation() : CBase(ST_ANIMATION) {}
70 virtual ~CAnimation() {}
71
72 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
73 public:
74 TAnimationVector m_vctAnim;
75 };
76
77 class CAnimLookup : public CBase
78 {
79 public:
80 typedef std::vector<unsigned short> TAnimIDVector;
81 public:
82 CAnimLookup() : CBase(ST_ANIMLOOKUP) {}
83 virtual ~CAnimLookup() {}
84
85 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
86 public:
87 TAnimIDVector m_vctAnimID;
88 };
89
90 class CBone : public CBase
91 {
92 public:
93 typedef std::vector<Bone_t> TBoneVector;
94 public:
95 CBone() : CBase(ST_BONE) {}
96 virtual ~CBone() {}
97
98 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
99 public:
100 TBoneVector m_vctBone;
101 };
102
103 class CBoneLookup : public CBase
104 {
105 public:
106 typedef std::vector<short> TBoneIDVector;
107 public:
108 CBoneLookup() : CBase(ST_BONELOOKUP) {}
109 virtual ~CBoneLookup() {}
110
111 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
112 public:
113 TBoneIDVector m_vctBoneID;
114 };
115
116 class CVertex : public CBase
117 {
118 public:
119 typedef std::vector<Vertex_t> TVertexVector;
120 public:
121 CVertex() : CBase(ST_VERTEX) {}
122 virtual ~CVertex() {}
123
124 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
125 public:
126 TVertexVector m_vctVertex;
127 };
128
129 //class CView;
130
131 class CColor : public CBase
132 {
133 public:
134 typedef std::vector<Color_t> TColorVector;
135 public:
136 CColor() : CBase(ST_COLOR) {}
137 virtual ~CColor() {}
138
139 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
140 public:
141 TColorVector m_vctColor;
142 };
143
144 class CTexture : public CBase
145 {
146 public:
147 typedef std::vector<Texture_t> TTexVector;
148 public:
149 CTexture() : CBase(ST_TEXTURE) {}
150 virtual ~CTexture() {}
151
152 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
153 public:
154 TTexVector m_vctTexture;
155 };
156
157 class CTransparency : public CBase
158 {
159 public:
160 typedef std::vector<Transparency_t> TTransVector;
161 public:
162 CTransparency() : CBase(ST_TRANSPARENCY) {}
163 virtual ~CTransparency() {}
164
165 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
166 public:
167 TTransVector m_vctTrans;
168 };
169
170 class CTexAnim : public CBase
171 {
172 public:
173 typedef std::vector<TextureAnim_t> TTexAnimVector;
174 public:
175 CTexAnim() : CBase(ST_TEXANIM) {}
176 virtual ~CTexAnim() {}
177
178 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
179 public:
180 TTexAnimVector m_vctTexture;
181 };
182
183 class CTexReplace : public CBase
184 {
185 public:
186 typedef std::vector<short> TTexIDVector;
187 public:
188 CTexReplace() : CBase(ST_TEXREPLACE) {}
189 virtual ~CTexReplace() {}
190
191 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
192 public:
193 TTexIDVector m_vctTexture;
194 };
195
196 class CRenderFlag : public CBase
197 {
198 public:
199 typedef std::vector<RenderFlag_t> TRenderVector;
200 public:
201 CRenderFlag() : CBase(ST_RENDERFLAG) {}
202 virtual ~CRenderFlag() {}
203
204 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
205 public:
206 TRenderVector m_vctRender;
207 };
208
209 class CBoneTable : public CBase
210 {
211 public:
212 typedef std::vector<unsigned short> TBoneVector;
213 public:
214 CBoneTable() : CBase(ST_BONETABLE) {}
215 virtual ~CBoneTable() {}
216
217 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
218 public:
219 TBoneVector m_vctBone;
220 };
221
222 class CTexLookup : public CBase
223 {
224 public:
225 typedef std::vector<unsigned short> TTexVector;
226 public:
227 CTexLookup() : CBase(ST_TEXLOOKUP) {}
228 virtual ~CTexLookup() {}
229
230 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
231 public:
232 TTexVector m_vctTex;
233 };
234
235 class CTexTable : public CBase
236 {
237 public:
238 typedef std::vector<unsigned short> TTexVector;
239 public:
240 CTexTable() : CBase(ST_TEXTABLE) {}
241 virtual ~CTexTable() {}
242
243 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
244 public:
245 TTexVector m_vctTex;
246 };
247
248 class CTransLookup : public CBase
249 {
250 public:
251 typedef std::vector<unsigned short> TTransVector;
252 public:
253 CTransLookup() : CBase(ST_TRANSLOOKUP) {}
254 virtual ~CTransLookup() {}
255
256 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
257 public:
258 TTransVector m_vctTrans;
259 };
260
261 class CTexAnimLookup : public CBase
262 {
263 public:
264 typedef std::vector<unsigned short> TTexAnimVector;
265 public:
266 CTexAnimLookup() : CBase(ST_TRANSLOOKUP) {}
267 virtual ~CTexAnimLookup() {}
268
269 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
270 public:
271 TTexAnimVector m_vctTexAnim;
272 };
273
274 class CBoundTriangle : public CBase
275 {
276 public:
277 typedef std::vector<BoundTriangle_t> TTriangleVector;
278 public:
279 CBoundTriangle() : CBase(ST_BOUNDTRIANGLE) {}
280 virtual ~CBoundTriangle() {}
281
282 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
283 public:
284 TTriangleVector m_vctTriangle;
285 };
286
287 class CBoundVertex : public CBase
288 {
289 public:
290 typedef std::vector<vec3d> TVertexVector;
291 public:
292 CBoundVertex() : CBase(ST_BOUNDVERTEX) {}
293 virtual ~CBoundVertex() {}
294
295 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
296 public:
297 TVertexVector m_vctVertex;
298 };
299
300 class CBoundNormal : public CBase
301 {
302 public:
303 typedef std::vector<vec3d> TVertexVector;
304 public:
305 CBoundNormal() : CBase(ST_BOUNDNORMAL) {}
306 virtual ~CBoundNormal() {}
307
308 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
309 public:
310 TVertexVector m_vctVertex;
311 };
312
313 class CAttachment : public CBase
314 {
315 public:
316 typedef std::vector<Attachment_t> TAttachVector;
317 public:
318 CAttachment() : CBase(ST_ATTACHMENT) {}
319 virtual ~CAttachment() {}
320
321 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
322 public:
323 TAttachVector m_vctAttach;
324 };
325
326 class CAttachLookup : public CBase
327 {
328 public:
329 typedef std::vector<unsigned short> TAttachVector;
330 public:
331 CAttachLookup() : CBase(ST_ATTACHLOOKUP) {}
332 virtual ~CAttachLookup() {}
333
334 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
335 public:
336 TAttachVector m_vctAttach;
337 };
338
339 class CAttachment2 : public CBase
340 {
341 public:
342 typedef std::vector<Attachment2_t> TAttachVector;
343 public:
344 CAttachment2() : CBase(ST_ATTACHMENT2) {}
345 virtual ~CAttachment2() {}
346
347 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
348 public:
349 TAttachVector m_vctAttach;
350 };
351
352 class CLight : public CBase
353 {
354 public:
355 typedef std::vector<Light_t> TLightVector;
356 public:
357 CLight() : CBase(ST_LIGHT) {}
358 virtual ~CLight() {}
359
360 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
361 public:
362 TLightVector m_vctLight;
363 };
364
365 class CCamera : public CBase
366 {
367 public:
368 typedef std::vector<Camera_t> TCameraVector;
369 public:
370 CCamera() : CBase(ST_CAMERA) {}
371 virtual ~CCamera() {}
372
373 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
374 public:
375 TCameraVector m_vctCamera;
376 };
377
378 class CCameraLookup : public CBase
379 {
380 public:
381 typedef std::vector<unsigned short> TCameraVector;
382 public:
383 CCameraLookup() : CBase(ST_CAMERALOOKUP) {}
384 virtual ~CCameraLookup() {}
385
386 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
387 public:
388 TCameraVector m_vctCamera;
389 };
390
391 class CRibbon : public CBase
392 {
393 public:
394 typedef std::vector<Ribbon_t> TRibbonVector;
395 public:
396 CRibbon() : CBase(ST_RIBBON) {}
397 virtual ~CRibbon() {}
398
399 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
400 public:
401 TRibbonVector m_vctRibbon;
402 };
403
404 class CParticle : public CBase
405 {
406 public:
407 typedef std::vector<Particle_t> TParticleVector;
408 public:
409 CParticle() : CBase(ST_PARTICLE) {}
410 virtual ~CParticle() {}
411
412 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size);
413 public:
414 TParticleVector m_vctParticle;
415 };
416
417
418 }
419
420 #endif
421 template定义方式
1 #ifndef __M2STRUCTURE_H__
2 #define __M2STRUCTURE_H__
3
4 #include <vector>
5
6 #include "DataTypes.h"
7 #include "FileBuffer.h"
8
9 namespace M2
10 {
11 enum StructType { ST_BASE, ST_HEADER, ST_NAME, ST_SEQUENCE, ST_ANIMATION, ST_ANIMLOOKUP,
12 ST_BONE, ST_BONELOOKUP, ST_VERTEX, ST_COLOR, ST_TEXTURE, ST_TRANSPARENCY,
13 ST_TEXANIM, ST_TEXREPLACE, ST_RENDERFLAG, ST_BONETABLE, ST_TEXLOOKUP, ST_TEXTABLE,
14 ST_TRANSLOOKUP, ST_TEXANIMLOOKUP, ST_BOUNDTRIANGLE, ST_BOUNDVERTEX, ST_BOUNDNORMAL, ST_ATTACHMENT,
15 ST_ATTACHLOOKUP, ST_ATTACHMENT2, ST_LIGHT, ST_CAMERA, ST_CAMERALOOKUP, ST_RIBBON, ST_PARTICLE};
16
17 class CBase
18 {
19 public:
20 CBase(StructType type)
21 : m_eType(type)
22 {
23 }
24 virtual ~CBase() {}
25 public:
26 StructType m_eType;
27 };
28
29
30 template<StructType ST, typename T>
31 class CSubStruct : public CBase
32 {
33 public:
34 typedef std::vector<T> TVector;
35 public:
36 CSubStruct() : CBase(ST) {}
37 virtual ~CSubStruct() {}
38
39 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size)
40 {
41 T data;
42 fb.Seek(offset);
43 for(unsigned int i = 0; i < size; ++ i)
44 {
45 data.Read(fb);
46 m_vct.push_back(data);
47 }
48 return fb.Good() ? 0 : -1;
49 }
50 public:
51 TVector m_vct;
52 };
53
54 template<>
55 class CSubStruct<ST_HEADER, Header_t> : public CBase
56 {
57 public:
58 CSubStruct() : CBase(ST_HEADER) {}
59 virtual ~CSubStruct() {}
60
61 int Read(CFileBuffer& fb)
62 {
63 return m_stHeader.Read(fb);
64 }
65 public:
66 Header_t m_stHeader;
67 };
68
69 template<>
70 class CSubStruct<ST_NAME, std::string> : public CBase
71 {
72 public:
73 CSubStruct() : CBase(ST_NAME) {}
74 virtual ~CSubStruct() {}
75
76 int Read(CFileBuffer& fb, unsigned int offset, unsigned int size)
77 {
78 fb.Seek(offset);
79 return fb.Read(m_strName);
80 }
81 public:
82 std::string m_strName;
83 };
84
85 typedef CSubStruct<ST_HEADER, Header_t> THeader;
86 typedef CSubStruct<ST_NAME, std::string> TName;
87 typedef CSubStruct<ST_SEQUENCE, UInt32_t> TSequence;
88 typedef CSubStruct<ST_ANIMATION, Animation_t> TAnimation;
89 typedef CSubStruct<ST_ANIMLOOKUP, UInt16_t> TAnimLookup;
90 typedef CSubStruct<ST_BONE, Bone_t> TBone;
91 typedef CSubStruct<ST_BONELOOKUP, Int16_t> TBoneLookup;
92 typedef CSubStruct<ST_VERTEX, Vertex_t> TVertex;
93 typedef CSubStruct<ST_COLOR, Color_t> TColor;
94 typedef CSubStruct<ST_TEXTURE, Texture_t> TTexture;
95 typedef CSubStruct<ST_TRANSPARENCY, Transparency_t> TTransparency;
96 typedef CSubStruct<ST_TEXANIM, TexAnim_t> TTexAnim;
97 typedef CSubStruct<ST_TEXREPLACE, Int16_t> TTexReplace;
98 typedef CSubStruct<ST_RENDERFLAG, RenderFlag_t> TRenderFlag;
99 typedef CSubStruct<ST_BONETABLE, UInt16_t> TBoneTable;
100 typedef CSubStruct<ST_TEXLOOKUP, UInt16_t> TTexLookup;
101 typedef CSubStruct<ST_TEXTABLE, UInt16_t> TTexTable;
102 typedef CSubStruct<ST_TRANSLOOKUP, UInt16_t> TTransLookup;
103 typedef CSubStruct<ST_TEXANIMLOOKUP, UInt16_t> TTexAnimLookup;
104 typedef CSubStruct<ST_BOUNDTRIANGLE, UInt32_t> TBoundTriangle;
105 typedef CSubStruct<ST_BOUNDVERTEX, Vec3d_t> TBoundVertex;
106 typedef CSubStruct<ST_BOUNDNORMAL, Vec3d_t> TBoundNormal;
107 typedef CSubStruct<ST_ATTACHMENT, Attachment_t> TAttachment;
108 typedef CSubStruct<ST_ATTACHLOOKUP, UInt16_t> TAttachLookup;
109 typedef CSubStruct<ST_ATTACHMENT2, Attachment2_t> TAttachment2;
110 typedef CSubStruct<ST_LIGHT, Light_t> TLight;
111 typedef CSubStruct<ST_CAMERA, Camera_t> TCamera;
112 typedef CSubStruct<ST_CAMERALOOKUP, UInt16_t> TCameraLookup;
113 typedef CSubStruct<ST_RIBBON, Ribbon_t> TRibbon;
114 typedef CSubStruct<ST_PARTICLE, Particle_t> TParticle;
115
116 }
117
118 #endif
119 三个模版就能搞定的事,我竟然写了30+个class,疯子啊~~~写了两天,活该!