2
0

font.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <bgfx.h>
  2. #include <bx/bx.h>
  3. #include <bx/timer.h>
  4. #include "../common/entry.h"
  5. #include "../common/dbg.h"
  6. #include "../common/math.h"
  7. #include "../common/processevents.h"
  8. #include "../common/font/font_manager.h"
  9. #include "../common/font/text_buffer_manager.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. int _main_(int /*_argc*/, char** /*_argv*/)
  13. {
  14. uint32_t width = 1280;
  15. uint32_t height = 720;
  16. uint32_t debug = BGFX_DEBUG_TEXT;
  17. uint32_t reset = 0;
  18. bgfx::init();
  19. bgfx::reset(width, height);
  20. // Enable debug text.
  21. bgfx::setDebug(debug);
  22. // Set view 0 clear state.
  23. bgfx::setViewClear(0
  24. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  25. , 0x303030ff
  26. , 1.0f
  27. , 0
  28. );
  29. //init the text rendering system
  30. FontManager* fontManager = new FontManager(512);
  31. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  32. //load some truetype files
  33. const char* fontNames[7] = {
  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. const uint32_t fontCount = sizeof(fontNames)/sizeof(const char*);
  43. TrueTypeHandle fontFiles[fontCount];
  44. FontHandle fonts[fontCount];
  45. for(int32_t ii = 0; ii<fontCount ; ++ii)
  46. {
  47. //instantiate a usable font
  48. fontFiles[ii] = fontManager->loadTrueTypeFromFile(fontNames[ii]);
  49. fonts[ii] = fontManager->createFontByPixelSize(fontFiles[ii], 0, 32);
  50. //preload glyphs and blit them to atlas
  51. fontManager->preloadGlyph(fonts[ii], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  52. //You can unload the truetype files at this stage, but in that case, the set of glyph's will be limited to the set of preloaded glyph
  53. fontManager->unloadTrueType(fontFiles[ii]);
  54. }
  55. TrueTypeHandle console_tt = fontManager->loadTrueTypeFromFile("font/visitor1.ttf");
  56. //this font doesn't have any preloaded glyph's but the truetype file is loaded
  57. //so glyph will be generated as needed
  58. FontHandle consola_16 = fontManager->createFontByPixelSize(console_tt, 0, 10);
  59. //create a static text buffer compatible with alpha font
  60. //a static text buffer content cannot be modified after its first submit.
  61. TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, STATIC);
  62. //the pen position represent the top left of the box of the first line of text
  63. textBufferManager->setPenPosition(staticText, 24.0f, 100.0f);
  64. for(int32_t ii = 0; ii<fontCount ; ++ii)
  65. {
  66. //add some text to the buffer
  67. textBufferManager->appendText(staticText, fonts[ii], L"The quick brown fox jumps over the lazy dog\n");
  68. //the position of the pen is adjusted when there is an endline
  69. }
  70. // Now write some styled text
  71. //setup style colors
  72. textBufferManager->setBackgroundColor(staticText, 0x551111FF);
  73. textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
  74. textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
  75. textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
  76. //text + bkg
  77. textBufferManager->setStyle(staticText, STYLE_BACKGROUND);
  78. textBufferManager->appendText(staticText, fonts[0], L"The quick ");
  79. //text + strike-through
  80. textBufferManager->setStyle(staticText, STYLE_STRIKE_THROUGH);
  81. textBufferManager->appendText(staticText, fonts[0], L"brown fox ");
  82. //text + overline
  83. textBufferManager->setStyle(staticText, STYLE_OVERLINE);
  84. textBufferManager->appendText(staticText, fonts[0], L"jumps over ");
  85. //text + underline
  86. textBufferManager->setStyle(staticText, STYLE_UNDERLINE);
  87. textBufferManager->appendText(staticText, fonts[0], L"the lazy ");
  88. //text + bkg + strike-through
  89. textBufferManager->setStyle(staticText, STYLE_BACKGROUND|STYLE_STRIKE_THROUGH);
  90. textBufferManager->appendText(staticText, fonts[0], L"dog\n");
  91. //create a transient buffer for realtime data
  92. TextBufferHandle transientText = textBufferManager->createTextBuffer(FONT_TYPE_ALPHA, TRANSIENT);
  93. uint32_t w = 0,h = 0;
  94. while (!processEvents(width, height, debug, reset) )
  95. {
  96. if(w!=width|| h!=height)
  97. {
  98. w=width;
  99. h= height;
  100. printf("ri: %d,%d\n",width,height);
  101. }
  102. // Set view 0 default viewport.
  103. bgfx::setViewRect(0, 0, 0, width, height);
  104. // This dummy draw call is here to make sure that view 0 is cleared
  105. // if no other draw calls are submitted to view 0.
  106. bgfx::submit(0);
  107. int64_t now = bx::getHPCounter();
  108. static int64_t last = now;
  109. const int64_t frameTime = now - last;
  110. last = now;
  111. const double freq = double(bx::getHPFrequency() );
  112. const double toMs = 1000.0/freq;
  113. // Use debug font to print information about this example.
  114. //bgfx::dbgTextClear();
  115. //bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/10-font");
  116. //bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use the font system to display text and styled text.");
  117. //bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  118. //Use transient text to display debug information
  119. //Code below is similar to commented code above
  120. wchar_t fpsText[64];
  121. swprintf(fpsText,L"Frame: % 7.3f[ms]", double(frameTime)*toMs);
  122. textBufferManager->clearTextBuffer(transientText);
  123. textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
  124. textBufferManager->appendText(transientText, consola_16, L"bgfx/examples/10-font\n");
  125. textBufferManager->appendText(transientText, consola_16, L"Description: Use the font system to display text and styled text.\n");
  126. textBufferManager->appendText(transientText, consola_16, fpsText);
  127. float at[3] = { 0, 0, 0.0f };
  128. float eye[3] = {0, 0, -1.0f };
  129. float view[16];
  130. float proj[16];
  131. mtxLookAt(view, eye, at);
  132. //setup a top-left ortho matrix for screen space drawing
  133. float centering = 0.5f;
  134. mtxOrtho(proj, centering, width+centering,height+centering, centering,-1.0f, 1.0f);
  135. // Set view and projection matrix for view 0.
  136. bgfx::setViewTransform(0, view, proj);
  137. //submit the debug text
  138. textBufferManager->submitTextBuffer(transientText, 0);
  139. //submit the static text
  140. textBufferManager->submitTextBuffer(staticText, 0);
  141. // Advance to next frame. Rendering thread will be kicked to
  142. // process submitted rendering primitives.
  143. bgfx::frame();
  144. }
  145. fontManager->unloadTrueType(console_tt);
  146. //destroy the fonts
  147. fontManager->destroyFont(consola_16);
  148. for(int32_t ii = 0; ii<fontCount ; ++ii)
  149. {
  150. fontManager->destroyFont(fonts[ii]);
  151. }
  152. textBufferManager->destroyTextBuffer(staticText);
  153. textBufferManager->destroyTextBuffer(transientText);
  154. delete textBufferManager;
  155. delete fontManager;
  156. // Shutdown bgfx.
  157. bgfx::shutdown();
  158. return 0;
  159. }