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