font.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <iconfontheaders/icons_font_awesome.h>
  14. #include <iconfontheaders/icons_kenney.h>
  15. #include <stdio.h>
  16. #include <wchar.h>
  17. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  18. {
  19. uint32_t size;
  20. void* data = load(_filePath, &size);
  21. if (NULL != data)
  22. {
  23. TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
  24. BX_FREE(entry::getAllocator(), data);
  25. return handle;
  26. }
  27. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  28. return invalid;
  29. }
  30. int _main_(int _argc, char** _argv)
  31. {
  32. Args args(_argc, _argv);
  33. uint32_t width = 1280;
  34. uint32_t height = 720;
  35. uint32_t debug = BGFX_DEBUG_TEXT;
  36. uint32_t reset = BGFX_RESET_VSYNC;
  37. bgfx::init(args.m_type, args.m_pciId);
  38. bgfx::reset(width, height, reset);
  39. // Enable debug text.
  40. bgfx::setDebug(debug);
  41. // Set view 0 clear state.
  42. bgfx::setViewClear(0
  43. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  44. , 0x303030ff
  45. , 1.0f
  46. , 0
  47. );
  48. // Init the text rendering system.
  49. FontManager* fontManager = new FontManager(512);
  50. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  51. // Load some TTF files.
  52. const char* fontFilePath[7] =
  53. {
  54. "font/droidsans.ttf",
  55. "font/chp-fire.ttf",
  56. "font/bleeding_cowboys.ttf",
  57. "font/mias_scribblings.ttf",
  58. "font/ruritania.ttf",
  59. "font/signika-regular.ttf",
  60. "font/five_minutes.otf",
  61. };
  62. const uint32_t numFonts = BX_COUNTOF(fontFilePath);
  63. TrueTypeHandle fontFiles[numFonts];
  64. FontHandle fonts[numFonts];
  65. for (uint32_t ii = 0; ii < numFonts; ++ii)
  66. {
  67. // Instantiate a usable font.
  68. fontFiles[ii] = loadTtf(fontManager, fontFilePath[ii]);
  69. fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
  70. // Preload glyphs and blit them to atlas.
  71. fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  72. // You can unload the truetype files at this stage, but in that
  73. // case, the set of glyph's will be limited to the set of preloaded
  74. // glyph.
  75. fontManager->destroyTtf(fontFiles[ii]);
  76. }
  77. TrueTypeHandle fontAwesomeTtf = loadTtf(fontManager, "font/fontawesome-webfont.ttf");
  78. TrueTypeHandle fontKenneyTtf = loadTtf(fontManager, "font/kenney-icon-font.ttf");
  79. // This font doesn't have any preloaded glyph's but the truetype file
  80. // is loaded so glyph will be generated as needed.
  81. FontHandle fontAwesome72 = fontManager->createFontByPixelSize(fontAwesomeTtf, 0, 72);
  82. FontHandle fontKenney64 = fontManager->createFontByPixelSize(fontKenneyTtf, 0, 64);
  83. TrueTypeHandle visitorTtf = loadTtf(fontManager, "font/visitor1.ttf");
  84. // This font doesn't have any preloaded glyph's but the truetype file
  85. // is loaded so glyph will be generated as needed.
  86. FontHandle visitor10 = fontManager->createFontByPixelSize(visitorTtf, 0, 10);
  87. //create a static text buffer compatible with alpha font
  88. //a static text buffer content cannot be modified after its first submit.
  89. TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Static);
  90. // The pen position represent the top left of the box of the first line
  91. // of text.
  92. textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
  93. for (uint32_t ii = 0; ii < numFonts; ++ii)
  94. {
  95. // Add some text to the buffer.
  96. // The position of the pen is adjusted when there is an endline.
  97. textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
  98. }
  99. // Now write some styled text.
  100. // Setup style colors.
  101. textBufferManager->setBackgroundColor(staticText, 0x551111ff);
  102. textBufferManager->setUnderlineColor(staticText, 0xff2222ff);
  103. textBufferManager->setOverlineColor(staticText, 0x2222ffff);
  104. textBufferManager->setStrikeThroughColor(staticText, 0x22ff22ff);
  105. // Background.
  106. textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
  107. textBufferManager->appendText(staticText, fonts[0], L"The quick ");
  108. // Strike-through.
  109. textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
  110. textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
  111. // Overline.
  112. textBufferManager->setStyle(staticText, STYLE_OVERLINE);
  113. textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
  114. // Underline.
  115. textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
  116. textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
  117. // Background + strike-through.
  118. textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
  119. textBufferManager->appendText(staticText, fonts[0], L"dog\n");
  120. textBufferManager->setStyle(staticText, STYLE_NORMAL);
  121. textBufferManager->appendText(staticText, fontAwesome72,
  122. " " ICON_FA_POWER_OFF
  123. " " ICON_FA_TWITTER_SQUARE
  124. " " ICON_FA_CERTIFICATE
  125. " " ICON_FA_FLOPPY_O
  126. " " ICON_FA_GITHUB
  127. " " ICON_FA_GITHUB_ALT
  128. "\n"
  129. );
  130. textBufferManager->appendText(staticText, fontKenney64,
  131. " " ICON_KI_COMPUTER
  132. " " ICON_KI_JOYSTICK
  133. " " ICON_KI_EXLAMATION
  134. " " ICON_KI_STAR
  135. " " ICON_KI_BUTTON_START
  136. " " ICON_KI_DOWNLOAD
  137. "\n"
  138. );
  139. // Create a transient buffer for real-time data.
  140. TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
  141. while (!entry::processEvents(width, height, debug, reset) )
  142. {
  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 print information about this example.
  153. bgfx::dbgTextClear();
  154. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font");
  155. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text.");
  156. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  157. // Use transient text to display debug information.
  158. wchar_t fpsText[64];
  159. bx::swnprintf(fpsText, BX_COUNTOF(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
  160. textBufferManager->clearTextBuffer(transientText);
  161. textBufferManager->setPenPosition(transientText, width - 150.0f, 10.0f);
  162. textBufferManager->appendText(transientText, visitor10, L"Transient\n");
  163. textBufferManager->appendText(transientText, visitor10, L"text buffer\n");
  164. textBufferManager->appendText(transientText, visitor10, fpsText);
  165. float at[3] = { 0, 0, 0.0f };
  166. float eye[3] = { 0, 0, -1.0f };
  167. float view[16];
  168. bx::mtxLookAt(view, eye, at);
  169. const float centering = 0.5f;
  170. // Setup a top-left ortho matrix for screen space drawing.
  171. const bgfx::HMD* hmd = bgfx::getHMD();
  172. if (NULL != hmd && 0 != (hmd->flags & BGFX_HMD_RENDERING) )
  173. {
  174. float proj[16];
  175. bx::mtxProj(proj, hmd->eye[0].fov, 0.1f, 100.0f);
  176. static float time = 0.0f;
  177. time += 0.05f;
  178. const float dist = 10.0f;
  179. const float offset0 = -proj[8] + (hmd->eye[0].viewOffset[0] / dist * proj[0]);
  180. const float offset1 = -proj[8] + (hmd->eye[1].viewOffset[0] / dist * proj[0]);
  181. float ortho[2][16];
  182. const float offsetx = width/2.0f;
  183. bx::mtxOrtho(ortho[0], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset0);
  184. bx::mtxOrtho(ortho[1], centering, offsetx + centering, height + centering, centering, -1.0f, 1.0f, offset1);
  185. bgfx::setViewTransform(0, view, ortho[0], BGFX_VIEW_STEREO, ortho[1]);
  186. bgfx::setViewRect(0, 0, 0, hmd->width, hmd->height);
  187. }
  188. else
  189. {
  190. float ortho[16];
  191. bx::mtxOrtho(ortho, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  192. bgfx::setViewTransform(0, view, ortho);
  193. bgfx::setViewRect(0, 0, 0, uint16_t(width), uint16_t(height) );
  194. }
  195. // Submit the debug text.
  196. textBufferManager->submitTextBuffer(transientText, 0);
  197. // Submit the static text.
  198. textBufferManager->submitTextBuffer(staticText, 0);
  199. // Advance to next frame. Rendering thread will be kicked to
  200. // process submitted rendering primitives.
  201. bgfx::frame();
  202. }
  203. fontManager->destroyTtf(fontKenneyTtf);
  204. fontManager->destroyTtf(fontAwesomeTtf);
  205. fontManager->destroyTtf(visitorTtf);
  206. // Destroy the fonts.
  207. fontManager->destroyFont(fontKenney64);
  208. fontManager->destroyFont(fontAwesome72);
  209. fontManager->destroyFont(visitor10);
  210. for (uint32_t ii = 0; ii < numFonts; ++ii)
  211. {
  212. fontManager->destroyFont(fonts[ii]);
  213. }
  214. textBufferManager->destroyTextBuffer(staticText);
  215. textBufferManager->destroyTextBuffer(transientText);
  216. delete textBufferManager;
  217. delete fontManager;
  218. // Shutdown bgfx.
  219. bgfx::shutdown();
  220. return 0;
  221. }