font.cpp 9.9 KB

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