fontsdf.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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)
  33. : entry::AppI(_name, _description)
  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(args.m_type, args.m_pciId);
  44. bgfx::reset(m_width, m_height, m_reset);
  45. // Enable debug text.
  46. bgfx::setDebug(m_debug);
  47. // Set view 0 clear state.
  48. bgfx::setViewClear(0
  49. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  50. , 0x303030ff
  51. , 1.0f
  52. , 0
  53. );
  54. // Imgui.
  55. imguiCreate();
  56. m_bigText = (char*)load("text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
  57. // Init the text rendering system.
  58. m_fontManager = new FontManager(512);
  59. m_textBufferManager = new TextBufferManager(m_fontManager);
  60. m_font = loadTtf(m_fontManager, "font/special_elite.ttf");
  61. // Create a distance field font.
  62. m_fontSdf = m_fontManager->createFontByPixelSize(m_font, 0, 48, FONT_TYPE_DISTANCE);
  63. // Create a scaled down version of the same font (without adding anything to the atlas).
  64. m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, 14);
  65. m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
  66. m_lineCount = m_metrics.getLineCount(m_bigText);
  67. m_visibleLineCount = 20.0f;
  68. m_textBegin = 0;
  69. m_textEnd = 0;
  70. m_metrics.getSubText(m_bigText, 0, (uint32_t)m_visibleLineCount, m_textBegin, m_textEnd);
  71. m_scrollableBuffer = m_textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, BufferType::Transient);
  72. m_textBufferManager->setTextColor(m_scrollableBuffer, 0xFFFFFFFF);
  73. m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
  74. m_textScroll = 0.0f;
  75. m_textRotation = 0.0f;
  76. m_textScale = 1.0f;
  77. m_textSize = 14.0f;
  78. }
  79. virtual int shutdown() override
  80. {
  81. imguiDestroy();
  82. BX_FREE(entry::getAllocator(), m_bigText);
  83. m_fontManager->destroyTtf(m_font);
  84. // Destroy the fonts.
  85. m_fontManager->destroyFont(m_fontSdf);
  86. m_fontManager->destroyFont(m_fontScaled);
  87. m_textBufferManager->destroyTextBuffer(m_scrollableBuffer);
  88. delete m_textBufferManager;
  89. delete m_fontManager;
  90. // Shutdown bgfx.
  91. bgfx::shutdown();
  92. return 0;
  93. }
  94. bool update() override
  95. {
  96. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  97. {
  98. imguiBeginFrame(m_mouseState.m_mx
  99. , m_mouseState.m_my
  100. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  101. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  102. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  103. , m_mouseState.m_mz
  104. , uint16_t(m_width)
  105. , uint16_t(m_height)
  106. );
  107. showExampleDialog(this);
  108. ImGui::SetNextWindowPos(
  109. ImVec2(m_width - m_width / 5.0f - 10.0f, 10.0f)
  110. , ImGuiCond_FirstUseEver
  111. );
  112. ImGui::SetNextWindowSize(
  113. ImVec2(m_width / 5.0f, m_height / 2.0f)
  114. , ImGuiCond_FirstUseEver
  115. );
  116. ImGui::Begin("Settings"
  117. , NULL
  118. , 0
  119. );
  120. bool recomputeVisibleText = false;
  121. recomputeVisibleText |= ImGui::SliderFloat("# of lines", &m_visibleLineCount, 1.0f, 177.0f);
  122. if (ImGui::SliderFloat("Font size", &m_textSize, 6.0f, 64.0f) )
  123. {
  124. m_fontManager->destroyFont(m_fontScaled);
  125. m_fontScaled = m_fontManager->createScaledFontToPixelSize(m_fontSdf, (uint32_t) m_textSize);
  126. m_metrics = TextLineMetrics(m_fontManager->getFontInfo(m_fontScaled) );
  127. recomputeVisibleText = true;
  128. }
  129. recomputeVisibleText |= ImGui::SliderFloat("Scroll", &m_textScroll, 0.0f, (m_lineCount-m_visibleLineCount));
  130. ImGui::SliderFloat("Rotate", &m_textRotation, 0.0f, bx::kPi*2.0f);
  131. recomputeVisibleText |= ImGui::SliderFloat("Scale", &m_textScale, 0.1f, 10.0f);
  132. if (recomputeVisibleText)
  133. {
  134. m_textBufferManager->clearTextBuffer(m_scrollableBuffer);
  135. m_metrics.getSubText(m_bigText,(uint32_t)m_textScroll, (uint32_t)(m_textScroll+m_visibleLineCount), m_textBegin, m_textEnd);
  136. m_textBufferManager->appendText(m_scrollableBuffer, m_fontScaled, m_textBegin, m_textEnd);
  137. }
  138. ImGui::End();
  139. imguiEndFrame();
  140. // Set view 0 default viewport.
  141. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  142. // This dummy draw call is here to make sure that view 0 is cleared
  143. // if no other draw calls are submitted to view 0.
  144. bgfx::touch(0);
  145. float at[3] = { 0.0f, 0.0f, 0.0f };
  146. float eye[3] = { 0.0f, 0.0f, -1.0f };
  147. float view[16];
  148. bx::mtxLookAt(view, eye, at);
  149. const float centering = 0.5f;
  150. // Setup a top-left ortho matrix for screen space drawing.
  151. const bgfx::HMD* hmd = bgfx::getHMD();
  152. const bgfx::Caps* caps = bgfx::getCaps();
  153. if (NULL != hmd
  154. && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  155. {
  156. float proj[16];
  157. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f, caps->homogeneousDepth);
  158. static float time = 0.0f;
  159. time += 0.05f;
  160. const float dist = 10.0f;
  161. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  162. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  163. float ortho[2][16];
  164. const float viewOffset = m_width/4.0f;
  165. const float viewWidth = m_width/2.0f;
  166. bx::mtxOrtho(ortho[0], centering + viewOffset, centering + viewOffset + viewWidth, m_height + centering, centering, -1.0f, 1.0f, offset0, caps->homogeneousDepth);
  167. bx::mtxOrtho(ortho[1], centering + viewOffset, centering + viewOffset + viewWidth, m_height + centering, centering, -1.0f, 1.0f, offset1, caps->homogeneousDepth);
  168. bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  169. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  170. }
  171. else
  172. {
  173. float ortho[16];
  174. bx::mtxOrtho(ortho, centering, m_width + centering, m_height + centering, centering, -1.0f, 1.0f, 0.0f, caps->homogeneousDepth);
  175. bgfx::setViewTransform(0, view, ortho);
  176. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  177. }
  178. //very crude approximation :(
  179. float textAreaWidth = 0.5f * 66.0f * m_fontManager->getFontInfo(m_fontScaled).maxAdvanceWidth;
  180. float textRotMat[16];
  181. float textCenterMat[16];
  182. float textScaleMat[16];
  183. float screenCenterMat[16];
  184. bx::mtxRotateZ(textRotMat, m_textRotation);
  185. bx::mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-m_visibleLineCount)*m_metrics.getLineHeight()*0.5f, 0);
  186. bx::mtxScale(textScaleMat, m_textScale, m_textScale, 1.0f);
  187. bx::mtxTranslate(screenCenterMat, ( (m_width) * 0.5f), ( (m_height) * 0.5f), 0);
  188. //first translate to text center, then scale, then rotate
  189. float tmpMat[16];
  190. bx::mtxMul(tmpMat, textCenterMat, textRotMat);
  191. float tmpMat2[16];
  192. bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
  193. float tmpMat3[16];
  194. bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
  195. // Set model matrix for rendering.
  196. bgfx::setTransform(tmpMat3);
  197. // Draw your text.
  198. m_textBufferManager->submitTextBuffer(m_scrollableBuffer, 0);
  199. // Advance to next frame. Rendering thread will be kicked to
  200. // process submitted rendering primitives.
  201. bgfx::frame();
  202. return true;
  203. }
  204. return false;
  205. }
  206. entry::MouseState m_mouseState;
  207. uint32_t m_width;
  208. uint32_t m_height;
  209. uint32_t m_debug;
  210. uint32_t m_reset;
  211. char* m_bigText;
  212. // Init the text rendering system.
  213. FontManager* m_fontManager;
  214. TextBufferManager* m_textBufferManager;
  215. TrueTypeHandle m_font;
  216. FontHandle m_fontSdf;
  217. FontHandle m_fontScaled;
  218. TextBufferHandle m_scrollableBuffer;
  219. TextLineMetrics m_metrics = TextLineMetrics(FontInfo());
  220. uint32_t m_lineCount;
  221. float m_visibleLineCount;
  222. const char* m_textBegin;
  223. const char* m_textEnd;
  224. float m_textScroll;
  225. float m_textRotation;
  226. float m_textScale;
  227. float m_textSize;
  228. };
  229. } // namespace
  230. ENTRY_IMPLEMENT_MAIN(ExampleFontSDF, "11-fontsdf", "Use a single distance field font to render text of various size.");