fontsdf.cpp 8.2 KB

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