font.cpp 9.8 KB

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