1 /*---------------------------------------------------------------------------*
2   Project:  NintendoWare
3   File:     main.cpp
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: 23867 $
14  *---------------------------------------------------------------------------*/
15 
16 //------------------------------------------------------------------
17 // デモ: extUserData
18 //
19 // 概要
20 //   拡張ユーザデータの取得を行うデモです。
21 //
22 //   レイアウトに配置されているテキストボックスには拡張ユーザデータが設定されています。
23 //   この拡張ユーザデータを取得し、NN_LOG()にて内容を出力しています。
24 //
25 //   実機の画面上にはテキストボックスが1つ表示される以外何も行われません。
26 //
27 //------------------------------------------------------------------
28 
29 
30 #include <nw/lyt.h>
31 #include <nw/demo.h>
32 
33 namespace
34 {
35 
36 typedef nw::ut::MoveArray<u8> File;
37 
38 /*---------------------------------------------------------------------------*
39   @brief 描画の初期設定を行います。
40 
41   @param width  画面の幅。
42   @param height  画面の高さ。
43  *---------------------------------------------------------------------------*/
44 void
InitDraw(int width,int height)45 InitDraw(int width, int height)
46 {
47     // カラーバッファ情報
48     // LCDの向きに合わせて、幅と高さを入れ替えています。
49     const nw::font::ColorBufferInfo colBufInfo =
50     {
51         height, width, PICA_DATA_DEPTH24_STENCIL8_EXT
52     };
53 
54     const u32 commands[] =
55     {
56         // ビューポートの設定
57         NW_FONT_CMD_SET_VIEWPORT(0, 0, colBufInfo.width, colBufInfo.height),
58 
59         // シザー処理を無効
60         NW_FONT_CMD_SET_DISABLE_SCISSOR(colBufInfo),
61 
62         // wバッファの無効化
63         // デプスレンジの設定
64         // ポリゴンオフセットの無効化
65         NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET(
66             0.0f,           // wScale : 0.0 でWバッファが無効
67             0.0f,           // depth range near
68             1.0f,           // depth range far
69             0,              // polygon offset units : 0.0 で ポリゴンオフセットが無効
70             colBufInfo),
71     };
72 
73     nngxAdd3DCommand(commands, sizeof(commands), true);
74 
75     static const u32 constCommands[] =
76     {
77         // カリングを無効
78         NW_FONT_CMD_SET_CULL_FACE(NW_FONT_CMD_CULL_FACE_DISABLE),
79 
80         // ステンシルテストを無効
81         NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
82 
83         // デプステストを無効
84         // カラーバッファの全ての成分を書き込み可
85         NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
86             false,  // isDepthTestEnabled
87             0,      // depthFunc
88             true,   // depthMask
89             true,   // red
90             true,   // green
91             true,   // blue
92             true),  // alpha
93 
94         // アーリーデプステストを無効
95         NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST(false),
96 
97         // フレームバッファアクセス制御
98         NW_FONT_CMD_SET_FBACCESS(
99             true,   // colorRead
100             true,   // colorWrite
101             false,  // depthRead
102             false,  // depthWrite
103             false,  // stencilRead
104             false), // stencilWrite
105     };
106 
107     nngxAdd3DCommand(constCommands, sizeof(constCommands), true);
108 }
109 
110 /*---------------------------------------------------------------------------*
111   @brief モデルビュー行列と射影行列を設定します。
112 
113   @param layout レイアウトです。
114   @param drawInfo 描画情報です。
115  *---------------------------------------------------------------------------*/
116 void
SetupCamera(nw::lyt::DrawInfo & drawInfo,const nw::lyt::Layout & layout)117 SetupCamera(nw::lyt::DrawInfo& drawInfo, const nw::lyt::Layout& layout)
118 {
119     nw::ut::Rect layoutRect = layout.GetLayoutRect();
120 
121     f32 znear = 0.f;
122     f32 zfar = 500.f;
123 
124     // 射影行列を正射影に設定
125     // (Layoutデータは横向きなので向きを変換する)
126     nw::math::MTX44 projMtx;
127     nw::math::MTX44Ortho(
128         &projMtx,
129         layoutRect.bottom,  // left
130         layoutRect.top,     // right
131         -layoutRect.right,  // bottom
132         -layoutRect.left,   // top
133         znear,
134         zfar);
135     drawInfo.SetProjectionMtx(projMtx);
136 
137     // モデルビュー行列を設定
138     // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向)
139     nw::math::VEC3 pos(0, 0, 1);
140     nw::math::VEC3 up(-1, 0, 0);
141     nw::math::VEC3 target(0, 0, 0);
142 
143     nw::math::MTX34 viewMtx;
144     nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target);
145     drawInfo.SetViewMtx(viewMtx);
146 }
147 
148 /*---------------------------------------------------------------------------*
149   @brief 拡張ユーザデータを取り出し、NN_LOG() にて出力します。
150 
151   @param pPane  拡張ユーザデータが設定されているペインです。
152  *---------------------------------------------------------------------------*/
153 void
PrintExtUserData(nw::lyt::Pane * pPane)154 PrintExtUserData(nw::lyt::Pane* pPane)
155 {
156     // 拡張ユーザデータ配列の先頭ポインタの取得
157     const nw::lyt::ExtUserData *const extUserDataAry =  pPane->GetExtUserDataArray();
158 
159     NN_LOG("\n ext user data\n");
160 
161     // 拡張ユーザデータ配列の各要素を出力
162     for (u16 udIdx = 0; udIdx < pPane->GetExtUserDataNum(); ++udIdx)
163     {
164         // 拡張ユーザデータ名の表示
165         NN_LOG("    %d \"%s\" [", udIdx, extUserDataAry[udIdx].GetName());
166 
167         // 拡張ユーザデータのデータタイプに応じて処理を切り分けます。
168         switch (extUserDataAry[udIdx].GetType())
169         {
170         case nw::lyt::EXTUSERDATATYPE_STRING:             // 文字列データ
171             NN_LOG("%s", extUserDataAry[udIdx].GetString());
172             break;
173 
174         case nw::lyt::EXTUSERDATATYPE_INT:                // 整数(s32)リスト
175             {
176                 const s32 *const intList = extUserDataAry[udIdx].GetIntArray();
177                 for (int i = 0; i < extUserDataAry[udIdx].GetNum(); ++i)
178                 {
179                     NN_LOG("%d ", intList[i]);
180                 }
181             }
182             break;
183 
184         case nw::lyt::EXTUSERDATATYPE_FLOAT:              // 浮動小数点(f32)リスト
185             {
186                 const f32 *const floatList = extUserDataAry[udIdx].GetFloatArray();
187                 for (int i = 0; i < extUserDataAry[udIdx].GetNum(); ++i)
188                 {
189                     NN_LOG("%f ", floatList[i]);
190                 }
191             }
192             break;
193         }
194 
195         NN_LOG("]\n");
196     }
197 
198     NN_LOG("\n");
199 
200     // 名前による拡張ユーザデータの検索
201     const nw::lyt::ExtUserData *const pExtUserDataIntList = pPane->FindExtUserDataByName("int1");
202     NW_NULL_ASSERT(pExtUserDataIntList);
203     NN_LOG("find ext user data \"%s\"\n", pExtUserDataIntList->GetName());
204 }
205 
206 }   // namespace
207 
208 /*---------------------------------------------------------------------------*
209   @brief サンプルのメイン関数です。
210  *---------------------------------------------------------------------------*/
211 void
nnMain()212 nnMain()
213 {
214     nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance();
215     demoApp.Initialize();
216 
217     nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator());
218 
219     nw::lyt::Layout* pLayout = new nw::lyt::Layout();
220 
221     // レイアウトのバイナリリソース(アーカイブ)を読み込み。
222     File fileLayout = nw::demo::Utility::LoadFile(
223         &demoApp.GetDeviceAllocator(), NW_DEMO_FILE_PATH(L"layout.arc"), 128);
224 
225     if (fileLayout.empty())
226     {
227         NW_FATAL_ERROR("can not open layout archive.\n");
228     }
229 
230     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。
231     nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor;
232     if (!pResAccessor->Attach(fileLayout.begin(), "."))
233     {
234         NW_FATAL_ERROR("can not attach layout archive.\n");
235     }
236 
237     // レイアウトリソースの読み込み
238     {
239         const void* lytRes = pResAccessor->GetResource(0, "extUserData.bclyt");
240         NW_NULL_ASSERT(lytRes);
241         pLayout->Build(lytRes, pResAccessor);
242     }
243 
244     nw::lyt::Pane *const pExtUserDataPane = pLayout->GetRootPane()->FindPaneByName("TextBox");
245     NW_NULL_ASSERT(pExtUserDataPane);
246 
247     // 拡張ユーザデータの表示
248     PrintExtUserData(pExtUserDataPane);
249 
250     f32 animFrame = 0;
251 
252     nw::lyt::GraphicsResource graphicsResource;
253     // グローバルなリソースファイルを読み込みます。
254     {
255         graphicsResource.StartSetup();
256         const wchar_t* resourcePath = 0;
257         for (int i = 0;
258              (resourcePath = graphicsResource.GetResourcePath(i)) != NULL;
259              ++i)
260         {
261             File file = nw::demo::Utility::LoadFile(&demoApp.GetAllocator(), resourcePath);
262 
263             if (file.empty())
264             {
265                 NW_FATAL_ERROR("can not read lyt resource file.");
266             }
267 
268             graphicsResource.SetResource(i, file.begin(), file.size(), false);
269         }
270 
271         graphicsResource.FinishSetup();
272     }
273 
274     nw::lyt::DrawInfo drawInfo;
275     drawInfo.SetGraphicsResource(&graphicsResource);
276 
277     nw::lyt::Drawer drawer;
278     drawer.Initialize(graphicsResource);
279 
280     const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f);
281     const f32 clearDepth = 0.0f;
282 
283     bool loop = true;
284     while (loop)
285     {
286         demoApp.SetRenderingTarget(demoApp.DISPLAY0);
287         {
288             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
289 
290             InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT);
291 
292             SetupCamera(drawInfo, *pLayout);
293 
294             pLayout->CalculateMtx(drawInfo);
295 
296             drawer.DrawBegin(drawInfo);
297             drawer.Draw(pLayout, drawInfo);
298             drawer.DrawEnd(drawInfo);
299         }
300 
301         demoApp.SetRenderingTarget(demoApp.DISPLAY1);
302         {
303             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
304         }
305 
306         demoApp.SwapBuffer(demoApp.DISPLAY_BOTH);
307     }
308 
309     delete pLayout;
310 
311     delete pResAccessor;
312 }
313 
314