ctr_Repair/trunk/CardSaveData/common/shfnt.cpp

466 lines
13 KiB
C++

#include <nn.h>
#include <nn/fs.h>
#include <nn/font.h>
#include <nn/math.h>
#include <nn/pl.h>
#include <nn/util.h>
#include "demo.h"
#include <wchar.h>
const char s_ShaderBinaryFilePath[] = "rom:/nnfont_RectDrawerShader.shbin";
extern nn::fnd::ExpHeap appHeap;
//---------------------------------------------------------------------------
//! @brief シェーダの初期化を行います。
//!
//! @param[in,out] pResource 描画用リソースを管理するオブジェクトへのポインタ。
//---------------------------------------------------------------------------
void*
InitShaders(nn::font::RectDrawer* pDrawer)
{
const size_t ROMFS_BUFFER_SIZE = 1024 * 64;
static char buffer[ROMFS_BUFFER_SIZE];
nn::fs::MountRom(16, 16, buffer, ROMFS_BUFFER_SIZE);
nn::fs::FileReader shaderReader(s_ShaderBinaryFilePath);
const u32 fileSize = (u32)shaderReader.GetSize();
void* shaderBinary = appHeap.Allocate(fileSize);
NN_NULL_ASSERT(shaderBinary);
shaderReader.Read(shaderBinary, fileSize);
const u32 vtxBufCmdBufSize =
nn::font::RectDrawer::GetVertexBufferCommandBufferSize(shaderBinary, fileSize);
void *const vtxBufCmdBuf = appHeap.Allocate(vtxBufCmdBufSize);
NN_NULL_ASSERT(vtxBufCmdBuf);
pDrawer->Initialize(vtxBufCmdBuf, shaderBinary, fileSize);
appHeap.Free(shaderBinary);
shaderReader.Finalize();
nn::fs::Unmount("rom:");
return vtxBufCmdBuf;
}
/*---------------------------------------------------------------------------*
@brief グラフィックスの初期設定を行います。
*---------------------------------------------------------------------------*/
void
InitGX()
{
glClearColor(0.3f, 0.3f, 0.3f, 1.0f);
}
//---------------------------------------------------------------------------
//! @brief 描画の初期設定を行います。
//!
//! @param[in] width 画面の幅。
//! @param[in] height 画面の高さ。
//---------------------------------------------------------------------------
void
InitDraw(
int width,
int height
)
{
// カラーバッファ情報
// LCDの向きに合わせて、幅と高さを入れ替えています。
const nn::font::ColorBufferInfo colBufInfo = { width, height, PICA_DATA_DEPTH24_STENCIL8_EXT };
const u32 screenSettingCommands[] =
{
// ビューポートの設定
NN_FONT_CMD_SET_VIEWPORT( 0, 0, colBufInfo.width, colBufInfo.height ),
// シザー処理を無効
NN_FONT_CMD_SET_DISABLE_SCISSOR( colBufInfo ),
// wバッファの無効化
// デプスレンジの設定
// ポリゴンオフセットの無効化
NN_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(screenSettingCommands, sizeof(screenSettingCommands), true);
static const u32 s_InitCommands[] =
{
// カリングを無効
NN_FONT_CMD_SET_CULL_FACE( NN_FONT_CMD_CULL_FACE_DISABLE ),
// ステンシルテストを無効
NN_FONT_CMD_SET_DISABLE_STENCIL_TEST(),
// デプステストを無効
// カラーバッファの全ての成分を書き込み可
NN_FONT_CMD_SET_DEPTH_FUNC_COLOR_MASK(
false, // isDepthTestEnabled
0, // depthFunc
true, // depthMask
true, // red
true, // green
true, // blue
true), // alpha
// アーリーデプステストを無効
NN_FONT_CMD_SET_ENABLE_EARLY_DEPTH_TEST( false ),
// フレームバッファアクセス制御
NN_FONT_CMD_SET_FBACCESS(
true, // colorRead
true, // colorWrite
false, // depthRead
false, // depthWrite
false, // stencilRead
false), // stencilWrite
};
nngxAdd3DCommand(s_InitCommands, sizeof(s_InitCommands), true);
}
//---------------------------------------------------------------------------
//! @brief ResFontを構築します。
//!
//! @param[out] pFont 構築するフォントへのポインタ。
//! @param[in] filePath ロードするフォントリソースファイル名。
//!
//! @return ResFont構築の成否を返します。
//---------------------------------------------------------------------------
bool
InitFont(
nn::font::ResFont* pFont,
void* pBuffer
)
{
// フォントリソースをセットします
bool bSuccess = pFont->SetResource(pBuffer);
// 描画用バッファを設定します。
const u32 drawBufferSize = nn::font::ResFont::GetDrawBufferSize(pBuffer);
void* drawBuffer = appHeap.Allocate(drawBufferSize, 4);
pFont->SetDrawBuffer(drawBuffer);
NN_NULL_ASSERT(drawBuffer);
return bSuccess;
}
//---------------------------------------------------------------------------
//! @brief ResFontを破棄します。
//!
//! @param[in] pFont 破棄するフォントへのポインタ。
//---------------------------------------------------------------------------
void
CleanupFont(nn::font::ResFont* pFont)
{
// 描画用バッファの無効化
// 描画用バッファがセットされているなら 構築時に SetDrawBuffer に渡したバッファへの
// ポインタが返ってきます。
void *const drawBuffer = pFont->SetDrawBuffer(NULL);
if (drawBuffer != NULL)
{
appHeap.Free(drawBuffer);
}
}
//---------------------------------------------------------------------------
//! @brief 表示文字列用バッファを確保します。
//!
//! @param[in] charMax 表示する文字列の最大文字数。
//!
//! @return 確保した表示文字列用バッファへのポインタを返します。
//---------------------------------------------------------------------------
nn::font::DispStringBuffer*
AllocDispStringBuffer(int charMax)
{
const u32 DrawBufferSize = nn::font::CharWriter::GetDispStringBufferSize(charMax);
void *const bufMem = appHeap.Allocate(DrawBufferSize);
NN_NULL_ASSERT(bufMem);
return nn::font::CharWriter::InitDispStringBuffer(bufMem, charMax);
}
//---------------------------------------------------------------------------
//! @brief 文字列表示用にモデルビュー行列と射影行列を設定します。
//!
//! @param[in] pDrawer RectDrawerオブジェクトへのポインタ。
//! @param[in] width 画面の幅。
//! @param[in] height 画面の高さ。
//---------------------------------------------------------------------------
void
SetupTextCamera(
nn::font::RectDrawer* pDrawer,
int width,
int height
)
{
// 射影行列を正射影に設定
{
// 左上原点とし、Y軸とZ軸の向きが逆になるように設定します。
nn::math::MTX44 proj;
f32 znear = 0.0f;
f32 zfar = -1.0f;
f32 t = 0;
f32 b = static_cast<f32>(width);
f32 l = 0;
f32 r = static_cast<f32>(height);
nn::math::MTX44OrthoPivot(&proj, l, r, b, t, znear, zfar, nn::math::PIVOT_UPSIDE_TO_TOP);
pDrawer->SetProjectionMtx(proj);
}
// モデルビュー行列を単位行列に設定
{
nn::math::MTX34 mv;
nn::math::MTX34Identity(&mv);
pDrawer->SetViewMtxForText(mv);
}
}
nn::font::RectDrawer drawer;
void *drawerBuf;
nn::font::DispStringBuffer *pDrawStringBuf0;
nn::font::DispStringBuffer *pDrawStringBuf1;
nn::font::ResFont font;
nn::font::TextWriter writer;
nn::font::WideTextWriter writer_w;
//---------------------------------------------------------------------------
//! @brief ASCII文字列を描画します。
//---------------------------------------------------------------------------
void shf_DrawText_0( u16 x,u16 y,char *s)
{
writer.SetCursor(x, y);
writer.StartPrint();
(void)writer.Print(s);
writer.EndPrint();
drawer.BuildTextCommand(&writer);
drawer.DrawBegin();
SetupTextCamera(&drawer, NN_GX_DISPLAY0_WIDTH, NN_GX_DISPLAY0_HEIGHT);
writer.UseCommandBuffer();
drawer.DrawEnd();
}
void shf_DrawText_1( u16 x,u16 y,char *s)
{
writer.SetCursor(x, y);
writer.StartPrint();
(void)writer.Print(s);
writer.EndPrint();
drawer.BuildTextCommand(&writer);
drawer.DrawBegin();
SetupTextCamera(&drawer, NN_GX_DISPLAY1_WIDTH, NN_GX_DISPLAY1_HEIGHT);
writer.UseCommandBuffer();
drawer.DrawEnd();
}
void shf_SetSize(f32 sz)
{
writer.SetScale(sz/14,sz/20);
}
void shf_SetScale(f32 h,f32 v)
{
writer.SetScale(h,v);
}
void shf_SetFontSize(f32 sz)
{
writer.SetFontSize(sz);
}
f32 shf_GetFontWidth()
{
return writer.GetFontWidth();
}
f32 shf_GetFontHeight()
{
return writer.GetFontHeight();
}
void shf_SetColor(f32 r,f32 g,f32 b,f32 a)
{
writer.SetTextColor(nn::util::FloatColor(r,g,b,a));
}
//******** ワイド文字列 ************
//エラーになるコードを置き換える
//int chgCode(int size,wchar_t *s)
//{
// for (int i=0;i<size;i++)
// {
// if ((s[i] & 0xff00) == 0xff00)s[i]= (s[i] & 0x00ff)+0x20;
// }
//}
void shf_DrawText_0w( u16 x,u16 y,wchar_t *s)
{
wchar_t ss[32];
int size = wcslen(s);
if (size > 32){
NN_LOG("DrawText:size over");
return;
}
wcscpy(ss,s);
writer_w.SetCursor(x, y);
// chgCode(size,ss);
writer_w.StartPrint();
(void)writer_w.Print(ss);
writer_w.EndPrint();
drawer.BuildTextCommand(&writer_w);
drawer.DrawBegin();
SetupTextCamera(&drawer, NN_GX_DISPLAY0_WIDTH, NN_GX_DISPLAY0_HEIGHT);
writer_w.UseCommandBuffer();
drawer.DrawEnd();
}
void shf_DrawText_1w( u16 x,u16 y,wchar_t *s)
{
writer_w.SetCursor(x, y);
writer_w.StartPrint();
(void)writer_w.Print(s);
writer_w.EndPrint();
drawer.BuildTextCommand(&writer_w);
drawer.DrawBegin();
SetupTextCamera(&drawer, NN_GX_DISPLAY1_WIDTH, NN_GX_DISPLAY1_HEIGHT);
writer_w.UseCommandBuffer();
drawer.DrawEnd();
}
void shf_SetSizeW(f32 sz)
{
writer_w.SetScale(sz/14,sz/20);
}
void shf_SetScaleW(f32 h,f32 v)
{
writer_w.SetScale(h,v);
}
void shf_SetFontSizeW(f32 sz)
{
writer_w.SetFontSize(sz);
}
f32 shf_GetFontWidthW()
{
return writer_w.GetFontWidth();
}
f32 shf_GetFontHeightW()
{
return writer_w.GetFontHeight();
}
void shf_SetColorW(f32 r,f32 g,f32 b,f32 a)
{
writer_w.SetTextColor(nn::util::FloatColor(r,g,b,a));
}
//初期化
//AppHeap確保、RenderSystem.Inititの後に呼ぶ
void SharedFontInit()
{
InitGX();
// 共有フォントの初期化
NN_UTIL_PANIC_IF_FAILED(nn::pl::InitializeSharedFont());
// 共有フォントのロードが完了するまで待機
while (nn::pl::GetSharedFontLoadState() != nn::pl::SHARED_FONT_LOAD_STATE_LOADED)
{
// 共有フォントのロードに失敗していないか確認
if (nn::pl::GetSharedFontLoadState() == nn::pl::SHARED_FONT_LOAD_STATE_FAILED)
{
NN_TPANIC_("failed to load shared font!\n");
}
nn::os::Thread::Sleep(nn::fnd::TimeSpan::FromMilliSeconds(1));
}
// 共有フォントの種類を取得
//nn::pl::SharedFontType sharedFontType = nn::pl::GetSharedFontType();
// 共有フォントデータのアドレスを取得
void* pFontBuffer = nn::pl::GetSharedFontAddress();
InitFont(&font, pFontBuffer);
// 描画リソースの構築
drawerBuf = InitShaders(&drawer);
// 描画文字列用バッファの確保
pDrawStringBuf0 = AllocDispStringBuffer(1024);
pDrawStringBuf1 = AllocDispStringBuffer(512);
writer.SetDispStringBuffer(pDrawStringBuf0);
writer.SetFont(&font);
writer_w.SetDispStringBuffer(pDrawStringBuf0);
writer_w.SetFont(&font);
SetupTextCamera(&drawer, NN_GX_DISPLAY0_WIDTH, NN_GX_DISPLAY0_HEIGHT);
}
void SharedFontFinalize()
{
drawer.Finalize();
// 描画リソースの破棄
appHeap.Free(drawerBuf);
// フォントの破棄
CleanupFont(&font);
// 描画文字列用バッファの解放
appHeap.Free(pDrawStringBuf1);
appHeap.Free(pDrawStringBuf0);
}
/*---------------------------------------------------------------------------*
End of file
*---------------------------------------------------------------------------*/