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