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 // デモ: group
20 //
21 // 概要
22 //   groupによるペインの列挙を行うデモです。
23 //
24 // 操作
25 //   Aボタンでカレントグループが切り替わります。
26 //   カレントグループに属するペインの rotate が 30.0f に設定されます。
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 }   // namespace
149 
150 /*---------------------------------------------------------------------------*
151   @brief サンプルのメイン関数です。
152  *---------------------------------------------------------------------------*/
153 void
nnMain()154 nnMain()
155 {
156     nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance();
157     demoApp.Initialize();
158 
159     nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator());
160 
161     // レイアウトのバイナリリソース(アーカイブ)を読み込み。
162     File fileLayout = nw::demo::Utility::LoadFile(
163         &demoApp.GetDeviceAllocator(), NW_DEMO_FILE_PATH(L"layout.arc"), 128);
164 
165     if (fileLayout.empty())
166     {
167         NW_FATAL_ERROR("can not open layout archive.\n");
168     }
169 
170     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。
171     nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor;
172     if (!pResAccessor->Attach(fileLayout.begin(), "."))
173     {
174         NW_FATAL_ERROR("can not attach layout archive.\n");
175     }
176 
177     nw::lyt::Layout* pLayout = new nw::lyt::Layout();
178 
179     // レイアウトリソースの読み込み
180     {
181         const void* lytRes = pResAccessor->GetResource(0, "group.bclyt");
182         NW_NULL_ASSERT(lytRes);
183         pLayout->Build(lytRes, pResAccessor);
184     }
185 
186     nw::lyt::GraphicsResource graphicsResource;
187     // グローバルなリソースファイルを読み込みます。
188     {
189         graphicsResource.StartSetup();
190         const wchar_t* resourcePath = 0;
191         for (int i = 0;
192              (resourcePath = graphicsResource.GetResourcePath(i)) != NULL;
193              ++i)
194         {
195             File file = nw::demo::Utility::LoadFile(&demoApp.GetAllocator(), resourcePath);
196 
197             if (file.empty())
198             {
199                 NW_FATAL_ERROR("can not read lyt resource file.");
200             }
201 
202             graphicsResource.SetResource(i, file.begin(), file.size(), false);
203         }
204 
205         graphicsResource.FinishSetup();
206     }
207 
208     nw::lyt::DrawInfo drawInfo;
209     drawInfo.SetGraphicsResource(&graphicsResource);
210 #if ! defined(NW_RELEASE)
211     drawInfo.SetDebugDrawMode(true);
212 #endif
213 
214     nw::lyt::Drawer drawer;
215     drawer.Initialize(graphicsResource);
216 
217     static const char *const groupName[] = {
218          "GroupLeft",   // 左のグループ
219          "GroupCenter", // 中心のグループ
220          "GroupRight"   // 右のグループ
221     };
222     nw::lyt::Group* pCurrentGroup = 0;
223     int currentGroup = 0;
224 
225     pCurrentGroup = pLayout->GetGroupContainer()->FindGroupByName(groupName[currentGroup]);
226 
227     nw::demo::Pad& pad = *nw::demo::PadFactory::GetPad();
228 
229     const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f);
230     const f32 clearDepth = 0.0f;
231 
232     int loopCnt = 0;
233     bool loop = true;
234     while (loop)
235     {
236         pad.Update();
237 
238         if (pad.IsButtonDown(pad.BUTTON_A))
239         {
240             // グループを切り替える
241             currentGroup = (currentGroup + 1) % 3;
242             pCurrentGroup = pLayout->GetGroupContainer()->FindGroupByName(groupName[currentGroup]);
243         }
244 
245         // カレントグループに属するペインのrotateを 30.0f に設定
246         if (pCurrentGroup)
247         {
248             nw::lyt::PaneLinkList& paneList = pCurrentGroup->GetPaneList();
249 
250             for (nw::lyt::PaneLinkList::Iterator it = paneList.GetBeginIter(); it != paneList.GetEndIter(); ++it)
251             {
252                 it->target->SetRotate(nw::math::VEC3(0.f, 0.f, 30.f));
253             }
254         }
255 
256         // NULLペインを横方向に移動
257         {
258             nw::lyt::PaneList& paneList = pLayout->GetRootPane()->GetChildList();
259             int paneIdx = 0;
260             for (nw::lyt::PaneList::Iterator it = paneList.GetBeginIter(); it != paneList.GetEndIter(); ++it, ++paneIdx)
261             {
262                 nw::math::VEC3 trans = it->GetTranslate();
263                 f32 xVal = (loopCnt >= 128) ? static_cast<f32>(256 - loopCnt) : static_cast<f32>(loopCnt);
264                 trans.x = xVal * ((paneIdx % 2) ? -0.25f: 0.25f);
265                 it->SetTranslate(trans);
266             }
267         }
268 
269         loopCnt = (++loopCnt % 256);
270 
271         demoApp.SetRenderingTarget(demoApp.DISPLAY0);
272         {
273             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
274 
275             InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT);
276 
277             SetupCamera(drawInfo, *pLayout);
278 
279             pLayout->CalculateMtx(drawInfo);
280 
281             drawer.DrawBegin(drawInfo);
282             drawer.Draw(pLayout, drawInfo);
283             drawer.DrawEnd(drawInfo);
284         }
285 
286         demoApp.SetRenderingTarget(demoApp.DISPLAY1);
287         {
288             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
289         }
290 
291         // カレントグループに属するペインのrotateを 0.0f に戻す
292         if (pCurrentGroup)
293         {
294             nw::lyt::PaneLinkList& paneList = pCurrentGroup->GetPaneList();
295 
296             for (nw::lyt::PaneLinkList::Iterator it = paneList.GetBeginIter(); it != paneList.GetEndIter(); ++it)
297             {
298                 it->target->SetRotate(nw::math::VEC3(0.f, 0.f, 0.f));
299             }
300         }
301 
302         demoApp.SwapBuffer(demoApp.DISPLAY_BOTH);
303     }
304 
305     delete pLayout;
306 
307     delete pResAccessor;
308 }
309 
310