My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <CtrlCore/CtrlCore.h>

#ifdef GUI_WINGL

NAMESPACE_UPP

void OpenGLFont::LoadBrc(const byte* xml, const byte** imagesData, const int* imagesSize, int imagesCount)
{
Parse((const char*) xml, false);
for(int i = 0; i < imagesCount; i++)
{
MemStream ms((void*) imagesData[i], imagesSize[i]);
Image img = StreamRaster::LoadAny(ms);
images.Add(img);
if(preload)
resources.Add(img, true);
}
}

void OpenGLFont::Load(const String& fileName, bool preload)
{
String filePath = GetDataFile(fileName);
String xml = LoadFile(filePath);
Parse(xml, true);
}

void OpenGLFont::Parse(const char* xml, bool parsePages)
{
chars.SetCount(512);

XmlParser p(xml);

while(!p.IsTag())
p.Skip();

p.PassTag("font");

while(!p.End())
{
if(p.TagE("common"))
{
scaleW = (float) p.Double("scaleW");
scaleH = (float) p.Double("scaleH");
lineHeight = (float) p.Double("lineHeight");
base = (float) p.Double("base");
}
else if(p.Tag("pages"))
{
while(!p.End())
{
if(p.TagE("page"))
{
if(parsePages)
{
String fileName = p["file"];
Image img = StreamRaster::LoadFileAny(GetDataFile(fileName));
images.Add(img);
if(preload)
resources.Add(img, true);
}
pages.Add(StrInt(p["id"]));
}
else
p.Skip();
}

}
else if(p.Tag("chars"))
{
while(!p.End())
{
if(p.TagE("char"))
{
int id = p.Int("id");
CharInfo& ci = chars[id];

ci.id = id;
ci.x = (float) p.Double("x");
ci.y = (float) p.Double("y");
ci.width = (float) p.Double("width");
ci.height = (float) p.Double("height");
ci.xoffset = (float) p.Double("xoffset");
ci.yoffset = (float) p.Double("yoffset");
ci.xadvance = (float) p.Double("xadvance");
ci.page = p.Int("page");
}
else
p.Skip();
}
}
else if(p.Tag("kernings"))
{
while(!p.End())
{
if(p.TagE("kerning"))
{
int first = p.Int("first");
int second = p.Int("second");
float amount = (float) p.Double("amount");

VectorMap<int, float>& vm = kerns.GetAdd(first);
vm.Add(second, amount);
}
else
p.Skip();
}
}
else
p.Skip();
}
}

void OpenGLFont::UpdateTextures()
{
if(texturesUpdated)
return;

for(int i = 0; i < images.GetCount(); i++)
{
const Texture& t = resources.Bind(images[i], Resources::LINEAR_FILTERING);
pages[i] = t.serialId;
}

texturesUpdated = true;
}

float ConvStrength(float min, float max, float p)
{
return min + (max - min) * p / 100.f;
}

