bgfx/examples/11-fontsdf/fontsdf.cpp

414 lines
12 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.
2022-01-15 22:59:06 +03:00
* License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
2013-05-17 07:37:54 +04:00
*/
2013-05-15 17:07:04 +04:00
#include "common.h"
2015-01-23 08:01:09 +03:00
#include "bgfx_utils.h"
#include <bgfx/bgfx.h>
2013-04-23 00:42:11 +04:00
#include <bx/timer.h>
2017-07-16 07:01:08 +03:00
#include <bx/math.h>
2013-04-23 00:42:11 +04:00
#include "font/font_manager.h"
#include "font/text_metrics.h"
#include "font/text_buffer_manager.h"
#include "imgui/imgui.h"
2017-06-26 07:44:04 +03:00
namespace
{
2013-08-09 09:18:19 +04:00
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
2013-05-30 08:53:19 +04:00
{
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-12 22:52:48 +03:00
class ExampleFontSDF : public entry::AppI
2013-04-23 00:42:11 +04:00
{
2017-06-26 07:44:04 +03:00
public:
2019-08-18 00:40:38 +03:00
ExampleFontSDF(const char* _name, const char* _description, const char* _url)
: entry::AppI(_name, _description, _url)
2017-06-26 07:44:04 +03:00
{
}
2017-07-15 10:17:29 +03:00
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
2013-04-23 00:42:11 +04:00
{
2017-06-12 22:52:48 +03:00
Args args(_argc, _argv);
2017-06-13 08:43:07 +03:00
2017-06-30 08:23:18 +03:00
m_width = _width;
m_height = _height;
2017-06-26 07:44:04 +03:00
m_debug = BGFX_DEBUG_NONE;
2017-06-12 22:52:48 +03:00
m_reset = BGFX_RESET_VSYNC;
2017-06-13 08:43:07 +03:00
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.platformData.nwh = entry::getNativeWindowHandle(entry::kDefaultWindowHandle);
init.platformData.ndt = entry::getNativeDisplayHandle();
init.platformData.type = entry::getNativeWindowHandleType(entry::kDefaultWindowHandle);
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Enable debug text.
bgfx::setDebug(m_debug);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Set view 0 clear state.
bgfx::setViewClear(0
2017-06-17 21:47:39 +03:00
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
2017-06-13 08:43:07 +03:00
// Initialize Imgui
// This initializes the same allocator used by stb_truetype, so must do that before creating the font manager
2017-06-12 22:52:48 +03:00
imguiCreate();
2017-06-13 08:43:07 +03:00
2019-03-08 18:53:15 +03:00
uint32_t size;
m_txtFile = load("text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt", &size);
m_bigText = bx::StringView( (const char*)m_txtFile, size);
2017-06-13 08:43:07 +03:00
// Set up some defaults.
m_outlineColor = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
m_outlineWidth = 1.0f;
m_dropShadowColor = ImVec4(0.0f, 0.0f, 0.0f, 0.35f);
m_dropShadowOffsetX = 1.0f;
m_dropShadowOffsetY = 1.0f;
m_dropShadowSoftener = 2.0f;
2017-06-13 08:43:07 +03:00
m_textScroll = 0.0f;
m_textRotation = 0.0f;
m_textScale = 1.0f;
m_textSize = 14.0f;
2017-06-13 08:43:07 +03:00
// Init the text rendering system.
m_fontManager = NULL;
m_textBufferManager = NULL;
m_font = BGFX_INVALID_HANDLE;
m_fontSdf = BGFX_INVALID_HANDLE;
m_fontScaled = BGFX_INVALID_HANDLE;
m_scrollableBuffer = BGFX_INVALID_HANDLE;
2017-06-13 08:43:07 +03:00
initializeFont(FONT_TYPE_DISTANCE);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
m_lineCount = m_metrics.getLineCount(m_bigText);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
m_visibleLineCount = 20.0f;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
m_textBegin = 0;
m_textEnd = 0;
m_metrics.getSubText(m_bigText, 0, (uint32_t)m_visibleLineCount, m_textBegin, m_textEnd);
2017-06-13 08:43:07 +03:00
initializeTextBuffer(FONT_TYPE_DISTANCE);
2017-06-13 08:43:07 +03:00
applyTextBufferAttributes();
2017-06-13 08:43:07 +03:00
m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
2017-06-12 22:52:48 +03:00
}
2017-06-13 08:43:07 +03:00
2017-07-15 10:17:29 +03:00
virtual int shutdown() override
2017-06-12 22:52:48 +03:00
{
imguiDestroy();
2017-06-13 08:43:07 +03:00
2019-03-08 18:53:15 +03:00
unload(m_txtFile);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
m_fontManager->destroyTtf(m_font);
// Destroy the fonts.
m_fontManager->destroyFont(m_fontSdf);
m_fontManager->destroyFont(m_fontScaled);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
delete m_textBufferManager;
delete m_fontManager;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Shutdown bgfx.
bgfx::shutdown();
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
return 0;
}
2014-11-04 06:11:08 +03:00
2017-07-15 10:17:29 +03:00
bool update() override
2017-06-12 22:52:48 +03:00
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
2014-11-04 06:11:08 +03:00
{
2017-06-12 22:52:48 +03:00
imguiBeginFrame(m_mouseState.m_mx
2017-06-17 21:47:39 +03:00
, m_mouseState.m_my
, (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
| (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
, m_mouseState.m_mz
, uint16_t(m_width)
, uint16_t(m_height)
);
2017-06-30 09:19:20 +03:00
showExampleDialog(this);
2017-06-26 07:44:04 +03:00
2017-06-27 08:51:56 +03:00
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
2017-12-02 08:04:27 +03:00
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(m_width / 5.0f, m_height / 2.0f)
, ImGuiCond_FirstUseEver
2017-06-27 08:51:56 +03:00
);
ImGui::Begin("Settings"
2017-06-17 21:47:39 +03:00
, NULL
2017-12-02 08:04:27 +03:00
, 0
2017-06-17 21:47:39 +03:00
);
2017-06-12 22:52:48 +03:00
bool recomputeVisibleText = false;
static int fontTypeIndex = 0;
if (ImGui::Combo("SDF Font Type", &fontTypeIndex, "Standard\0Outline\0Outline_Image\0DropShadow\0DropShadow_Image\0Outline_DropShadow_Image\0\0"))
{
uint32_t fontTypeList[] =
{
FONT_TYPE_DISTANCE,
FONT_TYPE_DISTANCE_OUTLINE,
FONT_TYPE_DISTANCE_OUTLINE_IMAGE,
FONT_TYPE_DISTANCE_DROP_SHADOW,
FONT_TYPE_DISTANCE_DROP_SHADOW_IMAGE,
FONT_TYPE_DISTANCE_OUTLINE_DROP_SHADOW_IMAGE
};
uint32_t fontType = fontTypeList[fontTypeIndex];
initializeFont(fontType);
initializeTextBuffer(fontType);
recomputeVisibleText = true;
}
2017-06-17 21:47:39 +03:00
if (ImGui::SliderFloat("Font size", &m_textSize, 6.0f, 64.0f) )
2017-06-12 22:52:48 +03:00
{
m_fontManager->destroyFont(m_fontScaled);
m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, (uint32_t) m_textSize);
m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
recomputeVisibleText = true;
}
2017-06-17 21:47:39 +03:00
if (ImGui::ColorEdit3("Outline Color", (float*)&m_outlineColor))
{
recomputeVisibleText = true;
}
if (ImGui::SliderFloat("Outline Width", &m_outlineWidth, 0.5f, 5.0f))
{
recomputeVisibleText = true;
}
if (ImGui::ColorEdit4("Drop Shadow Color", (float*)&m_dropShadowColor))
{
recomputeVisibleText = true;
}
if (ImGui::SliderFloat("Drop Shadow Offset X", &m_dropShadowOffsetX, -5.0f, 5.0f))
{
recomputeVisibleText = true;
}
if (ImGui::SliderFloat("Drop Shadow Offset Y", &m_dropShadowOffsetY, -5.0f, 5.0f))
{
recomputeVisibleText = true;
}
if (ImGui::SliderFloat("Drop Shadow Softener", &m_dropShadowSoftener, 0.0f, 5.0f))
{
recomputeVisibleText = true;
}
recomputeVisibleText |= ImGui::SliderFloat("# of lines", &m_visibleLineCount, 1.0f, 177.0f);
recomputeVisibleText |= ImGui::SliderFloat("Scroll", &m_textScroll, 0.0f, (m_lineCount-m_visibleLineCount));
ImGui::SliderFloat("Rotate", &m_textRotation, 0.0f, bx::kPi*2.0f);
recomputeVisibleText |= ImGui::SliderFloat("Scale", &m_textScale, 0.1f, 10.0f);
2017-06-17 21:47:39 +03:00
2017-06-12 22:52:48 +03:00
if (recomputeVisibleText)
{
m_textBufferManager->clearTextBuffer(m_scrollableBuffer);
applyTextBufferAttributes();
2017-06-12 22:52:48 +03:00
m_metrics.getSubText(m_bigText,(uint32_t)m_textScroll, (uint32_t)(m_textScroll+m_visibleLineCount), m_textBegin, m_textEnd);
m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
}
2017-06-17 21:47:39 +03:00
ImGui::End();
2017-06-17 21:47:39 +03:00
2017-06-12 22:52:48 +03:00
imguiEndFrame();
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// 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
2018-11-17 08:54:20 +03:00
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -1.0f };
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
float view[16];
bx::mtxLookAt(view, eye, at);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Setup a top-left ortho matrix for screen space drawing.
2017-06-13 08:43:07 +03:00
const bgfx::Caps* caps = bgfx::getCaps();
2017-06-12 22:52:48 +03:00
{
float ortho[16];
2023-11-04 07:15:42 +03:00
bx::mtxOrtho(ortho, 0.0f, float(m_width), float(m_height), 0.0f, -1.0f, 1.0f, 0.0f, caps->homogeneousDepth);
2017-06-12 22:52:48 +03:00
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
2017-06-12 22:52:48 +03:00
//very crude approximation :(
float textAreaWidth = 0.5f * 66.0f * m_fontManager->getFontInfo(m_fontScaled).maxAdvanceWidth;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
float textRotMat[16];
float textCenterMat[16];
float textScaleMat[16];
float screenCenterMat[16];
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
bx::mtxRotateZ(textRotMat, m_textRotation);
bx::mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-m_visibleLineCount)*m_metrics.getLineHeight()*0.5f, 0);
bx::mtxScale(textScaleMat, m_textScale, m_textScale, 1.0f);
bx::mtxTranslate(screenCenterMat, ( (m_width) * 0.5f), ( (m_height) * 0.5f), 0);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
//first translate to text center, then scale, then rotate
float tmpMat[16];
bx::mtxMul(tmpMat, textCenterMat, textRotMat);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
float tmpMat2[16];
bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
float tmpMat3[16];
bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Set model matrix for rendering.
bgfx::setTransform(tmpMat3);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Draw your text.
m_textBufferManager->submitTextBuffer(m_scrollableBuffer, 0);
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +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
2017-06-30 09:19:20 +03:00
return true;
2014-11-04 06:11:08 +03:00
}
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
return false;
2013-04-23 00:42:11 +04:00
}
void initializeFont(uint32_t fontType)
{
if (m_font.idx != bgfx::kInvalidHandle)
{
m_fontManager->destroyTtf(m_font);
}
if (m_scrollableBuffer.idx != bgfx::kInvalidHandle)
{
m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
}
if (m_fontScaled.idx != bgfx::kInvalidHandle)
{
m_fontManager->destroyFont(m_fontScaled);
}
if (m_fontSdf.idx != bgfx::kInvalidHandle)
{
m_fontManager->destroyFont(m_fontSdf);
}
delete m_textBufferManager;
delete m_fontManager;
m_fontManager = new FontManager(512);
m_textBufferManager = new TextBufferManager(m_fontManager);
m_font = loadTtf(m_fontManager, "font/special_elite.ttf");
m_fontSdf = m_fontManager->createFontByPixelSize(m_font, 0, 48, fontType, 6 + 2, 6 + 2);
m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, (uint32_t) m_textSize);
if (fontType & FONT_TYPE_MASK_DISTANCE_IMAGE)
{
float extraScale = 2.0f;
bimg::ImageContainer* glyphImage = imageLoad("font/glyph_space.png", bgfx::TextureFormat::Enum::BGRA8);
m_fontManager->addGlyphBitmap(m_fontSdf, 32, (uint16_t)glyphImage->m_width, (uint16_t)glyphImage->m_height, (uint16_t)(glyphImage->m_width * 4), extraScale, (const uint8_t*)glyphImage->m_data, 0.0f, -3.0f);
glyphImage = imageLoad("font/glyph_long.png", bgfx::TextureFormat::Enum::BGRA8);
m_fontManager->addGlyphBitmap(m_fontSdf, 65, (uint16_t)glyphImage->m_width, (uint16_t)glyphImage->m_height, (uint16_t)(glyphImage->m_width * 4), extraScale, (const uint8_t*)glyphImage->m_data, 0.0f, -3.0f);
}
m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
}
void initializeTextBuffer(uint32_t fontType)
{
m_scrollableBuffer = m_textBufferManager->createTextBuffer(fontType, BufferType::Transient);
}
void applyTextBufferAttributes()
{
m_textBufferManager->setTextColor(m_scrollableBuffer, 0xFFFFFFFF);
m_textBufferManager->setOutlineColor(m_scrollableBuffer, ((uint32_t)(m_outlineColor.x * 255) << 24) | ((uint32_t)(m_outlineColor.y * 255) << 16) | ((uint32_t)(m_outlineColor.z * 255) << 8) | ((uint32_t)(m_outlineColor.w * 255)));
m_textBufferManager->setOutlineWidth(m_scrollableBuffer, m_outlineWidth);
m_textBufferManager->setDropShadowColor(m_scrollableBuffer, ((uint32_t)(m_dropShadowColor.x * 255) << 24) | ((uint32_t)(m_dropShadowColor.y * 255) << 16) | ((uint32_t)(m_dropShadowColor.z * 255) << 8) | ((uint32_t)(m_dropShadowColor.w * 255)));
m_textBufferManager->setDropShadowOffset(m_scrollableBuffer, m_dropShadowOffsetX, m_dropShadowOffsetY);
m_textBufferManager->setDropShadowSoftener(m_scrollableBuffer, m_dropShadowSoftener);
}
2017-06-12 22:52:48 +03:00
entry::MouseState m_mouseState;
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
2017-06-13 08:43:07 +03:00
2019-03-08 18:53:15 +03:00
void* m_txtFile;
bx::StringView m_bigText;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
// Init the text rendering system.
FontManager* m_fontManager;
TextBufferManager* m_textBufferManager;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
TrueTypeHandle m_font;
FontHandle m_fontSdf;
FontHandle m_fontScaled;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
TextBufferHandle m_scrollableBuffer;
2017-06-13 08:43:07 +03:00
2017-06-12 22:52:48 +03:00
TextLineMetrics m_metrics = TextLineMetrics(FontInfo());
uint32_t m_lineCount;
float m_visibleLineCount;
const char* m_textBegin;
const char* m_textEnd;
2017-06-13 08:43:07 +03:00
ImVec4 m_outlineColor;
float m_outlineWidth;
ImVec4 m_dropShadowColor;
float m_dropShadowOffsetX;
float m_dropShadowOffsetY;
float m_dropShadowSoftener;
2017-06-12 22:52:48 +03:00
float m_textScroll;
float m_textRotation;
float m_textScale;
float m_textSize;
};
2017-06-26 07:44:04 +03:00
} // namespace
2019-08-17 23:25:39 +03:00
ENTRY_IMPLEMENT_MAIN(
2019-08-18 00:40:38 +03:00
ExampleFontSDF
, "11-fontsdf"
, "Use a single distance field font to render text of various size."
, "https://bkaradzic.github.io/bgfx/examples.html#fontsdf"
);