1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     lyt_Common.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_LYT_COMMON_H_
19 #define NW_LYT_COMMON_H_
20 
21 #include <nw/sdk.h>
22 #include <nw/lyt/lyt_Material.h>
23 #include <nw/lyt/lyt_Resources.h>
24 #include <nw/lyt/lyt_Types.h>
25 
26 #ifdef NW_PLATFORM_CTR
27 #define NW_LYT_WRITEONLY  __writeonly
28 #else
29 #define NW_LYT_WRITEONLY
30 #endif
31 
32 // ビルドターゲットにかかわらず出力します。
33 #define NW_LYT_PRINT ::nw::os::internal::Printf
34 
35 namespace nw
36 {
37 namespace lyt
38 {
39 
40 class Material;
41 struct Size;
42 class DrawInfo;
43 class GraphicsResource;
44 class Layout;
45 class ResourceAccessor;
46 
47 //---------------------------------------------------------------------------
48 //! :category リソースアクセサ
49 //!
50 //! @brief レイアウトリソースへのポインタを持つ構造体です。
51 //!
52 //! @since 2009/09/18 初版。
53 //---------------------------------------------------------------------------
54 struct ResBlockSet
55 {
56     //! テクスチャリソースのリストです。
57     const res::TextureList* pTextureList;
58     //! フォントリソースのリストです。
59     const res::FontList* pFontList;
60     //! マテリアルリソースのリストです。
61     const res::MaterialList* pMaterialList;
62     //! リソースアクセサです。
63     ResourceAccessor* pResAccessor;
64 };
65 
66 namespace internal
67 {
68 
69 enum FrameSpecFlag
70 {
71     FRAMESPECFLAG_CONST_VERTEX  = 1 << 0,
72     FRAMESPECFLAG_FRAME         = 1 << 1,
73     FRAMESPECFLAG_SWAP          = 1 << 2,
74     FRAMESPECFLAG_RIGHT         = 1 << 3,
75     FRAMESPECFLAG_BOTTOM        = 1 << 4,
76     FRAMESPECFLAG_HFLIP         = 1 << 5,
77     FRAMESPECFLAG_VFLIP         = 1 << 6,
78 
79     FRAMESPECFLAG_FRAME_LT = FRAMESPECFLAG_CONST_VERTEX | FRAMESPECFLAG_FRAME,
80     FRAMESPECFLAG_FRAME_RT = FRAMESPECFLAG_CONST_VERTEX | FRAMESPECFLAG_FRAME | FRAMESPECFLAG_RIGHT,
81     FRAMESPECFLAG_FRAME_LB = FRAMESPECFLAG_CONST_VERTEX | FRAMESPECFLAG_FRAME | FRAMESPECFLAG_BOTTOM,
82     FRAMESPECFLAG_FRAME_RB = FRAMESPECFLAG_CONST_VERTEX | FRAMESPECFLAG_FRAME | FRAMESPECFLAG_RIGHT | FRAMESPECFLAG_BOTTOM,
83     FRAMESPECFLAG_FLIP_HFLIP = FRAMESPECFLAG_HFLIP,
84     FRAMESPECFLAG_FLIP_VFLIP = FRAMESPECFLAG_VFLIP,
85     FRAMESPECFLAG_FLIP_R90 = FRAMESPECFLAG_HFLIP | FRAMESPECFLAG_SWAP,
86     FRAMESPECFLAG_FLIP_R180 = FRAMESPECFLAG_HFLIP | FRAMESPECFLAG_VFLIP,
87     FRAMESPECFLAG_FLIP_R270 = FRAMESPECFLAG_VFLIP | FRAMESPECFLAG_SWAP,
88     FRAMESPECFLAG_NORMAL = 0
89 };
90 
91 bool    EqualsResName(const char* name1, const char* name2);
92 bool    EqualsMaterialName(const char* name1, const char* name2);
93 
94 inline const char*
GetStrTableStr(const void * pStrTable,int index)95 GetStrTableStr(const void* pStrTable, int index)
96 {
97     const u32* offsets = static_cast<const u32*>(pStrTable);
98     const char* stringPool = static_cast<const char*>(pStrTable);
99 
100     return &stringPool[offsets[index]];
101 }
102 
103 // 矩形のテクスチャ座標を複数個保持するクラスです。
104 class TexCoordAry
105 {
106 public:
107     // コンストラクタです。
108     TexCoordAry();
109 
110     // 保持可能なセット数が0か調べます。
IsEmpty()111     bool IsEmpty() const
112     {
113         return m_Cap == 0;
114     }
115 
116     // メンバーを解放し、保持可能なセット数を0に設定します。
117     void Free();
118 
119     // 指定のセット数を保持するのに十分なメモリを確保します。
120     void Reserve(u8 num);
121 
122     // 保持しているセット数を取得します。
GetSize()123     u8 GetSize() const
124     {
125         return m_Num;
126     }
127 
128     // 保持するセット数を設定し、座標を初期化します。
129     void SetSize(u8 num);
130 
131     // テクスチャ座標配列の先頭アドレスを取得します。
GetArray()132     const TexCoordQuad* GetArray() const
133     {
134         return m_pData;
135     }
136 
137     // 指定したセットのテクスチャ座標配列の先頭アドレスを取得します。
138     void GetCoord(
139         u32 idx,
140         TexCoordQuad coord) const;
141 
142     // 指定したセットのテクスチャ座標を設定します。
143     void SetCoord(
144         u32 idx,
145         const TexCoordQuad coord);
146 
147     // リソースのテクスチャ座標をコピーします。
148     void Copy(
149         const void* pResTexCoord,
150         u8 texCoordNum);
151 
152 protected:
153     //! @details :private
154     u8 m_Cap;
155 
156     //! @details :private
157     u8 m_Num;
158 
159     //! @details :private
160     math::VEC2 (*m_pData)[VERTEX_MAX];
161 };
162 
163 const ut::Color8 MultipleAlpha(
164                     const ut::Color8   col,
165                     u8                  alpha);
166 
167 void            DrawQuad(
168                     const DrawInfo&     drawInfo,
169                     const math::VEC2&   basePt,
170                     const Size&         size,
171                     u8                  texCoordNum = 0,
172                     const math::VEC2    (*texCoords)[VERTEX_MAX] = NULL,
173                     const ut::Color8*  vtxColors = NULL);
174 
175 void            DrawQuad_Repeat(
176                     const DrawInfo&     drawInfo,
177                     const math::VEC2&   basePt,
178                     const Size&         size);
179 
180 void            DrawLine(
181                     const DrawInfo&     drawInfo,
182                     const math::VEC2&   pos,
183                     const Size&         size,
184                     ut::Color8         color);
185 
186 void            FinalizeGraphics();
187 
188 inline
189 u8
GetVtxColorElement(const ut::Color8 cols[],u32 idx)190 GetVtxColorElement(
191     const ut::Color8 cols[],
192     u32             idx
193 )
194 {
195     NW_ASSERT(idx < ANIMTARGET_VERTEXCOLOR_MAX);
196 
197     return reinterpret_cast<const u8*>(&cols[idx / sizeof(ut::Color8)])[idx % sizeof(ut::Color8)];
198 }
199 
200 inline
201 void
SetVtxColorElement(ut::Color8 cols[],u32 idx,u8 value)202 SetVtxColorElement(
203     ut::Color8 cols[],
204     u32         idx,
205     u8          value
206 )
207 {
208     NW_ASSERT(idx < ANIMTARGET_VERTEXCOLOR_MAX);
209 
210     reinterpret_cast<u8*>(&cols[idx / sizeof(ut::Color8)])[idx % sizeof(ut::Color8)] = value;
211 }
212 
213 inline
214 HorizontalPosition
GetHorizontalPosition(u8 var)215 GetHorizontalPosition(u8 var)
216 {
217     return static_cast<HorizontalPosition>(var % HORIZONTALPOSITION_MAX);
218 }
219 
220 inline
221 VerticalPosition
GetVerticalPosition(u8 var)222 GetVerticalPosition(u8 var)
223 {
224     return static_cast<VerticalPosition>(var / HORIZONTALPOSITION_MAX);
225 }
226 
227 inline
228 void
SetHorizontalPosition(u8 * pVar,u8 newVal)229 SetHorizontalPosition(u8* pVar, u8 newVal)
230 {
231     NW_ASSERT(newVal < HORIZONTALPOSITION_MAX);
232 
233     *pVar = u8(GetVerticalPosition(*pVar) * HORIZONTALPOSITION_MAX + newVal);
234 }
235 
236 inline
237 void
SetVerticalPosition(u8 * pVar,u8 newVal)238 SetVerticalPosition(u8* pVar, u8 newVal)
239 {
240     NW_ASSERT(newVal < VERTICALPOSITION_MAX);
241 
242     *pVar = u8(newVal * HORIZONTALPOSITION_MAX + GetHorizontalPosition(*pVar));
243 }
244 
245 class GL
246 {
247 public:
248     static void SetTextureSamplerType(GraphicsResource& graphicsResource, int index, int value);
249     static void SetTevCombineRgb(GraphicsResource& graphicsResource, int index, TevMode value);
250     static void SetTevCombineAlpha(GraphicsResource& graphicsResource, int index, TevMode value);
251     static void SetTevSrcRgb(GraphicsResource& graphicsResource, int index, TevSrc value0, TevSrc value1, TevSrc value2);
252     static void SetTevSrcAlpha(GraphicsResource& graphicsResource, int index, TevSrc value0, TevSrc value1, TevSrc value2);
253     static void SetTevOperandRgb(GraphicsResource& graphicsResource, int index, TevOpRgb value0, TevOpRgb value1, TevOpRgb value2);
254     static void SetTevOperandAlpha(GraphicsResource& graphicsResource, int index, TevOpAlp value0, TevOpAlp value1, TevOpAlp value2);
255     static void SetTevScaleRgb(GraphicsResource& graphicsResource, int index, TevScale value);
256     static void SetTevScaleAlpha(GraphicsResource& graphicsResource, int index, TevScale value);
257     static void SetTevConstRgba(GraphicsResource& graphicsResource, int index, ut::Color8 value);
258     static void SetTevConstRgba(GraphicsResource& graphicsResource, int index, ut::Color8 value0, ut::Color8 value1);
259     static void SetTevBufferColor(GraphicsResource &graphicsResource, ut::Color8 value);
260     static void SetTevBufferInput(GraphicsResource &graphicsResource, int index, bool valueRgb, bool valueAlpha);
261     static void SetEnableAlphaTest(GraphicsResource& graphicsResource, bool value);
262     static void SetAlphaRefValue(GraphicsResource& graphicsResource, f32 value);
263     static void SetAlphaTestFunc(GraphicsResource& graphicsResource, AlphaTest value);
264     static void SetTexMinFilter(GraphicsResource& graphicsResoruce, TexFilter value);
265     static void SetTexMagFilter(GraphicsResource& graphicsResoruce, TexFilter value);
266 };
267 
268 } // namespace nw::lyt::internal
269 } // namespace nw::lyt
270 } // namespace nw
271 
272 #endif // NW_LYT_COMMON_H_
273