跨平臺遊戲程式設計與 gameplay3d/文字和字型
外觀
Gameplay3d 支援 TrueType 字型,但這些字型必須首先被轉換為 gameplay 包檔案(見 跨平臺遊戲程式設計與 gameplay3d/gameplay3d 設計概念#建立和匯入資源),然後才能在你的專案中使用。
使用以下步驟將字型匯入你的專案
- 查詢或建立 TrueType 字型檔案(.ttf)。
- 將 .ttf 檔案傳遞給 gameplay-encoder 命令列工具,並指定你想要的尺寸。GamePlay/bin 下提供了一個預編譯的 gameplay-encoder 版本
gameplay-encoder -s 28 myfont.ttf
- gameplay-encoder 將輸出一個 .gpb 檔案。
- 將 .gpb 檔案複製到你的遊戲的資源目錄。(無需包含 .ttf 檔案。)
- 按如下方式將你的字型匯入你的專案
// Create a font from the gpb file
Font* _font = Font::create("res/myfont.gpb");
要使用字型進行渲染
- 首先,在你的字型例項上呼叫
Font::start()來開始繪製文字; - 其次,呼叫
Font::drawText()在定義的位置繪製一些文字。此函式有四種不同的過載,引數可以在 Font.h 中找到;以及 - 最後,呼叫
Font::finish()來完成相關字型的文字批處理,並渲染所有繪製的文字。
以下是如何載入字型並使用它在螢幕上渲染遊戲的幀率的示例。
void MyGame::initialize()
{
// Create a font from the gpb file
_font = Font::create("res/myfont.gpb");
}
void MyGame::render(float elapsedTime)
{
// Clear the frame buffer
clear(CLEAR_COLOR_DEPTH, Vector4(0, 0, 0, 1), 1.0f, 0);
// Draw the text at position 20,20 using red color
_font->start();
char text[1024];
sprintf(text, "FPS:%d", Game::getFrameRate());
_font->drawText(text, 20, 20, Vector4(1, 0, 0, 1), _font->getSize());
_font->finish();
}
void MyGame::finalize()
{
// Use built-in macros to clean up our resources.
SAFE_RELEASE(_font);
}
可以在 Font::start() 和 Font::finish() 之間多次呼叫 Font::drawText(),以便使用相同的字型在不同的位置繪製不同的文字。
從 2.0.0 版本開始,gameplay3d 支援距離場字型。你可以在 Valve 的論文 針對向量紋理和特殊效果的改進 Alpha 測試放大 中瞭解更多關於距離場字型的知識,但簡而言之,距離場字型比點陣圖字型看起來要好得多,尤其是在高放大倍率下!
要建立距離場字型,在執行 gameplay-encoder 時使用 -f:d 選項引數,例如
gameplay-encoder -s 50 -f:d myfont.ttf