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 // デモ: simple
20 //
21 // 概要
22 //   レイアウトデータの表示とアニメーションの再生を行うデモです。
23 //   レイアウトデータ、アニメーションデータは次のものが含まれています。
24 //
25 //      simple.clyt ... レイアウトデータ
26 //      simple.clan ... アニメーションデータ
27 //                        - ペインのSRTアニメーション
28 //                        - テクスチャパターンアニメーション
29 //                        - ビジビリティアニメーション
30 //                        - 頂点カラーアニメーション
31 //                        - マテリアルカラーアニメーション
32 //                        - テクスチャSRTアニメーション
33 //
34 //------------------------------------------------------------------
35 
36 #include <GLES2/gl2.h>
37 
38 #include <nn.h>
39 #include <nn/fs.h>
40 #include <nw/sdk.h>
41 #include <nw/demo.h>
42 #include <nw/lyt.h>
43 
44 namespace
45 {
46 
47 /*---------------------------------------------------------------------------*
48   @brief 描画の初期設定を行います。
49 
50   @param width  画面の幅。
51   @param height  画面の高さ。
52  *---------------------------------------------------------------------------*/
53 void
InitDraw(int width,int height)54 InitDraw(int width, int height)
55 {
56     // カラーバッファ情報
57     // LCDの向きに合わせて、幅と高さを入れ替えています。
58     const nw::font::ColorBufferInfo colBufInfo =
59     {
60         height, width, PICA_DATA_DEPTH24_STENCIL8_EXT
61     };
62 
63     const u32 commands[] =
64     {
65         // ビューポートの設定
66         NW_FONT_CMD_SET_VIEWPORT(0, 0, colBufInfo.width, colBufInfo.height),
67 
68         // シザー処理を無効
69         NW_FONT_CMD_SET_DISABLE_SCISSOR(colBufInfo),
70 
71         // wバッファの無効化
72         // デプスレンジの設定
73         // ポリゴンオフセットの無効化
74         NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET(
75             0.0f,           // wScale : 0.0 でWバッファが無効
76             0.0f,           // depth range near
77             1.0f,           // depth range far
78             0,              // polygon offset units : 0.0 で ポリゴンオフセットが無効
79             colBufInfo),
80     };
81 
82     nngxAdd3DCommand(commands, sizeof(commands), true);
83 
84     static const u32 constCommands[] =
85     {
86         // カリングを無効
87         NW_FONT_CMD_SET_CULL_FACE(NW_FONT_CMD_CULL_FACE_DISABLE),
88 
89         // ステンシルテストを無効
90         NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
91 
92         // デプステストを無効
93         // カラーバッファの全ての成分を書き込み可
94         NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
95             false,  // isDepthTestEnabled
96             0,      // depthFunc
97             true,   // depthMask
98             true,   // red
99             true,   // green
100             true,   // blue
101             true),  // alpha
102 
103         // アーリーデプステストを無効
104         NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST(false),
105 
106         // フレームバッファアクセス制御
107         NW_FONT_CMD_SET_FBACCESS(
108             true,   // colorRead
109             true,   // colorWrite
110             false,  // depthRead
111             false,  // depthWrite
112             false,  // stencilRead
113             false), // stencilWrite
114     };
115 
116     nngxAdd3DCommand(constCommands, sizeof(constCommands), true);
117 }
118 
119 /*---------------------------------------------------------------------------*
120   @brief ファイルの読み込みを行うクラスです。
121  *---------------------------------------------------------------------------*/
122 class File
123 {
124 public:
125     //!
126     //! @brief コンストラクタです。
127     //!
File()128     File()
129       : m_Buffer(NULL), m_Size(0)
130     {
131     }
132 
133     //!
134     //! @brief ファイルを読み込みます。
135     //!
136     //! @param fileName ファイル名です。
137     //! @param allocator メモリアロケータです。
138     //!
139     //! @return 読み込みに成功したら true を返します。
140     //!
Read(const wchar_t * fileName,nw::os::IAllocator & allocator)141     bool Read(const wchar_t* fileName, nw::os::IAllocator& allocator)
142     {
143         NW_ASSERT(m_Buffer == NULL);
144 
145         nn::fs::FileReader fileReader(fileName);
146         if (fileReader.GetSize() <= 0)
147         {
148             return false;
149         }
150 
151         this->m_Size = static_cast<u32>(fileReader.GetSize());
152         this->m_Buffer = static_cast<u8*>(allocator.Alloc(this->m_Size, 128));
153         if (this->m_Buffer == NULL)
154         {
155             this->m_Size = 0;
156             return false;
157         }
158 
159         fileReader.Read(this->m_Buffer, this->m_Size);
160         fileReader.Finalize();
161 
162         return true;
163     }
164 
165     //!
166     //! @brief ファイルの読み込まれたバッファを取得します。
167     //!
168     //! @return ポインタを返します。
169     //!
Buffer()170     u8* Buffer()
171     {
172         return this->m_Buffer;
173     }
174 
175     //!
176     //! @brief 読み込まれたファイルのサイズを取得します。
177     //!
178     //! @return 読み込まれたファイルのサイズを返します。
179     //!
Size()180     u32 Size()
181     {
182         return this->m_Size;
183     }
184 
185 private:
186     u8* m_Buffer;
187     u32 m_Size;
188 };
189 
190 /*---------------------------------------------------------------------------*
191   @brief モデルビュー行列と射影行列を設定します。
192 
193   @param drawInfo 描画情報です。
194   @param layout レイアウトです。
195  *---------------------------------------------------------------------------*/
196 void
SetupCamera(nw::lyt::DrawInfo & drawInfo,const nw::lyt::Layout & layout)197 SetupCamera(nw::lyt::DrawInfo& drawInfo, const nw::lyt::Layout& layout)
198 {
199     nw::ut::Rect layoutRect = layout.GetLayoutRect();
200 
201     f32 znear = 0.f;
202     f32 zfar = 500.f;
203 
204     // 射影行列を正射影に設定
205     // (Layoutデータは横向きなので向きを変換する)
206     nw::math::MTX44 projMtx;
207     nw::math::MTX44OrthoPivot(
208         &projMtx,
209         layoutRect.left,   // left
210         layoutRect.right,  // right
211         layoutRect.bottom, // bottom
212         layoutRect.top,    // top
213         znear,
214         zfar,
215         nw::math::PIVOT_UPSIDE_TO_TOP);
216     drawInfo.SetProjectionMtx(projMtx);
217 
218     // モデルビュー行列を設定
219     // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向)
220     nw::math::VEC3 pos(0, 0, 1);
221     nw::math::VEC3 up(0, 1, 0);
222     nw::math::VEC3 target(0, 0, 0);
223 
224     nw::math::MTX34 viewMtx;
225     nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target);
226     drawInfo.SetViewMtx(viewMtx);
227 }
228 
229 }   // namespace {anonymous}
230 
231 /*---------------------------------------------------------------------------*
232   @brief サンプルのメイン関数です。
233  *---------------------------------------------------------------------------*/
234 void
nnMain()235 nnMain()
236 {
237     // 基本ライブラリの初期化。
238     nn::os::Initialize();
239     nn::fs::Initialize();
240 
241     // アプリケーションの初期化。
242     nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance();
243     demoApp.Initialize();
244 
245     // レイアウトライブラリの初期化。
246     nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator());
247 
248     nw::lyt::Layout* pLayout = new nw::lyt::Layout();
249 
250     // レイアウトのバイナリリソース(アーカイブ)を読み込み。
251     File fileLayout;
252     if (!fileLayout.Read(NW_DEMO_FILE_PATH(L"layout.arc"), demoApp.GetDeviceAllocator()))
253     {
254         NW_FATAL_ERROR("can not open layout archive.\n");
255     }
256 
257     // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。
258     nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor;
259     if (!pResAccessor->Attach(fileLayout.Buffer(), "."))
260     {
261         NW_FATAL_ERROR("can not attach layout archive.\n");
262     }
263 
264     // レイアウトリソースの読み込み
265     {
266         const void* lytRes = pResAccessor->GetResource(0, "simple.bclyt");
267         NW_NULL_ASSERT(lytRes);
268         pLayout->Build(lytRes, pResAccessor);
269     }
270 
271     nw::lyt::AnimTransform* pAnimTrans = 0;
272     // アニメーションリソースの読み込み
273     {
274         const void* lpaRes = pResAccessor->GetResource(0, "simple.bclan");
275         NW_NULL_ASSERT(lpaRes);
276         pAnimTrans = pLayout->CreateAnimTransform(lpaRes, pResAccessor);
277         pLayout->BindAnimation(pAnimTrans);
278     }
279 
280     f32 animFrame = 0;
281 
282     nw::lyt::GraphicsResource graphicsResource;
283 
284     // グローバルなリソースファイルを読み込みます。
285     {
286         graphicsResource.StartSetup();
287         const wchar_t* resourcePath = 0;
288         for (int i = 0;
289              (resourcePath = graphicsResource.GetResourcePath(i)) != NULL;
290              ++i)
291         {
292             File file;
293             if (!file.Read(resourcePath, demoApp.GetAllocator()))
294             {
295                 NW_FATAL_ERROR("can not read lyt resource file.");
296             }
297             graphicsResource.SetResource(i, file.Buffer(), file.Size());
298         }
299         graphicsResource.FinishSetup();
300     }
301 
302     nw::lyt::DrawInfo drawInfo;
303     drawInfo.SetGraphicsResource(&graphicsResource);
304 
305     nw::lyt::Drawer drawer;
306     drawer.Initialize(graphicsResource);
307 
308     const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f);
309     const f32 clearDepth = 0.0f;
310 
311     bool mainloop = true;
312     while (mainloop)
313     {
314         demoApp.SetRenderingTarget(demoApp.DISPLAY0);
315         {
316             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
317 
318             SetupCamera(drawInfo, *pLayout);
319 
320             InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT);
321             pLayout->Animate();
322             pLayout->CalculateMtx(drawInfo);
323 
324             drawer.DrawBegin(drawInfo);
325             drawer.Draw(pLayout, drawInfo);
326             drawer.DrawEnd(drawInfo);
327         }
328 
329         demoApp.SetRenderingTarget(demoApp.DISPLAY1);
330         {
331             demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth);
332         }
333 
334         // フレームの更新
335         animFrame += 1.0f;
336         while (animFrame >= pAnimTrans->GetFrameSize())
337         {
338             animFrame -= pAnimTrans->GetFrameSize();
339         }
340         pAnimTrans->SetFrame(animFrame);
341 
342         demoApp.SwapBuffer(demoApp.DISPLAY_BOTH);
343     }
344 
345     delete pLayout;
346     delete pResAccessor;
347 
348     demoApp.GetDeviceAllocator().Free(fileLayout.Buffer());
349 }
350 
351