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