font.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/string.h>
  9. #include "fpumath.h"
  10. #include "font/font_manager.h"
  11. #include "font/text_buffer_manager.h"
  12. #include <stdio.h>
  13. #include <wchar.h>
  14. long int fsize(FILE* _file)
  15. {
  16. long int pos = ftell(_file);
  17. fseek(_file, 0L, SEEK_END);
  18. long int size = ftell(_file);
  19. fseek(_file, pos, SEEK_SET);
  20. return size;
  21. }
  22. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  23. {
  24. FILE* file = fopen(_filePath, "rb");
  25. if (NULL != file)
  26. {
  27. uint32_t size = (uint32_t)fsize(file);
  28. uint8_t* mem = (uint8_t*)malloc(size+1);
  29. size_t ignore = fread(mem, 1, size, file);
  30. BX_UNUSED(ignore);
  31. fclose(file);
  32. mem[size-1] = '\0';
  33. TrueTypeHandle handle = _fm->createTtf(mem, size);
  34. free(mem);
  35. return handle;
  36. }
  37. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  38. return invalid;
  39. }
  40. int _main_(int /*_argc*/, char** /*_argv*/)
  41. {
  42. uint32_t width = 1280;
  43. uint32_t height = 720;
  44. uint32_t debug = BGFX_DEBUG_TEXT;
  45. uint32_t reset = BGFX_RESET_VSYNC;
  46. bgfx::init();
  47. bgfx::reset(width, height, reset);
  48. // Enable debug text.
  49. bgfx::setDebug(debug);
  50. // Set view 0 clear state.
  51. bgfx::setViewClear(0
  52. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  53. , 0x303030ff
  54. , 1.0f
  55. , 0
  56. );
  57. // Init the text rendering system.
  58. FontManager* fontManager = new FontManager(512);
  59. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  60. // Load some TTF files.
  61. const char* fontFilePath[7] =
  62. {
  63. "font/droidsans.ttf",
  64. "font/chp-fire.ttf",
  65. "font/bleeding_cowboys.ttf",
  66. "font/mias_scribblings.ttf",
  67. "font/ruritania.ttf",
  68. "font/signika-regular.ttf",
  69. "font/five_minutes.otf",
  70. };
  71. const uint32_t numFonts = BX_COUNTOF(fontFilePath);
  72. TrueTypeHandle fontFiles[numFonts];
  73. FontHandle fonts[numFonts];
  74. for (uint32_t ii = 0; ii < numFonts; ++ii)
  75. {
  76. // Instantiate a usable font.
  77. fontFiles[ii] = loadTtf(fontManager, fontFilePath[ii]);
  78. fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
  79. // Preload glyphs and blit them to atlas.
  80. fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  81. // You can unload the truetype files at this stage, but in that
  82. // case, the set of glyph's will be limited to the set of preloaded
  83. // glyph.
  84. fontManager->destroyTtf(fontFiles[ii]);
  85. }
  86. TrueTypeHandle fontAwesomeTtf = loadTtf(fontManager, "font/fontawesome-webfont.ttf");
  87. // This font doesn't have any preloaded glyph's but the truetype file
  88. // is loaded so glyph will be generated as needed.
  89. FontHandle fontAwesome72 = fontManager->createFontByPixelSize(fontAwesomeTtf, 0, 72);
  90. TrueTypeHandle visitorTtf = loadTtf(fontManager, "font/visitor1.ttf");
  91. // This font doesn't have any preloaded glyph's but the truetype file
  92. // is loaded so glyph will be generated as needed.
  93. FontHandle visitor10 = fontManager->createFontByPixelSize(visitorTtf, 0, 10);
  94. //create a static text buffer compatible with alpha font
  95. //a static text buffer content cannot be modified after its first submit.
  96. TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Static);
  97. // The pen position represent the top left of the box of the first line
  98. // of text.
  99. textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
  100. for (uint32_t ii = 0; ii < numFonts; ++ii)
  101. {
  102. // Add some text to the buffer.
  103. // The position of the pen is adjusted when there is an endline.
  104. textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
  105. }
  106. // Now write some styled text.
  107. // Setup style colors.
  108. textBufferManager->setBackgroundColor(staticText, 0x551111ff);
  109. textBufferManager->setUnderlineColor(staticText, 0xff2222ff);
  110. textBufferManager->setOverlineColor(staticText, 0x2222ffff);
  111. textBufferManager->setStrikeThroughColor(staticText, 0x22ff22ff);
  112. // Background.
  113. textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
  114. textBufferManager->appendText(staticText, fonts[0], L"The quick ");
  115. // Strike-through.
  116. textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
  117. textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
  118. // Overline.
  119. textBufferManager->setStyle(staticText, STYLE_OVERLINE);
  120. textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
  121. // Underline.
  122. textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
  123. textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
  124. // Background + strike-through.
  125. textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
  126. textBufferManager->appendText(staticText, fonts[0], L"dog\n");
  127. textBufferManager->setStyle(staticText, STYLE_NORMAL);
  128. textBufferManager->appendText(staticText, fontAwesome72, L"\xf011 \xf02e \xf061 \xf087 \xf0d9 \xf099 \xf05c \xf021 \xf113\n");
  129. // Create a transient buffer for real-time data.
  130. TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
  131. while (!entry::processEvents(width, height, debug, reset) )
  132. {
  133. // Set view 0 default viewport.
  134. bgfx::setViewRect(0, 0, 0, width, height);
  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::submit(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. float proj[16];
  161. mtxLookAt(view, eye, at);
  162. // Setup a top-left ortho matrix for screen space drawing.
  163. float centering = 0.5f;
  164. mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  165. // Set view and projection matrix for view 0.
  166. bgfx::setViewTransform(0, view, proj);
  167. // Submit the debug text.
  168. textBufferManager->submitTextBuffer(transientText, 0);
  169. // Submit the static text.
  170. textBufferManager->submitTextBuffer(staticText, 0);
  171. // Advance to next frame. Rendering thread will be kicked to
  172. // process submitted rendering primitives.
  173. bgfx::frame();
  174. }
  175. fontManager->destroyTtf(fontAwesomeTtf);
  176. fontManager->destroyTtf(visitorTtf);
  177. // Destroy the fonts.
  178. fontManager->destroyFont(fontAwesome72);
  179. fontManager->destroyFont(visitor10);
  180. for (uint32_t ii = 0; ii < numFonts; ++ii)
  181. {
  182. fontManager->destroyFont(fonts[ii]);
  183. }
  184. textBufferManager->destroyTextBuffer(staticText);
  185. textBufferManager->destroyTextBuffer(transientText);
  186. delete textBufferManager;
  187. delete fontManager;
  188. // Shutdown bgfx.
  189. bgfx::shutdown();
  190. return 0;
  191. }