text_buffer_manager.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2013 Jeremie Roy. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef TEXT_BUFFER_MANAGER_H_HEADER_GUARD
  6. #define TEXT_BUFFER_MANAGER_H_HEADER_GUARD
  7. #include "font_manager.h"
  8. BGFX_HANDLE(TextBufferHandle);
  9. #define MAX_TEXT_BUFFER_COUNT 64
  10. /// type of vertex and index buffer to use with a TextBuffer
  11. struct BufferType
  12. {
  13. enum Enum
  14. {
  15. Static,
  16. Dynamic,
  17. Transient,
  18. };
  19. };
  20. /// special style effect (can be combined)
  21. enum TextStyleFlags
  22. {
  23. STYLE_NORMAL = 0,
  24. STYLE_OVERLINE = 1,
  25. STYLE_UNDERLINE = 1 << 1,
  26. STYLE_STRIKE_THROUGH = 1 << 2,
  27. STYLE_BACKGROUND = 1 << 3,
  28. };
  29. struct TextRectangle
  30. {
  31. float width, height;
  32. };
  33. class TextBuffer;
  34. class TextBufferManager
  35. {
  36. public:
  37. TextBufferManager(FontManager* _fontManager);
  38. ~TextBufferManager();
  39. TextBufferHandle createTextBuffer(uint32_t _type, BufferType::Enum _bufferType);
  40. void destroyTextBuffer(TextBufferHandle _handle);
  41. void submitTextBuffer(TextBufferHandle _handle, uint8_t _id, int32_t _depth = 0);
  42. void setStyle(TextBufferHandle _handle, uint32_t _flags = STYLE_NORMAL);
  43. void setTextColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
  44. void setBackgroundColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
  45. void setOverlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
  46. void setUnderlineColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
  47. void setStrikeThroughColor(TextBufferHandle _handle, uint32_t _rgba = 0x000000FF);
  48. void setPenPosition(TextBufferHandle _handle, float _x, float _y);
  49. /// Append an ASCII/utf-8 string to the buffer using current pen position and color.
  50. void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const char* _string, const char* _end = NULL);
  51. /// Append a wide char unicode string to the buffer using current pen position and color.
  52. void appendText(TextBufferHandle _handle, FontHandle _fontHandle, const wchar_t* _string, const wchar_t* _end = NULL);
  53. /// Append a whole face of the atlas cube, mostly used for debugging and visualizing atlas.
  54. void appendAtlasFace(TextBufferHandle _handle, uint16_t _faceIndex);
  55. /// Clear the text buffer and reset its state (pen/color).
  56. void clearTextBuffer(TextBufferHandle _handle);
  57. /// Return the rectangular size of the current text buffer (including all its content).
  58. TextRectangle getRectangle(TextBufferHandle _handle) const;
  59. private:
  60. struct BufferCache
  61. {
  62. uint16_t indexBufferHandleIdx;
  63. uint16_t vertexBufferHandleIdx;
  64. TextBuffer* textBuffer;
  65. BufferType::Enum bufferType;
  66. uint32_t fontType;
  67. };
  68. BufferCache* m_textBuffers;
  69. bx::HandleAllocT<MAX_TEXT_BUFFER_COUNT> m_textBufferHandles;
  70. FontManager* m_fontManager;
  71. bgfx::VertexDecl m_vertexDecl;
  72. bgfx::UniformHandle u_texColor;
  73. bgfx::ProgramHandle m_basicProgram;
  74. bgfx::ProgramHandle m_distanceProgram;
  75. bgfx::ProgramHandle m_distanceSubpixelProgram;
  76. };
  77. #endif // TEXT_BUFFER_MANAGER_H_HEADER_GUARD