Counter Strike : Global Offensive Source Code
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.

118 lines
3.1 KiB

  1. //========= Copyright � 1996-2005, 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 "utlmap.h"
  15. #include "bitmap/texturepacker.h"
  16. class ITexture;
  17. #define MAX_COMMON_CHARS 256
  18. //-----------------------------------------------------------------------------
  19. // Purpose: manages texture memory for unicode fonts
  20. //-----------------------------------------------------------------------------
  21. class CFontTextureCache
  22. {
  23. public:
  24. CFontTextureCache();
  25. ~CFontTextureCache();
  26. void SetPrefix( const char *pTexturePagePrefix );
  27. // returns a texture ID and a pointer to an array of 4 texture coords for the given character & font
  28. // generates+uploads more texture if necessary
  29. bool GetTextureForChar( FontHandle_t font, FontDrawType_t type, wchar_t wch, int *textureID, float **texCoords );
  30. // This function copies the texcoords out from the static into a preallocated passed in arg.
  31. bool GetTextureAndCoordsForChar( FontHandle_t font, FontDrawType_t type, wchar_t wch, int *textureID, float *texCoords );
  32. // for each character in an array (not assumed to be a NULL-terminated string), returns a
  33. // texture ID and a pointer to an array of 4 texture coords for the given character & font
  34. // generates+uploads more texture if necessary
  35. bool GetTextureForChars( FontHandle_t font, FontDrawType_t type, wchar_t *wch, int *textureID, float **texCoords, int numChars = 1 );
  36. // clears the cache
  37. void Clear();
  38. private:
  39. ITexture *AllocateNewPage( char *pTextureName );
  40. // hold the common characters
  41. struct charDetail_t
  42. {
  43. int page;
  44. float texCoords[4];
  45. };
  46. struct CommonChar_t
  47. {
  48. charDetail_t details[MAX_COMMON_CHARS];
  49. };
  50. // a single character in the cache
  51. typedef unsigned short HCacheEntry;
  52. struct CacheEntry_t
  53. {
  54. FontHandle_t font;
  55. wchar_t wch;
  56. short pad;
  57. int page;
  58. float texCoords[4];
  59. };
  60. struct CacheMapEntry_t
  61. {
  62. Rect_t rc;
  63. bool bInUse;
  64. };
  65. // a single texture page
  66. struct Page_t
  67. {
  68. public:
  69. Page_t()
  70. {
  71. pPackedFontTextureCache = NULL;
  72. }
  73. short textureID[FONT_DRAW_TYPE_COUNT];
  74. CTexturePacker *pPackedFontTextureCache; // the character mapping cache to use for this page.
  75. };
  76. // allocates a new page for a given character
  77. bool AllocatePageForChar(int charWide, int charTall, int &pageIndex, int &drawX, int &drawY, int &twide, int &ttall);
  78. // Creates font materials
  79. void CreateFontMaterials( Page_t &page, ITexture *pFontTexture, bool bitmapFont = false );
  80. CommonChar_t *m_CommonCharCache[384];
  81. static bool CacheEntryLessFunc(const CacheEntry_t &lhs, const CacheEntry_t &rhs);
  82. CUtlRBTree< CacheEntry_t, HCacheEntry > m_CharCache;
  83. // cache
  84. typedef CUtlVector<Page_t> FontPageList_t;
  85. FontPageList_t m_PageList;
  86. int m_CurrPage;
  87. CUtlMap< FontHandle_t, Page_t > m_FontPages;
  88. // Prefix to use when this cache creates font pages
  89. CUtlString m_TexturePagePrefix;
  90. };
  91. #endif // FONTTEXTURECACHE_H