font.cpp 9.4 KB

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