1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* Copyright 2013 Jeremie Roy. All rights reserved.
* License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
*/
#include "common.h"
#include "bgfx_utils.h"
#include <bgfx/bgfx.h>
#include <bx/timer.h>
#include <bx/math.h>
#include "font/font_manager.h"
#include "font/text_metrics.h"
#include "font/text_buffer_manager.h"
#include "imgui/imgui.h"
namespace
{
TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
{
uint32_t size;
void* data = load(_filePath, &size);
if (NULL != data)
{
TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
BX_FREE(entry::getAllocator(), data);
return handle;
}
TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
return invalid;
}
class ExampleFontSDF : public entry::AppI
{
public:
ExampleFontSDF(const char* _name, const char* _description, const char* _url)
: entry::AppI(_name, _description, _url)
{
}
void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
{
Args args(_argc, _argv);
m_width = _width;
m_height = _height;
m_debug = BGFX_DEBUG_NONE;
m_reset = BGFX_RESET_VSYNC;
bgfx::Init init;
init.type = args.m_type;
init.vendorId = args.m_pciId;
init.resolution.width = m_width;
init.resolution.height = m_height;
init.resolution.reset = m_reset;
bgfx::init(init);
// Enable debug text.
bgfx::setDebug(m_debug);
// Set view 0 clear state.
bgfx::setViewClear(0
, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
, 0x303030ff
, 1.0f
, 0
);
// Imgui.
imguiCreate();
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);
// Init the text rendering system.
m_fontManager = new FontManager(512);
m_textBufferManager = new TextBufferManager(m_fontManager);
m_font = loadTtf(m_fontManager, "font/special_elite.ttf");
// Create a distance field font.
m_fontSdf = m_fontManager->createFontByPixelSize(m_font, 0, 48, FONT_TYPE_DISTANCE);
// Create a scaled down version of the same font (without adding anything to the atlas).
m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, 14);
m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
m_lineCount = m_metrics.getLineCount(m_bigText);
m_visibleLineCount = 20.0f;
m_textBegin = 0;
m_textEnd = 0;
m_metrics.getSubText(m_bigText, 0, (uint32_t)m_visibleLineCount, m_textBegin, m_textEnd);
m_scrollableBuffer = m_textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, BufferType::Transient);
m_textBufferManager->setTextColor(m_scrollableBuffer, 0xFFFFFFFF);
m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
m_textScroll = 0.0f;
m_textRotation = 0.0f;
m_textScale = 1.0f;
m_textSize = 14.0f;
}
virtual int shutdown() override
{
imguiDestroy();
unload(m_txtFile);
m_fontManager->destroyTtf(m_font);
// Destroy the fonts.
m_fontManager->destroyFont(m_fontSdf);
m_fontManager->destroyFont(m_fontScaled);
m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
delete m_textBufferManager;
delete m_fontManager;
// Shutdown bgfx.
bgfx::shutdown();
return 0;
}
bool update() override
{
if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
{
imguiBeginFrame(m_mouseState.m_mx
, 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)
);
showExampleDialog(this);
ImGui::SetNextWindowPos(
ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
, ImGuiCond_FirstUseEver
);
ImGui::SetNextWindowSize(
ImVec2(m_width / 5.0f, m_height / 2.0f)
, ImGuiCond_FirstUseEver
);
ImGui::Begin("Settings"
, NULL
, 0
);
bool recomputeVisibleText = false;
recomputeVisibleText |= ImGui::SliderFloat("# of lines", &m_visibleLineCount, 1.0f, 177.0f);
if (ImGui::SliderFloat("Font size", &m_textSize, 6.0f, 64.0f) )
{
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;
}
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);
if (recomputeVisibleText)
{
m_textBufferManager->clearTextBuffer(m_scrollableBuffer);
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);
}
ImGui::End();
imguiEndFrame();
// Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_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::touch(0);
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -1.0f };
float view[16];
bx::mtxLookAt(view, eye, at);
const float centering = 0.5f;
// Setup a top-left ortho matrix for screen space drawing.
const bgfx::Caps* caps = bgfx::getCaps();
{
float ortho[16];
bx::mtxOrtho(ortho, centering, m_width + centering, m_height + centering, centering, -1.0f, 1.0f, 0.0f, caps->homogeneousDepth);
bgfx::setViewTransform(0, view, ortho);
bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
}
//very crude approximation :(
float textAreaWidth = 0.5f * 66.0f * m_fontManager->getFontInfo(m_fontScaled).maxAdvanceWidth;
float textRotMat[16];
float textCenterMat[16];
float textScaleMat[16];
float screenCenterMat[16];
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);
//first translate to text center, then scale, then rotate
float tmpMat[16];
bx::mtxMul(tmpMat, textCenterMat, textRotMat);
float tmpMat2[16];
bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
float tmpMat3[16];
bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
// Set model matrix for rendering.
bgfx::setTransform(tmpMat3);
// Draw your text.
m_textBufferManager->submitTextBuffer(m_scrollableBuffer, 0);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame();
return true;
}
return false;
}
entry::MouseState m_mouseState;
uint32_t m_width;
uint32_t m_height;
uint32_t m_debug;
uint32_t m_reset;
void* m_txtFile;
bx::StringView m_bigText;
// Init the text rendering system.
FontManager* m_fontManager;
TextBufferManager* m_textBufferManager;
TrueTypeHandle m_font;
FontHandle m_fontSdf;
FontHandle m_fontScaled;
TextBufferHandle m_scrollableBuffer;
TextLineMetrics m_metrics = TextLineMetrics(FontInfo());
uint32_t m_lineCount;
float m_visibleLineCount;
const char* m_textBegin;
const char* m_textEnd;
float m_textScroll;
float m_textRotation;
float m_textScale;
float m_textSize;
};
} // namespace
ENTRY_IMPLEMENT_MAIN(
ExampleFontSDF
, "11-fontsdf"
, "Use a single distance field font to render text of various size."
, "https://bkaradzic.github.io/bgfx/examples.html#fontsdf"
);
|