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