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