font.cpp 10 KB

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