font.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "common.h"
  6. #include <bgfx.h>
  7. #include <bx/timer.h>
  8. #include <bx/countof.h>
  9. #include <bx/string.h>
  10. #include "entry.h"
  11. #include "dbg.h"
  12. #include "fpumath.h"
  13. #include "processevents.h"
  14. #include "font/font_manager.h"
  15. #include "font/text_buffer_manager.h"
  16. #include <stdio.h>
  17. #include <wchar.h>
  18. TrueTypeHandle loadTtf(FontManager* _fm, const char* _fontPath)
  19. {
  20. FILE* pFile;
  21. pFile = fopen(_fontPath, "rb");
  22. if (NULL != pFile)
  23. {
  24. if (0 == fseek(pFile, 0L, SEEK_END) )
  25. {
  26. // Get the size of the file.
  27. long bufsize = ftell(pFile);
  28. if (bufsize == -1)
  29. {
  30. fclose(pFile);
  31. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  32. return invalid;
  33. }
  34. uint8_t* buffer = new uint8_t[bufsize];
  35. // Go back to the start of the file.
  36. fseek(pFile, 0L, SEEK_SET);
  37. // Read the entire file into memory.
  38. uint32_t newLen = fread( (void*)buffer, sizeof(char), bufsize, pFile);
  39. if (newLen == 0)
  40. {
  41. fclose(pFile);
  42. delete [] buffer;
  43. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  44. return invalid;
  45. }
  46. fclose(pFile);
  47. return _fm->createTtf(buffer, bufsize);
  48. }
  49. }
  50. TrueTypeHandle invalid = BGFX_INVALID_HANDLE;
  51. return invalid;
  52. }
  53. int _main_(int /*_argc*/, char** /*_argv*/)
  54. {
  55. uint32_t width = 1280;
  56. uint32_t height = 720;
  57. uint32_t debug = BGFX_DEBUG_TEXT;
  58. uint32_t reset = BGFX_RESET_VSYNC;
  59. bgfx::init();
  60. bgfx::reset(width, height, reset);
  61. // Enable debug text.
  62. bgfx::setDebug(debug);
  63. // Set view 0 clear state.
  64. bgfx::setViewClear(0
  65. , BGFX_CLEAR_COLOR_BIT | BGFX_CLEAR_DEPTH_BIT
  66. , 0x303030ff
  67. , 1.0f
  68. , 0
  69. );
  70. // Init the text rendering system.
  71. FontManager* fontManager = new FontManager(512);
  72. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  73. // Load some TTF files.
  74. const char* fontNames[7] =
  75. {
  76. "font/droidsans.ttf",
  77. "font/chp-fire.ttf",
  78. "font/bleeding_cowboys.ttf",
  79. "font/mias_scribblings.ttf",
  80. "font/ruritania.ttf",
  81. "font/signika-regular.ttf",
  82. "font/five_minutes.otf"
  83. };
  84. const uint32_t fontCount = countof(fontNames);
  85. TrueTypeHandle fontFiles[fontCount];
  86. FontHandle fonts[fontCount];
  87. for (uint32_t ii = 0; ii < fontCount; ++ii)
  88. {
  89. // Instantiate a usable font.
  90. fontFiles[ii] = loadTtf(fontManager, fontNames[ii]);
  91. fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
  92. // Preload glyphs and blit them to atlas.
  93. fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  94. // You can unload the truetype files at this stage, but in that
  95. // case, the set of glyph's will be limited to the set of preloaded
  96. // glyph.
  97. fontManager->destroyTtf(fontFiles[ii]);
  98. }
  99. TrueTypeHandle console_tt = loadTtf(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. FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 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. TextBufferHandle staticText = 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. textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
  109. for (uint32_t ii = 0; ii < fontCount; ++ii)
  110. {
  111. // Add some text to the buffer.
  112. // The position of the pen is adjusted when there is an endline.
  113. textBufferManager->appendText(staticText, 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. textBufferManager->setBackgroundColor(staticText, 0x551111FF);
  118. textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
  119. textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
  120. textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
  121. // Background.
  122. textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
  123. textBufferManager->appendText(staticText, fonts[0], L"The quick ");
  124. // Strike-through.
  125. textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
  126. textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
  127. // Overline.
  128. textBufferManager->setStyle(staticText, STYLE_OVERLINE);
  129. textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
  130. // Underline.
  131. textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
  132. textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
  133. // Background + strike-through.
  134. textBufferManager->setStyle(staticText, STYLE_BACKGROUND | STYLE_STRIKE_THROUGH);
  135. textBufferManager->appendText(staticText, fonts[0], L"dog\n");
  136. // Create a transient buffer for real-time data.
  137. TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, BufferType::Transient);
  138. while (!processEvents(width, height, debug, reset) )
  139. {
  140. // Set view 0 default viewport.
  141. bgfx::setViewRect(0, 0, 0, width, height);
  142. // This dummy draw call is here to make sure that view 0 is cleared
  143. // if no other draw calls are submitted to view 0.
  144. bgfx::submit(0);
  145. int64_t now = bx::getHPCounter();
  146. static int64_t last = now;
  147. const int64_t frameTime = now - last;
  148. last = now;
  149. const double freq = double(bx::getHPFrequency() );
  150. const double toMs = 1000.0 / freq;
  151. // Use transient text to display debug information.
  152. wchar_t fpsText[64];
  153. bx::swnprintf(fpsText, countof(fpsText), L"Frame: % 7.3f[ms]", double(frameTime) * toMs);
  154. textBufferManager->clearTextBuffer(transientText);
  155. textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
  156. textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n");
  157. textBufferManager->appendText(transientText, consola_16, L"Description: Use the font system to display text and styled text.\n");
  158. textBufferManager->appendText(transientText, consola_16, fpsText);
  159. float at[3] = { 0, 0, 0.0f };
  160. float eye[3] = {0, 0, -1.0f };
  161. float view[16];
  162. float proj[16];
  163. mtxLookAt(view, eye, at);
  164. // Setup a top-left ortho matrix for screen space drawing.
  165. float centering = 0.5f;
  166. mtxOrtho(proj, centering, width + centering, height + centering, centering, -1.0f, 1.0f);
  167. // Set view and projection matrix for view 0.
  168. bgfx::setViewTransform(0, view, proj);
  169. // Submit the debug text.
  170. textBufferManager->submitTextBuffer(transientText, 0);
  171. // Submit the static text.
  172. textBufferManager->submitTextBuffer(staticText, 0);
  173. // Advance to next frame. Rendering thread will be kicked to
  174. // process submitted rendering primitives.
  175. bgfx::frame();
  176. }
  177. fontManager->destroyTtf(console_tt);
  178. // Destroy the fonts.
  179. fontManager->destroyFont(consola_16);
  180. for (uint32_t ii = 0; ii < fontCount; ++ii)
  181. {
  182. fontManager->destroyFont(fonts[ii]);
  183. }
  184. textBufferManager->destroyTextBuffer(staticText);
  185. textBufferManager->destroyTextBuffer(transientText);
  186. delete textBufferManager;
  187. delete fontManager;
  188. // Shutdown bgfx.
  189. bgfx::shutdown();
  190. return 0;
  191. }