fontsdf.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/fpumath.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. #include <stdio.h>
  15. #include <string.h>
  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. int _main_(int _argc, char** _argv)
  30. {
  31. Args args(_argc, _argv);
  32. uint32_t width = 1280;
  33. uint32_t height = 720;
  34. uint32_t debug = BGFX_DEBUG_TEXT;
  35. uint32_t reset = BGFX_RESET_VSYNC;
  36. bgfx::init(args.m_type, args.m_pciId);
  37. bgfx::reset(width, height, reset);
  38. // Enable debug text.
  39. bgfx::setDebug(debug);
  40. // Set view 0 clear state.
  41. bgfx::setViewClear(0
  42. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  43. , 0x303030ff
  44. , 1.0f
  45. , 0
  46. );
  47. // Imgui.
  48. imguiCreate();
  49. char* bigText = (char*)load("text/sherlock_holmes_a_scandal_in_bohemia_arthur_conan_doyle.txt");
  50. // Init the text rendering system.
  51. FontManager* fontManager = new FontManager(512);
  52. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  53. TrueTypeHandle font = loadTtf(fontManager, "font/special_elite.ttf");
  54. // Create a distance field font.
  55. FontHandle fontSdf = fontManager->createFontByPixelSize(font, 0, 48, FONT_TYPE_DISTANCE);
  56. // Create a scaled down version of the same font (without adding anything to the atlas).
  57. FontHandle fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, 14);
  58. TextLineMetrics metrics(fontManager->getFontInfo(fontScaled) );
  59. uint32_t lineCount = metrics.getLineCount(bigText);
  60. float visibleLineCount = 20.0f;
  61. const char* textBegin = 0;
  62. const char* textEnd = 0;
  63. metrics.getSubText(bigText, 0, (uint32_t)visibleLineCount, textBegin, textEnd);
  64. TextBufferHandle scrollableBuffer = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, BufferType::Transient);
  65. textBufferManager->setTextColor(scrollableBuffer, 0xFFFFFFFF);
  66. textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
  67. entry::MouseState mouseState;
  68. int32_t scrollArea = 0;
  69. const int32_t guiPanelWidth = 250;
  70. const int32_t guiPanelHeight = 200;
  71. float textScroll = 0.0f;
  72. float textRotation = 0.0f;
  73. float textScale = 1.0f;
  74. float textSize = 14.0f;
  75. while (!entry::processEvents(width, height, debug, reset, &mouseState) )
  76. {
  77. imguiBeginFrame(mouseState.m_mx
  78. , mouseState.m_my
  79. , (mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  80. | (mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  81. | (mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  82. , mouseState.m_mz
  83. , width
  84. , height
  85. );
  86. imguiBeginScrollArea("Text Area"
  87. , width - guiPanelWidth - 10
  88. , 10
  89. , guiPanelWidth
  90. , guiPanelHeight
  91. , &scrollArea
  92. );
  93. imguiSeparatorLine();
  94. bool recomputeVisibleText = false;
  95. recomputeVisibleText |= imguiSlider("Number of lines", visibleLineCount, 1.0f, 177.0f , 1.0f);
  96. if (imguiSlider("Font size", textSize, 6.0f, 64.0f , 1.0f) )
  97. {
  98. fontManager->destroyFont(fontScaled);
  99. fontScaled = fontManager->createScaledFontToPixelSize(fontSdf, (uint32_t) textSize);
  100. metrics = TextLineMetrics(fontManager->getFontInfo(fontScaled) );
  101. recomputeVisibleText = true;
  102. }
  103. recomputeVisibleText |= imguiSlider("Scroll", textScroll, 0.0f, (lineCount-visibleLineCount) , 1.0f);
  104. imguiSlider("Rotate", textRotation, 0.0f, bx::pi*2.0f , 0.1f);
  105. recomputeVisibleText |= imguiSlider("Scale", textScale, 0.1f, 10.0f , 0.1f);
  106. if (recomputeVisibleText)
  107. {
  108. textBufferManager->clearTextBuffer(scrollableBuffer);
  109. metrics.getSubText(bigText,(uint32_t)textScroll, (uint32_t)(textScroll+visibleLineCount), textBegin, textEnd);
  110. textBufferManager->appendText(scrollableBuffer, fontScaled, textBegin, textEnd);
  111. }
  112. imguiEndScrollArea();
  113. imguiEndFrame();
  114. // Set view 0 default viewport.
  115. bgfx::setViewRect(0, 0, 0, width, height);
  116. // This dummy draw call is here to make sure that view 0 is cleared
  117. // if no other draw calls are submitted to view 0.
  118. bgfx::touch(0);
  119. int64_t now = bx::getHPCounter();
  120. static int64_t last = now;
  121. const int64_t frameTime = now - last;
  122. last = now;
  123. const double freq = double(bx::getHPFrequency() );
  124. const double toMs = 1000.0 / freq;
  125. // Use debug font to print32_t information about this example.
  126. bgfx::dbgTextClear();
  127. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontsdf");
  128. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of various size.");
  129. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime) * toMs);
  130. float at[3] = { 0, 0, 0.0f };
  131. float eye[3] = {0, 0, -1.0f };
  132. float view[16];
  133. bx::mtxLookAt(view, eye, at);
  134. const float centering = 0.5f;
  135. // Setup a top-left ortho matrix for screen space drawing.
  136. const bgfx::HMD* hmd = bgfx::getHMD();
  137. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  138. {
  139. float proj[16];
  140. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  141. static float time = 0.0f;
  142. time += 0.05f;
  143. const float dist = 10.0f;
  144. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  145. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  146. float ortho[2][16];
  147. const float viewOffset = width/4.0f;
  148. const float viewWidth = width/2.0f;
  149. bx::mtxOrtho(ortho[0], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset0);
  150. bx::mtxOrtho(ortho[1], centering + viewOffset, centering + viewOffset + viewWidth, height + centering, centering, -1.0f, 1.0f, offset1);
  151. bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  152. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  153. }
  154. else
  155. {
  156. float ortho[16];
  157. bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  158. bgfx::setViewTransform(0, view, ortho);
  159. bgfx::setViewRect(0, 0, 0, width, height);
  160. }
  161. //very crude approximation :(
  162. float textAreaWidth = 0.5f * 66.0f * fontManager->getFontInfo(fontScaled).maxAdvanceWidth;
  163. float textRotMat[16];
  164. float textCenterMat[16];
  165. float textScaleMat[16];
  166. float screenCenterMat[16];
  167. bx::mtxRotateZ(textRotMat, textRotation);
  168. bx::mtxTranslate(textCenterMat, -(textAreaWidth * 0.5f), (-visibleLineCount)*metrics.getLineHeight()*0.5f, 0);
  169. bx::mtxScale(textScaleMat, textScale, textScale, 1.0f);
  170. bx::mtxTranslate(screenCenterMat, ( (width) * 0.5f), ( (height) * 0.5f), 0);
  171. //first translate to text center, then scale, then rotate
  172. float tmpMat[16];
  173. bx::mtxMul(tmpMat, textCenterMat, textRotMat);
  174. float tmpMat2[16];
  175. bx::mtxMul(tmpMat2, tmpMat, textScaleMat);
  176. float tmpMat3[16];
  177. bx::mtxMul(tmpMat3, tmpMat2, screenCenterMat);
  178. // Set model matrix for rendering.
  179. bgfx::setTransform(tmpMat3);
  180. // Draw your text.
  181. textBufferManager->submitTextBuffer(scrollableBuffer, 0);
  182. // Advance to next frame. Rendering thread will be kicked to
  183. // process submitted rendering primitives.
  184. bgfx::frame();
  185. }
  186. imguiDestroy();
  187. BX_FREE(entry::getAllocator(), bigText);
  188. fontManager->destroyTtf(font);
  189. // Destroy the fonts.
  190. fontManager->destroyFont(fontSdf);
  191. fontManager->destroyFont(fontScaled);
  192. textBufferManager->destroyTextBuffer(scrollableBuffer);
  193. delete textBufferManager;
  194. delete fontManager;
  195. // Shutdown bgfx.
  196. bgfx::shutdown();
  197. return 0;
  198. }