font.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. static const char* s_shaderPath = NULL;
  13. int _main_(int /*_argc*/, char** /*_argv*/)
  14. {
  15. uint32_t width = 1280;
  16. uint32_t height = 720;
  17. uint32_t debug = BGFX_DEBUG_TEXT;
  18. uint32_t reset = 0;
  19. bgfx::init();
  20. bgfx::reset(width, height);
  21. // Enable debug text.
  22. bgfx::setDebug(debug);
  23. // Set view 0 clear state.
  24. bgfx::setViewClear(0
  25. , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT
  26. , 0x303030ff
  27. , 1.0f
  28. , 0
  29. );
  30. // Setup root path for binary shaders. Shader binaries are different
  31. // for each renderer.
  32. switch (bgfx::getRendererType() )
  33. {
  34. default:
  35. case bgfx::RendererType::Direct3D9:
  36. s_shaderPath = "shaders/dx9/";
  37. break;
  38. case bgfx::RendererType::Direct3D11:
  39. s_shaderPath = "shaders/dx11/";
  40. break;
  41. case bgfx::RendererType::OpenGL:
  42. s_shaderPath = "shaders/glsl/";
  43. break;
  44. case bgfx::RendererType::OpenGLES2:
  45. case bgfx::RendererType::OpenGLES3:
  46. s_shaderPath = "shaders/gles/";
  47. break;
  48. }
  49. //init the text rendering system
  50. bgfx_font::FontManager* fontManager = new bgfx_font::FontManager(512);
  51. bgfx_font::TextBufferManager* textBufferManager = new bgfx_font::TextBufferManager(fontManager);
  52. textBufferManager->init(s_shaderPath);
  53. //load some truetype files
  54. bgfx_font::TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/times.ttf");
  55. bgfx_font::TrueTypeHandle consola_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/consola.ttf");
  56. //create some usable font with of a specific size
  57. bgfx_font::FontHandle times_24 = fontManager->createFontByPixelSize(times_tt, 0, 24);
  58. //preload glyphs and blit them to atlas
  59. fontManager->preloadGlyph(times_24, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  60. //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
  61. fontManager->unloadTrueType(times_tt);
  62. //this font doesn't have any preloaded glyph's but the truetype file is loaded
  63. //so glyph will be generated as needed
  64. bgfx_font::FontHandle consola_16 = fontManager->createFontByPixelSize(consola_tt, 0, 16);
  65. //create a static text buffer compatible with alpha font
  66. //a static text buffer content cannot be modified after its first submit.
  67. bgfx_font::TextBufferHandle staticText = textBufferManager->createTextBuffer(bgfx_font::FONT_TYPE_ALPHA, bgfx_font::STATIC);
  68. //the pen position represent the top left of the box of the first line of text
  69. textBufferManager->setPenPosition(staticText, 20.0f, 100.0f);
  70. //add some text to the buffer
  71. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  72. //the position of the pen is adjusted when there is an endline
  73. //setup style colors
  74. textBufferManager->setBackgroundColor(staticText, 0x551111FF);
  75. textBufferManager->setUnderlineColor(staticText, 0xFF2222FF);
  76. textBufferManager->setOverlineColor(staticText, 0x2222FFFF);
  77. textBufferManager->setStrikeThroughColor(staticText, 0x22FF22FF);
  78. //text + bkg
  79. textBufferManager->setStyle(staticText, bgfx_font::STYLE_BACKGROUND);
  80. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  81. //text + strike-through
  82. textBufferManager->setStyle(staticText, bgfx_font::STYLE_STRIKE_THROUGH);
  83. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  84. //text + overline
  85. textBufferManager->setStyle(staticText, bgfx_font::STYLE_OVERLINE);
  86. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  87. //text + underline
  88. textBufferManager->setStyle(staticText, bgfx_font::STYLE_UNDERLINE);
  89. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  90. //text + bkg + strike-through
  91. textBufferManager->setStyle(staticText, bgfx_font::STYLE_BACKGROUND|bgfx_font::STYLE_STRIKE_THROUGH);
  92. textBufferManager->appendText(staticText, times_24, L"The quick brown fox jumps over the lazy dog\n");
  93. //create a transient buffer for realtime data
  94. bgfx_font::TextBufferHandle transientText = textBufferManager->createTextBuffer(bgfx_font::FONT_TYPE_ALPHA, bgfx_font::TRANSIENT);
  95. uint32_t w = 0,h = 0;
  96. while (!processEvents(width, height, debug, reset) )
  97. {
  98. if(w!=width|| h!=height)
  99. {
  100. w=width;
  101. h= height;
  102. printf("ri: %d,%d\n",width,height);
  103. }
  104. // Set view 0 default viewport.
  105. bgfx::setViewRect(0, 0, 0, width, height);
  106. // This dummy draw call is here to make sure that view 0 is cleared
  107. // if no other draw calls are submitted to view 0.
  108. bgfx::submit(0);
  109. int64_t now = bx::getHPCounter();
  110. static int64_t last = now;
  111. const int64_t frameTime = now - last;
  112. last = now;
  113. const double freq = double(bx::getHPFrequency() );
  114. const double toMs = 1000.0/freq;
  115. //float time = (float)(bx::getHPCounter()/double(bx::getHPFrequency() ) );
  116. float at[3] = { 0, 0, 0.0f };
  117. float eye[3] = {0, 0, -1.0f };
  118. float view[16];
  119. float proj[16];
  120. mtxLookAt(view, eye, at);
  121. //setup a top-left ortho matrix for screen space drawing
  122. float centering = 0.5f;
  123. mtxOrtho(proj, centering, width+centering,height+centering, centering,-1.0f, 1.0f);
  124. // Set view and projection matrix for view 0.
  125. bgfx::setViewTransform(0, view, proj);
  126. //submit the static text
  127. textBufferManager->submitTextBuffer(staticText, 0);
  128. //submit some realtime text
  129. wchar_t fpsText[64];
  130. swprintf(fpsText,L"Frame: % 7.3f[ms]", double(frameTime)*toMs);
  131. textBufferManager->clearTextBuffer(transientText);
  132. textBufferManager->setPenPosition(transientText, 20.0, 4.0f);
  133. textBufferManager->appendText(transientText, consola_16, L"bgfx_font\\sample\\01_basics\n");
  134. textBufferManager->appendText(transientText, consola_16, L"Description: truetype, font, text and style\n");
  135. textBufferManager->appendText(transientText, consola_16, fpsText);
  136. textBufferManager->submitTextBuffer(transientText, 0);
  137. // Advance to next frame. Rendering thread will be kicked to
  138. // process submitted rendering primitives.
  139. bgfx::frame();
  140. //just to prevent my CG Fan to howl
  141. Sleep(2);
  142. }
  143. fontManager->unloadTrueType(consola_tt);
  144. fontManager->destroyFont(consola_16);
  145. fontManager->destroyFont(times_24);
  146. textBufferManager->destroyTextBuffer(staticText);
  147. textBufferManager->destroyTextBuffer(transientText);
  148. delete textBufferManager;
  149. delete fontManager;
  150. // Shutdown bgfx.
  151. bgfx::shutdown();
  152. return 0;
  153. }