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