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