/*---------------------------------------------------------------------------* 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: 23844 $ *---------------------------------------------------------------------------*/ //------------------------------------------------------------------ // デモ: ResFont // // 概要 // nw::font::ResFont の構築と破棄のサンプルです。 // // 操作 // なし。 // //------------------------------------------------------------------ #include #include #include #include #include #include #include #include #include namespace { //--------------------------------------------------------------------------- //! @brief メモリを確保します。 //! //! @param[in] size 確保するメモリのサイズ。 //! @param[in] alignment 確保するメモリのアライメント値。 //! //! @return 確保したメモリへのポインタを返します。 //--------------------------------------------------------------------------- void* MemAlloc( size_t size, u8 alignment = 4 ) { #if defined(NW_PLATFORM_CTR) return nw::demo::SimpleApp::Allocate(size, alignment); #else return nw::demo::Alloc(size, alignment); #endif } //--------------------------------------------------------------------------- //! @brief デバイスメモリからメモリを確保します。 //! //! @param[in] size 確保するメモリのサイズ。 //! @param[in] alignment 確保するメモリのアライメント値。 //! //! @return 確保したメモリへのポインタを返します。 //--------------------------------------------------------------------------- void* DevMemAlloc( size_t size, u8 alignment = 4 ) { #if defined(NW_PLATFORM_CTR) return nw::demo::SimpleApp::AllocateDeviceMemory(size, alignment); #else return nw::demo::Alloc(size, alignment); #endif } //--------------------------------------------------------------------------- //! @brief メモリを解放します。 //! //! @param[in] memory 解放するメモリへのポインタ。 //--------------------------------------------------------------------------- void MemFree(void* memory) { #if defined(NW_PLATFORM_CTR) nw::demo::SimpleApp::Free(memory); #else nw::demo::Free(memory); #endif } //--------------------------------------------------------------------------- //! @brief シェーダの初期化を行います。 //! //! @param[in,out] pResource 描画用リソースを管理するオブジェクトへのポインタ。 //--------------------------------------------------------------------------- void InitShaders(nw::font::TextWriterResource* pResource) { const wchar_t* shaders = NW_DEMO_FILE_PATH( NW_FONT_SHADERBINARY ); nn::fs::FileReader shaderReader(shaders); const u32 fileSize = (u32)shaderReader.GetSize(); void* shaderBinary = MemAlloc(fileSize); s32 read = shaderReader.Read(shaderBinary, fileSize); NN_ASSERT(read == fileSize); pResource->InitResource(shaderBinary, fileSize); MemFree(shaderBinary); } //--------------------------------------------------------------------------- //! @brief 描画の初期設定を行います。 //--------------------------------------------------------------------------- void InitDraw() { glClearColor(0.3f, 0.3f, 0.3f, 1.0f); glDisable(GL_CULL_FACE); // カリングを無効 glDisable(GL_SCISSOR_TEST); // シザー処理を無効 glDisable(GL_POLYGON_OFFSET_FILL); // ポリゴンオフセットを無効 #if defined(NW_PLATFORM_CTR) glDisable(GL_EARLY_DEPTH_TEST_DMP); // アーリーデプステストを無効 #endif glDisable(GL_DEPTH_TEST); // デプステストを無効 glDisable(GL_STENCIL_TEST); // ステンシルテストを無効 // カラーバッファの全ての成分を書き込み可 glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); } //--------------------------------------------------------------------------- //! @brief ResFontを構築します。 //! //! @param[out] pFont 構築するフォントへのポインタ。 //! @param[in] filePath ロードするフォントリソースファイル名。 //! //! @return ResFont構築の成否を返します。 //--------------------------------------------------------------------------- bool InitFont( nw::font::ResFont* pFont, const wchar_t* filePath ) { // フォントリソースをロードします nn::fs::FileReader fontReader(filePath); s32 fileSize = (s32)fontReader.GetSize(); if ( fileSize <= 0 ) { return false; } void* buffer = DevMemAlloc(fileSize, nw::font::GlyphDataAlignment); if (buffer == NULL) { return false; } s32 readSize = fontReader.Read(buffer, fileSize); if (readSize != fileSize) { MemFree(buffer); return false; } // フォントリソースをセットします bool bSuccess = pFont->SetResource(buffer); NN_ASSERT(bSuccess); //--- 既にリソースをセット済みであるか,ロード済みであるか、リソースが不正な場合に失敗します。 if (! bSuccess) { MemFree(buffer); } // 描画用バッファを設定します。 // これを設定しなくても描画は可能ですが、 // 処理速度が遅くなります。 const u32 drawBufferSize = nw::font::ResFont::GetDrawBufferSize(buffer); void* drawBuffer = MemAlloc(drawBufferSize, 4); pFont->SetDrawBuffer(drawBuffer); return bSuccess; } //--------------------------------------------------------------------------- //! @brief ResFontを破棄します。 //! //! @param[in] pFont 破棄するフォントへのポインタ。 //--------------------------------------------------------------------------- void CleanupFont(nw::font::ResFont* pFont) { // 描画用バッファの無効化 // 描画用バッファがセットされているなら 構築時に SetDrawBuffer に渡したバッファへの // ポインタが返ってきます。 void *const drawBuffer = pFont->SetDrawBuffer(NULL); if (drawBuffer != NULL) { MemFree(drawBuffer); } // フォントがセットされているなら SetResource 時に渡したリソースへの // ポインタが返ってきます。 void *const resource = pFont->RemoveResource(); if (resource != NULL) { MemFree(resource); } // RemoveResource 後は再度 SetResource するまでフォントとして使用できません。 } //--------------------------------------------------------------------------- //! @brief 文字列表示用にモデルビュー行列と射影行列を設定します。 //! //! @param[in] textWriterResource 描画用リソースを管理するオブジェクトへの参照。 //! @param[in] width 画面の幅。 //! @param[in] height 画面の高さ。 //--------------------------------------------------------------------------- void SetupTextCamera( const nw::font::TextWriterResource& textWriterResource, int width, int height ) { #ifdef NW_PLATFORM_CTRWIN const GLsizei lcdWidth = width; const GLsizei lcdHeight = height; #else const GLsizei lcdWidth = height; const GLsizei lcdHeight = width; #endif glViewport(0, 0, lcdWidth, lcdHeight); // 射影行列を正射影に設定 { // 左上原点とし、Y軸とZ軸の向きが逆になるように設定します。 nn::math::MTX44 proj; f32 znear = 0.0f; f32 zfar = -1.0f; f32 t = 0; f32 b = static_cast(height); f32 l = 0; f32 r = static_cast(width); nn::math::MTX44OrthoPivot(&proj, l, r, b, t, znear, zfar, nn::math::PIVOT_UPSIDE_TO_TOP); textWriterResource.SetProjectionMtx(proj); } // モデルビュー行列を単位行列に設定 { nn::math::MTX34 mv; nn::math::MTX34Identity(&mv); textWriterResource.SetViewMtx(mv); } } //--------------------------------------------------------------------------- //! @brief ASCII文字列を描画します。 //! //! @param[in] pFont フォントへのポインタ。 //! @param[in] pTextWriterResource 描画用リソースを管理するオブジェクトへのポインタ。 //--------------------------------------------------------------------------- void DrawAscii( nw::font::ResFont* pFont, nw::font::TextWriterResource* pTextWriterResource ) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nw::font::TextWriter writer; writer.SetFont(pFont); writer.SetTextWriterResource(pTextWriterResource); writer.SetupGX(); writer.SetCursor(0, 0); (void)writer.Print("DEMO: ResFont\n"); (void)writer.Print("\n"); // ASCIIの文字見本を表示 (void)writer.Print("All ASCII Character listing:\n"); (void)writer.Print("\n"); (void)writer.Print(" !\"#$%&'()*+,-./\n"); (void)writer.Print("0123456789:;<=>?\n"); (void)writer.Print("@ABCDEFGHIJKLMNO\n"); (void)writer.Print("PQRSTUVWXYZ[\\]^_\n"); (void)writer.Print("`abcdefghijklmno\n"); (void)writer.Print("pqrstuvwxyz{|}~\n"); glFlush(); } } // namespace //--------------------------------------------------------------------------- //! @brief サンプルのメイン関数です。 //--------------------------------------------------------------------------- void nnMain() { // os の初期化 nn::os::Initialize(); // fs の初期化 nn::fs::Initialize(); nw::demo::SimpleApp& demoApp = nw::demo::SimpleApp::GetInstance(); demoApp.Initialize(); nw::font::ResFont font; // フォントの構築 { bool bSuccess = InitFont(&font, NW_DEMO_FILE_PATH(L"tahoma.bcfnt")); NN_ASSERTMSG(bSuccess, "Fail to load ResFont."); } // 描画リソースの構築 nw::font::TextWriterResource textWriterResource; InitShaders(&textWriterResource); textWriterResource.ActiveGlProgram(); InitDraw(); // フォント情報の表示 volatile bool loop = true; while (loop) { demoApp.SetRenderingTarget(demoApp.DISPLAY0); { glBindFramebuffer(GL_FRAMEBUFFER, demoApp.GetFrameBufferObject()); SetupTextCamera( textWriterResource, demoApp.DISPLAY0_WIDTH, demoApp.DISPLAY0_HEIGHT); DrawAscii(&font, &textWriterResource); } demoApp.SetRenderingTarget(demoApp.DISPLAY1); { glBindFramebuffer(GL_FRAMEBUFFER, demoApp.GetFrameBufferObject()); SetupTextCamera( textWriterResource, demoApp.DISPLAY1_WIDTH, demoApp.DISPLAY1_HEIGHT); DrawAscii(&font, &textWriterResource); } demoApp.SwapBuffer(demoApp.DISPLAY_BOTH); } // 描画リソースの破棄 textWriterResource.DeleteResource(); // フォントの破棄 CleanupFont(&font); }