1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     ut_ResTypes.h
4 
5   Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Revision: 24024 $
14  *---------------------------------------------------------------------------*/
15 
16 #ifndef NW_UT_RESTYPES_H_
17 #define NW_UT_RESTYPES_H_
18 
19 #include <nw/types.h>
20 #include <nw/ut/ut_Inlines.h>
21 #include <nw/math/math_Types.h>
22 
23 namespace nw {
24 namespace ut {
25 
26 namespace Endian {
27     typedef union
28     {
29         u64 UInt64;
30         s64 SInt64;
31 #if defined( NW_ENABLE_FLOAT64 )
32         f64 Float64;
33 #endif
34     } Type64;
35 
36     typedef union
37     {
38         u32 UInt32;
39         s32 SInt32;
40         f32 Float32;
41     } Type32;
42 
43     typedef union
44     {
45         u16 UInt16;
46         s16 SInt16;
47     } Type16;
48 
BSwap(u64 val)49     static u64 BSwap( u64 val )
50     {
51         const u64 MASK  = 0xFF00FF00FF00FF00ULL;
52         const u64 MASK2 = 0xFFFF0000FFFF0000ULL;
53         val = ( (val & MASK) >> 8 ) | ( (val << 8) & MASK );
54         val = ( (val & MASK2) >> 16 ) | ( (val << 16) & MASK2 );
55         return (val >> 32) | (val << 32);
56     }
57 
BSwap(s64 val)58     static s64 BSwap( s64 val )
59     {
60         Type64 data;
61         data.SInt64 = val;
62         data.UInt64 = BSwap( data.UInt64 );
63         return data.SInt64;
64     }
65 
66 #if defined( NW_ENABLE_FLOAT64 )
BSwap(f64 val)67     static f64 BSwap( f64 val )
68     {
69         Type64 data;
70         data.Float64 = val;
71         data.UInt64 = BSwap( data.UInt64 );
72         return data.Float64;
73     }
74 #endif
75 
BSwap(u32 val)76     static u32 BSwap( u32 val )
77     {
78         const u32 MASK = 0xFF00FF00;
79         val = ( (val & MASK) >> 8 ) | ( (val << 8) & MASK );
80         return (val >> 16) | (val << 16);
81     }
82 
BSwap(s32 val)83     static s32 BSwap( s32 val )
84     {
85         Type32 data;
86         data.SInt32 = val;
87         data.UInt32 = BSwap( data.UInt32 );
88         return data.SInt32;
89     }
90 
BSwap(f32 val)91     static f32 BSwap( f32 val )
92     {
93         Type32 data;
94         data.Float32 = val;
95         data.UInt32 = BSwap( data.UInt32 );
96         return data.Float32;
97     }
98 
BSwap(u16 val)99     static u16 BSwap( u16 val )
100     {
101         return (u16)( (val >> 8) | (val << 8) );
102     }
103 
BSwap(s16 val)104     static s16 BSwap( s16 val )
105     {
106         return (s16)( ((u16)val >> 8) | ((u16)val << 8) );
107     }
108 } /* namespace Endian */
109 
110 // #define NW_SWAP_ENDIAN
111 
112 #if !defined( NW_SWAP_ENDIAN )
113     typedef u8  ResU8;          //!< @details :private
114     typedef s8  ResS8;          //!< @details :private
115     typedef u16 ResU16;         //!< @details :private
116     typedef s16 ResS16;         //!< @details :private
117     typedef u32 ResU32;         //!< @details :private
118     typedef s32 ResS32;         //!< @details :private
119     typedef f32 ResF32;         //!< @details :private
120     typedef u32 ResU64;         //!< @details :private
121     typedef s32 ResS64;         //!< @details :private
122     typedef f32 ResF64;         //!< @details :private
123 #else // if defined( NW_SWAP_ENDIAN )
124     template <typename T>
125     class ResNum
126     {
127     public:
ResNum()128         /* ctor */ ResNum() {}
ResNum(const ResNum & other)129         /* ctor */ ResNum(const ResNum& other) : bits(other.bits) {}
ResNum(const T val)130         /* ctor */ ResNum(const T val ) : bits( Endian::BSwap( val ) ) {}
131 
132         void operator = (const ResNum& other) { bits = other.bits; }
T()133         /* T */ operator T () const { return Endian::BSwap( bits ); }
134         void operator = (T val) { bits = Endian::BSwap( val ); }
135     private:
136         T   bits;
137     };
138 
139     typedef u8          ResU8;      //!< @details :private
140     typedef s8          ResS8;      //!< @details :private
141     typedef ResNum<u16> ResU16;     //!< @details :private
142     typedef ResNum<s16> ResS16;     //!< @details :private
143     typedef ResNum<u32> ResU32;     //!< @details :private
144     typedef ResNum<s32> ResS32;     //!< @details :private
145     typedef ResNum<f32> ResF32;     //!< @details :private
146     typedef ResNum<u64> ResU64;     //!< @details :private
147     typedef ResNum<s64> ResS64;     //!< @details :private
148     typedef ResNum<f64> ResF64;     //!< @details :private
149 #endif // define( NW_SWAP_ENDIAN )
150 
151 
152 //! @details :private
153 typedef struct BinString
154 {
155     ResS32      offset;
156 
to_ptrBinString157     const char* to_ptr() const
158     {
159         const u8* p = reinterpret_cast<const u8*>(this);
160         if ( offset != 0 ) { return reinterpret_cast<const char*>(p + offset); }
161         else { return NULL; }
162     }
163 
set_ptrBinString164     void  set_ptr(const char* ptr)
165     {
166         if (ptr == NULL) { offset = 0; }
167         else { offset = GetOffsetFromPtr(this, ptr); }
168     }
169 
170     operator const char*() const { return to_ptr(); }
171 
172 } BinString;
173 
174 
175 //! @details :private
176 typedef struct Offset
177 {
178     ResS32      offset;
179 
180 //    共用体に入れるために、コンストラクタを実装しない。
181 //    /* ctor */  Offset() : offset(0) {}
182 //    /* ctor */  Offset(s32 ofs) : offset(ofs) {}
s32_to_ofsOffset183     static Offset s32_to_ofs(s32 ofs) { return *reinterpret_cast<Offset*>(&ofs); }
184 
185     Offset      operator=(s32 ofs) { offset = ofs; return *this; }
s32Offset186     operator s32() const { return offset; }
187     Offset      operator+(s32 ofs) const { return  s32_to_ofs(this->offset + ofs); }
188     Offset      operator-(s32 ofs) const { return  s32_to_ofs(this->offset - ofs); }
189     Offset      operator+=(s32 ofs) { this->offset += ofs; return *this; }
190     Offset      operator-=(s32 ofs) { this->offset -= ofs; return *this; }
191 
set_ptrOffset192     void  set_ptr(const void* ptr)
193     {
194         if (ptr == NULL) { offset = 0; }
195         else { offset = GetOffsetFromPtr(this, ptr); }
196     }
197 
to_ptrOffset198     void* to_ptr()
199     {
200         u8* p = reinterpret_cast<u8*>(this);
201         if ( offset != 0 ) { return p + offset; }
202         else { return NULL; }
203     }
204 
to_ptrOffset205     const void* to_ptr() const
206     {
207         const u8* p = reinterpret_cast<const u8*>(this);
208         if ( offset != 0 ) { return p + offset; }
209         else { return NULL; }
210     }
211 
212     template<typename T>
to_ptrOffset213     const T* to_ptr() const
214     {
215         return static_cast<const T*>( to_ptr() );
216     }
217 
218     template<typename T>
to_ptrOffset219     T* to_ptr()
220     {
221         return static_cast<T*>( to_ptr() );
222     }
223 
224   #if 0
225     u32   table_num() const
226     {
227         return *(static_cast<const u32*>( this ) - 1);
228     }
229   #endif
230 
to_table_ptrOffset231     void* to_table_ptr()
232     {
233         return to_ptr();
234     }
235 
to_table_ptrOffset236     const void* to_table_ptr() const
237     {
238         return to_ptr();
239     }
240 } Offset;
241 
242 
243 typedef ResU32 Size;            //!< @details :private
244 typedef ResU32 Length;          //!< @details :private
245 typedef ResU32 ResTypeInfo;     //!< @details :private
246 
247 //! @details :private
248 typedef struct ResBool
249 {
250     ResS8   value;
251 
252     operator bool() const { return (value != 0)? true : false; }
253     bool operator = (bool rhs) { value = rhs; return bool(*this); }
254 } ResBool;
255 
256 
257 #if !defined( NW_SWAP_ENDIAN )
258     typedef nw::math::VEC2        ResVec2; //!< バイナリリソース上の Vector2 型です。
259     typedef nw::math::VEC3        ResVec3; //!< バイナリリソース上の Vector3 型です。
260     typedef nw::math::VEC4        ResVec4; //!< バイナリリソース上の Vector4 型です。
261     typedef nw::math::Transform2  ResTransform2; //!< バイナリリソース上の Transform2 型です。
262     typedef nw::math::Transform3  ResTransform3; //!< バイナリリソース上の Transform3 型です。
263     typedef nw::math::QUAT        ResQuaternion; //!< バイナリリソース上の Quaternion 型です。
264 
265     typedef nw::math::MTX22       ResMtx22; //!< バイナリリソース上の Matrix22 型です。
266     typedef nw::math::MTX23       ResMtx23; //!< バイナリリソース上の Matrix23 型です。
267     typedef nw::math::MTX33       ResMtx33; //!< バイナリリソース上の Matrix33 型です。
268     typedef nw::math::MTX34       ResMtx34; //!< バイナリリソース上の Matrix34 型です。
269     typedef nw::math::MTX44       ResMtx44; //!< バイナリリソース上の Matrix44 型です。
270 
271     // TODO: ResVec3とResMtx34が共用体のメンバにできないため、
272     //       暫定で追加している。将来的に統合する予定。
273     typedef nw::math::VEC3_       ResVec3_;  //!< @details :private
274     typedef nw::math::MTX34_      ResMtx34_; //!< @details :private
275 #else // if defined( NW_SWAP_ENDIAN )
276     typedef struct
277     {
278         nw::ut::ResF32 x;
279         nw::ut::ResF32 y;
280 
VEC2__anonc77a8a660408281         operator nw::math::VEC2() const { nw::math::VEC2 vec; vec.x = x; vec.y = y; return vec; }
282         void operator = (const VEC2& val) { x = val.x; y = val.y; }
283     } ResVec2;
284 
285     typedef struct
286     {
287         nw::ut::ResF32 x;
288         nw::ut::ResF32 y;
289         nw::ut::ResF32 z;
290 
VEC3__anonc77a8a660508291         operator nw::math::VEC3() const
292         {
293             nw::math::VEC3 vec; vec.x = x; vec.y = y; vec.z = z; return vec;
294         }
295         void operator = (const VEC3& val) { x = val.x; y = val.y; z = val.z; }
296     } ResVec3;
297 
298     typedef struct
299     {
300         nw::ut::ResF32 x;
301         nw::ut::ResF32 y;
302         nw::ut::ResF32 z;
303         nw::ut::ResF32 w;
304 
VEC4__anonc77a8a660608305         operator nw::math::VEC4() const
306         {
307             nw::math::VEC4 vec; vec.x = x; vec.y = y; vec.z = z; vec.w = w; return vec;
308         }
309         void operator = (const nw::math::VEC4& val) { x = val.x; y = val.y; z = val.z; w = val.w; }
310     } ResVec4;
311 
312     typedef struct
313     {
314         ResVec2     scale;
315         ResVec2     rotate;
316         ResVec2     translate;
317 
Transform2__anonc77a8a660708318         operator nw::math::Transform2() const
319         {
320             nw::math::Transform2 transform;
321             transform.scale     = this->scale;
322             transform.rotate    = this->rotate;
323             transform.translate = this->translate;
324             return transform;
325         }
326     } ResTransform2;
327 
328     typedef struct
329     {
330         ResVec3     scale;
331         ResVec3     rotate;
332         ResVec3     translate;
333 
Transform3__anonc77a8a660808334         operator nw::math::Transform3() const
335         {
336             nw::math::Transform3 transform;
337             transform.scale     = this->scale;
338             transform.rotate    = this->rotate;
339             transform.translate = this->translate;
340             return transform;
341         }
342     } ResTransform3;
343 
344 
345     typedef struct
346     {
347         ResF32 x;
348         ResF32 y;
349         ResF32 z;
350         ResF32 w;
351 
QUOT__anonc77a8a660908352         operator nw::math::QUOT() const
353         {
354             nw::math::QUOT quot;
355             quot.x = x; quot.y = y; quot.z = z; quot.w = w;
356             return quot;
357         }
358     } ResQuaternion;
359 
360 
361     typedef struct
362     {
363         union
364         {
365             struct
366             {
367                 ResF32  _00, _01;
368                 ResF32  _10, _11;
369             };
370             ResF32 m[2][2];
371             ResF32 a[4];
372         };
373 
MTX22__anonc77a8a660a08374         operator nw::math::MTX22() const
375         {
376             nw::math::MTX22 m;
377             for (int i = 0; i < 4; ++i ) { m.a[i] = this->a[i]; }
378             return m;
379         }
380     } ResMtx22;
381 
382     typedef struct
383     {
384         union
385         {
386             struct
387             {
388                 ResF32  _00, _01, _02;
389                 ResF32  _10, _11, _12;
390             };
391             ResF32 m[2][3];
392             ResF32 a[6];
393         };
394 
MTX23__anonc77a8a660d08395         operator nw::math::MTX23() const
396         {
397             nw::math::MTX22 m;
398             for (int i = 0; i < 6; ++i ) { m.a[i] = this->a[i]; }
399             return m;
400         }
401     } ResMtx23;
402 
403     typedef struct
404     {
405         union
406         {
407             struct
408             {
409                 ResF32  _00, _01, _02;
410                 ResF32  _10, _11, _12;
411                 ResF32  _20, _21, _22;
412             };
413             ResF32 m[3][3];
414             ResF32 a[9];
415         };
416 
MTX33__anonc77a8a661008417         operator nw::math::MTX33() const
418         {
419             nw::math::MTX33 m;
420             for (int i = 0; i < 9; ++i ) { m.a[i] = this->a[i]; }
421             return m;
422         }
423     } ResMtx33;
424 
425     typedef struct
426     {
427         union
428         {
429             struct
430             {
431                 ResF32  _00, _01, _02, _03;
432                 ResF32  _10, _11, _12, _13;
433                 ResF32  _20, _21, _22, _23;
434             };
435             ResF32 m[3][4];
436             ResF32 a[12];
437         };
438 
MTX34__anonc77a8a661308439         operator nw::math::MTX34() const
440         {
441             nw::math::MTX34 m;
442             for (int i = 0; i < 12; ++i ) { m.a[i] = this->a[i]; }
443             return m;
444         }
445     } ResMtx34;
446 
447     typedef struct
448     {
449         union
450         {
451             struct
452             {
453                 ResF32  _00, _01, _02, _03;
454                 ResF32  _10, _11, _12, _13;
455                 ResF32  _20, _21, _22, _23;
456                 ResF32  _30, _31, _32, _33;
457             };
458             ResF32 m[4][4];
459             ResF32 a[16];
460         };
461 
MTX44__anonc77a8a661608462         operator nw::math::MTX44() const
463         {
464             nw::math::MTX44 m;
465             for (int i = 0; i < 16; ++i ) { m.a[i] = this->a[i]; }
466             return m;
467         }
468     } ResMtx44;
469 
470 #endif
471 
472 
473 
474 } /* namespace ut */
475 } /* namespace nw */
476 
477 #endif /* NW_UT_RESTYPES_H_ */
478