fontsdf.cpp 8.2 KB

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