fontsdf.cpp 8.2 KB

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