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