font.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 <bx/timer.h>
  8. #include <bx/string.h>
  9. #include <bx/fpumath.h>
  10. #include "font/font_manager.h"
  11. #include "font/text_buffer_manager.h"
  12. #include "entry/input.h"
  13. #include <stdio.h>
  14. #include <wchar.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. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  24. {
  25. FILE* file = fopen(_filePath, "rb");
  26. if (NULL != file)
  27. {
  28. uint32_t size = (uint32_t)fsize(file);
  29. uint8_t* mem = (uint8_t*)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. TrueTypeHandle handle = _fm->createTtf(mem, size);
  35. free(mem);
  36. return handle;
  37. }
  38. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  39. return invalid;
  40. }
  41. int _main_(int _argc, char** _argv)
  42. {
  43. Args args(_argc, _argv);
  44. uint32_t width = 1280;
  45. uint32_t height = 720;
  46. uint32_t debug = BGFX_DEBUG_TEXT;
  47. uint32_t reset = BGFX_RESET_VSYNC;
  48. bgfx::init(args.m_type, args.m_pciId);
  49. bgfx::reset(width, height, reset);
  50. // Enable debug text.
  51. bgfx::setDebug(debug);
  52. // Set view 0 clear state.
  53. bgfx::setViewClear(0
  54. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  55. , 0x303030ff
  56. , 1.0f
  57. , 0
  58. );
  59. // Init the text rendering system.
  60. FontManager* fontManager = new FontManager(512);
  61. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  62. // Load some TTF files.
  63. const char* fontFilePath[7] =
  64. {
  65. "font/droidsans.ttf",
  66. "font/chp-fire.ttf",
  67. "font/bleeding_cowboys.ttf",
  68. "font/mias_scribblings.ttf",
  69. "font/ruritania.ttf",
  70. "font/signika-regular.ttf",
  71. "font/five_minutes.otf",
  72. };
  73. const uint32_t numFonts = BX_COUNTOF(fontFilePath);
  74. TrueTypeHandle fontFiles[numFonts];
  75. FontHandle fonts[numFonts];
  76. for (uint32_t ii = 0; ii < numFonts; ++ii)
  77. {
  78. // Instantiate a usable font.
  79. fontFiles[ii] = loadTtf(fontManager, fontFilePath[ii]);
  80. fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
  81. // Preload glyphs and blit them to atlas.
  82. fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  83. // You can unload the truetype files at this stage, but in that
  84. // case, the set of glyph's will be limited to the set of preloaded
  85. // glyph.
  86. fontManager->destroyTtf(fontFiles[ii]);
  87. }
  88. TrueTypeHandle fontAwesomeTtf = loadTtf(fontManager, "font/fontawesome-webfont.ttf");
  89. // This font doesn't have any preloaded glyph's but the truetype file
  90. // is loaded so glyph will be generated as needed.
  91. FontHandle fontAwesome72 = fontManager->createFontByPixelSize(fontAwesomeTtf, 0, 72);
  92. TrueTypeHandle visitorTtf = loadTtf(fontManager, "font/visitor1.ttf");
  93. // This font doesn't have any preloaded glyph's but the truetype file
  94. // is loaded so glyph will be generated as needed.
  95. FontHandle visitor10 = fontManager->createFontByPixelSize(visitorTtf, 0, 10);
  96. //create a static text buffer compatible with alpha font
  97. //a static text buffer content cannot be modified after its first submit.
  98. TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Static);
  99. // The pen position represent the top left of the box of the first line
  100. // of text.
  101. textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
  102. for (uint32_t ii = 0; ii < numFonts; ++ii)
  103. {
  104. // Add some text to the buffer.
  105. // The position of the pen is adjusted when there is an endline.
  106. textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
  107. }
  108. // Now write some styled text.
  109. // Setup style colors.
  110. textBufferManager->setBackgroundColor(staticText, 0x551111ff);
  111. textBufferManager->setUnderlineColor(staticText, 0xff2222ff);
  112. textBufferManager->setOverlineColor(staticText, 0x2222ffff);
  113. textBufferManager->setStrikeThroughColor(staticText, 0x22ff22ff);
  114. // Background.
  115. textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
  116. textBufferManager->appendText(staticText, fonts[0], L"The quick ");
  117. // Strike-through.
  118. textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
  119. textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
  120. // Overline.
  121. textBufferManager->setStyle(staticText, STYLE_OVERLINE);
  122. textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
  123. // Underline.
  124. textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
  125. textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
  126. // Background + strike-through.
  127. textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
  128. textBufferManager->appendText(staticText, fonts[0], L"dog\n");
  129. textBufferManager->setStyle(staticText, STYLE_NORMAL);
  130. textBufferManager->appendText(staticText, fontAwesome72, L"\xf011 \xf02e \xf061 \xf087 \xf0d9 \xf099 \xf05c \xf021 \xf113\n");
  131. // Create a transient buffer for real-time data.
  132. TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
  133. while (!entry::processEvents(width, height, debug, reset) )
  134. {
  135. // This dummy draw call is here to make sure that view 0 is cleared
  136. // if no other draw calls are submitted to view 0.
  137. bgfx::touch(0);
  138. int64_t now = bx::getHPCounter();
  139. static int64_t last = now;
  140. const int64_t frameTime = now - last;
  141. last = now;
  142. const double freq = double(bx::getHPFrequency() );
  143. const double toMs = 1000.0 / freq;
  144. // Use debug font to print information about this example.
  145. bgfx::dbgTextClear();
  146. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font");
  147. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text.");
  148. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  149. // Use transient text to display debug information.
  150. wchar_t fpsText[64];
  151. bx::swnprintf(fpsText, BX_COUNTOF(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
  152. textBufferManager->clearTextBuffer(transientText);
  153. textBufferManager->setPenPosition(transientText, width - 150.0f, 10.0f);
  154. textBufferManager->appendText(transientText, visitor10, L"Transient\n");
  155. textBufferManager->appendText(transientText, visitor10, L"text buffer\n");
  156. textBufferManager->appendText(transientText, visitor10, fpsText);
  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 offsetx = width/2.0f;
  175. bx::mtxOrtho(ortho[0], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset0);
  176. bx::mtxOrtho(ortho[1], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset1);
  177. bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  178. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  179. }
  180. else
  181. {
  182. float ortho[16];
  183. bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  184. bgfx::setViewTransform(0, view, ortho);
  185. bgfx::setViewRect(0, 0, 0, width, height);
  186. }
  187. // Submit the debug text.
  188. textBufferManager->submitTextBuffer(transientText, 0);
  189. // Submit the static text.
  190. textBufferManager->submitTextBuffer(staticText, 0);
  191. // Advance to next frame. Rendering thread will be kicked to
  192. // process submitted rendering primitives.
  193. bgfx::frame();
  194. }
  195. fontManager->destroyTtf(fontAwesomeTtf);
  196. fontManager->destroyTtf(visitorTtf);
  197. // Destroy the fonts.
  198. fontManager->destroyFont(fontAwesome72);
  199. fontManager->destroyFont(visitor10);
  200. for (uint32_t ii = 0; ii < numFonts; ++ii)
  201. {
  202. fontManager->destroyFont(fonts[ii]);
  203. }
  204. textBufferManager->destroyTextBuffer(staticText);
  205. textBufferManager->destroyTextBuffer(transientText);
  206. delete textBufferManager;
  207. delete fontManager;
  208. // Shutdown bgfx.
  209. bgfx::shutdown();
  210. return 0;
  211. }