-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFontTextureCache.cpp
185 lines (144 loc) · 4.63 KB
/
FontTextureCache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include <malloc.h>
#include "FontTextureCache.h"
#include <IEngineSurface.h>
#include "vgui_internal.h"
#include "LoadTGA.h"
#include "qgl.h"
CFontTextureCache g_FontTextureCache;
#define TEXTURE_PAGE_WIDTH 256
#define TEXTURE_PAGE_HEIGHT 256
int CFontTextureCache::s_pFontPageSize[FONT_PAGE_SIZE_COUNT] =
{
16,
32,
64,
128,
};
CFontTextureCache::CFontTextureCache(void): m_CharCache(0, 256, CacheEntryLessFunc)
{
CacheEntry_t listHead = { 0, 0 };
m_LRUListHeadIndex = m_CharCache.Insert(listHead);
m_CharCache[m_LRUListHeadIndex].nextEntry = m_LRUListHeadIndex;
m_CharCache[m_LRUListHeadIndex].prevEntry = m_LRUListHeadIndex;
for (int i = 0; i < FONT_PAGE_SIZE_COUNT; ++i)
m_pCurrPage[i] = -1;
}
CFontTextureCache::~CFontTextureCache(void)
{
}
bool CFontTextureCache::GetTextureForChar(HFont font, wchar_t wch, int *textureID, float **texCoords)
{
static CacheEntry_t cacheitem;
cacheitem.font = font;
cacheitem.wch = wch;
HCacheEntry cacheHandle = m_CharCache.Find(cacheitem);
if (m_CharCache.IsValidIndex(cacheHandle))
{
int page = m_CharCache[cacheHandle].page;
*textureID = m_PageList[page].textureID;
*texCoords = m_CharCache[cacheHandle].texCoords;
return true;
}
CWin32Font *winFont = FontManager().GetFontForChar(font, wch);
if (!winFont)
return false;
int fontTall = winFont->GetHeight();
int a, b, c;
winFont->GetCharABCWidths(wch, a, b, c);
int fontWide = b;
int page, drawX, drawY, twide, ttall;
if (!AllocatePageForChar(fontWide, fontTall, page, drawX, drawY, twide, ttall))
return false;
int nByteCount = s_pFontPageSize[FONT_PAGE_SIZE_COUNT - 1] * s_pFontPageSize[FONT_PAGE_SIZE_COUNT - 1] * 4;
unsigned char *rgba = (unsigned char *)_alloca(nByteCount);
memset(rgba, 0, nByteCount);
winFont->GetCharRGBA(wch, 0, 0, fontWide, fontTall, rgba);
staticSurface->drawSetTexture(m_PageList[page].textureID);
staticSurface->drawSetSubTextureRGBA(m_PageList[page].textureID, drawX, drawY, rgba, fontWide, fontTall);
cacheitem.page = page;
double adjust = 0.0f;
cacheitem.texCoords[0] = (float)((double)drawX / ((double)twide + adjust));
cacheitem.texCoords[1] = (float)((double)drawY / ((double)ttall + adjust));
cacheitem.texCoords[2] = (float)((double)(drawX + fontWide) / (double)twide);
cacheitem.texCoords[3] = (float)((double)(drawY + fontTall) / (double)ttall);
m_CharCache.Insert(cacheitem);
*textureID = m_PageList[page].textureID;
*texCoords = cacheitem.texCoords;
return true;
}
bool CFontTextureCache::AllocatePageForChar(int charWide, int charTall, int &pageIndex, int &drawX, int &drawY, int &twide, int &ttall)
{
int nPageType = ComputePageType(charTall);
pageIndex = m_pCurrPage[nPageType];
int nNextX = 0;
bool bNeedsNewPage = true;
if (pageIndex > -1)
{
Page_t &page = m_PageList[pageIndex];
nNextX = page.nextX + charWide;
if (nNextX > page.wide)
{
page.nextX = 0;
nNextX = charWide;
page.nextY += page.fontHeight + 1;
}
bNeedsNewPage = ((page.nextY + page.fontHeight + 1) > page.tall);
}
if (bNeedsNewPage)
{
pageIndex = m_PageList.AddToTail();
Page_t &newPage = m_PageList[pageIndex];
m_pCurrPage[nPageType] = pageIndex;
newPage.textureID = staticSurface->createNewTextureID();
newPage.fontHeight = s_pFontPageSize[nPageType];
newPage.wide = TEXTURE_PAGE_WIDTH;
newPage.tall = TEXTURE_PAGE_HEIGHT;
newPage.nextX = 0;
newPage.nextY = 0;
nNextX = charWide;
unsigned char rgba[TEXTURE_PAGE_WIDTH * TEXTURE_PAGE_HEIGHT * 4];
memset(rgba, 0, sizeof(rgba));
staticSurface->drawSetTextureRGBA(newPage.textureID, rgba, newPage.wide, newPage.tall, false, true);
}
Page_t &page = m_PageList[pageIndex];
drawX = page.nextX;
drawY = page.nextY;
twide = page.wide;
ttall = page.tall;
page.nextX = nNextX + 1;
return true;
}
int CFontTextureCache::ComputePageType(int charTall) const
{
for (int i = 0; i < FONT_PAGE_SIZE_COUNT; ++i)
{
if (charTall < s_pFontPageSize[i])
return i;
}
return -1;
}
bool CFontTextureCache::CacheEntryLessFunc(CacheEntry_t const &lhs, CacheEntry_t const &rhs)
{
if (lhs.font < rhs.font)
return true;
else if (lhs.font > rhs.font)
return false;
return (lhs.wch < rhs.wch);
}
void CFontTextureCache::DumpPageTextures(void)
{
byte *pixels = new byte[TEXTURE_PAGE_WIDTH * TEXTURE_PAGE_HEIGHT * 3];
for (int i = 0; i < FONT_PAGE_SIZE_COUNT; ++i)
{
int pageIndex = m_pCurrPage[i];
if (pageIndex > -1)
{
qglBindTexture(GL_TEXTURE_2D, m_PageList[pageIndex].textureID);
qglGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
char filename[MAX_PATH];
sprintf(filename, "fonttexture_%d.tga", i + i);
WriteTGA(pixels, TEXTURE_PAGE_WIDTH, TEXTURE_PAGE_HEIGHT, filename);
}
}
delete pixels;
}