bgfx/examples/10-font/font.cpp

207 lines
6.3 KiB
C++
Raw Normal View History

2013-05-15 09:07:04 -04:00
/*
2013-05-16 23:37:54 -04:00
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
2013-05-15 09:07:04 -04:00
2013-04-22 16:42:11 -04:00
#include <bgfx.h>
#include <bx/bx.h>
#include <bx/timer.h>
2013-05-15 09:21:23 -04:00
#include <bx/countof.h>
2013-04-22 16:42:11 -04:00
#include "../common/entry.h"
#include "../common/dbg.h"
#include "../common/math.h"
#include "../common/processevents.h"
#include "../common/font/font_manager.h"
#include "../common/font/text_buffer_manager.h"
#include <stdio.h>
2013-05-16 12:03:56 -04:00
#include <wchar.h>
2013-04-22 16:42:11 -04:00
2013-05-17 02:45:37 -04:00
#if defined(__MINGW32__)
# define swnprintf(_out, _count, _format, ...) swprintf(_out, _format, ##__VA_ARGS__)
#else
# define swnprintf(_out, _count, _format, ...) swprintf(_out, _count, _format, ##__VA_ARGS__)
#endif // defined(__MINGW__)
2013-04-22 16:42:11 -04:00
int _main_(int /*_argc*/, char** /*_argv*/)
{
2013-05-15 09:21:23 -04:00
uint32_t width = 1280;
2013-04-22 16:42:11 -04:00
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = 0;
2013-05-15 09:21:23 -04:00
bgfx::init();
2013-04-22 16:42:11 -04:00
bgfx::reset(width, height);
// Enable debug text.
bgfx::setDebug(debug);
// Set view 0 clear state.
bgfx::setViewClear(0
2013-05-16 23:35:08 -04:00
, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
, 0x303030ff
, 1.0f
, 0
);
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Init the text rendering system.
FontManager* fontManager = new FontManager(512);
TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
2013-05-16 23:35:08 -04:00
// Load some TTF files.
2013-05-15 09:21:23 -04:00
const char* fontNames[7] =
{
2013-05-08 13:55:20 -04:00
"font/droidsans.ttf",
2013-05-15 09:21:23 -04:00
"font/chp-fire.ttf",
2013-05-08 13:55:20 -04:00
"font/bleeding_cowboys.ttf",
"font/mias_scribblings.ttf",
"font/ruritania.ttf",
"font/signika-regular.ttf",
"font/five_minutes.otf"
};
2013-05-16 23:35:08 -04:00
const uint32_t fontCount = countof(fontNames);
2013-05-15 09:21:23 -04:00
2013-05-08 13:55:20 -04:00
TrueTypeHandle fontFiles[fontCount];
FontHandle fonts[fontCount];
2013-05-16 00:35:26 -04:00
for (uint32_t ii = 0; ii < fontCount; ++ii)
2013-05-08 13:55:20 -04:00
{
2013-05-16 23:35:08 -04:00
// Instantiate a usable font.
2013-05-08 13:55:20 -04:00
fontFiles[ii] = fontManager->loadTrueTypeFromFile(fontNames[ii]);
fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
2013-05-16 23:35:08 -04:00
// Preload glyphs and blit them to atlas.
2013-05-08 13:55:20 -04:00
fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
2013-05-16 23:35:08 -04: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.
2013-05-08 13:55:20 -04:00
fontManager->unloadTrueType(fontFiles[ii]);
}
2013-05-15 09:21:23 -04:00
2013-05-08 13:55:20 -04:00
TrueTypeHandle console_tt = fontManager->loadTrueTypeFromFile("font/visitor1.ttf");
2013-04-22 16:42:11 -04:00
2013-05-16 23:35:08 -04:00
// This font doesn't have any preloaded glyph's but the truetype file
// is loaded so glyph will be generated as needed.
2013-05-08 13:55:20 -04:00
FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 0, 10);
2013-05-15 09:21:23 -04:00
2013-04-22 16:42:11 -04:00
//create a static text buffer compatible with alpha font
//a static text buffer content cannot be modified after its first submit.
TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, STATIC);
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// The pen position represent the top left of the box of the first line
// of text.
2013-05-08 13:55:20 -04:00
textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
2013-05-16 00:35:26 -04:00
for (uint32_t ii = 0; ii < fontCount; ++ii)
2013-05-08 13:55:20 -04:00
{
2013-05-16 23:35:08 -04:00
// Add some text to the buffer.
// The position of the pen is adjusted when there is an endline.
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
}
2013-05-16 23:35:08 -04:00
// Now write some styled text.
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Setup style colors.
2013-04-22 16:42:11 -04:00
textBufferManager->setBackgroundColor(staticText, 0x551111FF);
textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
2013-05-15 09:21:23 -04:00
textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
2013-04-22 16:42:11 -04:00
2013-05-16 23:35:08 -04:00
// Background.
textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[0], L"The quick ");
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Strike-through.
textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Overline.
textBufferManager->setStyle(staticText, STYLE_OVERLINE);
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
2013-05-16 23:35:08 -04:00
// Underline.
textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Background + strike-through.
2013-05-15 09:21:23 -04:00
textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
2013-05-08 13:55:20 -04:00
textBufferManager->appendText(staticText, fonts[0], L"dog\n");
2013-05-15 09:21:23 -04:00
2013-05-16 23:35:08 -04:00
// Create a transient buffer for real-time data.
2013-05-15 09:21:23 -04:00
TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
while (!processEvents(width, height, debug, reset) )
2013-04-22 16:42:11 -04:00
{
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, width, height);
// 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::submit(0);
int64_t now = bx::getHPCounter();
static int64_t last = now;
const int64_t frameTime = now - last;
last = now;
const double freq = double(bx::getHPFrequency() );
2013-05-15 09:21:23 -04:00
const double toMs = 1000.0 / freq;
2013-05-16 23:37:54 -04:00
// Use transient text to display debug information.
wchar_t fpsText[64];
2013-05-17 02:45:37 -04:00
swnprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
2013-05-15 09:21:23 -04:00
textBufferManager->clearTextBuffer(transientText);
2013-05-15 09:21:23 -04:00
textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n");
textBufferManager->appendText(transientText, consola_16, L"Description: Use the font system to display text and styled text.\n");
textBufferManager->appendText(transientText, consola_16, fpsText);
2013-04-22 16:42:11 -04:00
float at[3] = { 0, 0, 0.0f };
float eye[3] = {0, 0, -1.0f };
2013-05-15 09:21:23 -04:00
2013-04-22 16:42:11 -04:00
float view[16];
float proj[16];
2013-05-08 17:55:54 -04:00
mtxLookAt(view, eye, at);
2013-05-16 23:37:54 -04:00
// Setup a top-left ortho matrix for screen space drawing.
2013-04-22 16:42:11 -04:00
float centering = 0.5f;
2013-05-15 09:21:23 -04:00
mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
2013-04-22 16:42:11 -04:00
// Set view and projection matrix for view 0.
bgfx::setViewTransform(0, view, proj);
2013-05-16 23:35:08 -04:00
// Submit the debug text.
textBufferManager->submitTextBuffer(transientText, 0);
2013-05-16 23:35:08 -04:00
// Submit the static text.
2013-05-15 09:21:23 -04:00
textBufferManager->submitTextBuffer(staticText, 0);
// Advance to next frame. Rendering thread will be kicked to
2013-04-22 16:42:11 -04:00
// process submitted rendering primitives.
bgfx::frame();
}
2013-05-15 09:21:23 -04:00
2013-05-08 13:55:20 -04:00
fontManager->unloadTrueType(console_tt);
2013-05-16 23:35:08 -04:00
// Destroy the fonts.
2013-05-15 09:21:23 -04:00
fontManager->destroyFont(consola_16);
2013-05-16 12:03:56 -04:00
for (uint32_t ii = 0; ii < fontCount; ++ii)
2013-05-08 13:55:20 -04:00
{
fontManager->destroyFont(fonts[ii]);
}
2013-04-22 16:42:11 -04:00
textBufferManager->destroyTextBuffer(staticText);
2013-05-15 09:21:23 -04:00
textBufferManager->destroyTextBuffer(transientText);
2013-04-22 16:42:11 -04:00
delete textBufferManager;
2013-05-15 09:21:23 -04:00
delete fontManager;
2013-04-22 16:42:11 -04:00
// Shutdown bgfx.
2013-05-15 09:21:23 -04:00
bgfx::shutdown();
2013-04-22 16:42:11 -04:00
return 0;
}