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