void SystemDraw::Text(int x, int y, int angle, const wchar *text, Font font, Color ink, int outlineStrength, Color outlineColor, int glowStrength, Color glowColor, int n, const int *dx)
{
if(!text)
return;

const wchar* s = text;
OpenGLFont& fi = resources.GetFont(font);

glEnable(GL_TEXTURE_2D);

#if CLIP_MODE == 3
float cl = (float) clip.left;
float ct = (float) clip.top;
float cr = (float) clip.right;
float cb = (float) clip.bottom;
#endif
fi.UpdateTextures();

glEnableClientState(GL_TEXTURE_COORD_ARRAY);
alphaMagProg.Start();

const float ic = 1.f / 255.f;

float outlineCenter = ConvStrength(0.1f, 0.45f, (float) 100 - min(outlineStrength, 100));
float glowCenter = ConvStrength(0.55f, 0.95f, (float) 100 - min(glowStrength, 100));

float a = alpha * ic;

alphaMagProg.Set("GlyphColor", ink.GetR() * ic, ink.GetG() * ic, ink.GetB() * ic, a);
alphaMagProg.Set("OutlineColor", outlineColor.GetR() * ic, outlineColor.GetG() * ic, outlineColor.GetB() * ic, a);
alphaMagProg.Set("GlowColor", glowColor.GetR() * ic, glowColor.GetG() * ic, glowColor.GetB() * ic, a);
alphaMagProg.Set("Outline", outlineStrength > 0); //0.1 - 0.45
alphaMagProg.Set("Glow", glowStrength > 0); //0.55 - 0.095
alphaMagProg.Set("Shadow", false);
alphaMagProg.Set("OutlineCenter", outlineCenter);
alphaMagProg.Set("GlowCenter", glowCenter);

float xp = (float) x;
float yp = (float) y;

int page = -1;

float sw = (float) fi.scaleW;
float sh = (float) fi.scaleH;

float tw = 1.f / sw;
float th = 1.f / sh;

while(*s && n > 0)
{
int cn = *s;

if(cn >= 0 && cn < fi.chars.GetCount())
{
const OpenGLFont::CharInfo& ci = fi.chars[cn];

cn <<= 3;

if(ci.page != page)
{
resources.Bind(fi.pages[ci.page], Resources::LINEAR_FILTERING);
glActiveTexture(GL_TEXTURE0);
alphaMagProg.Set("Texture", 0);
page = ci.page;
}

float sx = (float) ci.xoffset * fi.scale + xp + drawing_offset.x;
float sy = (float) ci.yoffset * fi.scale + yp + drawing_offset.y;
float dx = sx + ci.width * fi.scale;
float dy = sy + ci.height * fi.scale;

#if CLIP_MODE == 3
if(sx <= clip.right && sy <= clip.bottom && dx >= clip.left && dy >= clip.top)
#endif
{
float tl = (float) ci.x;
float tt = (float) ci.y;
float tr = (float) ci.x + ci.width;
float tb = (float) ci.y + ci.height;

#if CLIP_MODE == 3
if(sx < cl)
{
tl += (cl - sx);
sx = cl;
}

if(sy < ct)
{
tt += (ct - sy);
sy = ct;
}

if(dx > cr)
{
tr -= dx - cr;
dx = cr;
}

if(dy > cb)
{
tb -= dy - cb;
dy = cb;
}
#endif

tl = (tl + TEXEL_OFFSET) * tw;
tr = (tr - TEXEL_OFFSET) * tw;
tt = (tt + TEXEL_OFFSET) * th;
tb = (tb - TEXEL_OFFSET) * th;
/*
float vtx[] = {
sx, dy,
sx, sy,
dx, dy,
dx, sy
};*/
float vtx[12];
SetVtx(vtx, sx, sy, dx, dy);

float crd[] = {
tl, tb,
tl, tt,
tr, tb,
tr, tt
};

glTexCoordPointer(2, GL_FLOAT, 0, crd);
glVertexPointer(projection_mode ? 3: 2, GL_FLOAT, 0, vtx);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}

xp += int(ci.xadvance * fi.scale + 0.5f);

int k = fi.kerns.Find(*s);
if(k >= 0)
xp += int(fi.kerns[k].Get(*(s + 1), 0) * fi.scale);
}

++s;
--n;
}

alphaMagProg.Stop();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
}

void SystemDraw::DrawTextOp(int x, int y, int angle, const wchar *text, Font font, Color ink, int n, const int *dx)
{
Text(x, y, angle, text, font, ink, 0, White, 0, White, n, dx);
}

Size GetTextSize(const wchar *text, const OpenGLFont& fi, int n)
{
if(n < 0)
n = wstrlen(text);
Size sz;
sz.cx = 0;
const wchar *wtext = (const wchar *)text;
while(n > 0) {
int cn = *wtext++;
if(cn >= 0 && cn < fi.chars.GetCount())
{
const OpenGLFont::CharInfo& ci = fi.chars[cn];
sz.cx += int(ci.xadvance * fi.scale + 0.5f);
int k = fi.kerns.Find(cn);
if(k >= 0)
sz.cx += int(fi.kerns[k].Get(*wtext, 0) * fi.scale);
}
n--;
}
sz.cy = int(fi.lineHeight * fi.scale + 0.5f);
return sz;
}

END_UPP_NAMESPACE

#endif

Change log

r4853 by unodgs on Apr 24, 2012   Diff
WinGL: Fixed timers, added support for
perspective view
Go to: 
Project members, sign in to write a code review

Older revisions

r4365 by unodgs on Jan 6, 2012   Diff
WinGL: Added painting through fbo,
added blur shader, fixed children
clipping, optimized painting a bit
r4204 by unodgs on Nov 26, 2011   Diff
WinGL: Added possibility to change
texture filtering mode in Bind. Made
top frame's title bar gradient
background smoother
r4188 by unodgs on Nov 22, 2011   Diff
WinGL: Added automatic atlas textures,
fixed some bugs
All revisions of this file

File info

Size: 6750 bytes, 310 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting