font.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "common.h"
  6. #include "bgfx_utils.h"
  7. #include <bx/timer.h>
  8. #include <bx/string.h>
  9. #include <bx/math.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 <wchar.h>
  16. #include "imgui/imgui.h"
  17. namespace
  18. {
  19. TrueTypeHandle loadTtf(FontManager* _fm, const char* _filePath)
  20. {
  21. uint32_t size;
  22. void* data = load(_filePath, &size);
  23. if (NULL != data)
  24. {
  25. TrueTypeHandle handle = _fm->createTtf( (uint8_t*)data, size);
  26. BX_FREE(entry::getAllocator(), data);
  27. return handle;
  28. }
  29. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  30. return invalid;
  31. }
  32. static const char* s_fontFilePath[] =
  33. {
  34. "font/droidsans.ttf",
  35. "font/chp-fire.ttf",
  36. "font/bleeding_cowboys.ttf",
  37. "font/mias_scribblings.ttf",
  38. "font/ruritania.ttf",
  39. "font/signika-regular.ttf",
  40. "font/five_minutes.otf",
  41. };
  42. class ExampleFont : public entry::AppI
  43. {
  44. public:
  45. ExampleFont(const char* _name, const char* _description, const char* _url)
  46. : entry::AppI(_name, _description, _url)
  47. {
  48. }
  49. void init(int32_t _argc, const char* const* _argv, uint32_t _width, uint32_t _height) override
  50. {
  51. Args args(_argc, _argv);
  52. m_width = _width;
  53. m_height = _height;
  54. m_debug = BGFX_DEBUG_NONE;
  55. m_reset = BGFX_RESET_VSYNC;
  56. bgfx::Init init;
  57. init.type = args.m_type;
  58. init.vendorId = args.m_pciId;
  59. init.resolution.width = m_width;
  60. init.resolution.height = m_height;
  61. init.resolution.reset = m_reset;
  62. bgfx::init(init);
  63. // Enable debug text.
  64. bgfx::setDebug(m_debug);
  65. // Set view 0 clear state.
  66. bgfx::setViewClear(0
  67. , BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH
  68. , 0x303030ff
  69. , 1.0f
  70. , 0
  71. );
  72. // Initialize Imgui
  73. // This initializes the same allocator used by stb_truetype, so must do that before creating the font manager
  74. imguiCreate();
  75. // Init the text rendering system.
  76. m_fontManager = new FontManager(512);
  77. m_textBufferManager = new TextBufferManager(m_fontManager);
  78. // Load some TTF files.
  79. for (uint32_t ii = 0; ii < numFonts; ++ii)
  80. {
  81. // Instantiate a usable font.
  82. m_fontFiles[ii] = loadTtf(m_fontManager, s_fontFilePath[ii]);
  83. m_fonts[ii] = m_fontManager->createFontByPixelSize(m_fontFiles[ii], 0, 32);
  84. // Preload glyphs and blit them to atlas.
  85. m_fontManager->preloadGlyph(m_fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  86. // You can unload the truetype files at this stage, but in that
  87. // case, the set of glyph's will be limited to the set of preloaded
  88. // glyph.
  89. m_fontManager->destroyTtf(m_fontFiles[ii]);
  90. }
  91. m_fontAwesomeTtf = loadTtf(m_fontManager, "font/fontawesome-webfont.ttf");
  92. m_fontKenneyTtf = loadTtf(m_fontManager, "font/kenney-icon-font.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. m_fontAwesome72 = m_fontManager->createFontByPixelSize(m_fontAwesomeTtf, 0, 72);
  96. m_fontKenney64 = m_fontManager->createFontByPixelSize(m_fontKenneyTtf, 0, 64);
  97. m_visitorTtf = loadTtf(m_fontManager, "font/visitor1.ttf");
  98. // This font doesn't have any preloaded glyph's but the truetype file
  99. // is loaded so glyph will be generated as needed.
  100. m_visitor10 = m_fontManager->createFontByPixelSize(m_visitorTtf, 0, 10);
  101. //create a static text buffer compatible with alpha font
  102. //a static text buffer content cannot be modified after its first submit.
  103. m_staticText = m_textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Static);
  104. // The pen position represent the top left of the box of the first line
  105. // of text.
  106. m_textBufferManager->setPenPosition(m_staticText, 24.0f, 100.0f);
  107. for (uint32_t ii = 0; ii < numFonts; ++ii)
  108. {
  109. // Add some text to the buffer.
  110. // The position of the pen is adjusted when there is an endline.
  111. m_textBufferManager->appendText(m_staticText, m_fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
  112. }
  113. // Now write some styled text.
  114. // Setup style colors.
  115. m_textBufferManager->setBackgroundColor(m_staticText, 0x551111ff);
  116. m_textBufferManager->setUnderlineColor(m_staticText, 0xff2222ff);
  117. m_textBufferManager->setOverlineColor(m_staticText, 0x2222ffff);
  118. m_textBufferManager->setStrikeThroughColor(m_staticText, 0x22ff22ff);
  119. // Background.
  120. m_textBufferManager->setStyle(m_staticText, STYLE_BACKGROUND);
  121. m_textBufferManager->appendText(m_staticText, m_fonts[0], L"The quick ");
  122. // Strike-through.
  123. m_textBufferManager->setStyle(m_staticText, STYLE_STRIKE_THROUGH);
  124. m_textBufferManager->appendText(m_staticText, m_fonts[0], L"brown fox ");
  125. // Overline.
  126. m_textBufferManager->setStyle(m_staticText, STYLE_OVERLINE);
  127. m_textBufferManager->appendText(m_staticText, m_fonts[0], L"jumps over ");
  128. // Underline.
  129. m_textBufferManager->setStyle(m_staticText, STYLE_UNDERLINE);
  130. m_textBufferManager->appendText(m_staticText, m_fonts[0], L"the lazy ");
  131. // Background + strike-through.
  132. m_textBufferManager->setStyle(m_staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
  133. m_textBufferManager->appendText(m_staticText, m_fonts[0], L"dog\n");
  134. m_textBufferManager->setStyle(m_staticText, STYLE_NORMAL);
  135. m_textBufferManager->appendText(m_staticText, m_fontAwesome72,
  136. " " ICON_FA_POWER_OFF
  137. " " ICON_FA_TWITTER_SQUARE
  138. " " ICON_FA_CERTIFICATE
  139. " " ICON_FA_FLOPPY_O
  140. " " ICON_FA_GITHUB
  141. " " ICON_FA_GITHUB_ALT
  142. "\n"
  143. );
  144. m_textBufferManager->appendText(m_staticText, m_fontKenney64,
  145. " " ICON_KI_COMPUTER
  146. " " ICON_KI_JOYSTICK
  147. " " ICON_KI_EXLAMATION
  148. " " ICON_KI_STAR
  149. " " ICON_KI_BUTTON_START
  150. " " ICON_KI_DOWNLOAD
  151. "\n"
  152. );
  153. // Create a transient buffer for real-time data.
  154. m_transientText = m_textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
  155. }
  156. virtual int shutdown() override
  157. {
  158. imguiDestroy();
  159. m_fontManager->destroyTtf(m_fontKenneyTtf);
  160. m_fontManager->destroyTtf(m_fontAwesomeTtf);
  161. m_fontManager->destroyTtf(m_visitorTtf);
  162. // Destroy the fonts.
  163. m_fontManager->destroyFont(m_fontKenney64);
  164. m_fontManager->destroyFont(m_fontAwesome72);
  165. m_fontManager->destroyFont(m_visitor10);
  166. for (uint32_t ii = 0; ii < numFonts; ++ii)
  167. {
  168. m_fontManager->destroyFont(m_fonts[ii]);
  169. }
  170. m_textBufferManager->destroyTextBuffer(m_staticText);
  171. m_textBufferManager->destroyTextBuffer(m_transientText);
  172. delete m_textBufferManager;
  173. delete m_fontManager;
  174. // Shutdown bgfx.
  175. bgfx::shutdown();
  176. return 0;
  177. }
  178. bool update() override
  179. {
  180. if (!entry::processEvents(m_width, m_height, m_debug, m_reset, &m_mouseState) )
  181. {
  182. imguiBeginFrame(m_mouseState.m_mx
  183. , m_mouseState.m_my
  184. , (m_mouseState.m_buttons[entry::MouseButton::Left ] ? IMGUI_MBUT_LEFT : 0)
  185. | (m_mouseState.m_buttons[entry::MouseButton::Right ] ? IMGUI_MBUT_RIGHT : 0)
  186. | (m_mouseState.m_buttons[entry::MouseButton::Middle] ? IMGUI_MBUT_MIDDLE : 0)
  187. , m_mouseState.m_mz
  188. , uint16_t(m_width)
  189. , uint16_t(m_height)
  190. );
  191. showExampleDialog(this);
  192. imguiEndFrame();
  193. // This dummy draw call is here to make sure that view 0 is cleared
  194. // if no other draw calls are submitted to view 0.
  195. bgfx::touch(0);
  196. int64_t now = bx::getHPCounter();
  197. static int64_t last = now;
  198. const int64_t frameTime = now - last;
  199. last = now;
  200. const double freq = double(bx::getHPFrequency() );
  201. const double toMs = 1000.0 / freq;
  202. // Use transient text to display debug information.
  203. char fpsText[64];
  204. bx::snprintf(fpsText, BX_COUNTOF(fpsText), "Frame: % 7.3f[ms]", double(frameTime) * toMs);
  205. m_textBufferManager->clearTextBuffer(m_transientText);
  206. m_textBufferManager->setPenPosition(m_transientText, m_width - 150.0f, 10.0f);
  207. m_textBufferManager->appendText(m_transientText, m_visitor10, "Transient\n");
  208. m_textBufferManager->appendText(m_transientText, m_visitor10, "text buffer\n");
  209. m_textBufferManager->appendText(m_transientText, m_visitor10, fpsText);
  210. const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
  211. const bx::Vec3 eye = { 0.0f, 0.0f, -1.0f };
  212. float view[16];
  213. bx::mtxLookAt(view, eye, at);
  214. float centering = 0.0f;
  215. if (bgfx::getRendererType() == bgfx::RendererType::Direct3D9) {
  216. centering = -0.5f;
  217. }
  218. // Setup a top-left ortho matrix for screen space drawing.
  219. const bgfx::Caps* caps = bgfx::getCaps();
  220. {
  221. float ortho[16];
  222. bx::mtxOrtho(
  223. ortho
  224. , centering
  225. , m_width + centering
  226. , m_height + centering
  227. , centering
  228. , 0.0f
  229. , 100.0f
  230. , 0.0f
  231. , caps->homogeneousDepth
  232. );
  233. bgfx::setViewTransform(0, view, ortho);
  234. bgfx::setViewRect(0, 0, 0, uint16_t(m_width), uint16_t(m_height) );
  235. }
  236. // Submit the debug text.
  237. m_textBufferManager->submitTextBuffer(m_transientText, 0);
  238. // Submit the static text.
  239. m_textBufferManager->submitTextBuffer(m_staticText, 0);
  240. // Advance to next frame. Rendering thread will be kicked to
  241. // process submitted rendering primitives.
  242. bgfx::frame();
  243. return true;
  244. }
  245. return false;
  246. }
  247. entry::MouseState m_mouseState;
  248. uint32_t m_width;
  249. uint32_t m_height;
  250. uint32_t m_debug;
  251. uint32_t m_reset;
  252. FontManager* m_fontManager;
  253. TextBufferManager* m_textBufferManager;
  254. FontHandle m_visitor10;
  255. TrueTypeHandle m_fontAwesomeTtf;
  256. TrueTypeHandle m_fontKenneyTtf;
  257. FontHandle m_fontAwesome72;
  258. FontHandle m_fontKenney64;
  259. TrueTypeHandle m_visitorTtf;
  260. TextBufferHandle m_transientText;
  261. TextBufferHandle m_staticText;
  262. static const uint32_t numFonts = BX_COUNTOF(s_fontFilePath);
  263. TrueTypeHandle m_fontFiles[numFonts];
  264. FontHandle m_fonts[numFonts];
  265. };
  266. } // namespace
  267. ENTRY_IMPLEMENT_MAIN(
  268. ExampleFont
  269. , "10-font"
  270. , "Use the font system to display text and styled text."
  271. , "https://bkaradzic.github.io/bgfx/examples.html#font"
  272. );