2
0

fontsdf.cpp 7.8 KB

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