fontdfs.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. //, 0xffffffff
  28. , 0x000000FF
  29. , 1.0f
  30. , 0
  31. );
  32. // Setup root path for binary shaders. Shader binaries are different
  33. // for each renderer.
  34. switch (bgfx::getRendererType() )
  35. {
  36. default:
  37. case bgfx::RendererType::Direct3D9:
  38. s_shaderPath = "shaders/dx9/";
  39. break;
  40. case bgfx::RendererType::Direct3D11:
  41. s_shaderPath = "shaders/dx11/";
  42. break;
  43. case bgfx::RendererType::OpenGL:
  44. s_shaderPath = "shaders/glsl/";
  45. break;
  46. case bgfx::RendererType::OpenGLES2:
  47. case bgfx::RendererType::OpenGLES3:
  48. s_shaderPath = "shaders/gles/";
  49. break;
  50. }
  51. //init the text rendering system
  52. FontManager* fontManager = new FontManager(512);
  53. TextBufferManager* textBufferManager = new TextBufferManager(fontManager);
  54. textBufferManager->init(s_shaderPath);
  55. //load a truetype files
  56. TrueTypeHandle times_tt = fontManager->loadTrueTypeFromFile("c:/windows/fonts/times.ttf");
  57. FontHandle distance_font = fontManager->createFontByPixelSize(times_tt, 0, 48, FONT_TYPE_DISTANCE);
  58. //preload glyph and generate (generate bitmap's)
  59. fontManager->preloadGlyph(distance_font, L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. \n");
  60. uint32_t fontsCount = 0;
  61. FontHandle fonts[64];
  62. fonts[fontsCount++] = distance_font;
  63. //generate various sub distance field fonts at various size
  64. int step=4;
  65. for(int i = 64; i>1 ; i-=step)
  66. {
  67. if(i<32) step = 2;
  68. //instantiate a usable font
  69. FontHandle font = fontManager->createScaledFontToPixelSize(distance_font, i);
  70. fonts[fontsCount++] = font;
  71. }
  72. //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
  73. fontManager->unloadTrueType(times_tt);
  74. TextBufferHandle staticText = textBufferManager->createTextBuffer(FONT_TYPE_DISTANCE, STATIC);
  75. textBufferManager->setPenPosition(staticText, 10.0f, 70.0f);
  76. textBufferManager->setTextColor(staticText, 0xFFFFFFFF);
  77. //textBufferManager->setTextColor(staticText, 0x000000FF);
  78. for(size_t i = 0; i< fontsCount; ++i)
  79. {
  80. textBufferManager->appendText(staticText, fonts[i], L"The quick brown fox jumps over the lazy dog\n");
  81. //textBufferManager->appendText(staticText, fonts[i], L"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\n");
  82. }
  83. while (!processEvents(width, height, debug, reset) )
  84. {
  85. // Set view 0 default viewport.
  86. bgfx::setViewRect(0, 0, 0, width, height);
  87. // This dummy draw call is here to make sure that view 0 is cleared
  88. // if no other draw calls are submitted to view 0.
  89. bgfx::submit(0);
  90. int64_t now = bx::getHPCounter();
  91. static int64_t last = now;
  92. const int64_t frameTime = now - last;
  93. last = now;
  94. const double freq = double(bx::getHPFrequency() );
  95. const double toMs = 1000.0/freq;
  96. // Use debug font to print information about this example.
  97. bgfx::dbgTextClear();
  98. bgfx::dbgTextPrintf(0, 1, 0x4f, "bgfx/examples/11-fontdfs");
  99. bgfx::dbgTextPrintf(0, 2, 0x6f, "Description: Use a single distance field font to render text of different size.");
  100. bgfx::dbgTextPrintf(0, 3, 0x0f, "Frame: % 7.3f[ms]", double(frameTime)*toMs);
  101. float at[3] = { 0, 0, 0.0f };
  102. float eye[3] = {0, 0, -1.0f };
  103. float view[16];
  104. float proj[16];
  105. mtxLookAt(view, eye, at);
  106. float centering = 0.5f;
  107. //setup a top-left ortho matrix for screen space drawing
  108. mtxOrtho(proj, centering, width+centering,height+centering, centering,-1.0f, 1.0f);
  109. // Set view and projection matrix for view 0.
  110. bgfx::setViewTransform(0, view, proj);
  111. //draw your text
  112. textBufferManager->submitTextBuffer(staticText, 0);
  113. // Advance to next frame. Rendering thread will be kicked to
  114. // process submitted rendering primitives.
  115. bgfx::frame();
  116. //just to prevent my CG Fan to howl
  117. Sleep(2);
  118. }
  119. //destroy the fonts
  120. for(size_t i=0; i<fontsCount;++i)
  121. {
  122. fontManager->destroyFont(fonts[i]);
  123. }
  124. textBufferManager->destroyTextBuffer(staticText);
  125. delete textBufferManager;
  126. delete fontManager;
  127. // Shutdown bgfx.
  128. bgfx::shutdown();
  129. return 0;
  130. }