Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef FONTTEXTURECACHE_H
  8. #define FONTTEXTURECACHE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "vgui_surfacelib/FontManager.h"
  13. #include "utlrbtree.h"
  14. #include <vgui/ISurface.h>
  15. #include "utlmap.h"
  16. class ITexture;
  17. //-----------------------------------------------------------------------------
  18. // Purpose: manages texture memory for unicode fonts in vgui
  19. //-----------------------------------------------------------------------------
  20. class CFontTextureCache
  21. {
  22. public:
  23. CFontTextureCache();
  24. ~CFontTextureCache();
  25. // returns a texture ID and a pointer to an array of 4 texture coords for the given character & font
  26. // generates+uploads more texture if necessary
  27. bool GetTextureForChar( vgui::HFont font, vgui::FontDrawType_t type, wchar_t wch, int *textureID, float **texCoords );
  28. // for each character in an array (not assumed to be a NULL-terminated string), returns a
  29. // texture ID and a pointer to an array of 4 texture coords for the given character & font
  30. // generates+uploads more texture if necessary
  31. bool GetTextureForChars( vgui::HFont font, vgui::FontDrawType_t type, const wchar_t *wch, int *textureID, float **texCoords, int numChars = 1 );
  32. // clears the cache
  33. void Clear();
  34. private:
  35. // NOTE: If you change this, change s_pFontPageSize
  36. enum
  37. {
  38. FONT_PAGE_SIZE_16,
  39. FONT_PAGE_SIZE_32,
  40. FONT_PAGE_SIZE_64,
  41. FONT_PAGE_SIZE_128,
  42. FONT_PAGE_SIZE_256,
  43. FONT_PAGE_SIZE_COUNT,
  44. };
  45. // a single character in the cache
  46. typedef unsigned short HCacheEntry;
  47. struct CacheEntry_t
  48. {
  49. vgui::HFont font;
  50. wchar_t wch;
  51. unsigned char page;
  52. float texCoords[4];
  53. // doubly-linked list for use in the LRU
  54. HCacheEntry nextEntry;
  55. HCacheEntry prevEntry;
  56. };
  57. // a single texture page
  58. struct Page_t
  59. {
  60. short textureID[vgui::FONT_DRAW_TYPE_COUNT];
  61. short maxFontHeight;
  62. short tallestCharOnLine;
  63. short wide, tall; // total size of the page
  64. short nextX, nextY; // position to draw any new character positions
  65. };
  66. // allocates a new page for a given character
  67. bool AllocatePageForChar(int charWide, int charTall, int &pageIndex, int &drawX, int &drawY, int &twide, int &ttall);
  68. // Creates font materials
  69. void CreateFontMaterials( Page_t &page, ITexture *pFontTexture, bool bitmapFont = false );
  70. // Computes the page size given a character height
  71. int ComputePageType( int charTall ) const;
  72. static bool CacheEntryLessFunc(const CacheEntry_t &lhs, const CacheEntry_t &rhs);
  73. CUtlRBTree<CacheEntry_t, HCacheEntry> m_CharCache;
  74. // cache
  75. typedef CUtlVector<Page_t> FontPageList_t;
  76. FontPageList_t m_PageList;
  77. int m_pCurrPage[FONT_PAGE_SIZE_COUNT];
  78. HCacheEntry m_LRUListHeadIndex;
  79. static int s_pFontPageSize[FONT_PAGE_SIZE_COUNT];
  80. CUtlMap< vgui::HFont, Page_t > m_FontPages;
  81. };
  82. #endif // FONTTEXTURECACHE_H