bgfx/examples/10-font/font.cpp

343 lines
9.9 KiB
C++
Raw Normal View History

2013-05-15 17:07:04 +04:00
/*
2013-05-17 07:37:54 +04:00
* Copyright 2013 Jeremie Roy. All rights reserved.
2016-01-01 11:11:04 +03:00
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
2013-05-17 07:37:54 +04:00
*/
2013-05-15 17:07:04 +04:00
#include "common.h"
2015-10-24 06:52:22 +03:00
#include "bgfx_utils.h"
2013-05-19 09:12:40 +04:00
2013-04-23 00:42:11 +04:00
#include <bx/timer.h>
2013-05-18 09:13:32 +04:00
#include <bx/string.h>
2014-05-27 06:31:37 +04:00
#include <bx/fpumath.h>
2013-04-23 00:42:11 +04:00
#include "font/font_manager.h"
#include "font/text_buffer_manager.h"
#include "entry/input.h"
2013-04-23 00:42:11 +04:00
2016-02-11 09:37:09 +03:00
#include <iconfontheaders/icons_font_awesome.h>
#include <iconfontheaders/icons_kenney.h>
2013-05-16 20:03:56 +04:00
#include <wchar.h>
2013-04-23 00:42:11 +04:00
2013-08-09 09:18:19 +04:00
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
{
2016-12-09 07:45:01 +03:00
uint32_t size;
void* data = load(_filePath, &size);
if (NULL != data)
2013-05-30 08:53:19 +04:00
{
2016-12-09 07:45:01 +03:00
TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
BX_FREE(entry::getAllocator(), data);
2013-08-09 09:18:19 +04:00
return handle;
2013-05-30 08:53:19 +04:00
}
TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
return invalid;
}
2017-06-13 08:43:07 +03:00
static const char* s_fontFilePath[] =
2013-04-23 00:42:11 +04:00
{
"font/droidsans.ttf",
"font/chp-fire.ttf",
"font/bleeding_cowboys.ttf",
"font/mias_scribblings.ttf",
"font/ruritania.ttf",
"font/signika-regular.ttf",
"font/five_minutes.otf",
};
class ExampleFont : public entry::AppI
{
void init(int _argc, char** _argv) BX_OVERRIDE
2013-04-23 00:42:11 +04:00
{
Args args(_argc, _argv);
2017-06-13 08:43:07 +03:00
m_width = 1280;
m_height = 720;
m_debug = BGFX_DEBUG_TEXT;
m_reset = BGFX_RESET_VSYNC;
2017-06-13 08:43:07 +03:00
bgfx::init(args.m_type, args.m_pciId);
bgfx::reset(m_width, m_height, m_reset);
2017-06-13 08:43:07 +03:00
// Enable debug text.
bgfx::setDebug(m_debug);
2017-06-13 08:43:07 +03:00
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
2017-06-13 08:43:07 +03:00
// Init the text rendering system.
m_fontManager = new FontManager(512);
m_textBufferManager = new TextBufferManager(m_fontManager);
2017-06-13 08:43:07 +03:00
// Load some TTF files.
for (uint32_t ii = 0; ii < numFonts; ++ii)
2014-11-04 06:11:08 +03:00
{
// Instantiate a usable font.
2017-06-13 08:43:07 +03:00
m_fontFiles[ii] = loadTtf(m_fontManager, s_fontFilePath[ii]);
m_fonts[ii] = m_fontManager->createFontByPixelSize(m_fontFiles[ii], 0, 32);
2017-06-13 08:43:07 +03:00
// Preload glyphs and blit them to atlas.
m_fontManager->preloadGlyph(m_fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
2017-06-13 08:43:07 +03:00
// You can unload the truetype files at this stage, but in that
// case, the set of glyph's will be limited to the set of preloaded
// glyph.
m_fontManager->destroyTtf(m_fontFiles[ii]);
2014-11-04 06:11:08 +03:00
}
2017-06-13 08:43:07 +03:00
m_fontAwesomeTtf = loadTtf(m_fontManager, "font/fontawesome-webfont.ttf");
m_fontKenneyTtf = loadTtf(m_fontManager, "font/kenney-icon-font.ttf");
2017-06-13 08:43:07 +03:00
// This font doesn't have any preloaded glyph's but the truetype file
// is loaded so glyph will be generated as needed.
m_fontAwesome72 = m_fontManager->createFontByPixelSize(m_fontAwesomeTtf, 0, 72);
m_fontKenney64 = m_fontManager->createFontByPixelSize(m_fontKenneyTtf, 0, 64);
2017-06-13 08:43:07 +03:00
m_visitorTtf = loadTtf(m_fontManager, "font/visitor1.ttf");
2017-06-13 08:43:07 +03:00
// This font doesn't have any preloaded glyph's but the truetype file
// is loaded so glyph will be generated as needed.
m_visitor10 = m_fontManager->createFontByPixelSize(m_visitorTtf, 0, 10);
2017-06-13 08:43:07 +03:00
//create a static text buffer compatible with alpha font
//a static text buffer content cannot be modified after its first submit.
m_staticText = m_textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Static);
2017-06-13 08:43:07 +03:00
// The pen position represent the top left of the box of the first line
// of text.
m_textBufferManager->setPenPosition(m_staticText, 24.0f, 100.0f);
2017-06-13 08:43:07 +03:00
for (uint32_t ii = 0; ii < numFonts; ++ii)
2014-11-04 06:11:08 +03:00
{
// Add some text to the buffer.
// The position of the pen is adjusted when there is an endline.
m_textBufferManager->appendText(m_staticText, m_fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
2014-11-04 06:11:08 +03:00
}
2017-06-13 08:43:07 +03:00
// Now write some styled text.
2017-06-13 08:43:07 +03:00
// Setup style colors.
m_textBufferManager->setBackgroundColor(m_staticText, 0x551111ff);
m_textBufferManager->setUnderlineColor(m_staticText, 0xff2222ff);
m_textBufferManager->setOverlineColor(m_staticText, 0x2222ffff);
m_textBufferManager->setStrikeThroughColor(m_staticText, 0x22ff22ff);
2017-06-13 08:43:07 +03:00
// Background.
m_textBufferManager->setStyle(m_staticText, STYLE_BACKGROUND);
m_textBufferManager->appendText(m_staticText, m_fonts[0], L"The quick ");
2017-06-13 08:43:07 +03:00
// Strike-through.
m_textBufferManager->setStyle(m_staticText, STYLE_STRIKE_THROUGH);
m_textBufferManager->appendText(m_staticText, m_fonts[0], L"brown fox ");
2017-06-13 08:43:07 +03:00
// Overline.
m_textBufferManager->setStyle(m_staticText, STYLE_OVERLINE);
m_textBufferManager->appendText(m_staticText, m_fonts[0], L"jumps over ");
2017-06-13 08:43:07 +03:00
// Underline.
m_textBufferManager->setStyle(m_staticText, STYLE_UNDERLINE);
m_textBufferManager->appendText(m_staticText, m_fonts[0], L"the lazy ");
2017-06-13 08:43:07 +03:00
// Background + strike-through.
m_textBufferManager->setStyle(m_staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
m_textBufferManager->appendText(m_staticText, m_fonts[0], L"dog\n");
2017-06-13 08:43:07 +03:00
m_textBufferManager->setStyle(m_staticText, STYLE_NORMAL);
m_textBufferManager->appendText(m_staticText, m_fontAwesome72,
2017-06-13 08:43:07 +03:00
" " ICON_FA_POWER_OFF
" " ICON_FA_TWITTER_SQUARE
" " ICON_FA_CERTIFICATE
" " ICON_FA_FLOPPY_O
" " ICON_FA_GITHUB
" " ICON_FA_GITHUB_ALT
"\n"
);
m_textBufferManager->appendText(m_staticText, m_fontKenney64,
2017-06-13 08:43:07 +03:00
" " ICON_KI_COMPUTER
" " ICON_KI_JOYSTICK
" " ICON_KI_EXLAMATION
" " ICON_KI_STAR
" " ICON_KI_BUTTON_START
" " ICON_KI_DOWNLOAD
"\n"
);
// Create a transient buffer for real-time data.
m_transientText = m_textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
2013-04-23 00:42:11 +04:00
}
2017-06-13 08:43:07 +03:00
virtual int shutdown() BX_OVERRIDE
2013-05-08 21:55:20 +04:00
{
m_fontManager->destroyTtf(m_fontKenneyTtf);
m_fontManager->destroyTtf(m_fontAwesomeTtf);
m_fontManager->destroyTtf(m_visitorTtf);
2017-06-13 08:43:07 +03:00
// Destroy the fonts.
m_fontManager->destroyFont(m_fontKenney64);
m_fontManager->destroyFont(m_fontAwesome72);
m_fontManager->destroyFont(m_visitor10);
for (uint32_t ii = 0; ii < numFonts; ++ii)
{
m_fontManager->destroyFont(m_fonts[ii]);
}
2017-06-13 08:43:07 +03:00
m_textBufferManager->destroyTextBuffer(m_staticText);
m_textBufferManager->destroyTextBuffer(m_transientText);
2017-06-13 08:43:07 +03:00
delete m_textBufferManager;
delete m_fontManager;
2017-06-13 08:43:07 +03:00
// Shutdown bgfx.
bgfx::shutdown();
2017-06-13 08:43:07 +03:00
return 0;
2013-05-08 21:55:20 +04:00
}
2017-06-13 08:43:07 +03:00
bool update() BX_OVERRIDE
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset) )
{
// This dummy draw call is here to make sure that view 0 is cleared
// if no other draw calls are submitted to view 0.
bgfx::touch(0);
2017-06-13 08:43:07 +03:00
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
const double toMs = 1000.0 / freq;
2017-06-13 08:43:07 +03:00
// Use debug font to print information about this example.
bgfx::dbgTextClear();
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text.");
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
2017-06-13 08:43:07 +03:00
// Use transient text to display debug information.
wchar_t fpsText[64];
bx::swnprintf(fpsText, BX_COUNTOF(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
2017-06-13 08:43:07 +03:00
m_textBufferManager->clearTextBuffer(m_transientText);
m_textBufferManager->setPenPosition(m_transientText, m_width - 150.0f, 10.0f);
m_textBufferManager->appendText(m_transientText, m_visitor10, L"Transient\n");
m_textBufferManager->appendText(m_transientText, m_visitor10, L"text buffer\n");
m_textBufferManager->appendText(m_transientText, m_visitor10, fpsText);
2017-06-13 08:43:07 +03:00
float at[3] = { 0, 0, 0.0f };
float eye[3] = { 0, 0, -1.0f };
2017-06-13 08:43:07 +03:00
float view[16];
bx::mtxLookAt(view, eye, at);
2017-06-13 08:43:07 +03:00
const float centering = 0.5f;
2017-06-13 08:43:07 +03:00
// Setup a top-left ortho matrix for screen space drawing.
2017-06-13 08:43:07 +03:00
const bgfx::HMD* hmd = bgfx::getHMD();
const bgfx::Caps* caps = bgfx::getCaps();
if (NULL != hmd
&& 0 != (hmd->flags & BGFX_HMD_RENDERING) )
{
float proj[16];
2017-06-13 08:43:07 +03:00
bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f, caps->homogeneousDepth);
static float time = 0.0f;
time += 0.05f;
2017-06-13 08:43:07 +03:00
const float dist = 10.0f;
const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
2017-06-13 08:43:07 +03:00
float ortho[2][16];
const float offsetx = m_width/2.0f;
2017-06-13 08:43:07 +03:00
bx::mtxOrtho(
ortho[0]
, centering
, offsetx + centering
, m_height + centering
, centering
, -1.0f
, 1.0f
, offset0
, caps->homogeneousDepth
);
bx::mtxOrtho(
ortho[1]
, centering
, offsetx + centering
, m_height + centering
, centering
, -1.0f
, 1.0f
, offset1
, caps->homogeneousDepth
);
bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
}
else
{
float ortho[16];
2017-06-13 08:43:07 +03:00
bx::mtxOrtho(
ortho
, centering
, m_width + centering
, m_height + centering
, centering
, 0.0f
, 100.0f
, 0.0f
, caps->homogeneousDepth
);
bgfx::setViewTransform(0, view, ortho);
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
}
2017-06-13 08:43:07 +03:00
// Submit the debug text.
m_textBufferManager->submitTextBuffer(m_transientText, 0);
2017-06-13 08:43:07 +03:00
// Submit the static text.
m_textBufferManager->submitTextBuffer(m_staticText, 0);
2017-06-13 08:43:07 +03:00
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
2017-06-13 08:43:07 +03:00
return true;
}
2017-06-13 08:43:07 +03:00
return false;
}
2017-06-13 08:43:07 +03:00
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
2017-06-13 08:43:07 +03:00
FontManager* m_fontManager;
TextBufferManager* m_textBufferManager;
2017-06-13 08:43:07 +03:00
FontHandle m_visitor10;
TrueTypeHandle m_fontAwesomeTtf;
TrueTypeHandle m_fontKenneyTtf;
FontHandle m_fontAwesome72;
FontHandle m_fontKenney64;
TrueTypeHandle m_visitorTtf;
2017-06-13 08:43:07 +03:00
TextBufferHandle m_transientText;
TextBufferHandle m_staticText;
2017-06-13 08:43:07 +03:00
static const uint32_t numFonts = BX_COUNTOF(s_fontFilePath);
TrueTypeHandle m_fontFiles[numFonts];
FontHandle m_fonts[numFonts];
};
ENTRY_IMPLEMENT_MAIN(ExampleFont);