bgfx/examples/11-fontsdf/fontsdf.cpp

161 lines
4.6 KiB
C++
Raw Normal View History

2013-05-15 17:07:04 +04:00
/*
2013-05-17 07:35:08 +04:00
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: http://www.opensource.org/licenses/BSD-2-Clause
*/
2013-05-15 17:07:04 +04:00
2013-04-23 00:42:11 +04:00
#include <bgfx.h>
#include <bx/bx.h>
#include <bx/timer.h>
#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"
2013-05-09 01:55:54 +04:00
inline void mtxTranslate(float* _result, float x, float y, float z)
{
2013-05-15 17:21:23 +04:00
memset(_result, 0, sizeof(float) * 16);
2013-05-09 01:55:54 +04:00
_result[0] = _result[5] = _result[10] = _result[15] = 1.0f;
_result[12] = x;
_result[13] = y;
_result[14] = z;
}
2013-05-09 01:55:54 +04:00
inline void mtxScale(float* _result, float x, float y, float z)
{
2013-05-15 17:21:23 +04:00
memset(_result, 0, sizeof(float) * 16);
2013-05-09 01:55:54 +04:00
_result[0] = x;
_result[5] = y;
_result[10] = z;
_result[15] = 1.0f;
}
2013-04-23 00:42:11 +04:00
int _main_(int /*_argc*/, char** /*_argv*/)
{
2013-05-15 17:21:23 +04:00
uint32_t width = 1280;
2013-04-23 00:42:11 +04:00
uint32_t height = 720;
uint32_t debug = BGFX_DEBUG_TEXT;
uint32_t reset = 0;
bgfx::init();
bgfx::reset(width, height);
// Enable debug text.
bgfx::setDebug(debug);
// Set view 0 clear state.
bgfx::setViewClear(0
2013-05-17 07:35:08 +04:00
, BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
, 0x303030ff
, 1.0f
, 0
);
// Init the text rendering system.
FontManager* fontManager = new FontManager(512);
TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
2013-04-23 00:42:11 +04:00
2013-05-09 01:55:54 +04:00
TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("font/bleeding_cowboys.ttf");
2013-05-15 17:21:23 +04:00
2013-05-17 07:35:08 +04:00
// Create a distance field font.
FontHandle distance_font = fontManager->createFontByPixelSize(times_tt, 0, 48, FONT_TYPE_DISTANCE);
2013-05-17 07:35:08 +04:00
// Create a scalled down version of the same font (without adding
// anything to the atlas).
2013-05-09 01:55:54 +04:00
FontHandle smaller_font = fontManager->createScaledFontToPixelSize(distance_font, 32);
2013-05-15 17:21:23 +04:00
2013-05-17 07:35:08 +04:00
// Preload glyph and generate (generate bitmap's).
2013-05-09 01:55:54 +04:00
fontManager->preloadGlyph(distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,\" \n");
2013-05-17 07:35:08 +04:00
// You can unload the TTF files at this stage, but in that case, the
// set of glyph's will be limited to the set of preloaded glyph.
2013-04-23 00:42:11 +04:00
fontManager->unloadTrueType(times_tt);
2013-05-15 17:21:23 +04:00
TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
2013-05-09 01:55:54 +04:00
textBufferManager->setTextColor(staticText, 0xDD0000FF);
2013-05-15 17:21:23 +04:00
2013-05-09 02:05:45 +04:00
textBufferManager->appendText(staticText, distance_font, L"BGFX ");
2013-05-09 01:55:54 +04:00
textBufferManager->appendText(staticText, smaller_font, L"bgfx");
int64_t timeOffset = bx::getHPCounter();
2013-05-15 17:21:23 +04:00
while (!processEvents(width, height, debug, reset) )
2013-04-23 00: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);
2013-04-23 16:32:31 +04: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() );
2013-05-15 17:21:23 +04:00
const double toMs = 1000.0 / freq;
float time = (float)( (now - timeOffset) / double(bx::getHPFrequency() ) );
2013-04-23 16:32:31 +04:00
2013-04-23 00:42:11 +04:00
// Use debug font to print information about this example.
bgfx::dbgTextClear();
2013-04-23 19:23:00 +04:00
bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontsdf");
bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
2013-05-15 17:21:23 +04:00
bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
2013-04-23 16:32:31 +04:00
2013-04-23 00:42:11 +04:00
float at[3] = { 0, 0, 0.0f };
float eye[3] = {0, 0, -1.0f };
2013-05-09 01:55:54 +04:00
2013-04-23 00:42:11 +04:00
float view[16];
float proj[16];
2013-05-09 01:55:54 +04:00
mtxLookAt(view, eye, at);
2013-04-23 00:42:11 +04:00
float centering = 0.5f;
2013-05-17 07:35:08 +04:00
// Setup a top-left ortho matrix for screen space drawing.
2013-05-15 17:21:23 +04:00
mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
2013-05-09 01:55:54 +04:00
2013-04-23 00:42:11 +04:00
// Set view and projection matrix for view 0.
2013-05-15 17:21:23 +04:00
bgfx::setViewTransform(0, view, proj);
2013-05-09 01:55:54 +04:00
TextRectangle rect = textBufferManager->getRectangle(staticText);
float mtxA[16];
float mtxB[16];
float mtxC[16];
2013-05-15 17:21:23 +04:00
mtxRotateZ(mtxA, time * 0.37f);
mtxTranslate(mtxB, -(rect.width * 0.5f), -(rect.height * 0.5f), 0);
2013-05-09 01:55:54 +04:00
mtxMul(mtxC, mtxB, mtxA);
2013-05-15 17:21:23 +04:00
float scale = 4.1f + 4.0f * sinf(time);
2013-05-09 01:55:54 +04:00
mtxScale(mtxA, scale, scale, 1.0f);
mtxMul(mtxB, mtxC, mtxA);
2013-05-15 17:21:23 +04:00
mtxTranslate(mtxC, ( (width) * 0.5f), ( (height) * 0.5f), 0);
2013-05-09 01:55:54 +04:00
mtxMul(mtxA, mtxB, mtxC);
2013-05-15 17:21:23 +04:00
2013-05-09 01:55:54 +04:00
// Set model matrix for rendering.
bgfx::setTransform(mtxA);
2013-05-17 07:35:08 +04:00
// Draw your text.
2013-05-15 17:21:23 +04:00
textBufferManager->submitTextBuffer(staticText, 0);
2013-04-23 00:42:11 +04:00
2013-05-15 17:21:23 +04:00
// Advance to next frame. Rendering thread will be kicked to
2013-04-23 00:42:11 +04:00
// process submitted rendering primitives.
bgfx::frame();
}
2013-05-17 07:35:08 +04:00
// Destroy the fonts.
2013-05-09 01:55:54 +04:00
fontManager->destroyFont(distance_font);
2013-05-15 17:21:23 +04:00
fontManager->destroyFont(smaller_font);
textBufferManager->destroyTextBuffer(staticText);
2013-04-23 00:42:11 +04:00
delete textBufferManager;
2013-05-15 17:21:23 +04:00
delete fontManager;
2013-05-17 07:35:08 +04:00
2013-04-23 00:42:11 +04:00
// Shutdown bgfx.
2013-05-15 17:21:23 +04:00
bgfx::shutdown();
2013-04-23 00:42:11 +04:00
return 0;
}