forked from FWGS/xash3d
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvgui_font.cpp
186 lines (146 loc) · 5.15 KB
/
vgui_font.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
186
/*
vgui_font.cpp - fonts management
Copyright (C) 2011 Uncle Mike
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
In addition, as a special exception, the author gives permission
to link the code of this program with VGUI library developed by
Valve, L.L.C ("Valve"). You must obey the GNU General Public License
in all respects for all of the code used other than VGUI library.
If you modify this file, you may extend this exception to your
version of the file, but you are not obligated to do so. If
you do not wish to do so, delete this exception statement
from your version.
*/
#ifdef XASH_VGUI
#include "vgui_main.h"
int FontCache::s_pFontPageSize[FONT_PAGE_SIZE_COUNT] = { 16, 32, 64, 128 };
FontCache::FontCache() : 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;
}
}
bool FontCache::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.ch < rhs.ch );
}
bool FontCache::GetTextureForChar( Font *font, char ch, int *textureID, float **texCoords )
{
static CacheEntry_t cacheitem;
cacheitem.font = font;
cacheitem.ch = ch;
Assert( texCoords != NULL );
*texCoords = cacheitem.texCoords;
HCacheEntry cacheHandle = m_CharCache.Find( cacheitem );
if( cacheHandle != 65535 && m_CharCache.IsValidIndex( cacheHandle ))
{
// we have an entry already, return that
int page = m_CharCache[cacheHandle].page;
*textureID = m_PageList[page].textureID;
//else return false;
*texCoords = m_CharCache[cacheHandle].texCoords;
return true;
}
// get the char details
int fontTall = font->getTall();
int a, b, c;
font->getCharABCwide( (byte)ch, a, b, c );
int fontWide = b;
// get a texture to render into
int page, drawX, drawY, twide, ttall;
if( !AllocatePageForChar( fontWide, fontTall, page, drawX, drawY, twide, ttall ))
return false;
// create a buffer and render the character into it
int nByteCount = s_pFontPageSize[FONT_PAGE_SIZE_COUNT-1] * s_pFontPageSize[FONT_PAGE_SIZE_COUNT-1] * 4;
byte * rgba = (byte *)g_api->EngineMalloc(nByteCount);//(byte *)Z_Malloc( nByteCount );
font->getCharRGBA( (byte)ch, 0, 0, fontWide, fontTall, rgba );
// upload the new sub texture
g_api->BindTexture( m_PageList[page].textureID );
g_api->UploadTextureBlock( m_PageList[page].textureID, drawX, drawY, rgba, fontWide, fontTall );
// set the cache info
cacheitem.page = page;
cacheitem.texCoords[0] = (float)((double)drawX / ((double)twide));
cacheitem.texCoords[1] = (float)((double)drawY / ((double)ttall));
cacheitem.texCoords[2] = (float)((double)(drawX + fontWide) / (double)twide);
cacheitem.texCoords[3] = (float)((double)(drawY + fontTall) / (double)ttall);
m_CharCache.Insert( cacheitem );
// return the data
*textureID = m_PageList[page].textureID;
// memcpy( texCoords, cacheitem.texCoords, sizeof( float ) * 4 );
return true;
}
int FontCache::ComputePageType( int charTall ) const
{
for( int i = 0; i < FONT_PAGE_SIZE_COUNT; i++ )
{
if( charTall < s_pFontPageSize[i] )
return i;
}
return -1;
}
bool FontCache::AllocatePageForChar( int charWide, int charTall, int &pageIndex, int &drawX, int &drawY, int &twide, int &ttall )
{
// see if there is room in the last page for this character
int nPageType = ComputePageType( charTall );
if( nPageType < 0 )
return false;
pageIndex = m_pCurrPage[nPageType];
int nNextX = 0;
bool bNeedsNewPage = true;
if( pageIndex > -1 )
{
Page_t &page = m_PageList[pageIndex];
nNextX = page.nextX + charWide;
// make sure we have room on the current line of the texture page
if( nNextX > page.wide )
{
// move down a line
page.nextX = 0;
nNextX = charWide;
page.nextY += page.fontHeight + 1;
}
bNeedsNewPage = (( page.nextY + page.fontHeight + 1 ) > page.tall );
}
if( bNeedsNewPage )
{
// allocate a new page
pageIndex = m_PageList.AddToTail();
Page_t &newPage = m_PageList[pageIndex];
m_pCurrPage[nPageType] = pageIndex;
newPage.textureID = g_api->GenerateTexture();
newPage.fontHeight = s_pFontPageSize[nPageType];
newPage.wide = 256;
newPage.tall = 256;
newPage.nextX = 0;
newPage.nextY = 0;
nNextX = charWide;
// create empty texture
g_api->CreateTexture( newPage.textureID, 256, 256 );
}
// output the position
Page_t &page = m_PageList[pageIndex];
drawX = page.nextX;
drawY = page.nextY;
twide = page.wide;
ttall = page.tall;
// update the next position to draw in
page.nextX = nNextX + 1;
return true;
}
#endif