forked from Chronial/foo_chronflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextDisplay.cpp
226 lines (193 loc) · 6.61 KB
/
TextDisplay.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "stdafx.h"
#include "config.h"
#include "TextDisplay.h"
#include "ImgTexture.h"
#include "Renderer.h"
using namespace Gdiplus;
TextDisplay::TextDisplay(Renderer* renderer)
: bitmapFontInitialized(false),
renderer(renderer)
{
}
TextDisplay::~TextDisplay(){
clearCache();
if (bitmapFontInitialized)
glDeleteLists(bitmapDisplayList, 96);
}
void TextDisplay::buildDisplayFont(){
HFONT font = CreateFont( -14, // Height Of Font
0, // Width Of Font
0, // Angle Of Escapement
0, // Orientation Angle
FW_NORMAL, // Font Weight
FALSE, // Italic
FALSE, // Underline
FALSE, // Strikeout
ANSI_CHARSET, // Character Set Identifier
OUT_TT_PRECIS, // Output Precision
CLIP_DEFAULT_PRECIS, // Clipping Precision
ANTIALIASED_QUALITY, // Output Quality
FF_SCRIPT|DEFAULT_PITCH, // Family And Pitch
L"Courier New"); // Font Name
Gdiplus::Bitmap dcBitmap(5, 5, PixelFormat32bppARGB);
Gdiplus::Graphics dcGraphics(&dcBitmap);
HDC hDC = dcGraphics.GetHDC();
SelectObject(hDC, font); // Selects The Font We Want
bitmapDisplayList = glGenLists(96);
wglUseFontBitmaps(hDC, 32, 96, bitmapDisplayList); // Builds 96 Characters Starting At Character 32
dcGraphics.ReleaseHDC(hDC);
DeleteObject(font); // Delete The Font
bitmapFontInitialized = true;
}
void TextDisplay::displayBitmapText(const char* text, int x, int y){
if (!bitmapFontInitialized)
buildDisplayFont();
renderer->glPushOrthoMatrix();
glDisable(GL_TEXTURE_2D);
glColor3f(GetRValue(cfgTitleColor)/255.0f, GetGValue(cfgTitleColor)/255.0f, GetBValue(cfgTitleColor)/255.0f);
glRasterPos2i(x, y);
glPushAttrib(GL_LIST_BIT); // Pushes The Display List Bits
glListBase(bitmapDisplayList - 32); // Sets The Base Character to 32
glCallLists(strlen(text), GL_UNSIGNED_BYTE, text); // Draws The Display List Text
glPopAttrib(); // Pops The Display List Bits
glEnable(GL_TEXTURE_2D);
renderer->glPopOrthoMatrix();
}
void TextDisplay::clearCache()
{
for (int i=0; i < CACHE_SIZE; i++){
if (texCache[i].text) {
glDeleteTextures(i, &texCache[i].glTex);
delete texCache[i].text;
}
texCache[i].text = 0;
texCache[i].age = ~0;
}
}
void TextDisplay::displayText(const char* text, int x, int y, HAlignment hAlign, VAlignment vAlign)
{
if (text == 0 || text[0] == '\0')
return;
DisplayTexture* dTex = 0;
int oldestElem = 0;
unsigned int maxAge = 0;
for (int i=0; i < CACHE_SIZE; i++){
if (texCache[i].text
&& texCache[i].color == cfgTitleColor
&& (strcmp(texCache[i].text, text) == 0)){
dTex = &texCache[i];
texCache[i].age = 0;
} else {
if (texCache[i].age < ~0)
texCache[i].age++;
if (texCache[i].age > maxAge){
maxAge = texCache[i].age;
oldestElem = i;
}
}
}
if (!dTex) { // not in cache
if (texCache[oldestElem].text) { // is oldest Element initialized?
glDeleteTextures(1, &texCache[oldestElem].glTex);
delete texCache[oldestElem].text;
}
texCache[oldestElem] = createTexture(text);
texCache[oldestElem].age = 0;
dTex = &texCache[oldestElem];
}
renderer->glPushOrthoMatrix();
if (hAlign == right)
x -= dTex->textWidth;
else if (hAlign == center)
x -= dTex->textWidth/2;
if (vAlign == bottom)
y -= dTex->textHeight;
else if (vAlign == middle)
y -= dTex->textHeight/2;
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor3f( 1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, dTex->glTex);
glBegin(GL_QUADS);
{
glTexCoord2f(0.0f, 1.0f); // top left
glVertex3i(x, y+dTex->texHeight, 0);
glTexCoord2f(1.0f, 1.0f); // top right
glVertex3i(x+dTex->texWidth, y+dTex->texHeight, 0);
glTexCoord2f(1.0f, 0.0f); // bottom right
glVertex3i(x+dTex->texWidth, y, 0);
glTexCoord2f(0.0f, 0.0f); // bottom left
glVertex3i(x, y, 0);
}
glEnd();
glDisable(GL_BLEND);
renderer->glPopOrthoMatrix();
}
TextDisplay::DisplayTexture TextDisplay::createTexture(const char* text)
{
DisplayTexture displayTex;
size_t textLen = strlen(text);
displayTex.text = new char[textLen+1];
strcpy_s(displayTex.text, textLen+1, text);
Bitmap* bitmap;
Gdiplus::Font* font;
{
pfc::stringcvt::string_wide_from_utf8 w_text(text);
StringFormat strFormat;
strFormat.SetAlignment(StringAlignmentCenter);
strFormat.SetTrimming(StringTrimmingNone);
strFormat.SetFormatFlags(StringFormatFlagsNoFitBlackBox |
StringFormatFlagsNoWrap |
StringFormatFlagsNoClip);
RectF stringSize(0, 0, 1024, 128);
{ // calculate Text Size
Bitmap calcBitmap(5, 5, PixelFormat32bppARGB);
Graphics graphics(&calcBitmap);
HDC fontDC = graphics.GetHDC();
font = new Gdiplus::Font(fontDC, &(cfgTitleFont.get_value()));
graphics.ReleaseHDC(fontDC);
if (!font->IsAvailable()){
delete font;
font = new Gdiplus::Font(L"Verdana", 8.0f);
}
graphics.MeasureString(w_text, -1, font, PointF(), &stringSize);
}
// round to multiples of two, so centering is consistent
stringSize.Width = ceil(stringSize.Width / 2.0f) * 2;
stringSize.Height = ceil(stringSize.Height);
displayTex.texWidth = displayTex.textWidth = (int)stringSize.Width;
displayTex.texHeight = displayTex.textHeight = (int)stringSize.Height;
// Make the texture size a power of two
displayTex.texWidth = 1;
while (displayTex.texWidth < displayTex.textWidth)
displayTex.texWidth = displayTex.texWidth << 1;
displayTex.texHeight = 1;
while (displayTex.texHeight < displayTex.textHeight)
displayTex.texHeight = displayTex.texHeight << 1;
bitmap = new Bitmap(displayTex.texWidth, displayTex.texHeight, PixelFormat32bppARGB);
Graphics drawer(bitmap);
drawer.SetTextRenderingHint(TextRenderingHintAntiAliasGridFit);
Color textColor(255, 255, 255);
textColor.SetFromCOLORREF(cfgTitleColor);
SolidBrush textBrush(textColor);
displayTex.color = cfgTitleColor;
drawer.DrawString(w_text, -1, font, stringSize, &strFormat, &textBrush);
}
{
bitmap->RotateFlip(RotateNoneFlipY);
Rect rc(0,0,bitmap->GetWidth(),bitmap->GetHeight());
BitmapData bitmapData;
bitmap->LockBits(&rc,ImageLockModeRead,PixelFormat32bppARGB,&bitmapData);
glGenTextures(1,&displayTex.glTex);
glBindTexture(GL_TEXTURE_2D, displayTex.glTex);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
void* data = bitmapData.Scan0;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, displayTex.texWidth, displayTex.texHeight, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE,data);
bitmap->UnlockBits(&bitmapData);
}
delete bitmap;
delete font;
return displayTex;
}