font.cpp 9.0 KB

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