/*---------------------------------------------------------------------------* Project: NintendoWare File: main.cpp Copyright (C)2009-2010 Nintendo Co., Ltd./HAL Laboratory, Inc. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. $Revision: 23867 $ *---------------------------------------------------------------------------*/ //------------------------------------------------------------------ // デモ: simple // // 概要 // レイアウトデータの表示とアニメーションの再生を行うデモです。 // レイアウトデータ、アニメーションデータは次のものが含まれています。 // // simple.clyt ... レイアウトデータ // simple.clan ... アニメーションデータ // - ペインのSRTアニメーション // - テクスチャパターンアニメーション // - ビジビリティアニメーション // - 頂点カラーアニメーション // - マテリアルカラーアニメーション // - テクスチャSRTアニメーション // //------------------------------------------------------------------ #include #include #include #include #include #include namespace { /*---------------------------------------------------------------------------* @brief 描画の初期設定を行います。 @param width 画面の幅。 @param height 画面の高さ。 *---------------------------------------------------------------------------*/ void InitDraw(int width, int height) { // カラーバッファ情報 // LCDの向きに合わせて、幅と高さを入れ替えています。 const nw::font::ColorBufferInfo colBufInfo = { height, width, PICA_DATA_DEPTH24_STENCIL8_EXT }; const u32 commands[] = { // ビューポートの設定 NW_FONT_CMD_SET_VIEWPORT(0, 0, colBufInfo.width, colBufInfo.height), // シザー処理を無効 NW_FONT_CMD_SET_DISABLE_SCISSOR(colBufInfo), // wバッファの無効化 // デプスレンジの設定 // ポリゴンオフセットの無効化 NW_FONT_CMD_SET_WBUFFER_DEPTHRANGE_POLYGONOFFSET( 0.0f, // wScale : 0.0 でWバッファが無効 0.0f, // depth range near 1.0f, // depth range far 0, // polygon offset units : 0.0 で ポリゴンオフセットが無効 colBufInfo), }; nngxAdd3DCommand(commands, sizeof(commands), true); static const u32 constCommands[] = { // カリングを無効 NW_FONT_CMD_SET_CULL_FACE(NW_FONT_CMD_CULL_FACE_DISABLE), // ステンシルテストを無効 NW_FONT_CMD_SET_DISABLE_STENCIL_TEST(), // デプステストを無効 // カラーバッファの全ての成分を書き込み可 NW_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK( false, // isDepthTestEnabled 0, // depthFunc true, // depthMask true, // red true, // green true, // blue true), // alpha // アーリーデプステストを無効 NW_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST(false), // フレームバッファアクセス制御 NW_FONT_CMD_SET_FBACCESS( true, // colorRead true, // colorWrite false, // depthRead false, // depthWrite false, // stencilRead false), // stencilWrite }; nngxAdd3DCommand(constCommands, sizeof(constCommands), true); } /*---------------------------------------------------------------------------* @brief ファイルの読み込みを行うクラスです。 *---------------------------------------------------------------------------*/ class File { public: //! //! @brief コンストラクタです。 //! File() : m_Buffer(NULL), m_Size(0) { } //! //! @brief ファイルを読み込みます。 //! //! @param fileName ファイル名です。 //! @param allocator メモリアロケータです。 //! //! @return 読み込みに成功したら true を返します。 //! bool Read(const wchar_t* fileName, nw::os::IAllocator& allocator) { NW_ASSERT(m_Buffer == NULL); nn::fs::FileReader fileReader(fileName); if (fileReader.GetSize() <= 0) { return false; } this->m_Size = static_cast(fileReader.GetSize()); this->m_Buffer = static_cast(allocator.Alloc(this->m_Size, 128)); if (this->m_Buffer == NULL) { this->m_Size = 0; return false; } fileReader.Read(this->m_Buffer, this->m_Size); fileReader.Finalize(); return true; } //! //! @brief ファイルの読み込まれたバッファを取得します。 //! //! @return ポインタを返します。 //! u8* Buffer() { return this->m_Buffer; } //! //! @brief 読み込まれたファイルのサイズを取得します。 //! //! @return 読み込まれたファイルのサイズを返します。 //! u32 Size() { return this->m_Size; } private: u8* m_Buffer; u32 m_Size; }; /*---------------------------------------------------------------------------* @brief モデルビュー行列と射影行列を設定します。 @param drawInfo 描画情報です。 @param layout レイアウトです。 *---------------------------------------------------------------------------*/ void SetupCamera(nw::lyt::DrawInfo& drawInfo, const nw::lyt::Layout& layout) { nw::ut::Rect layoutRect = layout.GetLayoutRect(); f32 znear = 0.f; f32 zfar = 500.f; // 射影行列を正射影に設定 // (Layoutデータは横向きなので向きを変換する) nw::math::MTX44 projMtx; nw::math::MTX44OrthoPivot( &projMtx, layoutRect.left, // left layoutRect.right, // right layoutRect.bottom, // bottom layoutRect.top, // top znear, zfar, nw::math::PIVOT_UPSIDE_TO_TOP); drawInfo.SetProjectionMtx(projMtx); // モデルビュー行列を設定 // (Layoutデータは横向きなので画面の上方向はレイアウトの-X方向) nw::math::VEC3 pos(0, 0, 1); nw::math::VEC3 up(0, 1, 0); nw::math::VEC3 target(0, 0, 0); nw::math::MTX34 viewMtx; nw::math::MTX34LookAt(&viewMtx, &pos, &up, &target); drawInfo.SetViewMtx(viewMtx); } } // namespace {anonymous} /*---------------------------------------------------------------------------* @brief サンプルのメイン関数です。 *---------------------------------------------------------------------------*/ void nnMain() { // 基本ライブラリの初期化。 nn::os::Initialize(); nn::fs::Initialize(); // アプリケーションの初期化。 nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance(); demoApp.Initialize(); // レイアウトライブラリの初期化。 nw::lyt::Initialize(&demoApp.GetAllocator(), &demoApp.GetDeviceAllocator()); nw::lyt::Layout* pLayout = new nw::lyt::Layout(); // レイアウトのバイナリリソース(アーカイブ)を読み込み。 File fileLayout; if (!fileLayout.Read(NW_DEMO_FILE_PATH(L"layout.arc"), demoApp.GetDeviceAllocator())) { NW_FATAL_ERROR("can not open layout archive.\n"); } // バイナリリソースのルートディレクトリを指定してリソースアクセサを生成。 nw::lyt::ArcResourceAccessor* pResAccessor = new nw::lyt::ArcResourceAccessor; if (!pResAccessor->Attach(fileLayout.Buffer(), ".")) { NW_FATAL_ERROR("can not attach layout archive.\n"); } // レイアウトリソースの読み込み { const void* lytRes = pResAccessor->GetResource(0, "simple.bclyt"); NW_NULL_ASSERT(lytRes); pLayout->Build(lytRes, pResAccessor); } nw::lyt::AnimTransform* pAnimTrans = 0; // アニメーションリソースの読み込み { const void* lpaRes = pResAccessor->GetResource(0, "simple.bclan"); NW_NULL_ASSERT(lpaRes); pAnimTrans = pLayout->CreateAnimTransform(lpaRes, pResAccessor); pLayout->BindAnimation(pAnimTrans); } f32 animFrame = 0; nw::lyt::GraphicsResource graphicsResource; // グローバルなリソースファイルを読み込みます。 { graphicsResource.StartSetup(); const wchar_t* resourcePath = 0; for (int i = 0; (resourcePath = graphicsResource.GetResourcePath(i)) != NULL; ++i) { File file; if (!file.Read(resourcePath, demoApp.GetAllocator())) { NW_FATAL_ERROR("can not read lyt resource file."); } graphicsResource.SetResource(i, file.Buffer(), file.Size()); } graphicsResource.FinishSetup(); } nw::lyt::DrawInfo drawInfo; drawInfo.SetGraphicsResource(&graphicsResource); nw::lyt::Drawer drawer; drawer.Initialize(graphicsResource); const nw::ut::FloatColor clearColor(0.3f, 0.3f, 0.3f, 1.0f); const f32 clearDepth = 0.0f; bool mainloop = true; while (mainloop) { demoApp.SetRenderingTarget(demoApp.DISPLAY0); { demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth); SetupCamera(drawInfo, *pLayout); InitDraw(demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT); pLayout->Animate(); pLayout->CalculateMtx(drawInfo); drawer.DrawBegin(drawInfo); drawer.Draw(pLayout, drawInfo); drawer.DrawEnd(drawInfo); } demoApp.SetRenderingTarget(demoApp.DISPLAY1); { demoApp.GetFrameBufferObject().ClearBuffer(clearColor, clearDepth); } // フレームの更新 animFrame += 1.0f; while (animFrame >= pAnimTrans->GetFrameSize()) { animFrame -= pAnimTrans->GetFrameSize(); } pAnimTrans->SetFrame(animFrame); demoApp.SwapBuffer(demoApp.DISPLAY_BOTH); } delete pLayout; delete pResAccessor; demoApp.GetDeviceAllocator().Free(fileLayout.Buffer()); }